1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/test/embedded_test_server/http_response.h"
#include <algorithm>
#include <iterator>
#include <map>
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/check.h"
#include "base/containers/flat_map.h"
#include "base/format_macros.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "net/http/http_status_code.h"
#include "net/test/embedded_test_server/http_request.h"
namespace net {
namespace test_server {
HttpResponseDelegate::HttpResponseDelegate() = default;
HttpResponseDelegate::~HttpResponseDelegate() = default;
HttpResponse::~HttpResponse() = default;
RawHttpResponse::RawHttpResponse(const std::string& headers,
const std::string& contents)
: headers_(headers), contents_(contents) {}
RawHttpResponse::~RawHttpResponse() = default;
void RawHttpResponse::SendResponse(
base::WeakPtr<HttpResponseDelegate> delegate) {
if (!headers_.empty()) {
std::string response = headers_;
// LocateEndOfHeadersHelper() searches for the first "\n\n" and "\n\r\n" as
// the end of the header.
std::size_t index = response.find_last_not_of("\r\n");
if (index != std::string::npos)
response.erase(index + 1);
response += "\n\n";
delegate->SendRawResponseHeaders(response);
}
delegate->SendContentsAndFinish(contents_);
}
void RawHttpResponse::AddHeader(const std::string& key_value_pair) {
headers_.append(base::StringPrintf("%s\r\n", key_value_pair.c_str()));
}
BasicHttpResponse::BasicHttpResponse() = default;
BasicHttpResponse::~BasicHttpResponse() = default;
std::string BasicHttpResponse::ToResponseString() const {
base::StringPairs headers = BuildHeaders();
// Response line with headers.
std::string response_builder;
// TODO(mtomasz): For http/1.0 requests, send http/1.0.
base::StringAppendF(&response_builder, "HTTP/1.1 %d %s\r\n", code_,
reason().c_str());
for (const auto& header : headers)
base::StringAppendF(&response_builder, "%s: %s\r\n", header.first.c_str(),
header.second.c_str());
base::StringAppendF(&response_builder, "\r\n");
return response_builder + content_;
}
base::StringPairs BasicHttpResponse::BuildHeaders() const {
base::StringPairs headers;
headers.emplace_back("Connection", "close");
headers.emplace_back("Content-Length", base::NumberToString(content_.size()));
headers.emplace_back("Content-Type", content_type_);
std::copy(custom_headers_.begin(), custom_headers_.end(),
std::back_inserter(headers));
return headers;
}
void BasicHttpResponse::SendResponse(
base::WeakPtr<HttpResponseDelegate> delegate) {
delegate->SendHeadersContentAndFinish(code_, reason(), BuildHeaders(),
content_);
}
DelayedHttpResponse::DelayedHttpResponse(const base::TimeDelta delay)
: delay_(delay) {}
DelayedHttpResponse::~DelayedHttpResponse() = default;
void DelayedHttpResponse::SendResponse(
base::WeakPtr<HttpResponseDelegate> delegate) {
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&HttpResponseDelegate::SendHeadersContentAndFinish,
delegate, code(), reason(), BuildHeaders(), content()),
delay_);
}
void HungResponse::SendResponse(base::WeakPtr<HttpResponseDelegate> delegate) {}
HungAfterHeadersHttpResponse::HungAfterHeadersHttpResponse(
base::StringPairs headers)
: headers_(headers) {}
HungAfterHeadersHttpResponse::~HungAfterHeadersHttpResponse() = default;
void HungAfterHeadersHttpResponse::SendResponse(
base::WeakPtr<HttpResponseDelegate> delegate) {
delegate->SendResponseHeaders(HTTP_OK, "OK", headers_);
}
} // namespace test_server
} // namespace net
|