1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/test/embedded_test_server/http_request.h"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "net/http/http_chunked_decoder.h"
16 namespace test_server
{
20 size_t kRequestSizeLimit
= 64 * 1024 * 1024; // 64 mb.
22 // Helper function used to trim tokens in http request headers.
23 std::string
Trim(const std::string
& value
) {
25 base::TrimString(value
, " \t", &result
);
31 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN
),
35 HttpRequest::~HttpRequest() {
38 HttpRequestParser::HttpRequestParser()
39 : http_request_(new HttpRequest()),
41 state_(STATE_HEADERS
),
42 declared_content_length_(0) {
45 HttpRequestParser::~HttpRequestParser() {
48 void HttpRequestParser::ProcessChunk(const base::StringPiece
& data
) {
49 data
.AppendToString(&buffer_
);
50 DCHECK_LE(buffer_
.size() + data
.size(), kRequestSizeLimit
) <<
51 "The HTTP request is too large.";
54 std::string
HttpRequestParser::ShiftLine() {
55 size_t eoln_position
= buffer_
.find("\r\n", buffer_position_
);
56 DCHECK_NE(std::string::npos
, eoln_position
);
57 const int line_length
= eoln_position
- buffer_position_
;
58 std::string result
= buffer_
.substr(buffer_position_
, line_length
);
59 buffer_position_
+= line_length
+ 2;
63 HttpRequestParser::ParseResult
HttpRequestParser::ParseRequest() {
64 DCHECK_NE(STATE_ACCEPTED
, state_
);
65 // Parse the request from beginning. However, entire request may not be
66 // available in the buffer.
67 if (state_
== STATE_HEADERS
) {
68 if (ParseHeaders() == ACCEPTED
)
71 // This should not be 'else if' of the previous block, as |state_| can be
72 // changed in ParseHeaders().
73 if (state_
== STATE_CONTENT
) {
74 if (ParseContent() == ACCEPTED
)
80 HttpRequestParser::ParseResult
HttpRequestParser::ParseHeaders() {
81 // Check if the all request headers are available.
82 if (buffer_
.find("\r\n\r\n", buffer_position_
) == std::string::npos
)
85 // Parse request's the first header line.
86 // Request main main header, eg. GET /foobar.html HTTP/1.1
87 std::string request_headers
;
89 const std::string header_line
= ShiftLine();
90 http_request_
->all_headers
+= header_line
+ "\r\n";
91 std::vector
<std::string
> header_line_tokens
= base::SplitString(
92 header_line
, " ", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
93 DCHECK_EQ(3u, header_line_tokens
.size());
95 http_request_
->method_string
= header_line_tokens
[0];
96 http_request_
->method
= GetMethodType(base::StringToLowerASCII(
97 header_line_tokens
[0]));
99 // Don't build an absolute URL as the parser does not know (should not
100 // know) anything about the server address.
101 http_request_
->relative_url
= header_line_tokens
[1];
103 const std::string protocol
=
104 base::StringToLowerASCII(header_line_tokens
[2]);
105 CHECK(protocol
== "http/1.0" || protocol
== "http/1.1") <<
106 "Protocol not supported: " << protocol
;
109 // Parse further headers.
111 std::string header_name
;
113 std::string header_line
= ShiftLine();
114 if (header_line
.empty())
117 http_request_
->all_headers
+= header_line
+ "\r\n";
118 if (header_line
[0] == ' ' || header_line
[0] == '\t') {
119 // Continuation of the previous multi-line header.
120 std::string header_value
=
121 Trim(header_line
.substr(1, header_line
.size() - 1));
122 http_request_
->headers
[header_name
] += " " + header_value
;
125 size_t delimiter_pos
= header_line
.find(":");
126 DCHECK_NE(std::string::npos
, delimiter_pos
) << "Syntax error.";
127 header_name
= Trim(header_line
.substr(0, delimiter_pos
));
128 std::string header_value
= Trim(header_line
.substr(
130 header_line
.size() - delimiter_pos
- 1));
131 http_request_
->headers
[header_name
] = header_value
;
136 // Headers done. Is any content data attached to the request?
137 declared_content_length_
= 0;
138 if (http_request_
->headers
.count("Content-Length") > 0) {
139 http_request_
->has_content
= true;
140 const bool success
= base::StringToSizeT(
141 http_request_
->headers
["Content-Length"],
142 &declared_content_length_
);
143 DCHECK(success
) << "Malformed Content-Length header's value.";
144 } else if (http_request_
->headers
.count("Transfer-Encoding") > 0) {
145 if (http_request_
->headers
["Transfer-Encoding"] == "chunked") {
146 http_request_
->has_content
= true;
147 chunked_decoder_
.reset(new HttpChunkedDecoder());
148 state_
= STATE_CONTENT
;
152 if (declared_content_length_
== 0) {
153 // No content data, so parsing is finished.
154 state_
= STATE_ACCEPTED
;
158 // The request has not yet been parsed yet, content data is still to be
160 state_
= STATE_CONTENT
;
164 HttpRequestParser::ParseResult
HttpRequestParser::ParseContent() {
165 const size_t available_bytes
= buffer_
.size() - buffer_position_
;
166 if (chunked_decoder_
.get()) {
167 int bytes_written
= chunked_decoder_
->FilterBuf(
168 const_cast<char*>(buffer_
.data()) + buffer_position_
, available_bytes
);
169 http_request_
->content
.append(buffer_
.data() + buffer_position_
,
172 if (chunked_decoder_
->reached_eof()) {
174 buffer_
.substr(buffer_
.size() - chunked_decoder_
->bytes_after_eof());
175 buffer_position_
= 0;
176 state_
= STATE_ACCEPTED
;
180 buffer_position_
= 0;
181 state_
= STATE_CONTENT
;
185 const size_t fetch_bytes
= std::min(
187 declared_content_length_
- http_request_
->content
.size());
188 http_request_
->content
.append(buffer_
.data() + buffer_position_
,
190 buffer_position_
+= fetch_bytes
;
192 if (declared_content_length_
== http_request_
->content
.size()) {
193 state_
= STATE_ACCEPTED
;
197 state_
= STATE_CONTENT
;
201 scoped_ptr
<HttpRequest
> HttpRequestParser::GetRequest() {
202 DCHECK_EQ(STATE_ACCEPTED
, state_
);
203 scoped_ptr
<HttpRequest
> result
= http_request_
.Pass();
205 // Prepare for parsing a new request.
206 state_
= STATE_HEADERS
;
207 http_request_
.reset(new HttpRequest());
209 buffer_position_
= 0;
210 declared_content_length_
= 0;
212 return result
.Pass();
215 HttpMethod
HttpRequestParser::GetMethodType(const std::string
& token
) const {
216 if (token
== "get") {
218 } else if (token
== "head") {
220 } else if (token
== "post") {
222 } else if (token
== "put") {
224 } else if (token
== "delete") {
225 return METHOD_DELETE
;
226 } else if (token
== "patch") {
229 NOTREACHED() << "Method not implemented: " << token
;
230 return METHOD_UNKNOWN
;
233 } // namespace test_server