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) | |
72 return ACCEPTED; | |
73 } | |
74 // This should not be 'else if' of the previous block, as |state_| can be | |
75 // changed in ParseHeaders(). | |
76 if (state_ == STATE_CONTENT) { | |
77 if (ParseContent() == ACCEPTED) | |
78 return ACCEPTED; | |
79 } | |
80 return WAITING; | |
81 } | |
82 | |
83 HttpRequestParser::ParseResult HttpRequestParser::ParseHeaders() { | |
84 // Check if the all request headers are available. | |
85 if (buffer_.find("\r\n\r\n", buffer_position_) == std::string::npos) | |
86 return WAITING; | |
87 | |
88 // Parse request's the first header line. | |
89 // Request main main header, eg. GET /foobar.html HTTP/1.1 | |
90 { | |
91 const std::string header_line = ShiftLine(); | |
92 std::vector<std::string> header_line_tokens; | |
93 base::SplitString(header_line, ' ', &header_line_tokens); | |
94 DCHECK_EQ(3u, header_line_tokens.size()); | |
95 // Method. | |
96 http_request_->method = GetMethodType(StringToLowerASCII( | |
97 header_line_tokens[0])); | |
98 // Address. | |
99 const GURL host = GURL("http://localhost/"); | |
100 http_request_->url = host.Resolve(header_line_tokens[1]); | |
101 // Protocol. | |
102 const std::string protocol = StringToLowerASCII(header_line_tokens[2]); | |
103 CHECK(protocol == "http/1.0" || protocol == "http/1.1") << | |
104 "Protocol not supported: " << protocol; | |
105 } | |
106 | |
107 // Parse further headers. | |
108 { | |
109 std::string header_name; | |
110 while (true) { | |
111 std::string header_line = ShiftLine(); | |
112 if (header_line.empty()) | |
113 break; | |
114 | |
115 if (header_line[0] == ' ' || header_line[0] == '\t') { | |
116 // Continuation of the previous multi-line header. | |
117 std::string header_value = | |
118 Trim(header_line.substr(1, header_line.size() - 1)); | |
119 http_request_->headers[header_name] += " " + header_value; | |
120 } else { | |
121 // New header. | |
122 size_t delimiter_pos = header_line.find(":"); | |
123 DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error."; | |
124 header_name = Trim(header_line.substr(0, delimiter_pos)); | |
125 std::string header_value = Trim(header_line.substr( | |
126 delimiter_pos + 1, | |
127 header_line.size() - delimiter_pos - 1)); | |
128 http_request_->headers[header_name] = header_value; | |
129 } | |
130 } | |
131 } | |
132 | |
133 // Headers done. Is any content data attached to the request? | |
134 declared_content_length_ = 0; | |
135 if (http_request_->headers.count("Content-Length") > 0) { | |
136 http_request_->has_content = true; | |
137 const bool success = base::StringToSizeT( | |
138 http_request_->headers["Content-Length"], | |
139 &declared_content_length_); | |
140 DCHECK(success) << "Malformed Content-Length header's value."; | |
141 } | |
142 if (declared_content_length_ == 0) { | |
143 // No content data, so parsing is finished. | |
144 state_ = STATE_ACCEPTED; | |
145 return ACCEPTED; | |
146 } | |
147 | |
148 // The request has not yet been parsed yet, content data is still to be | |
149 // processed. | |
150 state_ = STATE_CONTENT; | |
151 return WAITING; | |
152 } | |
153 | |
154 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { | |
155 const size_t available_bytes = buffer_.size() - buffer_position_; | |
156 const size_t fetch_bytes = std::min( | |
157 available_bytes, | |
158 declared_content_length_ - http_request_->content.size()); | |
159 http_request_->content.append(buffer_.data() + buffer_position_, | |
160 fetch_bytes); | |
161 buffer_position_ += fetch_bytes; | |
162 | |
163 if (declared_content_length_ == http_request_->content.size()) { | |
164 state_ = STATE_ACCEPTED; | |
165 return ACCEPTED; | |
166 } | |
167 | |
168 state_ = STATE_CONTENT; | |
169 return WAITING; | |
170 } | |
171 | |
172 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() { | |
173 DCHECK_EQ(STATE_ACCEPTED, state_); | |
174 scoped_ptr<HttpRequest> result = http_request_.Pass(); | |
175 | |
176 // Prepare for parsing a new request. | |
177 state_ = STATE_HEADERS; | |
178 http_request_.reset(new HttpRequest()); | |
179 buffer_.clear(); | |
180 buffer_position_ = 0; | |
181 declared_content_length_ = 0; | |
182 | |
183 return result.Pass(); | |
184 } | |
185 | |
186 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const { | |
187 if (token == "get") { | |
188 return GET; | |
189 } else if (token == "head") { | |
190 return HEAD; | |
191 } else if (token == "post") { | |
192 return POST; | |
193 } else if (token == "put") { | |
194 return PUT; | |
195 } else if (token == "delete") { | |
196 return DELETE; | |
197 } | |
198 NOTREACHED() << "Method not implemented: " << token; | |
199 return UNKNOWN; | |
200 } | |
201 | |
202 } // namespace test_server | |
203 } // namespace drive | |
OLD | NEW |