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"
15 namespace test_server
{
19 size_t kRequestSizeLimit
= 64 * 1024 * 1024; // 64 mb.
21 // Helper function used to trim tokens in http request headers.
22 std::string
Trim(const std::string
& value
) {
24 base::TrimString(value
, " \t", &result
);
30 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN
),
34 HttpRequest::~HttpRequest() {
37 HttpRequestParser::HttpRequestParser()
38 : http_request_(new HttpRequest()),
40 state_(STATE_HEADERS
),
41 declared_content_length_(0) {
44 HttpRequestParser::~HttpRequestParser() {
47 void HttpRequestParser::ProcessChunk(const base::StringPiece
& data
) {
48 data
.AppendToString(&buffer_
);
49 DCHECK_LE(buffer_
.size() + data
.size(), kRequestSizeLimit
) <<
50 "The HTTP request is too large.";
53 std::string
HttpRequestParser::ShiftLine() {
54 size_t eoln_position
= buffer_
.find("\r\n", buffer_position_
);
55 DCHECK_NE(std::string::npos
, eoln_position
);
56 const int line_length
= eoln_position
- buffer_position_
;
57 std::string result
= buffer_
.substr(buffer_position_
, line_length
);
58 buffer_position_
+= line_length
+ 2;
62 HttpRequestParser::ParseResult
HttpRequestParser::ParseRequest() {
63 DCHECK_NE(STATE_ACCEPTED
, state_
);
64 // Parse the request from beginning. However, entire request may not be
65 // available in the buffer.
66 if (state_
== STATE_HEADERS
) {
67 if (ParseHeaders() == ACCEPTED
)
70 // This should not be 'else if' of the previous block, as |state_| can be
71 // changed in ParseHeaders().
72 if (state_
== STATE_CONTENT
) {
73 if (ParseContent() == ACCEPTED
)
79 HttpRequestParser::ParseResult
HttpRequestParser::ParseHeaders() {
80 // Check if the all request headers are available.
81 if (buffer_
.find("\r\n\r\n", buffer_position_
) == std::string::npos
)
84 // Parse request's the first header line.
85 // Request main main header, eg. GET /foobar.html HTTP/1.1
87 const std::string header_line
= ShiftLine();
88 std::vector
<std::string
> header_line_tokens
;
89 base::SplitString(header_line
, ' ', &header_line_tokens
);
90 DCHECK_EQ(3u, header_line_tokens
.size());
92 http_request_
->method_string
= header_line_tokens
[0];
93 http_request_
->method
= GetMethodType(base::StringToLowerASCII(
94 header_line_tokens
[0]));
96 // Don't build an absolute URL as the parser does not know (should not
97 // know) anything about the server address.
98 http_request_
->relative_url
= header_line_tokens
[1];
100 const std::string protocol
=
101 base::StringToLowerASCII(header_line_tokens
[2]);
102 CHECK(protocol
== "http/1.0" || protocol
== "http/1.1") <<
103 "Protocol not supported: " << protocol
;
106 // Parse further headers.
108 std::string header_name
;
110 std::string header_line
= ShiftLine();
111 if (header_line
.empty())
114 if (header_line
[0] == ' ' || header_line
[0] == '\t') {
115 // Continuation of the previous multi-line header.
116 std::string header_value
=
117 Trim(header_line
.substr(1, header_line
.size() - 1));
118 http_request_
->headers
[header_name
] += " " + header_value
;
121 size_t delimiter_pos
= header_line
.find(":");
122 DCHECK_NE(std::string::npos
, delimiter_pos
) << "Syntax error.";
123 header_name
= Trim(header_line
.substr(0, delimiter_pos
));
124 std::string header_value
= Trim(header_line
.substr(
126 header_line
.size() - delimiter_pos
- 1));
127 http_request_
->headers
[header_name
] = header_value
;
132 // Headers done. Is any content data attached to the request?
133 declared_content_length_
= 0;
134 if (http_request_
->headers
.count("Content-Length") > 0) {
135 http_request_
->has_content
= true;
136 const bool success
= base::StringToSizeT(
137 http_request_
->headers
["Content-Length"],
138 &declared_content_length_
);
139 DCHECK(success
) << "Malformed Content-Length header's value.";
141 if (declared_content_length_
== 0) {
142 // No content data, so parsing is finished.
143 state_
= STATE_ACCEPTED
;
147 // The request has not yet been parsed yet, content data is still to be
149 state_
= STATE_CONTENT
;
153 HttpRequestParser::ParseResult
HttpRequestParser::ParseContent() {
154 const size_t available_bytes
= buffer_
.size() - buffer_position_
;
155 const size_t fetch_bytes
= std::min(
157 declared_content_length_
- http_request_
->content
.size());
158 http_request_
->content
.append(buffer_
.data() + buffer_position_
,
160 buffer_position_
+= fetch_bytes
;
162 if (declared_content_length_
== http_request_
->content
.size()) {
163 state_
= STATE_ACCEPTED
;
167 state_
= STATE_CONTENT
;
171 scoped_ptr
<HttpRequest
> HttpRequestParser::GetRequest() {
172 DCHECK_EQ(STATE_ACCEPTED
, state_
);
173 scoped_ptr
<HttpRequest
> result
= http_request_
.Pass();
175 // Prepare for parsing a new request.
176 state_
= STATE_HEADERS
;
177 http_request_
.reset(new HttpRequest());
179 buffer_position_
= 0;
180 declared_content_length_
= 0;
182 return result
.Pass();
185 HttpMethod
HttpRequestParser::GetMethodType(const std::string
& token
) const {
186 if (token
== "get") {
188 } else if (token
== "head") {
190 } else if (token
== "post") {
192 } else if (token
== "put") {
194 } else if (token
== "delete") {
195 return METHOD_DELETE
;
196 } else if (token
== "patch") {
199 NOTREACHED() << "Method not implemented: " << token
;
200 return METHOD_UNKNOWN
;
203 } // namespace test_server