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 | |
11 #include "base/basictypes.h" | |
12 #include "base/logging.h" | |
13 #include "base/string_number_conversions.h" | |
14 #include "base/string_split.h" | |
15 #include "base/string_util.h" | |
16 #include "googleurl/src/gurl.h" | |
17 | |
18 | |
19 namespace drive { | |
20 namespace test_server { | |
21 | |
22 namespace { | |
23 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. | |
24 | |
25 // Helper function used to trim tokens in http request headers. | |
26 std::string Trim(const std::string& value) { | |
27 std::string result; | |
28 TrimString(value, " \t", &result); | |
29 return result; | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 HttpRequest::HttpRequest() : method(UNKNOWN), | |
35 has_content(false) { | |
36 } | |
37 | |
38 HttpRequest::~HttpRequest() { | |
39 } | |
40 | |
41 HttpRequestParser::HttpRequestParser() | |
42 : http_request_(new HttpRequest()), | |
43 buffer_position_(0), | |
44 state_(STATE_HEADERS), | |
45 declared_content_length_(0) { | |
46 } | |
47 | |
48 HttpRequestParser::~HttpRequestParser() { | |
49 } | |
50 | |
51 void HttpRequestParser::ProcessChunk(const base::StringPiece& data) { | |
52 data.AppendToString(&buffer_); | |
53 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit) << | |
54 "The HTTP request is too large."; | |
55 } | |
56 | |
57 std::string HttpRequestParser::ShiftLine() { | |
58 size_t eoln_position = buffer_.find("\r\n", buffer_position_); | |
59 DCHECK_NE(std::string::npos, eoln_position); | |
60 const int line_length = eoln_position - buffer_position_; | |
61 std::string result = buffer_.substr(buffer_position_, line_length); | |
62 buffer_position_ += line_length + 2; | |
63 return result; | |
64 } | |
65 | |
66 HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() { | |
67 DCHECK_NE(STATE_ACCEPTED, state_); | |
68 // Parse the request from beginning. However, entire request may not be | |
69 // available in the buffer. | |
70 if (state_ == STATE_HEADERS) { | |
71 if (ParseHeaders() == ACCEPTED) | |
mtomasz
2012/11/14 05:00:44
There must not be else here. It will fail if we ha
satorux1
2012/11/14 05:04:10
Ah, I see. state_ can be changed in ParseHeaders()
Lei Zhang
2012/11/14 05:05:43
Ah, did not realize ParseHeaders() changes |state_
| |
72 return ACCEPTED; | |
73 } else if (state_ == STATE_CONTENT) { | |
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(3u, header_line_tokens.size()); | |
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(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( | |
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 const bool success = base::StringToSizeT( | |
135 http_request_->headers["Content-Length"], | |
136 &declared_content_length_); | |
137 DCHECK(success) << | |
138 "Malformed Content-Length header's value."; | |
Lei Zhang
2012/11/14 05:05:43
this fits on the previous line, right?
satorux1
2012/11/14 05:13:39
Done.
| |
139 } | |
140 if (declared_content_length_ == 0) { | |
141 // No content data, so parsing is finished. | |
142 state_ = STATE_ACCEPTED; | |
143 return ACCEPTED; | |
144 } | |
145 | |
146 // The request has not yet been parsed yet, content data is still to be | |
147 // processed. | |
148 state_ = STATE_CONTENT; | |
149 return WAITING; | |
150 } | |
151 | |
152 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { | |
153 const size_t available_bytes = buffer_.size() - buffer_position_; | |
154 const size_t fetch_bytes = std::min( | |
155 available_bytes, | |
156 declared_content_length_ - http_request_->content.size()); | |
157 http_request_->content.append(buffer_.data() + buffer_position_, | |
158 fetch_bytes); | |
159 buffer_position_ += fetch_bytes; | |
160 | |
161 if (declared_content_length_ == http_request_->content.size()) { | |
162 state_ = STATE_ACCEPTED; | |
163 return ACCEPTED; | |
164 } | |
165 | |
166 state_ = STATE_CONTENT; | |
167 return WAITING; | |
168 } | |
169 | |
170 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() { | |
171 DCHECK_EQ(STATE_ACCEPTED, state_); | |
172 scoped_ptr<HttpRequest> result = http_request_.Pass(); | |
173 | |
174 // Prepare for parsing a new request. | |
175 state_ = STATE_HEADERS; | |
176 http_request_.reset(new HttpRequest()); | |
177 buffer_.clear(); | |
178 buffer_position_ = 0; | |
179 declared_content_length_ = 0; | |
180 | |
181 return result.Pass(); | |
182 } | |
183 | |
184 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const { | |
185 if (token == "get") { | |
186 return GET; | |
187 } else if (token == "head") { | |
188 return HEAD; | |
189 } else if (token == "post") { | |
190 return POST; | |
191 } else if (token == "put") { | |
192 return PUT; | |
193 } else if (token == "delete") { | |
194 return DELETE; | |
195 } | |
196 NOTREACHED() << "Method not implemented: " << token; | |
197 return UNKNOWN; | |
198 } | |
199 | |
200 } // namespace test_server | |
201 } // namespace drive | |
OLD | NEW |