Update V8 to version 4.7.50.
[chromium-blink-merge.git] / net / test / embedded_test_server / http_request.cc
blob9e0c80f96fa092aa2ce05dee5975ba9c68734625
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.
5 #include "net/test/embedded_test_server/http_request.h"
7 #include <algorithm>
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"
15 namespace net {
16 namespace test_server {
18 namespace {
20 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb.
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;
29 } // namespace
31 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN),
32 has_content(false) {
35 HttpRequest::~HttpRequest() {
38 HttpRequestParser::HttpRequestParser()
39 : http_request_(new HttpRequest()),
40 buffer_position_(0),
41 state_(STATE_HEADERS),
42 declared_content_length_(0) {
45 HttpRequestParser::~HttpRequestParser() {
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.";
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;
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;
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;
77 return WAITING;
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;
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;
89 const std::string header_line = ShiftLine();
90 http_request_->all_headers += header_line + "\r\n";
91 std::vector<std::string> header_line_tokens = base::SplitString(
92 header_line, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
93 DCHECK_EQ(3u, header_line_tokens.size());
94 // Method.
95 http_request_->method_string = header_line_tokens[0];
96 http_request_->method =
97 GetMethodType(base::ToLowerASCII(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 = base::ToLowerASCII(header_line_tokens[2]);
104 CHECK(protocol == "http/1.0" || protocol == "http/1.1") <<
105 "Protocol not supported: " << protocol;
108 // Parse further headers.
110 std::string header_name;
111 while (true) {
112 std::string header_line = ShiftLine();
113 if (header_line.empty())
114 break;
116 http_request_->all_headers += header_line + "\r\n";
117 if (header_line[0] == ' ' || header_line[0] == '\t') {
118 // Continuation of the previous multi-line header.
119 std::string header_value =
120 Trim(header_line.substr(1, header_line.size() - 1));
121 http_request_->headers[header_name] += " " + header_value;
122 } else {
123 // New header.
124 size_t delimiter_pos = header_line.find(":");
125 DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error.";
126 header_name = Trim(header_line.substr(0, delimiter_pos));
127 std::string header_value = Trim(header_line.substr(
128 delimiter_pos + 1,
129 header_line.size() - delimiter_pos - 1));
130 http_request_->headers[header_name] = header_value;
135 // Headers done. Is any content data attached to the request?
136 declared_content_length_ = 0;
137 if (http_request_->headers.count("Content-Length") > 0) {
138 http_request_->has_content = true;
139 const bool success = base::StringToSizeT(
140 http_request_->headers["Content-Length"],
141 &declared_content_length_);
142 DCHECK(success) << "Malformed Content-Length header's value.";
143 } else if (http_request_->headers.count("Transfer-Encoding") > 0) {
144 if (http_request_->headers["Transfer-Encoding"] == "chunked") {
145 http_request_->has_content = true;
146 chunked_decoder_.reset(new HttpChunkedDecoder());
147 state_ = STATE_CONTENT;
148 return WAITING;
151 if (declared_content_length_ == 0) {
152 // No content data, so parsing is finished.
153 state_ = STATE_ACCEPTED;
154 return ACCEPTED;
157 // The request has not yet been parsed yet, content data is still to be
158 // processed.
159 state_ = STATE_CONTENT;
160 return WAITING;
163 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() {
164 const size_t available_bytes = buffer_.size() - buffer_position_;
165 if (chunked_decoder_.get()) {
166 int bytes_written = chunked_decoder_->FilterBuf(
167 const_cast<char*>(buffer_.data()) + buffer_position_, available_bytes);
168 http_request_->content.append(buffer_.data() + buffer_position_,
169 bytes_written);
171 if (chunked_decoder_->reached_eof()) {
172 buffer_ =
173 buffer_.substr(buffer_.size() - chunked_decoder_->bytes_after_eof());
174 buffer_position_ = 0;
175 state_ = STATE_ACCEPTED;
176 return ACCEPTED;
178 buffer_ = "";
179 buffer_position_ = 0;
180 state_ = STATE_CONTENT;
181 return WAITING;
184 const size_t fetch_bytes = std::min(
185 available_bytes,
186 declared_content_length_ - http_request_->content.size());
187 http_request_->content.append(buffer_.data() + buffer_position_,
188 fetch_bytes);
189 buffer_position_ += fetch_bytes;
191 if (declared_content_length_ == http_request_->content.size()) {
192 state_ = STATE_ACCEPTED;
193 return ACCEPTED;
196 state_ = STATE_CONTENT;
197 return WAITING;
200 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() {
201 DCHECK_EQ(STATE_ACCEPTED, state_);
202 scoped_ptr<HttpRequest> result = http_request_.Pass();
204 // Prepare for parsing a new request.
205 state_ = STATE_HEADERS;
206 http_request_.reset(new HttpRequest());
207 buffer_.clear();
208 buffer_position_ = 0;
209 declared_content_length_ = 0;
211 return result.Pass();
214 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const {
215 if (token == "get") {
216 return METHOD_GET;
217 } else if (token == "head") {
218 return METHOD_HEAD;
219 } else if (token == "post") {
220 return METHOD_POST;
221 } else if (token == "put") {
222 return METHOD_PUT;
223 } else if (token == "delete") {
224 return METHOD_DELETE;
225 } else if (token == "patch") {
226 return METHOD_PATCH;
228 NOTREACHED() << "Method not implemented: " << token;
229 return METHOD_UNKNOWN;
232 } // namespace test_server
233 } // namespace net