Chromium Code Reviews| 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/chromeos/drive/test_servers/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_util.h" | |
| 13 #include "googleurl/src/gurl.h" | |
| 14 | |
| 15 | |
| 16 namespace drive { | |
| 17 namespace test_servers { | |
| 18 | |
| 19 namespace { | |
| 20 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. | |
| 21 } // namespace | |
| 22 | |
| 23 HttpRequest::HttpRequest() : method(UNKNOWN) { | |
| 24 } | |
| 25 | |
| 26 HttpRequest::~HttpRequest() { | |
| 27 } | |
| 28 | |
| 29 HttpRequestParser::HttpRequestParser() : http_request_(new HttpRequest()), | |
| 30 state_(STATE_HEADERS), | |
| 31 buffer_position_(0), | |
| 32 current_content_length_(0) { | |
|
satorux1
2012/11/12 06:07:00
Maybe:
HttpRequestParser::HttpRequestParser()
mtomasz
2012/11/12 12:17:44
Done.
| |
| 33 } | |
| 34 | |
| 35 HttpRequestParser::~HttpRequestParser() { | |
| 36 } | |
| 37 | |
| 38 void HttpRequestParser::ProcessChunk(const char *data, size_t length) { | |
| 39 buffer_.append(data, length); | |
|
satorux1
2012/11/12 06:07:00
If you use StringPiece for buffer_position_, it sh
mtomasz
2012/11/12 12:17:44
I am not sure if I understand correctly. So, we wi
satorux1
2012/11/13 05:13:18
Yes, but it's no worse than now. We already have t
mtomasz
2012/11/13 12:23:07
I am still not convinced. To append, the code will
satorux1
2012/11/14 01:54:31
You are right that this is tricky. Let's keep buff
| |
| 40 DCHECK(buffer_.length() + length <= kRequestSizeLimit) << | |
|
satorux1
2012/11/12 06:07:00
DCHECK_LE
mtomasz
2012/11/12 12:17:44
Why is it better? Done.
satorux1
2012/11/13 05:13:18
The failure message is better. You'll see the two
mtomasz
2012/11/13 12:23:07
Got it.
| |
| 41 "The HTTP request is too large."; | |
| 42 } | |
| 43 | |
| 44 std::string HttpRequestParser::ShiftLine() { | |
| 45 size_t eoln_position = buffer_.find("\r\n", buffer_position_); | |
| 46 if (eoln_position == std::string::npos) | |
| 47 return ""; | |
| 48 const int line_length = eoln_position - buffer_position_; | |
| 49 std::string result = buffer_.substr(buffer_position_, line_length); | |
| 50 buffer_position_ += line_length + 2; | |
| 51 return result; | |
|
satorux1
2012/11/12 06:07:00
I think it'd be simpler to use StringPiece for buf
mtomasz
2012/11/12 12:17:44
See above. This would be great if we could easily
| |
| 52 } | |
| 53 | |
| 54 HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() { | |
| 55 DCHECK(state_ != STATE_ACCEPTED); | |
|
satorux1
2012/11/12 06:07:00
DCHECK_NE
mtomasz
2012/11/12 12:17:44
Done.
| |
| 56 // Parse the request from beginning. However, entire request may not be | |
| 57 // available in the buffer. | |
| 58 if (state_ == STATE_HEADERS) { | |
| 59 if (ParseHeaders() == ACCEPTED) | |
| 60 return ACCEPTED; | |
| 61 } | |
| 62 if (state_ == STATE_CONTENT) { | |
| 63 if (ParseContent() == ACCEPTED) | |
| 64 return ACCEPTED; | |
| 65 } | |
| 66 return WAITING; | |
| 67 } | |
| 68 | |
| 69 HttpRequestParser::ParseResult HttpRequestParser::ParseHeaders() { | |
| 70 // Check if the whole request input is available. | |
|
satorux1
2012/11/12 06:07:00
request input is -> request headers are
mtomasz
2012/11/12 12:17:44
Done.
| |
| 71 if (buffer_.find("\r\n\r\n", buffer_position_) == std::string::npos) | |
|
satorux1
2012/11/12 06:07:00
if you use StringPiece: buffer_position_.find("\r\
mtomasz
2012/11/12 12:17:44
Ack. Let's discuss offline.
| |
| 72 return WAITING; | |
| 73 | |
| 74 // Parse request's the first header line. | |
| 75 // Request main main header, eg. GET /foobar.html HTTP/1.1 | |
| 76 { | |
| 77 const std::string header_line = ShiftLine(); | |
| 78 std::vector<std::string> header_line_tokens; | |
| 79 Tokenize(header_line, " ", &header_line_tokens); | |
|
satorux1
2012/11/12 06:07:00
Can we use SplitString() instead? SplitString() is
mtomasz
2012/11/12 12:17:44
Done!
| |
| 80 DCHECK(header_line_tokens.size() == 3); | |
| 81 // Method. | |
| 82 http_request_->method = GetMethodType(StringToLowerASCII( | |
| 83 header_line_tokens[0])); | |
| 84 // Address. | |
| 85 const GURL host = GURL("http://localhost/"); | |
| 86 http_request_->uri = host.Resolve(header_line_tokens[1]); | |
| 87 // Protocol. | |
| 88 const std::string protocol = StringToLowerASCII(header_line_tokens[2]); | |
| 89 CHECK(protocol == "http/1.0" || protocol == "http/1.1") << | |
| 90 "Protocol not supported: " << protocol; | |
| 91 } | |
| 92 | |
| 93 // Parse further headers. | |
| 94 { | |
| 95 std::string header_line = ShiftLine(); | |
| 96 std::string header_name; | |
| 97 while (header_line != "") { | |
|
satorux1
2012/11/12 06:07:00
!header_line.empty()
mtomasz
2012/11/12 12:17:44
Done.
| |
| 98 if (header_line[0] == ' ') { | |
|
satorux1
2012/11/12 06:07:00
|| header_line[0] == '\t'
mtomasz
2012/11/12 12:17:44
Done.
| |
| 99 // Continuation of the previous multi-line header. | |
| 100 http_request_->headers[header_name] += "\n" + | |
|
satorux1
2012/11/12 06:07:00
IIRC, I think white spaces should be interpreted a
mtomasz
2012/11/12 12:17:44
You're right. Done.
| |
| 101 header_line.substr(1, header_line.length() - 1); | |
|
satorux1
2012/11/12 06:07:00
Please use size(). I think size() is more widely u
mtomasz
2012/11/12 12:17:44
Done.
| |
| 102 } else { | |
| 103 // New header. | |
| 104 size_t delimiter_pos = header_line.find(":"); | |
| 105 DCHECK(delimiter_pos != std::string::npos) << "Syntax error."; | |
| 106 header_name = header_line.substr(0, delimiter_pos); | |
| 107 size_t header_value_pos = delimiter_pos + 1; | |
| 108 // Skip spaces (if any) after the colon. | |
| 109 while (header_value_pos < header_line.length() && | |
| 110 header_line[header_value_pos] == ' ') { | |
| 111 header_value_pos++; | |
| 112 } | |
| 113 http_request_->headers[header_name] = | |
| 114 header_line.substr(header_value_pos, | |
| 115 header_line.length() - header_value_pos); | |
| 116 } | |
| 117 header_line = ShiftLine(); | |
|
satorux1
2012/11/12 06:07:00
Maybe the loop is easier to read with:
while (tru
mtomasz
2012/11/12 12:17:44
Done.
| |
| 118 } | |
| 119 } | |
| 120 | |
| 121 // Headers done. Is any content data attached to the request? | |
| 122 if (http_request_->headers.find("Content-Length") != | |
|
satorux1
2012/11/12 06:07:00
more like a matter of taste, but the following is
mtomasz
2012/11/12 12:17:44
Done.
| |
| 123 http_request_->headers.end()) { | |
| 124 current_content_length_ = | |
| 125 atoi(http_request_->headers["Content-Length"].c_str()); | |
|
satorux1
2012/11/12 06:07:00
Please use StringToSizeT in base/string_number_con
mtomasz
2012/11/12 12:17:44
Done.
| |
| 126 if (!current_content_length_) | |
|
satorux1
2012/11/12 06:07:00
current_content_length_ == 0
mtomasz
2012/11/12 12:17:44
Done.
| |
| 127 return ACCEPTED; | |
| 128 } | |
|
satorux1
2012/11/12 06:07:00
If Content-Length: is not present, I think we can
mtomasz
2012/11/12 12:17:44
Right! Done.
| |
| 129 | |
| 130 // The request has not yet been parsed, content data is still to be parsed. | |
| 131 state_ = STATE_CONTENT; | |
| 132 return WAITING; | |
| 133 } | |
| 134 | |
| 135 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { | |
| 136 const size_t available_bytes = buffer_.length() - buffer_position_; | |
| 137 const size_t fetch_bytes = std::min(available_bytes, | |
| 138 current_content_length_); | |
|
satorux1
2012/11/12 06:07:00
Are you trying to limit the size of |http_request_
mtomasz
2012/11/12 12:17:44
Fixed std::min(). Done.
As for the DCHECK, I thin
| |
| 139 http_request_->content.append(buffer_.data() + buffer_position_, | |
| 140 fetch_bytes); | |
| 141 buffer_position_ += fetch_bytes; | |
|
satorux1
2012/11/12 06:07:00
If you use StringPiece for buffer_position_, the c
mtomasz
2012/11/12 12:17:44
Ack. Let's discuss.
| |
| 142 | |
| 143 if (current_content_length_ == http_request_->content.length()) { | |
| 144 state_ = STATE_ACCEPTED; | |
| 145 return ACCEPTED; | |
| 146 } | |
| 147 | |
| 148 return WAITING; | |
| 149 } | |
| 150 | |
| 151 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() { | |
| 152 DCHECK(state_ == STATE_ACCEPTED); | |
| 153 scoped_ptr<HttpRequest> result = http_request_.Pass(); | |
| 154 | |
| 155 // Prepare for parsing a new request. | |
| 156 state_ = STATE_HEADERS; | |
| 157 http_request_.reset(new HttpRequest()); | |
| 158 buffer_ = buffer_.substr(buffer_position_, | |
|
satorux1
2012/11/12 06:07:00
Why not just buffer_.clear()?
At this moment, All
mtomasz
2012/11/12 12:17:44
This is in case, we get two (or more) requests in
satorux1
2012/11/13 05:13:18
I think we don't need to support the case. The ser
mtomasz
2012/11/13 12:23:07
I've just checked the rfc. In http/1.1 all connect
satorux1
2012/11/14 01:34:32
Let's remove the support for the multiple requests
mtomasz
2012/11/14 03:23:35
Done.
| |
| 159 buffer_.length() - buffer_position_); | |
| 160 buffer_position_ = 0; | |
| 161 current_content_length_ = 0; | |
| 162 | |
| 163 return result.Pass(); | |
| 164 } | |
| 165 | |
| 166 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const { | |
| 167 if (token == "get") { | |
| 168 return GET; | |
| 169 } else if (token == "head") { | |
| 170 return HEAD; | |
| 171 } else if (token == "post") { | |
| 172 return POST; | |
| 173 } else if (token == "put") { | |
| 174 return PUT; | |
| 175 } else if (token == "delete") { | |
| 176 return DELETE; | |
| 177 } | |
| 178 NOTREACHED() << "Method not implemented: " << token; | |
| 179 return UNKNOWN; | |
| 180 } | |
| 181 | |
| 182 } // namespace test_servers | |
| 183 } // namespace drive | |
| OLD | NEW |