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

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, 10 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_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
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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 } else if (request_body_ != NULL && 266 } else if (request_body_ != NULL &&
259 (request_body_->is_chunked() || request_body_->size())) { 267 (request_body_->is_chunked() || request_body_->size())) {
260 io_state_ = STATE_SENDING_BODY; 268 io_state_ = STATE_SENDING_BODY;
261 result = OK; 269 result = OK;
262 } else { 270 } else {
263 io_state_ = STATE_REQUEST_SENT; 271 io_state_ = STATE_REQUEST_SENT;
264 } 272 }
265 return result; 273 return result;
266 } 274 }
267 275
268 int HttpStreamParser::DoSendBody(int result) { 276 int HttpStreamParser::DoSendBody(int result) {
willchan no longer on Chromium 2011/02/01 23:35:59 Please add a bunch of assertions to validate this
Satish 2011/02/22 14:25:44 In the newly added code MarkConsumedAndFillBuffer(
willchan no longer on Chromium 2011/02/24 19:03:39 I don't recall the control flow =/ I looked at don
277 if (request_body_->is_chunked()) {
278 chunk_length_ -= result;
279 if (chunk_length_) {
280 memcpy(chunk_buf_->data(), chunk_buf_->data() + result, chunk_length_);
willchan no longer on Chromium 2011/02/01 23:35:59 You need to use memmove instead of memcpy because
Satish 2011/02/22 14:25:44 Done
willchan no longer on Chromium 2011/02/24 19:03:39 DrainableIOBuffer is the right solution here. Of c
281 return connection_->socket()->Write(chunk_buf_, chunk_length_,
282 &io_callback_);
283 }
284
285 if (sent_last_chunk_) {
286 io_state_ = STATE_REQUEST_SENT;
287 return OK;
288 }
289
290 request_body_->MarkConsumedAndFillBuffer(chunk_length_without_encoding_);
291 chunk_length_without_encoding_ = 0;
292 chunk_length_ = 0;
293
294 int buf_len = static_cast<int>(request_body_->buf_len());
295 if (request_body_->eof()) {
296 chunk_length_ = 5;
297 memcpy(chunk_buf_->data(), "0\r\n\r\n", chunk_length_);
298 sent_last_chunk_ = true;
299 } else if (buf_len) {
300 // Encode and send the buffer as 1 chunk.
willchan no longer on Chromium 2011/02/01 23:35:59 Why don't you use a single sprintf() call? Somethi
Satish 2011/02/22 14:25:44 MSDN says that for %s 'characters are printed up t
willchan no longer on Chromium 2011/02/24 19:03:39 Doh, I forgot about the NUL. You're absolutely cor
301 std::string chunk_header = StringPrintf("%X\r\n", buf_len);
302 char* chunk_ptr = chunk_buf_->data();
303 memcpy(chunk_ptr, chunk_header.data(), chunk_header.length());
304 memcpy(chunk_ptr + chunk_header.length(), request_body_->buf()->data(),
305 buf_len);
306 memcpy(chunk_ptr + chunk_header.length() + buf_len, "\r\n", 2);
307 chunk_length_without_encoding_ = buf_len;
308 chunk_length_ = chunk_header.length() + buf_len + 2;
309 }
310
311 if (!chunk_length_) // More POST data is yet to come?
312 return ERR_IO_PENDING;
313
314 return connection_->socket()->Write(chunk_buf_, chunk_length_,
315 &io_callback_);
316 }
317
318 // Non-chunked request body.
269 request_body_->MarkConsumedAndFillBuffer(result); 319 request_body_->MarkConsumedAndFillBuffer(result);
270 320
271 if (!request_body_->eof()) { 321 if (!request_body_->eof()) {
272 int buf_len = static_cast<int>(request_body_->buf_len()); 322 int buf_len = static_cast<int>(request_body_->buf_len());
273 if (buf_len) { 323 result = connection_->socket()->Write(request_body_->buf(), buf_len,
274 result = connection_->socket()->Write(request_body_->buf(), buf_len, 324 &io_callback_);
275 &io_callback_);
276 } else {
277 // More POST data is to come hence wait for the callback.
278 result = ERR_IO_PENDING;
279 }
280 } else { 325 } else {
281 io_state_ = STATE_REQUEST_SENT; 326 io_state_ = STATE_REQUEST_SENT;
282 } 327 }
283 return result; 328 return result;
284 } 329 }
285 330
286 int HttpStreamParser::DoReadHeaders() { 331 int HttpStreamParser::DoReadHeaders() {
287 io_state_ = STATE_READ_HEADERS_COMPLETE; 332 io_state_ = STATE_READ_HEADERS_COMPLETE;
288 333
289 // Grow the read buffer if necessary. 334 // Grow the read buffer if necessary.
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 void HttpStreamParser::GetSSLCertRequestInfo( 699 void HttpStreamParser::GetSSLCertRequestInfo(
655 SSLCertRequestInfo* cert_request_info) { 700 SSLCertRequestInfo* cert_request_info) {
656 if (request_->url.SchemeIs("https") && connection_->socket()) { 701 if (request_->url.SchemeIs("https") && connection_->socket()) {
657 SSLClientSocket* ssl_socket = 702 SSLClientSocket* ssl_socket =
658 static_cast<SSLClientSocket*>(connection_->socket()); 703 static_cast<SSLClientSocket*>(connection_->socket());
659 ssl_socket->GetSSLCertRequestInfo(cert_request_info); 704 ssl_socket->GetSSLCertRequestInfo(cert_request_info);
660 } 705 }
661 } 706 }
662 707
663 } // namespace net 708 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698