1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/test/embedded_test_server/http_request.h"
6
7 #include <algorithm>
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/http/http_chunked_decoder.h"
16 #include "url/gurl.h"
17
18 namespace net::test_server {
19
20 namespace {
21
22 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb.
23
24 // Helper function used to trim tokens in http request headers.
Trim(const std::string & value)25 std::string Trim(const std::string& value) {
26 std::string result;
27 base::TrimString(value, " \t", &result);
28 return result;
29 }
30
31 } // namespace
32
33 HttpRequest::HttpRequest() = default;
34
35 HttpRequest::HttpRequest(const HttpRequest& other) = default;
36
37 HttpRequest::~HttpRequest() = default;
38
GetURL() const39 GURL HttpRequest::GetURL() const {
40 if (base_url.is_valid())
41 return base_url.Resolve(relative_url);
42 return GURL("http://localhost" + relative_url);
43 }
44
HttpRequestParser()45 HttpRequestParser::HttpRequestParser()
46 : http_request_(std::make_unique<HttpRequest>()) {}
47
48 HttpRequestParser::~HttpRequestParser() = default;
49
ProcessChunk(base::StringPiece data)50 void HttpRequestParser::ProcessChunk(base::StringPiece data) {
51 buffer_.append(data.data(), data.size());
52 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit) <<
53 "The HTTP request is too large.";
54 }
55
ShiftLine()56 std::string HttpRequestParser::ShiftLine() {
57 size_t eoln_position = buffer_.find("\r\n", buffer_position_);
58 DCHECK_NE(std::string::npos, eoln_position);
59 const int line_length = eoln_position - buffer_position_;
60 std::string result = buffer_.substr(buffer_position_, line_length);
61 buffer_position_ += line_length + 2;
62 return result;
63 }
64
ParseRequest()65 HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() {
66 DCHECK_NE(STATE_ACCEPTED, state_);
67 // Parse the request from beginning. However, entire request may not be
68 // available in the buffer.
69 if (state_ == STATE_HEADERS) {
70 if (ParseHeaders() == ACCEPTED)
71 return ACCEPTED;
72 }
73 // This should not be 'else if' of the previous block, as |state_| can be
74 // changed in ParseHeaders().
75 if (state_ == STATE_CONTENT) {
76 if (ParseContent() == ACCEPTED)
77 return ACCEPTED;
78 }
79 return WAITING;
80 }
81
ParseHeaders()82 HttpRequestParser::ParseResult HttpRequestParser::ParseHeaders() {
83 // Check if the all request headers are available.
84 if (buffer_.find("\r\n\r\n", buffer_position_) == std::string::npos)
85 return WAITING;
86
87 // Parse request's the first header line.
88 // Request main main header, eg. GET /foobar.html HTTP/1.1
89 std::string request_headers;
90 {
91 const std::string header_line = ShiftLine();
92 http_request_->all_headers += header_line + "\r\n";
93 std::vector<std::string> header_line_tokens = base::SplitString(
94 header_line, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
95 DCHECK_EQ(3u, header_line_tokens.size());
96 // Method.
97 http_request_->method_string = header_line_tokens[0];
98 http_request_->method = GetMethodType(http_request_->method_string);
99 // Target resource. See
100 // https://www.rfc-editor.org/rfc/rfc9112#name-request-line
101 // https://www.rfc-editor.org/rfc/rfc9110#name-determining-the-target-reso
102 if (http_request_->method == METHOD_CONNECT) {
103 // CONNECT uses a special authority-form. Just report the value as
104 // `relative_url`.
105 // https://www.rfc-editor.org/rfc/rfc9112#section-3.2.3
106 CHECK(!HostPortPair::FromString(header_line_tokens[1]).IsEmpty());
107 http_request_->relative_url = header_line_tokens[1];
108 } else if (http_request_->method == METHOD_OPTIONS &&
109 header_line_tokens[1] == "*") {
110 // OPTIONS allows a special asterisk-form for the request target.
111 // https://www.rfc-editor.org/rfc/rfc9112#section-3.2.4
112 http_request_->relative_url = "*";
113 } else {
114 // The request target should be origin-form, unless connecting through a
115 // proxy, in which case it is absolute-form.
116 // https://www.rfc-editor.org/rfc/rfc9112#name-origin-form
117 // https://www.rfc-editor.org/rfc/rfc9112#name-absolute-form
118 if (!header_line_tokens[1].empty() &&
119 header_line_tokens[1].front() == '/') {
120 http_request_->relative_url = header_line_tokens[1];
121 } else {
122 GURL url(header_line_tokens[1]);
123 CHECK(url.is_valid());
124 // TODO(crbug.com/1375303): This should retain the entire URL.
125 http_request_->relative_url = url.PathForRequest();
126 }
127 }
128
129 // Protocol.
130 const std::string protocol = base::ToLowerASCII(header_line_tokens[2]);
131 CHECK(protocol == "http/1.0" || protocol == "http/1.1") <<
132 "Protocol not supported: " << protocol;
133 }
134
135 // Parse further headers.
136 {
137 std::string header_name;
138 while (true) {
139 std::string header_line = ShiftLine();
140 if (header_line.empty())
141 break;
142
143 http_request_->all_headers += header_line + "\r\n";
144 if (header_line[0] == ' ' || header_line[0] == '\t') {
145 // Continuation of the previous multi-line header.
146 std::string header_value =
147 Trim(header_line.substr(1, header_line.size() - 1));
148 http_request_->headers[header_name] += " " + header_value;
149 } else {
150 // New header.
151 size_t delimiter_pos = header_line.find(":");
152 DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error.";
153 header_name = Trim(header_line.substr(0, delimiter_pos));
154 std::string header_value = Trim(header_line.substr(
155 delimiter_pos + 1,
156 header_line.size() - delimiter_pos - 1));
157 http_request_->headers[header_name] = header_value;
158 }
159 }
160 }
161
162 // Headers done. Is any content data attached to the request?
163 declared_content_length_ = 0;
164 if (http_request_->headers.count("Content-Length") > 0) {
165 http_request_->has_content = true;
166 const bool success = base::StringToSizeT(
167 http_request_->headers["Content-Length"],
168 &declared_content_length_);
169 if (!success) {
170 declared_content_length_ = 0;
171 LOG(WARNING) << "Malformed Content-Length header's value.";
172 }
173 } else if (http_request_->headers.count("Transfer-Encoding") > 0) {
174 if (base::CompareCaseInsensitiveASCII(
175 http_request_->headers["Transfer-Encoding"], "chunked") == 0) {
176 http_request_->has_content = true;
177 chunked_decoder_ = std::make_unique<HttpChunkedDecoder>();
178 state_ = STATE_CONTENT;
179 return WAITING;
180 }
181 }
182 if (declared_content_length_ == 0) {
183 // No content data, so parsing is finished.
184 state_ = STATE_ACCEPTED;
185 return ACCEPTED;
186 }
187
188 // The request has not yet been parsed yet, content data is still to be
189 // processed.
190 state_ = STATE_CONTENT;
191 return WAITING;
192 }
193
ParseContent()194 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() {
195 const size_t available_bytes = buffer_.size() - buffer_position_;
196 if (chunked_decoder_.get()) {
197 int bytes_written = chunked_decoder_->FilterBuf(
198 const_cast<char*>(buffer_.data()) + buffer_position_, available_bytes);
199 http_request_->content.append(buffer_.data() + buffer_position_,
200 bytes_written);
201
202 if (chunked_decoder_->reached_eof()) {
203 buffer_ =
204 buffer_.substr(buffer_.size() - chunked_decoder_->bytes_after_eof());
205 buffer_position_ = 0;
206 state_ = STATE_ACCEPTED;
207 return ACCEPTED;
208 }
209 buffer_ = "";
210 buffer_position_ = 0;
211 state_ = STATE_CONTENT;
212 return WAITING;
213 }
214
215 const size_t fetch_bytes = std::min(
216 available_bytes,
217 declared_content_length_ - http_request_->content.size());
218 http_request_->content.append(buffer_.data() + buffer_position_,
219 fetch_bytes);
220 buffer_position_ += fetch_bytes;
221
222 if (declared_content_length_ == http_request_->content.size()) {
223 state_ = STATE_ACCEPTED;
224 return ACCEPTED;
225 }
226
227 state_ = STATE_CONTENT;
228 return WAITING;
229 }
230
GetRequest()231 std::unique_ptr<HttpRequest> HttpRequestParser::GetRequest() {
232 DCHECK_EQ(STATE_ACCEPTED, state_);
233 std::unique_ptr<HttpRequest> result = std::move(http_request_);
234
235 // Prepare for parsing a new request.
236 state_ = STATE_HEADERS;
237 http_request_ = std::make_unique<HttpRequest>();
238 buffer_.clear();
239 buffer_position_ = 0;
240 declared_content_length_ = 0;
241
242 return result;
243 }
244
245 // static
GetMethodType(base::StringPiece token)246 HttpMethod HttpRequestParser::GetMethodType(base::StringPiece token) {
247 if (token == "GET") {
248 return METHOD_GET;
249 } else if (token == "HEAD") {
250 return METHOD_HEAD;
251 } else if (token == "POST") {
252 return METHOD_POST;
253 } else if (token == "PUT") {
254 return METHOD_PUT;
255 } else if (token == "DELETE") {
256 return METHOD_DELETE;
257 } else if (token == "PATCH") {
258 return METHOD_PATCH;
259 } else if (token == "CONNECT") {
260 return METHOD_CONNECT;
261 } else if (token == "OPTIONS") {
262 return METHOD_OPTIONS;
263 }
264 LOG(WARNING) << "Method not implemented: " << token;
265 return METHOD_UNKNOWN;
266 }
267
268 } // namespace net::test_server
269