OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/google_apis/test_server/http_request.h" | |
6 | |
7 #include <algorithm> | |
8 #include <map> | |
9 #include <string> | |
10 #include "base/basictypes.h" | |
11 #include "base/logging.h" | |
12 #include "base/string_number_conversions.h" | |
13 #include "base/string_split.h" | |
14 #include "base/string_util.h" | |
15 #include "googleurl/src/gurl.h" | |
16 | |
17 | |
18 namespace drive { | |
19 namespace test_server { | |
20 | |
21 namespace { | |
22 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. | |
23 | |
24 // Helper function used to trim tokens in http request headers. | |
25 std::string Trim(const std::string& value) { | |
26 std::string result; | |
27 TrimString(value, " \t", &result); | |
28 return result; | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 HttpRequest::HttpRequest() : method(UNKNOWN), | |
34 has_content(false) { | |
35 } | |
36 | |
37 HttpRequest::~HttpRequest() { | |
38 } | |
39 | |
40 HttpRequestParser::HttpRequestParser() | |
41 : http_request_(new HttpRequest()), | |
42 buffer_position_(0), | |
43 state_(STATE_HEADERS), | |
44 declared_content_length_(0) { | |
45 } | |
46 | |
47 HttpRequestParser::~HttpRequestParser() { | |
48 } | |
49 | |
50 void HttpRequestParser::ProcessChunk(const base::StringPiece& data) { | |
51 data.AppendToString(&buffer_); | |
52 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit) << | |
53 "The HTTP request is too large."; | |
54 } | |
55 | |
56 std::string HttpRequestParser::ShiftLine() { | |
57 size_t eoln_position = buffer_.find("\r\n", buffer_position_); | |
58 DCHECK_NE(std::string::npos, eoln_position); | |
59 const int line_length = eoln_position - buffer_position_; | |
60 std::string result = buffer_.substr(buffer_position_, line_length); | |
61 buffer_position_ += line_length + 2; | |
62 return result; | |
63 } | |
64 | |
65 HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() { | |
66 DCHECK_NE(state_, STATE_ACCEPTED); | |
67 // Parse the request from beginning. However, entire request may not be | |
68 // available in the buffer. | |
69 if (state_ == STATE_HEADERS) { | |
70 if (ParseHeaders() == ACCEPTED) | |
71 return ACCEPTED; | |
72 } | |
73 if (state_ == STATE_CONTENT) { | |
Lei Zhang
2012/11/14 04:24:15
Can this block be combined with the one immediatel
satorux1
2012/11/14 04:43:36
Done.
| |
74 if (ParseContent() == ACCEPTED) | |
75 return ACCEPTED; | |
76 } | |
77 return WAITING; | |
78 } | |
79 | |
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) | |
83 return WAITING; | |
84 | |
85 // Parse request's the first header line. | |
86 // Request main main header, eg. GET /foobar.html HTTP/1.1 | |
87 { | |
88 const std::string header_line = ShiftLine(); | |
89 std::vector<std::string> header_line_tokens; | |
90 base::SplitString(header_line, ' ', &header_line_tokens); | |
91 DCHECK_EQ(header_line_tokens.size(), 3u); | |
Lei Zhang
2012/11/14 04:24:15
nit: Preferably, keep the order identical to EXPEC
satorux1
2012/11/14 04:43:36
Done.
| |
92 // Method. | |
93 http_request_->method = GetMethodType(StringToLowerASCII( | |
94 header_line_tokens[0])); | |
95 // Address. | |
96 const GURL host = GURL("http://localhost/"); | |
97 http_request_->uri = host.Resolve(header_line_tokens[1]); | |
98 // Protocol. | |
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; | |
102 } | |
103 | |
104 // Parse further headers. | |
105 { | |
106 std::string header_name; | |
107 while (true) { | |
108 std::string header_line = ShiftLine(); | |
109 if (header_line.empty()) | |
110 break; | |
111 | |
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; | |
117 } else { | |
118 // New header. | |
119 size_t delimiter_pos = header_line.find(":"); | |
120 DCHECK_NE(delimiter_pos, std::string::npos) << "Syntax error."; | |
121 header_name = Trim(header_line.substr(0, delimiter_pos)); | |
122 std::string header_value = Trim(header_line.substr( | |
123 delimiter_pos + 1, | |
124 header_line.size() - delimiter_pos - 1)); | |
125 http_request_->headers[header_name] = header_value; | |
126 } | |
127 } | |
128 } | |
129 | |
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 DCHECK(base::StringToSizeT(http_request_->headers["Content-Length"], | |
Lei Zhang
2012/11/14 04:24:15
This line won't get executed in a release build. I
satorux1
2012/11/14 04:28:00
No. This is not OK. We should fix this. Great catc
satorux1
2012/11/14 04:43:36
Done.
| |
135 &declared_content_length_)) << | |
136 "Malformed Content-Length header's value."; | |
137 } | |
138 if (declared_content_length_ == 0) { | |
139 // No content data, so parsing is finished. | |
140 state_ = STATE_ACCEPTED; | |
141 return ACCEPTED; | |
142 } | |
143 | |
144 // The request has not yet been parsed yet, content data is still to be | |
145 // processed. | |
146 state_ = STATE_CONTENT; | |
147 return WAITING; | |
148 } | |
149 | |
150 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { | |
151 const size_t available_bytes = buffer_.size() - buffer_position_; | |
152 const size_t fetch_bytes = std::min( | |
153 available_bytes, | |
154 declared_content_length_ - http_request_->content.size()); | |
155 http_request_->content.append(buffer_.data() + buffer_position_, | |
156 fetch_bytes); | |
157 buffer_position_ += fetch_bytes; | |
158 | |
159 if (declared_content_length_ == http_request_->content.size()) { | |
160 state_ = STATE_ACCEPTED; | |
161 return ACCEPTED; | |
162 } | |
163 | |
164 state_ = STATE_CONTENT; | |
165 return WAITING; | |
166 } | |
167 | |
168 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() { | |
169 DCHECK_EQ(state_, STATE_ACCEPTED); | |
170 scoped_ptr<HttpRequest> result = http_request_.Pass(); | |
171 | |
172 // Prepare for parsing a new request. | |
173 state_ = STATE_HEADERS; | |
174 http_request_.reset(new HttpRequest()); | |
175 buffer_ = ""; | |
satorux1
2012/11/14 03:33:57
nit: buffer_.clear();
satorux1
2012/11/14 04:43:36
Done.
| |
176 buffer_position_ = 0; | |
177 declared_content_length_ = 0; | |
178 | |
179 return result.Pass(); | |
180 } | |
181 | |
182 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const { | |
183 if (token == "get") { | |
184 return GET; | |
185 } else if (token == "head") { | |
186 return HEAD; | |
187 } else if (token == "post") { | |
188 return POST; | |
189 } else if (token == "put") { | |
190 return PUT; | |
191 } else if (token == "delete") { | |
192 return DELETE; | |
193 } | |
194 NOTREACHED() << "Method not implemented: " << token; | |
195 return UNKNOWN; | |
196 } | |
197 | |
198 } // namespace test_server | |
199 } // namespace drive | |
OLD | NEW |