Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: net/test/embedded_test_server/http_request.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/test/embedded_test_server/http_request.h" 5 #include "net/test/embedded_test_server/http_request.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.h"
13 13
14 namespace net { 14 namespace net {
15 namespace test_server { 15 namespace test_server {
16 16
17 namespace { 17 namespace {
18 18
19 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. 19 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb.
20 20
21 // Helper function used to trim tokens in http request headers. 21 // Helper function used to trim tokens in http request headers.
22 std::string Trim(const std::string& value) { 22 std::string Trim(const std::string& value) {
23 std::string result; 23 std::string result;
24 base::TrimString(value, " \t", &result); 24 base::TrimString(value, " \t", &result);
25 return result; 25 return result;
26 } 26 }
27 27
28 } // namespace 28 } // namespace
29 29
30 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN), 30 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN), has_content(false) {
31 has_content(false) {
32 } 31 }
33 32
34 HttpRequest::~HttpRequest() { 33 HttpRequest::~HttpRequest() {
35 } 34 }
36 35
37 HttpRequestParser::HttpRequestParser() 36 HttpRequestParser::HttpRequestParser()
38 : http_request_(new HttpRequest()), 37 : http_request_(new HttpRequest()),
39 buffer_position_(0), 38 buffer_position_(0),
40 state_(STATE_HEADERS), 39 state_(STATE_HEADERS),
41 declared_content_length_(0) { 40 declared_content_length_(0) {
42 } 41 }
43 42
44 HttpRequestParser::~HttpRequestParser() { 43 HttpRequestParser::~HttpRequestParser() {
45 } 44 }
46 45
47 void HttpRequestParser::ProcessChunk(const base::StringPiece& data) { 46 void HttpRequestParser::ProcessChunk(const base::StringPiece& data) {
48 data.AppendToString(&buffer_); 47 data.AppendToString(&buffer_);
49 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit) << 48 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit)
50 "The HTTP request is too large."; 49 << "The HTTP request is too large.";
51 } 50 }
52 51
53 std::string HttpRequestParser::ShiftLine() { 52 std::string HttpRequestParser::ShiftLine() {
54 size_t eoln_position = buffer_.find("\r\n", buffer_position_); 53 size_t eoln_position = buffer_.find("\r\n", buffer_position_);
55 DCHECK_NE(std::string::npos, eoln_position); 54 DCHECK_NE(std::string::npos, eoln_position);
56 const int line_length = eoln_position - buffer_position_; 55 const int line_length = eoln_position - buffer_position_;
57 std::string result = buffer_.substr(buffer_position_, line_length); 56 std::string result = buffer_.substr(buffer_position_, line_length);
58 buffer_position_ += line_length + 2; 57 buffer_position_ += line_length + 2;
59 return result; 58 return result;
60 } 59 }
(...skipping 21 matching lines...) Expand all
82 return WAITING; 81 return WAITING;
83 82
84 // Parse request's the first header line. 83 // Parse request's the first header line.
85 // Request main main header, eg. GET /foobar.html HTTP/1.1 84 // Request main main header, eg. GET /foobar.html HTTP/1.1
86 { 85 {
87 const std::string header_line = ShiftLine(); 86 const std::string header_line = ShiftLine();
88 std::vector<std::string> header_line_tokens; 87 std::vector<std::string> header_line_tokens;
89 base::SplitString(header_line, ' ', &header_line_tokens); 88 base::SplitString(header_line, ' ', &header_line_tokens);
90 DCHECK_EQ(3u, header_line_tokens.size()); 89 DCHECK_EQ(3u, header_line_tokens.size());
91 // Method. 90 // Method.
92 http_request_->method = GetMethodType(StringToLowerASCII( 91 http_request_->method =
93 header_line_tokens[0])); 92 GetMethodType(StringToLowerASCII(header_line_tokens[0]));
94 // Address. 93 // Address.
95 // Don't build an absolute URL as the parser does not know (should not 94 // Don't build an absolute URL as the parser does not know (should not
96 // know) anything about the server address. 95 // know) anything about the server address.
97 http_request_->relative_url = header_line_tokens[1]; 96 http_request_->relative_url = header_line_tokens[1];
98 // Protocol. 97 // Protocol.
99 const std::string protocol = StringToLowerASCII(header_line_tokens[2]); 98 const std::string protocol = StringToLowerASCII(header_line_tokens[2]);
100 CHECK(protocol == "http/1.0" || protocol == "http/1.1") << 99 CHECK(protocol == "http/1.0" || protocol == "http/1.1")
101 "Protocol not supported: " << protocol; 100 << "Protocol not supported: " << protocol;
102 } 101 }
103 102
104 // Parse further headers. 103 // Parse further headers.
105 { 104 {
106 std::string header_name; 105 std::string header_name;
107 while (true) { 106 while (true) {
108 std::string header_line = ShiftLine(); 107 std::string header_line = ShiftLine();
109 if (header_line.empty()) 108 if (header_line.empty())
110 break; 109 break;
111 110
112 if (header_line[0] == ' ' || header_line[0] == '\t') { 111 if (header_line[0] == ' ' || header_line[0] == '\t') {
113 // Continuation of the previous multi-line header. 112 // Continuation of the previous multi-line header.
114 std::string header_value = 113 std::string header_value =
115 Trim(header_line.substr(1, header_line.size() - 1)); 114 Trim(header_line.substr(1, header_line.size() - 1));
116 http_request_->headers[header_name] += " " + header_value; 115 http_request_->headers[header_name] += " " + header_value;
117 } else { 116 } else {
118 // New header. 117 // New header.
119 size_t delimiter_pos = header_line.find(":"); 118 size_t delimiter_pos = header_line.find(":");
120 DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error."; 119 DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error.";
121 header_name = Trim(header_line.substr(0, delimiter_pos)); 120 header_name = Trim(header_line.substr(0, delimiter_pos));
122 std::string header_value = Trim(header_line.substr( 121 std::string header_value = Trim(header_line.substr(
123 delimiter_pos + 1, 122 delimiter_pos + 1, header_line.size() - delimiter_pos - 1));
124 header_line.size() - delimiter_pos - 1));
125 http_request_->headers[header_name] = header_value; 123 http_request_->headers[header_name] = header_value;
126 } 124 }
127 } 125 }
128 } 126 }
129 127
130 // Headers done. Is any content data attached to the request? 128 // Headers done. Is any content data attached to the request?
131 declared_content_length_ = 0; 129 declared_content_length_ = 0;
132 if (http_request_->headers.count("Content-Length") > 0) { 130 if (http_request_->headers.count("Content-Length") > 0) {
133 http_request_->has_content = true; 131 http_request_->has_content = true;
134 const bool success = base::StringToSizeT( 132 const bool success = base::StringToSizeT(
135 http_request_->headers["Content-Length"], 133 http_request_->headers["Content-Length"], &declared_content_length_);
136 &declared_content_length_);
137 DCHECK(success) << "Malformed Content-Length header's value."; 134 DCHECK(success) << "Malformed Content-Length header's value.";
138 } 135 }
139 if (declared_content_length_ == 0) { 136 if (declared_content_length_ == 0) {
140 // No content data, so parsing is finished. 137 // No content data, so parsing is finished.
141 state_ = STATE_ACCEPTED; 138 state_ = STATE_ACCEPTED;
142 return ACCEPTED; 139 return ACCEPTED;
143 } 140 }
144 141
145 // The request has not yet been parsed yet, content data is still to be 142 // The request has not yet been parsed yet, content data is still to be
146 // processed. 143 // processed.
147 state_ = STATE_CONTENT; 144 state_ = STATE_CONTENT;
148 return WAITING; 145 return WAITING;
149 } 146 }
150 147
151 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { 148 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() {
152 const size_t available_bytes = buffer_.size() - buffer_position_; 149 const size_t available_bytes = buffer_.size() - buffer_position_;
153 const size_t fetch_bytes = std::min( 150 const size_t fetch_bytes =
154 available_bytes, 151 std::min(available_bytes,
155 declared_content_length_ - http_request_->content.size()); 152 declared_content_length_ - http_request_->content.size());
156 http_request_->content.append(buffer_.data() + buffer_position_, 153 http_request_->content.append(buffer_.data() + buffer_position_, fetch_bytes);
157 fetch_bytes);
158 buffer_position_ += fetch_bytes; 154 buffer_position_ += fetch_bytes;
159 155
160 if (declared_content_length_ == http_request_->content.size()) { 156 if (declared_content_length_ == http_request_->content.size()) {
161 state_ = STATE_ACCEPTED; 157 state_ = STATE_ACCEPTED;
162 return ACCEPTED; 158 return ACCEPTED;
163 } 159 }
164 160
165 state_ = STATE_CONTENT; 161 state_ = STATE_CONTENT;
166 return WAITING; 162 return WAITING;
167 } 163 }
(...skipping 25 matching lines...) Expand all
193 return METHOD_DELETE; 189 return METHOD_DELETE;
194 } else if (token == "patch") { 190 } else if (token == "patch") {
195 return METHOD_PATCH; 191 return METHOD_PATCH;
196 } 192 }
197 NOTREACHED() << "Method not implemented: " << token; 193 NOTREACHED() << "Method not implemented: " << token;
198 return METHOD_UNKNOWN; 194 return METHOD_UNKNOWN;
199 } 195 }
200 196
201 } // namespace test_server 197 } // namespace test_server
202 } // namespace net 198 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698