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/auth.h" | 10 #include "net/base/auth.h" |
10 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
11 #include "net/base/ssl_cert_request_info.h" | 12 #include "net/base/ssl_cert_request_info.h" |
12 #include "net/http/http_net_log_params.h" | 13 #include "net/http/http_net_log_params.h" |
13 #include "net/http/http_request_headers.h" | 14 #include "net/http/http_request_headers.h" |
14 #include "net/http/http_request_info.h" | 15 #include "net/http/http_request_info.h" |
15 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
16 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
17 #include "net/socket/ssl_client_socket.h" | 18 #include "net/socket/ssl_client_socket.h" |
18 #include "net/socket/client_socket_handle.h" | 19 #include "net/socket/client_socket_handle.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
32 response_header_start_offset_(-1), | 33 response_header_start_offset_(-1), |
33 response_body_length_(-1), | 34 response_body_length_(-1), |
34 response_body_read_(0), | 35 response_body_read_(0), |
35 chunked_decoder_(NULL), | 36 chunked_decoder_(NULL), |
36 user_read_buf_(NULL), | 37 user_read_buf_(NULL), |
37 user_read_buf_len_(0), | 38 user_read_buf_len_(0), |
38 user_callback_(NULL), | 39 user_callback_(NULL), |
39 connection_(connection), | 40 connection_(connection), |
40 net_log_(net_log), | 41 net_log_(net_log), |
41 ALLOW_THIS_IN_INITIALIZER_LIST( | 42 ALLOW_THIS_IN_INITIALIZER_LIST( |
42 io_callback_(this, &HttpStreamParser::OnIOComplete)) { | 43 io_callback_(this, &HttpStreamParser::OnIOComplete)), |
44 chunk_length_(0), | |
45 chunk_length_without_encoding_(0), | |
46 sent_last_chunk_(false) { | |
43 DCHECK_EQ(0, read_buffer->offset()); | 47 DCHECK_EQ(0, read_buffer->offset()); |
44 } | 48 } |
45 | 49 |
46 HttpStreamParser::~HttpStreamParser() { | 50 HttpStreamParser::~HttpStreamParser() { |
47 if (request_body_ != NULL && request_body_->is_chunked()) | 51 if (request_body_ != NULL && request_body_->is_chunked()) |
48 request_body_->set_chunk_callback(NULL); | 52 request_body_->set_chunk_callback(NULL); |
49 } | 53 } |
50 | 54 |
51 int HttpStreamParser::SendRequest(const std::string& request_line, | 55 int HttpStreamParser::SendRequest(const std::string& request_line, |
52 const HttpRequestHeaders& headers, | 56 const HttpRequestHeaders& headers, |
(...skipping 10 matching lines...) Expand all Loading... | |
63 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, | 67 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
64 make_scoped_refptr(new NetLogHttpRequestParameter( | 68 make_scoped_refptr(new NetLogHttpRequestParameter( |
65 request_line, headers))); | 69 request_line, headers))); |
66 } | 70 } |
67 response_ = response; | 71 response_ = response; |
68 std::string request = request_line + headers.ToString(); | 72 std::string request = request_line + headers.ToString(); |
69 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); | 73 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); |
70 request_headers_ = new DrainableIOBuffer(headers_io_buf, | 74 request_headers_ = new DrainableIOBuffer(headers_io_buf, |
71 headers_io_buf->size()); | 75 headers_io_buf->size()); |
72 request_body_.reset(request_body); | 76 request_body_.reset(request_body); |
73 if (request_body_ != NULL && request_body_->is_chunked()) | 77 if (request_body_ != NULL && request_body_->is_chunked()) { |
74 request_body_->set_chunk_callback(this); | 78 request_body_->set_chunk_callback(this); |
79 const int kChunkHeaderFooterSize = 12; // 2 CRLFs + max of 8 hex chars. | |
80 chunk_buf_ = new IOBuffer(request_body_->GetMaxBufferSize() + | |
81 kChunkHeaderFooterSize); | |
82 } | |
75 | 83 |
76 io_state_ = STATE_SENDING_HEADERS; | 84 io_state_ = STATE_SENDING_HEADERS; |
77 int result = DoLoop(OK); | 85 int result = DoLoop(OK); |
78 if (result == ERR_IO_PENDING) | 86 if (result == ERR_IO_PENDING) |
79 user_callback_ = callback; | 87 user_callback_ = callback; |
80 | 88 |
81 return result > 0 ? OK : result; | 89 return result > 0 ? OK : result; |
82 } | 90 } |
83 | 91 |
84 int HttpStreamParser::ReadResponseHeaders(CompletionCallback* callback) { | 92 int HttpStreamParser::ReadResponseHeaders(CompletionCallback* callback) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 (request_body_->is_chunked() || request_body_->size())) { | 268 (request_body_->is_chunked() || request_body_->size())) { |
261 io_state_ = STATE_SENDING_BODY; | 269 io_state_ = STATE_SENDING_BODY; |
262 result = OK; | 270 result = OK; |
263 } else { | 271 } else { |
264 io_state_ = STATE_REQUEST_SENT; | 272 io_state_ = STATE_REQUEST_SENT; |
265 } | 273 } |
266 return result; | 274 return result; |
267 } | 275 } |
268 | 276 |
269 int HttpStreamParser::DoSendBody(int result) { | 277 int HttpStreamParser::DoSendBody(int result) { |
278 if (request_body_->is_chunked()) { | |
279 chunk_length_ -= result; | |
280 if (chunk_length_) { | |
281 memmove(chunk_buf_->data(), chunk_buf_->data() + result, chunk_length_); | |
willchan no longer on Chromium
2011/02/24 19:41:34
Please add a regression test here, since last time
| |
282 return connection_->socket()->Write(chunk_buf_, chunk_length_, | |
283 &io_callback_); | |
284 } | |
285 | |
286 if (sent_last_chunk_) { | |
287 io_state_ = STATE_REQUEST_SENT; | |
288 return OK; | |
289 } | |
290 | |
291 request_body_->MarkConsumedAndFillBuffer(chunk_length_without_encoding_); | |
292 chunk_length_without_encoding_ = 0; | |
293 chunk_length_ = 0; | |
294 | |
295 int buf_len = static_cast<int>(request_body_->buf_len()); | |
296 if (request_body_->eof()) { | |
297 chunk_length_ = 5; | |
willchan no longer on Chromium
2011/02/24 19:41:34
Please do something like:
static const char kLastC
| |
298 memcpy(chunk_buf_->data(), "0\r\n\r\n", chunk_length_); | |
299 sent_last_chunk_ = true; | |
300 } else if (buf_len) { | |
301 // Encode and send the buffer as 1 chunk. | |
302 std::string chunk_header = StringPrintf("%X\r\n", buf_len); | |
303 char* chunk_ptr = chunk_buf_->data(); | |
304 memcpy(chunk_ptr, chunk_header.data(), chunk_header.length()); | |
305 memcpy(chunk_ptr + chunk_header.length(), request_body_->buf()->data(), | |
306 buf_len); | |
307 memcpy(chunk_ptr + chunk_header.length() + buf_len, "\r\n", 2); | |
308 chunk_length_without_encoding_ = buf_len; | |
309 chunk_length_ = chunk_header.length() + buf_len + 2; | |
310 } | |
311 | |
312 if (!chunk_length_) // More POST data is yet to come? | |
313 return ERR_IO_PENDING; | |
314 | |
315 return connection_->socket()->Write(chunk_buf_, chunk_length_, | |
316 &io_callback_); | |
317 } | |
318 | |
319 // Non-chunked request body. | |
270 request_body_->MarkConsumedAndFillBuffer(result); | 320 request_body_->MarkConsumedAndFillBuffer(result); |
271 | 321 |
272 if (!request_body_->eof()) { | 322 if (!request_body_->eof()) { |
273 int buf_len = static_cast<int>(request_body_->buf_len()); | 323 int buf_len = static_cast<int>(request_body_->buf_len()); |
274 if (buf_len) { | 324 result = connection_->socket()->Write(request_body_->buf(), buf_len, |
275 result = connection_->socket()->Write(request_body_->buf(), buf_len, | 325 &io_callback_); |
276 &io_callback_); | |
277 } else { | |
278 // More POST data is to come hence wait for the callback. | |
279 result = ERR_IO_PENDING; | |
280 } | |
281 } else { | 326 } else { |
282 io_state_ = STATE_REQUEST_SENT; | 327 io_state_ = STATE_REQUEST_SENT; |
283 } | 328 } |
284 return result; | 329 return result; |
285 } | 330 } |
286 | 331 |
287 int HttpStreamParser::DoReadHeaders() { | 332 int HttpStreamParser::DoReadHeaders() { |
288 io_state_ = STATE_READ_HEADERS_COMPLETE; | 333 io_state_ = STATE_READ_HEADERS_COMPLETE; |
289 | 334 |
290 // Grow the read buffer if necessary. | 335 // Grow the read buffer if necessary. |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
655 void HttpStreamParser::GetSSLCertRequestInfo( | 700 void HttpStreamParser::GetSSLCertRequestInfo( |
656 SSLCertRequestInfo* cert_request_info) { | 701 SSLCertRequestInfo* cert_request_info) { |
657 if (request_->url.SchemeIs("https") && connection_->socket()) { | 702 if (request_->url.SchemeIs("https") && connection_->socket()) { |
658 SSLClientSocket* ssl_socket = | 703 SSLClientSocket* ssl_socket = |
659 static_cast<SSLClientSocket*>(connection_->socket()); | 704 static_cast<SSLClientSocket*>(connection_->socket()); |
660 ssl_socket->GetSSLCertRequestInfo(cert_request_info); | 705 ssl_socket->GetSSLCertRequestInfo(cert_request_info); |
661 } | 706 } |
662 } | 707 } |
663 | 708 |
664 } // namespace net | 709 } // namespace net |
OLD | NEW |