diff options
Diffstat (limited to 'net/http')
| -rw-r--r-- | net/http/http_stream_parser.cc | 65 | ||||
| -rw-r--r-- | net/http/http_stream_parser.h | 7 |
2 files changed, 63 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; } diff --git a/net/http/http_stream_parser.h b/net/http/http_stream_parser.h index 5f7e943..d9241a6 100644 --- a/net/http/http_stream_parser.h +++ b/net/http/http_stream_parser.h @@ -189,6 +189,13 @@ class HttpStreamParser : public ChunkCallback { // Callback to be used when doing IO. CompletionCallbackImpl<HttpStreamParser> io_callback_; + // Stores an encoded chunk for chunked uploads. + // Note: This should perhaps be improved to not create copies of the data. + scoped_refptr<IOBuffer> chunk_buf_; + size_t chunk_length_; + size_t chunk_length_without_encoding_; + bool sent_last_chunk_; + DISALLOW_COPY_AND_ASSIGN(HttpStreamParser); }; |
