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