diff options
author | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-04 07:54:39 +0000 |
---|---|---|
committer | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-04 07:54:39 +0000 |
commit | bf96f533df6515f9ddea3278515a77ab81c00263 (patch) | |
tree | 72ba664e83a3a5880b1b16bd379d041c2e85e368 /net/http/http_stream_parser.cc | |
parent | 26c2f823d194dc69819b7def92f920f0ec861df5 (diff) | |
download | chromium_src-bf96f533df6515f9ddea3278515a77ab81c00263.zip chromium_src-bf96f533df6515f9ddea3278515a77ab81c00263.tar.gz chromium_src-bf96f533df6515f9ddea3278515a77ab81c00263.tar.bz2 |
Add chunked uploads support to SPDY
As part of this, I had to move the chunked encoding part from UploadData::Element::SetChunk
to HttpStreamParser::DoSendBody as SPDY doesn't have this encoded format and UploadData
needs to serve both.
BUG=none
TEST=net_unittests (2 new tests added)
Review URL: http://codereview.chromium.org/6292013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@76892 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http/http_stream_parser.cc')
-rw-r--r-- | net/http/http_stream_parser.cc | 65 |
1 files changed, 56 insertions, 9 deletions
diff --git a/net/http/http_stream_parser.cc b/net/http/http_stream_parser.cc index 6621f0b..eb1ed35 100644 --- a/net/http/http_stream_parser.cc +++ b/net/http/http_stream_parser.cc @@ -6,6 +6,7 @@ #include "base/compiler_specific.h" #include "base/metrics/histogram.h" +#include "base/string_util.h" #include "net/base/address_list.h" #include "net/base/auth.h" #include "net/base/io_buffer.h" @@ -40,7 +41,10 @@ HttpStreamParser::HttpStreamParser(ClientSocketHandle* connection, connection_(connection), net_log_(net_log), ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &HttpStreamParser::OnIOComplete)) { + io_callback_(this, &HttpStreamParser::OnIOComplete)), + chunk_length_(0), + chunk_length_without_encoding_(0), + sent_last_chunk_(false) { DCHECK_EQ(0, read_buffer->offset()); } @@ -79,8 +83,12 @@ int HttpStreamParser::SendRequest(const std::string& request_line, request_headers_ = new DrainableIOBuffer(headers_io_buf, headers_io_buf->size()); request_body_.reset(request_body); - if (request_body_ != NULL && request_body_->is_chunked()) + if (request_body_ != NULL && request_body_->is_chunked()) { request_body_->set_chunk_callback(this); + const int kChunkHeaderFooterSize = 12; // 2 CRLFs + max of 8 hex chars. + chunk_buf_ = new IOBuffer(request_body_->GetMaxBufferSize() + + kChunkHeaderFooterSize); + } io_state_ = STATE_SENDING_HEADERS; result = DoLoop(OK); @@ -276,17 +284,56 @@ int HttpStreamParser::DoSendHeaders(int result) { } int HttpStreamParser::DoSendBody(int result) { + if (request_body_->is_chunked()) { + chunk_length_ -= result; + if (chunk_length_) { + memmove(chunk_buf_->data(), chunk_buf_->data() + result, chunk_length_); + return connection_->socket()->Write(chunk_buf_, chunk_length_, + &io_callback_); + } + + if (sent_last_chunk_) { + io_state_ = STATE_REQUEST_SENT; + return OK; + } + + request_body_->MarkConsumedAndFillBuffer(chunk_length_without_encoding_); + chunk_length_without_encoding_ = 0; + chunk_length_ = 0; + + int buf_len = static_cast<int>(request_body_->buf_len()); + if (request_body_->eof()) { + static const char kLastChunk[] = "0\r\n\r\n"; + chunk_length_ = strlen(kLastChunk); + memcpy(chunk_buf_->data(), kLastChunk, chunk_length_); + sent_last_chunk_ = true; + } else if (buf_len) { + // Encode and send the buffer as 1 chunk. + std::string chunk_header = StringPrintf("%X\r\n", buf_len); + char* chunk_ptr = chunk_buf_->data(); + memcpy(chunk_ptr, chunk_header.data(), chunk_header.length()); + chunk_ptr += chunk_header.length(); + memcpy(chunk_ptr, request_body_->buf()->data(), buf_len); + chunk_ptr += buf_len; + memcpy(chunk_ptr, "\r\n", 2); + chunk_length_without_encoding_ = buf_len; + chunk_length_ = chunk_header.length() + buf_len + 2; + } + + if (!chunk_length_) // More POST data is yet to come? + return ERR_IO_PENDING; + + return connection_->socket()->Write(chunk_buf_, chunk_length_, + &io_callback_); + } + + // Non-chunked request body. request_body_->MarkConsumedAndFillBuffer(result); if (!request_body_->eof()) { int buf_len = static_cast<int>(request_body_->buf_len()); - if (buf_len) { - result = connection_->socket()->Write(request_body_->buf(), buf_len, - &io_callback_); - } else { - // More POST data is to come hence wait for the callback. - result = ERR_IO_PENDING; - } + result = connection_->socket()->Write(request_body_->buf(), buf_len, + &io_callback_); } else { io_state_ = STATE_REQUEST_SENT; } |