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 "net/test/embedded_test_server/http_request.h" | |
6 | |
7 #include <algorithm> | |
8 | |
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" | |
13 #include "net/http/http_chunked_decoder.h" | |
14 | |
15 namespace net { | |
16 namespace test_server { | |
17 | |
18 namespace { | |
19 | |
20 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. | |
21 | |
22 // Helper function used to trim tokens in http request headers. | |
23 std::string Trim(const std::string& value) { | |
24 std::string result; | |
25 base::TrimString(value, " \t", &result); | |
26 return result; | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN), | |
32 has_content(false) { | |
33 } | |
34 | |
35 HttpRequest::~HttpRequest() { | |
36 } | |
37 | |
38 HttpRequestParser::HttpRequestParser() | |
39 : http_request_(new HttpRequest()), | |
40 buffer_position_(0), | |
41 state_(STATE_HEADERS), | |
42 declared_content_length_(0) { | |
43 } | |
44 | |
45 HttpRequestParser::~HttpRequestParser() { | |
46 } | |
47 | |
48 void HttpRequestParser::ProcessChunk(const base::StringPiece& data) { | |
49 data.AppendToString(&buffer_); | |
50 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit) << | |
51 "The HTTP request is too large."; | |
52 } | |
53 | |
54 std::string HttpRequestParser::ShiftLine() { | |
55 size_t eoln_position = buffer_.find("\r\n", buffer_position_); | |
56 DCHECK_NE(std::string::npos, eoln_position); | |
57 const int line_length = eoln_position - buffer_position_; | |
58 std::string result = buffer_.substr(buffer_position_, line_length); | |
59 buffer_position_ += line_length + 2; | |
60 return result; | |
61 } | |
62 | |
63 HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() { | |
64 DCHECK_NE(STATE_ACCEPTED, state_); | |
65 // Parse the request from beginning. However, entire request may not be | |
66 // available in the buffer. | |
67 if (state_ == STATE_HEADERS) { | |
68 if (ParseHeaders() == ACCEPTED) | |
69 return ACCEPTED; | |
70 } | |
71 // This should not be 'else if' of the previous block, as |state_| can be | |
72 // changed in ParseHeaders(). | |
73 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 std::string request_headers; | |
88 { | |
89 const std::string header_line = ShiftLine(); | |
90 http_request_->all_headers += header_line + "\r\n"; | |
91 std::vector<std::string> header_line_tokens; | |
92 base::SplitString(header_line, ' ', &header_line_tokens); | |
93 DCHECK_EQ(3u, header_line_tokens.size()); | |
94 // Method. | |
95 http_request_->method_string = header_line_tokens[0]; | |
96 http_request_->method = GetMethodType(base::StringToLowerASCII( | |
97 header_line_tokens[0])); | |
98 // Address. | |
99 // Don't build an absolute URL as the parser does not know (should not | |
100 // know) anything about the server address. | |
101 http_request_->relative_url = header_line_tokens[1]; | |
102 // Protocol. | |
103 const std::string protocol = | |
104 base::StringToLowerASCII(header_line_tokens[2]); | |
105 CHECK(protocol == "http/1.0" || protocol == "http/1.1") << | |
106 "Protocol not supported: " << protocol; | |
107 } | |
108 | |
109 // Parse further headers. | |
110 { | |
111 std::string header_name; | |
112 while (true) { | |
113 std::string header_line = ShiftLine(); | |
114 if (header_line.empty()) | |
115 break; | |
116 | |
117 http_request_->all_headers += header_line + "\r\n"; | |
118 if (header_line[0] == ' ' || header_line[0] == '\t') { | |
119 // Continuation of the previous multi-line header. | |
120 std::string header_value = | |
121 Trim(header_line.substr(1, header_line.size() - 1)); | |
122 http_request_->headers[header_name] += " " + header_value; | |
123 } else { | |
124 // New header. | |
125 size_t delimiter_pos = header_line.find(":"); | |
126 DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error."; | |
127 header_name = Trim(header_line.substr(0, delimiter_pos)); | |
128 std::string header_value = Trim(header_line.substr( | |
129 delimiter_pos + 1, | |
130 header_line.size() - delimiter_pos - 1)); | |
131 http_request_->headers[header_name] = header_value; | |
132 } | |
133 } | |
134 } | |
135 | |
136 // Headers done. Is any content data attached to the request? | |
137 declared_content_length_ = 0; | |
138 if (http_request_->headers.count("Content-Length") > 0) { | |
139 http_request_->has_content = true; | |
140 const bool success = base::StringToSizeT( | |
141 http_request_->headers["Content-Length"], | |
142 &declared_content_length_); | |
143 DCHECK(success) << "Malformed Content-Length header's value."; | |
144 } else if (http_request_->headers.count("Transfer-Encoding") > 0) { | |
145 if (http_request_->headers["Transfer-Encoding"] == "chunked") { | |
146 http_request_->has_content = true; | |
147 chunked_decoder_.reset(new HttpChunkedDecoder()); | |
148 state_ = STATE_CONTENT; | |
149 return WAITING; | |
150 } | |
151 } | |
152 if (declared_content_length_ == 0) { | |
153 // No content data, so parsing is finished. | |
154 state_ = STATE_ACCEPTED; | |
155 return ACCEPTED; | |
156 } | |
157 | |
158 // The request has not yet been parsed yet, content data is still to be | |
159 // processed. | |
160 state_ = STATE_CONTENT; | |
161 return WAITING; | |
162 } | |
163 | |
164 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { | |
165 const size_t available_bytes = buffer_.size() - buffer_position_; | |
166 if (chunked_decoder_.get()) { | |
167 int bytes_written = chunked_decoder_->FilterBuf( | |
168 const_cast<char*>(buffer_.data()) + buffer_position_, available_bytes); | |
169 http_request_->content.append(buffer_.data() + buffer_position_, | |
170 bytes_written); | |
171 | |
172 if (chunked_decoder_->reached_eof()) { | |
173 buffer_ = | |
174 buffer_.substr(buffer_.size() - chunked_decoder_->bytes_after_eof()); | |
175 buffer_position_ = 0; | |
176 state_ = STATE_ACCEPTED; | |
177 return ACCEPTED; | |
178 } | |
179 buffer_ = ""; | |
180 buffer_position_ = 0; | |
181 state_ = STATE_CONTENT; | |
182 return WAITING; | |
183 } | |
184 | |
185 const size_t fetch_bytes = std::min( | |
186 available_bytes, | |
187 declared_content_length_ - http_request_->content.size()); | |
188 http_request_->content.append(buffer_.data() + buffer_position_, | |
189 fetch_bytes); | |
190 buffer_position_ += fetch_bytes; | |
191 | |
192 if (declared_content_length_ == http_request_->content.size()) { | |
193 state_ = STATE_ACCEPTED; | |
194 return ACCEPTED; | |
195 } | |
196 | |
197 state_ = STATE_CONTENT; | |
198 return WAITING; | |
199 } | |
200 | |
201 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() { | |
202 DCHECK_EQ(STATE_ACCEPTED, state_); | |
203 scoped_ptr<HttpRequest> result = http_request_.Pass(); | |
204 | |
205 // Prepare for parsing a new request. | |
206 state_ = STATE_HEADERS; | |
207 http_request_.reset(new HttpRequest()); | |
208 buffer_.clear(); | |
209 buffer_position_ = 0; | |
210 declared_content_length_ = 0; | |
211 | |
212 return result.Pass(); | |
213 } | |
214 | |
215 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const { | |
216 if (token == "get") { | |
217 return METHOD_GET; | |
218 } else if (token == "head") { | |
219 return METHOD_HEAD; | |
220 } else if (token == "post") { | |
221 return METHOD_POST; | |
222 } else if (token == "put") { | |
223 return METHOD_PUT; | |
224 } else if (token == "delete") { | |
225 return METHOD_DELETE; | |
226 } else if (token == "patch") { | |
227 return METHOD_PATCH; | |
228 } | |
229 NOTREACHED() << "Method not implemented: " << token; | |
230 return METHOD_UNKNOWN; | |
231 } | |
232 | |
233 } // namespace test_server | |
234 } // namespace net | |
OLD | NEW |