OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_util.h" |
9 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
10 #include "net/base/auth.h" | 11 #include "net/base/auth.h" |
11 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
12 #include "net/base/ssl_cert_request_info.h" | 13 #include "net/base/ssl_cert_request_info.h" |
13 #include "net/http/http_net_log_params.h" | 14 #include "net/http/http_net_log_params.h" |
14 #include "net/http/http_request_headers.h" | 15 #include "net/http/http_request_headers.h" |
15 #include "net/http/http_request_info.h" | 16 #include "net/http/http_request_info.h" |
16 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
17 #include "net/http/http_util.h" | 18 #include "net/http/http_util.h" |
18 #include "net/socket/ssl_client_socket.h" | 19 #include "net/socket/ssl_client_socket.h" |
(...skipping 14 matching lines...) Expand all Loading... |
33 response_header_start_offset_(-1), | 34 response_header_start_offset_(-1), |
34 response_body_length_(-1), | 35 response_body_length_(-1), |
35 response_body_read_(0), | 36 response_body_read_(0), |
36 chunked_decoder_(NULL), | 37 chunked_decoder_(NULL), |
37 user_read_buf_(NULL), | 38 user_read_buf_(NULL), |
38 user_read_buf_len_(0), | 39 user_read_buf_len_(0), |
39 user_callback_(NULL), | 40 user_callback_(NULL), |
40 connection_(connection), | 41 connection_(connection), |
41 net_log_(net_log), | 42 net_log_(net_log), |
42 ALLOW_THIS_IN_INITIALIZER_LIST( | 43 ALLOW_THIS_IN_INITIALIZER_LIST( |
43 io_callback_(this, &HttpStreamParser::OnIOComplete)) { | 44 io_callback_(this, &HttpStreamParser::OnIOComplete)), |
| 45 chunk_length_(0), |
| 46 chunk_length_without_encoding_(0), |
| 47 sent_last_chunk_(false) { |
44 DCHECK_EQ(0, read_buffer->offset()); | 48 DCHECK_EQ(0, read_buffer->offset()); |
45 } | 49 } |
46 | 50 |
47 HttpStreamParser::~HttpStreamParser() { | 51 HttpStreamParser::~HttpStreamParser() { |
48 if (request_body_ != NULL && request_body_->is_chunked()) | 52 if (request_body_ != NULL && request_body_->is_chunked()) |
49 request_body_->set_chunk_callback(NULL); | 53 request_body_->set_chunk_callback(NULL); |
50 } | 54 } |
51 | 55 |
52 int HttpStreamParser::SendRequest(const std::string& request_line, | 56 int HttpStreamParser::SendRequest(const std::string& request_line, |
53 const HttpRequestHeaders& headers, | 57 const HttpRequestHeaders& headers, |
(...skipping 18 matching lines...) Expand all Loading... |
72 int result = connection_->socket()->GetPeerAddress(&address); | 76 int result = connection_->socket()->GetPeerAddress(&address); |
73 if (result != OK) | 77 if (result != OK) |
74 return result; | 78 return result; |
75 response_->socket_address = HostPortPair::FromAddrInfo(address.head()); | 79 response_->socket_address = HostPortPair::FromAddrInfo(address.head()); |
76 | 80 |
77 std::string request = request_line + headers.ToString(); | 81 std::string request = request_line + headers.ToString(); |
78 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); | 82 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); |
79 request_headers_ = new DrainableIOBuffer(headers_io_buf, | 83 request_headers_ = new DrainableIOBuffer(headers_io_buf, |
80 headers_io_buf->size()); | 84 headers_io_buf->size()); |
81 request_body_.reset(request_body); | 85 request_body_.reset(request_body); |
82 if (request_body_ != NULL && request_body_->is_chunked()) | 86 if (request_body_ != NULL && request_body_->is_chunked()) { |
83 request_body_->set_chunk_callback(this); | 87 request_body_->set_chunk_callback(this); |
| 88 const int kChunkHeaderFooterSize = 12; // 2 CRLFs + max of 8 hex chars. |
| 89 chunk_buf_ = new IOBuffer(request_body_->GetMaxBufferSize() + |
| 90 kChunkHeaderFooterSize); |
| 91 } |
84 | 92 |
85 io_state_ = STATE_SENDING_HEADERS; | 93 io_state_ = STATE_SENDING_HEADERS; |
86 result = DoLoop(OK); | 94 result = DoLoop(OK); |
87 if (result == ERR_IO_PENDING) | 95 if (result == ERR_IO_PENDING) |
88 user_callback_ = callback; | 96 user_callback_ = callback; |
89 | 97 |
90 return result > 0 ? OK : result; | 98 return result > 0 ? OK : result; |
91 } | 99 } |
92 | 100 |
93 int HttpStreamParser::ReadResponseHeaders(CompletionCallback* callback) { | 101 int HttpStreamParser::ReadResponseHeaders(CompletionCallback* callback) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 (request_body_->is_chunked() || request_body_->size())) { | 277 (request_body_->is_chunked() || request_body_->size())) { |
270 io_state_ = STATE_SENDING_BODY; | 278 io_state_ = STATE_SENDING_BODY; |
271 result = OK; | 279 result = OK; |
272 } else { | 280 } else { |
273 io_state_ = STATE_REQUEST_SENT; | 281 io_state_ = STATE_REQUEST_SENT; |
274 } | 282 } |
275 return result; | 283 return result; |
276 } | 284 } |
277 | 285 |
278 int HttpStreamParser::DoSendBody(int result) { | 286 int HttpStreamParser::DoSendBody(int result) { |
| 287 if (request_body_->is_chunked()) { |
| 288 chunk_length_ -= result; |
| 289 if (chunk_length_) { |
| 290 memmove(chunk_buf_->data(), chunk_buf_->data() + result, chunk_length_); |
| 291 return connection_->socket()->Write(chunk_buf_, chunk_length_, |
| 292 &io_callback_); |
| 293 } |
| 294 |
| 295 if (sent_last_chunk_) { |
| 296 io_state_ = STATE_REQUEST_SENT; |
| 297 return OK; |
| 298 } |
| 299 |
| 300 request_body_->MarkConsumedAndFillBuffer(chunk_length_without_encoding_); |
| 301 chunk_length_without_encoding_ = 0; |
| 302 chunk_length_ = 0; |
| 303 |
| 304 int buf_len = static_cast<int>(request_body_->buf_len()); |
| 305 if (request_body_->eof()) { |
| 306 static const char kLastChunk[] = "0\r\n\r\n"; |
| 307 chunk_length_ = strlen(kLastChunk); |
| 308 memcpy(chunk_buf_->data(), kLastChunk, chunk_length_); |
| 309 sent_last_chunk_ = true; |
| 310 } else if (buf_len) { |
| 311 // Encode and send the buffer as 1 chunk. |
| 312 std::string chunk_header = StringPrintf("%X\r\n", buf_len); |
| 313 char* chunk_ptr = chunk_buf_->data(); |
| 314 memcpy(chunk_ptr, chunk_header.data(), chunk_header.length()); |
| 315 chunk_ptr += chunk_header.length(); |
| 316 memcpy(chunk_ptr, request_body_->buf()->data(), buf_len); |
| 317 chunk_ptr += buf_len; |
| 318 memcpy(chunk_ptr, "\r\n", 2); |
| 319 chunk_length_without_encoding_ = buf_len; |
| 320 chunk_length_ = chunk_header.length() + buf_len + 2; |
| 321 } |
| 322 |
| 323 if (!chunk_length_) // More POST data is yet to come? |
| 324 return ERR_IO_PENDING; |
| 325 |
| 326 return connection_->socket()->Write(chunk_buf_, chunk_length_, |
| 327 &io_callback_); |
| 328 } |
| 329 |
| 330 // Non-chunked request body. |
279 request_body_->MarkConsumedAndFillBuffer(result); | 331 request_body_->MarkConsumedAndFillBuffer(result); |
280 | 332 |
281 if (!request_body_->eof()) { | 333 if (!request_body_->eof()) { |
282 int buf_len = static_cast<int>(request_body_->buf_len()); | 334 int buf_len = static_cast<int>(request_body_->buf_len()); |
283 if (buf_len) { | 335 result = connection_->socket()->Write(request_body_->buf(), buf_len, |
284 result = connection_->socket()->Write(request_body_->buf(), buf_len, | 336 &io_callback_); |
285 &io_callback_); | |
286 } else { | |
287 // More POST data is to come hence wait for the callback. | |
288 result = ERR_IO_PENDING; | |
289 } | |
290 } else { | 337 } else { |
291 io_state_ = STATE_REQUEST_SENT; | 338 io_state_ = STATE_REQUEST_SENT; |
292 } | 339 } |
293 return result; | 340 return result; |
294 } | 341 } |
295 | 342 |
296 int HttpStreamParser::DoReadHeaders() { | 343 int HttpStreamParser::DoReadHeaders() { |
297 io_state_ = STATE_READ_HEADERS_COMPLETE; | 344 io_state_ = STATE_READ_HEADERS_COMPLETE; |
298 | 345 |
299 // Grow the read buffer if necessary. | 346 // Grow the read buffer if necessary. |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 void HttpStreamParser::GetSSLCertRequestInfo( | 711 void HttpStreamParser::GetSSLCertRequestInfo( |
665 SSLCertRequestInfo* cert_request_info) { | 712 SSLCertRequestInfo* cert_request_info) { |
666 if (request_->url.SchemeIs("https") && connection_->socket()) { | 713 if (request_->url.SchemeIs("https") && connection_->socket()) { |
667 SSLClientSocket* ssl_socket = | 714 SSLClientSocket* ssl_socket = |
668 static_cast<SSLClientSocket*>(connection_->socket()); | 715 static_cast<SSLClientSocket*>(connection_->socket()); |
669 ssl_socket->GetSSLCertRequestInfo(cert_request_info); | 716 ssl_socket->GetSSLCertRequestInfo(cert_request_info); |
670 } | 717 } |
671 } | 718 } |
672 | 719 |
673 } // namespace net | 720 } // namespace net |
OLD | NEW |