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

Side by Side Diff: net/http/http_stream_parser.cc

Issue 6292013: Add chunked uploads support to SPDY (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 11 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) 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
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_data_length_(0) {
43 DCHECK_EQ(0, read_buffer->offset()); 45 DCHECK_EQ(0, read_buffer->offset());
44 } 46 }
45 47
46 HttpStreamParser::~HttpStreamParser() { 48 HttpStreamParser::~HttpStreamParser() {
47 if (request_body_ != NULL && request_body_->is_chunked()) 49 if (request_body_ != NULL && request_body_->is_chunked())
48 request_body_->set_chunk_callback(NULL); 50 request_body_->set_chunk_callback(NULL);
49 } 51 }
50 52
51 int HttpStreamParser::SendRequest(const std::string& request_line, 53 int HttpStreamParser::SendRequest(const std::string& request_line,
52 const HttpRequestHeaders& headers, 54 const HttpRequestHeaders& headers,
(...skipping 10 matching lines...) Expand all
63 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, 65 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS,
64 make_scoped_refptr(new NetLogHttpRequestParameter( 66 make_scoped_refptr(new NetLogHttpRequestParameter(
65 request_line, headers))); 67 request_line, headers)));
66 } 68 }
67 response_ = response; 69 response_ = response;
68 std::string request = request_line + headers.ToString(); 70 std::string request = request_line + headers.ToString();
69 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); 71 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request));
70 request_headers_ = new DrainableIOBuffer(headers_io_buf, 72 request_headers_ = new DrainableIOBuffer(headers_io_buf,
71 headers_io_buf->size()); 73 headers_io_buf->size());
72 request_body_.reset(request_body); 74 request_body_.reset(request_body);
73 if (request_body_ != NULL && request_body_->is_chunked()) 75 if (request_body_ != NULL && request_body_->is_chunked()) {
74 request_body_->set_chunk_callback(this); 76 request_body_->set_chunk_callback(this);
77 const int kChunkHeaderFooterSize = 12; // 2 CRLFs + max of 8 hex chars.
78 chunk_buf_ = new IOBuffer(request_body_->GetMaxBufferSize() +
79 kChunkHeaderFooterSize);
80 }
75 81
76 io_state_ = STATE_SENDING_HEADERS; 82 io_state_ = STATE_SENDING_HEADERS;
77 int result = DoLoop(OK); 83 int result = DoLoop(OK);
78 if (result == ERR_IO_PENDING) 84 if (result == ERR_IO_PENDING)
79 user_callback_ = callback; 85 user_callback_ = callback;
80 86
81 return result > 0 ? OK : result; 87 return result > 0 ? OK : result;
82 } 88 }
83 89
84 int HttpStreamParser::ReadResponseHeaders(CompletionCallback* callback) { 90 int HttpStreamParser::ReadResponseHeaders(CompletionCallback* callback) {
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 (request_body_->is_chunked() || request_body_->size())) { 265 (request_body_->is_chunked() || request_body_->size())) {
260 io_state_ = STATE_SENDING_BODY; 266 io_state_ = STATE_SENDING_BODY;
261 result = OK; 267 result = OK;
262 } else { 268 } else {
263 io_state_ = STATE_REQUEST_SENT; 269 io_state_ = STATE_REQUEST_SENT;
264 } 270 }
265 return result; 271 return result;
266 } 272 }
267 273
268 int HttpStreamParser::DoSendBody(int result) { 274 int HttpStreamParser::DoSendBody(int result) {
269 request_body_->MarkConsumedAndFillBuffer(result); 275 request_body_->MarkConsumedAndFillBuffer(request_body_->is_chunked() ?
276 chunk_data_length_ : result);
Satish 2011/01/26 22:36:33 We use "chunk_data_length_" here because UploadDat
willchan no longer on Chromium 2011/01/27 18:47:49 This is buggy. ClientSocket::Write() may not write
Satish 2011/01/27 20:42:09 Ok. I can stash the number of bytes given to Write
willchan no longer on Chromium 2011/01/28 00:56:17 I'm not sure I follow. Write() with 10 bytes may h
270 277
271 if (!request_body_->eof()) { 278 if (!request_body_->eof()) {
272 int buf_len = static_cast<int>(request_body_->buf_len()); 279 int buf_len = static_cast<int>(request_body_->buf_len());
273 if (buf_len) { 280 if (request_body_->is_chunked()) {
281 if (buf_len) {
282 // Encode and send the buffer as 1 chunk.
283 std::string chunk_header = StringPrintf("%X\r\n", buf_len);
284 char* chunk_ptr = chunk_buf_->data();
285 memcpy(chunk_ptr, chunk_header.data(), chunk_header.length());
286 memcpy(chunk_ptr + chunk_header.length(), request_body_->buf()->data(),
287 buf_len);
288 memcpy(chunk_ptr + chunk_header.length() + buf_len, "\r\n", 2);
289 chunk_data_length_ = buf_len;
290 result = connection_->socket()->Write(chunk_buf_,
291 chunk_header.length() + buf_len + 2, &io_callback_);
292 } else {
293 // More POST data is to come hence wait for the callback.
294 result = ERR_IO_PENDING;
295 }
296 } else {
274 result = connection_->socket()->Write(request_body_->buf(), buf_len, 297 result = connection_->socket()->Write(request_body_->buf(), buf_len,
275 &io_callback_); 298 &io_callback_);
276 } else {
277 // More POST data is to come hence wait for the callback.
278 result = ERR_IO_PENDING;
279 } 299 }
280 } else { 300 } else {
301 if (request_body_->is_chunked()) {
302 chunk_data_length_ = 0;
303 memcpy(chunk_buf_->data(), "0\r\n\r\n", 5);
304 result = connection_->socket()->Write(chunk_buf_, 5, &io_callback_);
305 }
281 io_state_ = STATE_REQUEST_SENT; 306 io_state_ = STATE_REQUEST_SENT;
282 } 307 }
283 return result; 308 return result;
284 } 309 }
285 310
286 int HttpStreamParser::DoReadHeaders() { 311 int HttpStreamParser::DoReadHeaders() {
287 io_state_ = STATE_READ_HEADERS_COMPLETE; 312 io_state_ = STATE_READ_HEADERS_COMPLETE;
288 313
289 // Grow the read buffer if necessary. 314 // Grow the read buffer if necessary.
290 if (read_buf_->RemainingCapacity() == 0) 315 if (read_buf_->RemainingCapacity() == 0)
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 void HttpStreamParser::GetSSLCertRequestInfo( 679 void HttpStreamParser::GetSSLCertRequestInfo(
655 SSLCertRequestInfo* cert_request_info) { 680 SSLCertRequestInfo* cert_request_info) {
656 if (request_->url.SchemeIs("https") && connection_->socket()) { 681 if (request_->url.SchemeIs("https") && connection_->socket()) {
657 SSLClientSocket* ssl_socket = 682 SSLClientSocket* ssl_socket =
658 static_cast<SSLClientSocket*>(connection_->socket()); 683 static_cast<SSLClientSocket*>(connection_->socket());
659 ssl_socket->GetSSLCertRequestInfo(cert_request_info); 684 ssl_socket->GetSSLCertRequestInfo(cert_request_info);
660 } 685 }
661 } 686 }
662 687
663 } // namespace net 688 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698