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 "chrome/browser/google_apis/test_server/http_request.h"
9 #include "base/logging.h"
10 #include "base/string_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
14 namespace google_apis
{
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 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
= GetMethodType(StringToLowerASCII(
93 header_line_tokens
[0]));
95 // Don't build an absolute URL as the parser does not know (should not
96 // know) anything about the server address.
97 http_request_
->relative_url
= header_line_tokens
[1];
99 const std::string protocol
= StringToLowerASCII(header_line_tokens
[2]);
100 CHECK(protocol
== "http/1.0" || protocol
== "http/1.1") <<
101 "Protocol not supported: " << protocol
;
104 // Parse further headers.
106 std::string header_name
;
108 std::string header_line
= ShiftLine();
109 if (header_line
.empty())
112 if (header_line
[0] == ' ' || header_line
[0] == '\t') {
113 // Continuation of the previous multi-line header.
114 std::string header_value
=
115 Trim(header_line
.substr(1, header_line
.size() - 1));
116 http_request_
->headers
[header_name
] += " " + header_value
;
119 size_t delimiter_pos
= header_line
.find(":");
120 DCHECK_NE(std::string::npos
, delimiter_pos
) << "Syntax error.";
121 header_name
= Trim(header_line
.substr(0, delimiter_pos
));
122 std::string header_value
= Trim(header_line
.substr(
124 header_line
.size() - delimiter_pos
- 1));
125 http_request_
->headers
[header_name
] = header_value
;
130 // Headers done. Is any content data attached to the request?
131 declared_content_length_
= 0;
132 if (http_request_
->headers
.count("Content-Length") > 0) {
133 http_request_
->has_content
= true;
134 const bool success
= base::StringToSizeT(
135 http_request_
->headers
["Content-Length"],
136 &declared_content_length_
);
137 DCHECK(success
) << "Malformed Content-Length header's value.";
139 if (declared_content_length_
== 0) {
140 // No content data, so parsing is finished.
141 state_
= STATE_ACCEPTED
;
145 // The request has not yet been parsed yet, content data is still to be
147 state_
= STATE_CONTENT
;
151 HttpRequestParser::ParseResult
HttpRequestParser::ParseContent() {
152 const size_t available_bytes
= buffer_
.size() - buffer_position_
;
153 const size_t fetch_bytes
= std::min(
155 declared_content_length_
- http_request_
->content
.size());
156 http_request_
->content
.append(buffer_
.data() + buffer_position_
,
158 buffer_position_
+= fetch_bytes
;
160 if (declared_content_length_
== http_request_
->content
.size()) {
161 state_
= STATE_ACCEPTED
;
165 state_
= STATE_CONTENT
;
169 scoped_ptr
<HttpRequest
> HttpRequestParser::GetRequest() {
170 DCHECK_EQ(STATE_ACCEPTED
, state_
);
171 scoped_ptr
<HttpRequest
> result
= http_request_
.Pass();
173 // Prepare for parsing a new request.
174 state_
= STATE_HEADERS
;
175 http_request_
.reset(new HttpRequest());
177 buffer_position_
= 0;
178 declared_content_length_
= 0;
180 return result
.Pass();
183 HttpMethod
HttpRequestParser::GetMethodType(const std::string
& token
) const {
184 if (token
== "get") {
186 } else if (token
== "head") {
188 } else if (token
== "post") {
190 } else if (token
== "put") {
192 } else if (token
== "delete") {
193 return METHOD_DELETE
;
194 } else if (token
== "patch") {
197 NOTREACHED() << "Method not implemented: " << token
;
198 return METHOD_UNKNOWN
;
201 } // namespace test_server
202 } // namespace google_apis