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
=
97 GetMethodType(base::ToLowerASCII(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
= base::ToLowerASCII(header_line_tokens
[2]);
104 CHECK(protocol
== "http/1.0" || protocol
== "http/1.1") <<
105 "Protocol not supported: " << protocol
;
108 // Parse further headers.
110 std::string header_name
;
112 std::string header_line
= ShiftLine();
113 if (header_line
.empty())
116 http_request_
->all_headers
+= header_line
+ "\r\n";
117 if (header_line
[0] == ' ' || header_line
[0] == '\t') {
118 // Continuation of the previous multi-line header.
119 std::string header_value
=
120 Trim(header_line
.substr(1, header_line
.size() - 1));
121 http_request_
->headers
[header_name
] += " " + header_value
;
124 size_t delimiter_pos
= header_line
.find(":");
125 DCHECK_NE(std::string::npos
, delimiter_pos
) << "Syntax error.";
126 header_name
= Trim(header_line
.substr(0, delimiter_pos
));
127 std::string header_value
= Trim(header_line
.substr(
129 header_line
.size() - delimiter_pos
- 1));
130 http_request_
->headers
[header_name
] = header_value
;
135 // Headers done. Is any content data attached to the request?
136 declared_content_length_
= 0;
137 if (http_request_
->headers
.count("Content-Length") > 0) {
138 http_request_
->has_content
= true;
139 const bool success
= base::StringToSizeT(
140 http_request_
->headers
["Content-Length"],
141 &declared_content_length_
);
142 DCHECK(success
) << "Malformed Content-Length header's value.";
143 } else if (http_request_
->headers
.count("Transfer-Encoding") > 0) {
144 if (http_request_
->headers
["Transfer-Encoding"] == "chunked") {
145 http_request_
->has_content
= true;
146 chunked_decoder_
.reset(new HttpChunkedDecoder());
147 state_
= STATE_CONTENT
;
151 if (declared_content_length_
== 0) {
152 // No content data, so parsing is finished.
153 state_
= STATE_ACCEPTED
;
157 // The request has not yet been parsed yet, content data is still to be
159 state_
= STATE_CONTENT
;
163 HttpRequestParser::ParseResult
HttpRequestParser::ParseContent() {
164 const size_t available_bytes
= buffer_
.size() - buffer_position_
;
165 if (chunked_decoder_
.get()) {
166 int bytes_written
= chunked_decoder_
->FilterBuf(
167 const_cast<char*>(buffer_
.data()) + buffer_position_
, available_bytes
);
168 http_request_
->content
.append(buffer_
.data() + buffer_position_
,
171 if (chunked_decoder_
->reached_eof()) {
173 buffer_
.substr(buffer_
.size() - chunked_decoder_
->bytes_after_eof());
174 buffer_position_
= 0;
175 state_
= STATE_ACCEPTED
;
179 buffer_position_
= 0;
180 state_
= STATE_CONTENT
;
184 const size_t fetch_bytes
= std::min(
186 declared_content_length_
- http_request_
->content
.size());
187 http_request_
->content
.append(buffer_
.data() + buffer_position_
,
189 buffer_position_
+= fetch_bytes
;
191 if (declared_content_length_
== http_request_
->content
.size()) {
192 state_
= STATE_ACCEPTED
;
196 state_
= STATE_CONTENT
;
200 scoped_ptr
<HttpRequest
> HttpRequestParser::GetRequest() {
201 DCHECK_EQ(STATE_ACCEPTED
, state_
);
202 scoped_ptr
<HttpRequest
> result
= http_request_
.Pass();
204 // Prepare for parsing a new request.
205 state_
= STATE_HEADERS
;
206 http_request_
.reset(new HttpRequest());
208 buffer_position_
= 0;
209 declared_content_length_
= 0;
211 return result
.Pass();
214 HttpMethod
HttpRequestParser::GetMethodType(const std::string
& token
) const {
215 if (token
== "get") {
217 } else if (token
== "head") {
219 } else if (token
== "post") {
221 } else if (token
== "put") {
223 } else if (token
== "delete") {
224 return METHOD_DELETE
;
225 } else if (token
== "patch") {
228 NOTREACHED() << "Method not implemented: " << token
;
229 return METHOD_UNKNOWN
;
232 } // namespace test_server