Add include.
[chromium-blink-merge.git] / net / test / embedded_test_server / http_request.cc
blob3acb550292f38bc420bad1ccc5020025c66997ff
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"
14 namespace net {
15 namespace test_server {
17 namespace {
19 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb.
21 // Helper function used to trim tokens in http request headers.
22 std::string Trim(const std::string& value) {
23 std::string result;
24 base::TrimString(value, " \t", &result);
25 return result;
28 } // namespace
30 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN),
31 has_content(false) {
34 HttpRequest::~HttpRequest() {
37 HttpRequestParser::HttpRequestParser()
38 : http_request_(new HttpRequest()),
39 buffer_position_(0),
40 state_(STATE_HEADERS),
41 declared_content_length_(0) {
44 HttpRequestParser::~HttpRequestParser() {
47 void HttpRequestParser::ProcessChunk(const base::StringPiece& data) {
48 data.AppendToString(&buffer_);
49 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit) <<
50 "The HTTP request is too large.";
53 std::string HttpRequestParser::ShiftLine() {
54 size_t eoln_position = buffer_.find("\r\n", buffer_position_);
55 DCHECK_NE(std::string::npos, eoln_position);
56 const int line_length = eoln_position - buffer_position_;
57 std::string result = buffer_.substr(buffer_position_, line_length);
58 buffer_position_ += line_length + 2;
59 return result;
62 HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() {
63 DCHECK_NE(STATE_ACCEPTED, state_);
64 // Parse the request from beginning. However, entire request may not be
65 // available in the buffer.
66 if (state_ == STATE_HEADERS) {
67 if (ParseHeaders() == ACCEPTED)
68 return ACCEPTED;
70 // This should not be 'else if' of the previous block, as |state_| can be
71 // changed in ParseHeaders().
72 if (state_ == STATE_CONTENT) {
73 if (ParseContent() == ACCEPTED)
74 return ACCEPTED;
76 return WAITING;
79 HttpRequestParser::ParseResult HttpRequestParser::ParseHeaders() {
80 // Check if the all request headers are available.
81 if (buffer_.find("\r\n\r\n", buffer_position_) == std::string::npos)
82 return WAITING;
84 // Parse request's the first header line.
85 // Request main main header, eg. GET /foobar.html HTTP/1.1
87 const std::string header_line = ShiftLine();
88 std::vector<std::string> header_line_tokens;
89 base::SplitString(header_line, ' ', &header_line_tokens);
90 DCHECK_EQ(3u, header_line_tokens.size());
91 // Method.
92 http_request_->method_string = header_line_tokens[0];
93 http_request_->method = GetMethodType(base::StringToLowerASCII(
94 header_line_tokens[0]));
95 // Address.
96 // Don't build an absolute URL as the parser does not know (should not
97 // know) anything about the server address.
98 http_request_->relative_url = header_line_tokens[1];
99 // Protocol.
100 const std::string protocol =
101 base::StringToLowerASCII(header_line_tokens[2]);
102 CHECK(protocol == "http/1.0" || protocol == "http/1.1") <<
103 "Protocol not supported: " << protocol;
106 // Parse further headers.
108 std::string header_name;
109 while (true) {
110 std::string header_line = ShiftLine();
111 if (header_line.empty())
112 break;
114 if (header_line[0] == ' ' || header_line[0] == '\t') {
115 // Continuation of the previous multi-line header.
116 std::string header_value =
117 Trim(header_line.substr(1, header_line.size() - 1));
118 http_request_->headers[header_name] += " " + header_value;
119 } else {
120 // New header.
121 size_t delimiter_pos = header_line.find(":");
122 DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error.";
123 header_name = Trim(header_line.substr(0, delimiter_pos));
124 std::string header_value = Trim(header_line.substr(
125 delimiter_pos + 1,
126 header_line.size() - delimiter_pos - 1));
127 http_request_->headers[header_name] = header_value;
132 // Headers done. Is any content data attached to the request?
133 declared_content_length_ = 0;
134 if (http_request_->headers.count("Content-Length") > 0) {
135 http_request_->has_content = true;
136 const bool success = base::StringToSizeT(
137 http_request_->headers["Content-Length"],
138 &declared_content_length_);
139 DCHECK(success) << "Malformed Content-Length header's value.";
141 if (declared_content_length_ == 0) {
142 // No content data, so parsing is finished.
143 state_ = STATE_ACCEPTED;
144 return ACCEPTED;
147 // The request has not yet been parsed yet, content data is still to be
148 // processed.
149 state_ = STATE_CONTENT;
150 return WAITING;
153 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() {
154 const size_t available_bytes = buffer_.size() - buffer_position_;
155 const size_t fetch_bytes = std::min(
156 available_bytes,
157 declared_content_length_ - http_request_->content.size());
158 http_request_->content.append(buffer_.data() + buffer_position_,
159 fetch_bytes);
160 buffer_position_ += fetch_bytes;
162 if (declared_content_length_ == http_request_->content.size()) {
163 state_ = STATE_ACCEPTED;
164 return ACCEPTED;
167 state_ = STATE_CONTENT;
168 return WAITING;
171 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() {
172 DCHECK_EQ(STATE_ACCEPTED, state_);
173 scoped_ptr<HttpRequest> result = http_request_.Pass();
175 // Prepare for parsing a new request.
176 state_ = STATE_HEADERS;
177 http_request_.reset(new HttpRequest());
178 buffer_.clear();
179 buffer_position_ = 0;
180 declared_content_length_ = 0;
182 return result.Pass();
185 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const {
186 if (token == "get") {
187 return METHOD_GET;
188 } else if (token == "head") {
189 return METHOD_HEAD;
190 } else if (token == "post") {
191 return METHOD_POST;
192 } else if (token == "put") {
193 return METHOD_PUT;
194 } else if (token == "delete") {
195 return METHOD_DELETE;
196 } else if (token == "patch") {
197 return METHOD_PATCH;
199 NOTREACHED() << "Method not implemented: " << token;
200 return METHOD_UNKNOWN;
203 } // namespace test_server
204 } // namespace net