Index: net/http/http_stream_parser.cc |
diff --git a/net/http/http_stream_parser.cc b/net/http/http_stream_parser.cc |
index 6621f0b1ca21b505f429ff7a0988266e6cb56ff1..eb1ed35225acc7ca55385fefa18c1ebe7f95c9ae 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; |
} |