OLD | NEW |
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/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/profiler/scoped_tracker.h" | 10 #include "base/profiler/scoped_tracker.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
15 #include "net/base/upload_data_stream.h" | 15 #include "net/base/upload_data_stream.h" |
16 #include "net/http/http_chunked_decoder.h" | 16 #include "net/http/http_chunked_decoder.h" |
17 #include "net/http/http_request_headers.h" | 17 #include "net/http/http_request_headers.h" |
18 #include "net/http/http_request_info.h" | 18 #include "net/http/http_request_info.h" |
19 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
20 #include "net/http/http_util.h" | 20 #include "net/http/http_util.h" |
21 #include "net/socket/client_socket_handle.h" | 21 #include "net/socket/client_socket_handle.h" |
22 #include "net/socket/ssl_client_socket.h" | 22 #include "net/socket/ssl_client_socket.h" |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const size_t kMaxMergedHeaderAndBodySize = 1400; | 28 const uint64 kMaxMergedHeaderAndBodySize = 1400; |
29 const size_t kRequestBodyBufferSize = 1 << 14; // 16KB | 29 const size_t kRequestBodyBufferSize = 1 << 14; // 16KB |
30 | 30 |
31 std::string GetResponseHeaderLines(const HttpResponseHeaders& headers) { | 31 std::string GetResponseHeaderLines(const HttpResponseHeaders& headers) { |
32 std::string raw_headers = headers.raw_headers(); | 32 std::string raw_headers = headers.raw_headers(); |
33 const char* null_separated_headers = raw_headers.c_str(); | 33 const char* null_separated_headers = raw_headers.c_str(); |
34 const char* header_line = null_separated_headers; | 34 const char* header_line = null_separated_headers; |
35 std::string cr_separated_headers; | 35 std::string cr_separated_headers; |
36 while (header_line[0] != 0) { | 36 while (header_line[0] != 0) { |
37 cr_separated_headers += header_line; | 37 cr_separated_headers += header_line; |
38 cr_separated_headers += "\n"; | 38 cr_separated_headers += "\n"; |
(...skipping 13 matching lines...) Expand all Loading... |
52 // There's at least one |field_name| header. Check if there are any more | 52 // There's at least one |field_name| header. Check if there are any more |
53 // such headers, and if so, return true if they have different values. | 53 // such headers, and if so, return true if they have different values. |
54 std::string field_value2; | 54 std::string field_value2; |
55 while (headers.EnumerateHeader(&it, field_name, &field_value2)) { | 55 while (headers.EnumerateHeader(&it, field_name, &field_value2)) { |
56 if (field_value != field_value2) | 56 if (field_value != field_value2) |
57 return true; | 57 return true; |
58 } | 58 } |
59 return false; | 59 return false; |
60 } | 60 } |
61 | 61 |
62 base::Value* NetLogSendRequestBodyCallback(int length, | 62 base::Value* NetLogSendRequestBodyCallback(uint64 length, |
63 bool is_chunked, | 63 bool is_chunked, |
64 bool did_merge, | 64 bool did_merge, |
65 NetLog::LogLevel /* log_level */) { | 65 NetLog::LogLevel /* log_level */) { |
66 base::DictionaryValue* dict = new base::DictionaryValue(); | 66 base::DictionaryValue* dict = new base::DictionaryValue(); |
67 dict->SetInteger("length", length); | 67 dict->SetInteger("length", static_cast<int>(length)); |
68 dict->SetBoolean("is_chunked", is_chunked); | 68 dict->SetBoolean("is_chunked", is_chunked); |
69 dict->SetBoolean("did_merge", did_merge); | 69 dict->SetBoolean("did_merge", did_merge); |
70 return dict; | 70 return dict; |
71 } | 71 } |
72 | 72 |
73 // Returns true if |error_code| is an error for which we give the server a | 73 // Returns true if |error_code| is an error for which we give the server a |
74 // chance to send a body containing error information, if the error was received | 74 // chance to send a body containing error information, if the error was received |
75 // while trying to upload a request body. | 75 // while trying to upload a request body. |
76 bool ShouldTryReadingOnUploadError(int error_code) { | 76 bool ShouldTryReadingOnUploadError(int error_code) { |
77 return (error_code == ERR_CONNECTION_RESET); | 77 return (error_code == ERR_CONNECTION_RESET); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 request_body_read_buf_ = request_body_send_buf_; | 246 request_body_read_buf_ = request_body_send_buf_; |
247 } | 247 } |
248 } | 248 } |
249 | 249 |
250 io_state_ = STATE_SEND_HEADERS; | 250 io_state_ = STATE_SEND_HEADERS; |
251 | 251 |
252 // If we have a small request body, then we'll merge with the headers into a | 252 // If we have a small request body, then we'll merge with the headers into a |
253 // single write. | 253 // single write. |
254 bool did_merge = false; | 254 bool did_merge = false; |
255 if (ShouldMergeRequestHeadersAndBody(request, request_->upload_data_stream)) { | 255 if (ShouldMergeRequestHeadersAndBody(request, request_->upload_data_stream)) { |
256 size_t merged_size = | 256 int merged_size = static_cast<int>( |
257 request_headers_length_ + request_->upload_data_stream->size(); | 257 request_headers_length_ + request_->upload_data_stream->size()); |
258 scoped_refptr<IOBuffer> merged_request_headers_and_body( | 258 scoped_refptr<IOBuffer> merged_request_headers_and_body( |
259 new IOBuffer(merged_size)); | 259 new IOBuffer(merged_size)); |
260 // We'll repurpose |request_headers_| to store the merged headers and | 260 // We'll repurpose |request_headers_| to store the merged headers and |
261 // body. | 261 // body. |
262 request_headers_ = new DrainableIOBuffer( | 262 request_headers_ = new DrainableIOBuffer( |
263 merged_request_headers_and_body.get(), merged_size); | 263 merged_request_headers_and_body.get(), merged_size); |
264 | 264 |
265 memcpy(request_headers_->data(), request.data(), request_headers_length_); | 265 memcpy(request_headers_->data(), request.data(), request_headers_length_); |
266 request_headers_->DidConsume(request_headers_length_); | 266 request_headers_->DidConsume(request_headers_length_); |
267 | 267 |
268 size_t todo = request_->upload_data_stream->size(); | 268 uint64 todo = request_->upload_data_stream->size(); |
269 while (todo) { | 269 while (todo) { |
270 int consumed = request_->upload_data_stream | 270 int consumed = request_->upload_data_stream->Read( |
271 ->Read(request_headers_.get(), todo, CompletionCallback()); | 271 request_headers_.get(), static_cast<int>(todo), CompletionCallback()); |
272 DCHECK_GT(consumed, 0); // Read() won't fail if not chunked. | 272 DCHECK_GT(consumed, 0); // Read() won't fail if not chunked. |
273 request_headers_->DidConsume(consumed); | 273 request_headers_->DidConsume(consumed); |
274 todo -= consumed; | 274 todo -= consumed; |
275 } | 275 } |
276 DCHECK(request_->upload_data_stream->IsEOF()); | 276 DCHECK(request_->upload_data_stream->IsEOF()); |
277 // Reset the offset, so the buffer can be read from the beginning. | 277 // Reset the offset, so the buffer can be read from the beginning. |
278 request_headers_->SetOffset(0); | 278 request_headers_->SetOffset(0); |
279 did_merge = true; | 279 did_merge = true; |
280 | 280 |
281 net_log_.AddEvent( | 281 net_log_.AddEvent( |
(...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1052 } | 1052 } |
1053 | 1053 |
1054 // static | 1054 // static |
1055 bool HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 1055 bool HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
1056 const std::string& request_headers, | 1056 const std::string& request_headers, |
1057 const UploadDataStream* request_body) { | 1057 const UploadDataStream* request_body) { |
1058 if (request_body != NULL && | 1058 if (request_body != NULL && |
1059 // IsInMemory() ensures that the request body is not chunked. | 1059 // IsInMemory() ensures that the request body is not chunked. |
1060 request_body->IsInMemory() && | 1060 request_body->IsInMemory() && |
1061 request_body->size() > 0) { | 1061 request_body->size() > 0) { |
1062 size_t merged_size = request_headers.size() + request_body->size(); | 1062 uint64 merged_size = request_headers.size() + request_body->size(); |
1063 if (merged_size <= kMaxMergedHeaderAndBodySize) | 1063 if (merged_size <= kMaxMergedHeaderAndBodySize) |
1064 return true; | 1064 return true; |
1065 } | 1065 } |
1066 return false; | 1066 return false; |
1067 } | 1067 } |
1068 | 1068 |
1069 } // namespace net | 1069 } // namespace net |
OLD | NEW |