Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / net / test / embedded_test_server / http_request.cc
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
14 namespace net {
15 namespace test_server {
16
17 namespace {
18
19 size_t kRequestSizeLimit = 64 * 1024 * 1024;  // 64 mb.
20
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;
26 }
27
28 }  // namespace
29
30 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN),
31                              has_content(false) {
32 }
33
34 HttpRequest::~HttpRequest() {
35 }
36
37 HttpRequestParser::HttpRequestParser()
38     : http_request_(new HttpRequest()),
39       buffer_position_(0),
40       state_(STATE_HEADERS),
41       declared_content_length_(0) {
42 }
43
44 HttpRequestParser::~HttpRequestParser() {
45 }
46
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.";
51 }
52
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;
60 }
61
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;
69   }
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;
75   }
76   return WAITING;
77 }
78
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;
83
84   // Parse request's the first header line.
85   // Request main main header, eg. GET /foobar.html HTTP/1.1
86   {
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 = GetMethodType(base::StringToLowerASCII(
93         header_line_tokens[0]));
94     // Address.
95     // Don't build an absolute URL as the parser does not know (should not
96     // know) anything about the server address.
97     http_request_->relative_url = header_line_tokens[1];
98     // Protocol.
99     const std::string protocol =
100         base::StringToLowerASCII(header_line_tokens[2]);
101     CHECK(protocol == "http/1.0" || protocol == "http/1.1") <<
102         "Protocol not supported: " << protocol;
103   }
104
105   // Parse further headers.
106   {
107     std::string header_name;
108     while (true) {
109       std::string header_line = ShiftLine();
110       if (header_line.empty())
111         break;
112
113       if (header_line[0] == ' ' || header_line[0] == '\t') {
114         // Continuation of the previous multi-line header.
115         std::string header_value =
116             Trim(header_line.substr(1, header_line.size() - 1));
117         http_request_->headers[header_name] += " " + header_value;
118       } else {
119         // New header.
120         size_t delimiter_pos = header_line.find(":");
121         DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error.";
122         header_name = Trim(header_line.substr(0, delimiter_pos));
123         std::string header_value = Trim(header_line.substr(
124             delimiter_pos + 1,
125             header_line.size() - delimiter_pos - 1));
126         http_request_->headers[header_name] = header_value;
127       }
128     }
129   }
130
131   // Headers done. Is any content data attached to the request?
132   declared_content_length_ = 0;
133   if (http_request_->headers.count("Content-Length") > 0) {
134     http_request_->has_content = true;
135     const bool success = base::StringToSizeT(
136         http_request_->headers["Content-Length"],
137         &declared_content_length_);
138     DCHECK(success) << "Malformed Content-Length header's value.";
139   }
140   if (declared_content_length_ == 0) {
141     // No content data, so parsing is finished.
142     state_ = STATE_ACCEPTED;
143     return ACCEPTED;
144   }
145
146   // The request has not yet been parsed yet, content data is still to be
147   // processed.
148   state_ = STATE_CONTENT;
149   return WAITING;
150 }
151
152 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() {
153   const size_t available_bytes = buffer_.size() - buffer_position_;
154   const size_t fetch_bytes = std::min(
155       available_bytes,
156       declared_content_length_ - http_request_->content.size());
157   http_request_->content.append(buffer_.data() + buffer_position_,
158                                 fetch_bytes);
159   buffer_position_ += fetch_bytes;
160
161   if (declared_content_length_ == http_request_->content.size()) {
162     state_ = STATE_ACCEPTED;
163     return ACCEPTED;
164   }
165
166   state_ = STATE_CONTENT;
167   return WAITING;
168 }
169
170 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() {
171   DCHECK_EQ(STATE_ACCEPTED, state_);
172   scoped_ptr<HttpRequest> result = http_request_.Pass();
173
174   // Prepare for parsing a new request.
175   state_ = STATE_HEADERS;
176   http_request_.reset(new HttpRequest());
177   buffer_.clear();
178   buffer_position_ = 0;
179   declared_content_length_ = 0;
180
181   return result.Pass();
182 }
183
184 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const {
185   if (token == "get") {
186     return METHOD_GET;
187   } else if (token == "head") {
188     return METHOD_HEAD;
189   } else if (token == "post") {
190     return METHOD_POST;
191   } else if (token == "put") {
192     return METHOD_PUT;
193   } else if (token == "delete") {
194     return METHOD_DELETE;
195   } else if (token == "patch") {
196     return METHOD_PATCH;
197   }
198   NOTREACHED() << "Method not implemented: " << token;
199   return METHOD_UNKNOWN;
200 }
201
202 }  // namespace test_server
203 }  // namespace net