Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/string_util.h" |
| 10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 | 60 |
| 61 } // namespace | 61 } // namespace |
| 62 | 62 |
| 63 namespace net { | 63 namespace net { |
| 64 | 64 |
| 65 // 2 CRLFs + max of 8 hex chars. | 65 // 2 CRLFs + max of 8 hex chars. |
| 66 const size_t HttpStreamParser::kChunkHeaderFooterSize = 12; | 66 const size_t HttpStreamParser::kChunkHeaderFooterSize = 12; |
| 67 | 67 |
| 68 HttpStreamParser::HttpStreamParser(ClientSocketHandle* connection, | 68 HttpStreamParser::HttpStreamParser(ClientSocketHandle* connection, |
| 69 const HttpRequestInfo* request, | 69 const HttpRequestInfo* request, |
| 70 GrowableIOBuffer* read_buffer, | 70 GrowableIOBuffer* read_buffer, |
|
rvargas (doing something else)
2012/02/03 23:13:34
This is of course not related to your change, but
satorux1
2012/02/03 23:46:11
Thank you for sharing the thought. I'll keep in mi
| |
| 71 const BoundNetLog& net_log) | 71 const BoundNetLog& net_log) |
| 72 : io_state_(STATE_NONE), | 72 : io_state_(STATE_NONE), |
| 73 request_(request), | 73 request_(request), |
| 74 request_headers_(NULL), | 74 request_headers_(NULL), |
| 75 request_body_(NULL), | 75 request_body_(NULL), |
| 76 read_buf_(read_buffer), | 76 read_buf_(read_buffer), |
| 77 read_buf_unused_offset_(0), | 77 read_buf_unused_offset_(0), |
| 78 response_header_start_offset_(-1), | 78 response_header_start_offset_(-1), |
| 79 response_body_length_(-1), | 79 response_body_length_(-1), |
| 80 response_body_read_(0), | 80 response_body_read_(0), |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 AddressList address; | 121 AddressList address; |
| 122 int result = connection_->socket()->GetPeerAddress(&address); | 122 int result = connection_->socket()->GetPeerAddress(&address); |
| 123 if (result != OK) | 123 if (result != OK) |
| 124 return result; | 124 return result; |
| 125 response_->socket_address = HostPortPair::FromAddrInfo(address.head()); | 125 response_->socket_address = HostPortPair::FromAddrInfo(address.head()); |
| 126 | 126 |
| 127 std::string request = request_line + headers.ToString(); | 127 std::string request = request_line + headers.ToString(); |
| 128 request_body_.reset(request_body); | 128 request_body_.reset(request_body); |
| 129 if (request_body_ != NULL && request_body_->is_chunked()) { | 129 if (request_body_ != NULL && request_body_->is_chunked()) { |
| 130 request_body_->set_chunk_callback(this); | 130 request_body_->set_chunk_callback(this); |
| 131 // The raw chunk buffer is guaranteed to be large enough to hold the | 131 // The chunk buffer is guaranteed to be large enough to hold the encoded |
| 132 // encoded chunk. | 132 // chunk. |
| 133 raw_chunk_buf_ = new IOBufferWithSize(UploadDataStream::GetBufferSize() + | 133 chunk_buf_ = new SeekableIOBuffer(UploadDataStream::GetBufferSize() + |
| 134 kChunkHeaderFooterSize); | 134 kChunkHeaderFooterSize); |
| 135 } | 135 } |
| 136 | 136 |
| 137 io_state_ = STATE_SENDING_HEADERS; | 137 io_state_ = STATE_SENDING_HEADERS; |
| 138 | 138 |
| 139 // If we have a small request body, then we'll merge with the headers into a | 139 // If we have a small request body, then we'll merge with the headers into a |
| 140 // single write. | 140 // single write. |
| 141 bool did_merge = false; | 141 bool did_merge = false; |
| 142 if (ShouldMergeRequestHeadersAndBody(request, request_body_.get())) { | 142 if (ShouldMergeRequestHeadersAndBody(request, request_body_.get())) { |
| 143 size_t merged_size = request.size() + request_body->size(); | 143 size_t merged_size = request.size() + request_body->size(); |
| 144 scoped_refptr<IOBuffer> merged_request_headers_and_body( | 144 scoped_refptr<IOBuffer> merged_request_headers_and_body( |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 io_state_ = STATE_REQUEST_SENT; | 342 io_state_ = STATE_REQUEST_SENT; |
| 343 } | 343 } |
| 344 return result; | 344 return result; |
| 345 } | 345 } |
| 346 | 346 |
| 347 int HttpStreamParser::DoSendChunkedBody(int result) { | 347 int HttpStreamParser::DoSendChunkedBody(int result) { |
| 348 // |result| is the number of bytes sent from the last call to | 348 // |result| is the number of bytes sent from the last call to |
| 349 // DoSendChunkedBody(), or 0 (i.e. OK) the first time. | 349 // DoSendChunkedBody(), or 0 (i.e. OK) the first time. |
| 350 | 350 |
| 351 // Send the remaining data in the chunk buffer. | 351 // Send the remaining data in the chunk buffer. |
| 352 if (chunk_buf_.get()) { | 352 chunk_buf_->DidConsume(result); |
| 353 chunk_buf_->DidConsume(result); | 353 if (chunk_buf_->BytesRemaining() > 0) { |
| 354 if (chunk_buf_->BytesRemaining() > 0) { | 354 return connection_->socket()->Write(chunk_buf_, |
| 355 return connection_->socket()->Write(chunk_buf_, | 355 chunk_buf_->BytesRemaining(), |
| 356 chunk_buf_->BytesRemaining(), | 356 io_callback_); |
| 357 io_callback_); | |
| 358 } | |
| 359 } | 357 } |
| 360 | 358 |
| 361 if (sent_last_chunk_) { | 359 if (sent_last_chunk_) { |
| 362 io_state_ = STATE_REQUEST_SENT; | 360 io_state_ = STATE_REQUEST_SENT; |
| 363 return OK; | 361 return OK; |
| 364 } | 362 } |
| 365 | 363 |
| 366 // |chunk_length_without_encoding_| is 0 when DoSendBody() is first | 364 // |chunk_length_without_encoding_| is 0 when DoSendBody() is first |
| 367 // called, hence the first call to MarkConsumedAndFillBuffer() is a noop. | 365 // called, hence the first call to MarkConsumedAndFillBuffer() is a noop. |
| 368 request_body_->MarkConsumedAndFillBuffer(chunk_length_without_encoding_); | 366 request_body_->MarkConsumedAndFillBuffer(chunk_length_without_encoding_); |
| 369 chunk_length_without_encoding_ = 0; | 367 chunk_length_without_encoding_ = 0; |
| 370 | 368 |
| 371 if (request_body_->eof()) { | 369 if (request_body_->eof()) { |
| 370 chunk_buf_->Clear(); | |
| 372 const int chunk_length = EncodeChunk( | 371 const int chunk_length = EncodeChunk( |
| 373 base::StringPiece(), raw_chunk_buf_->data(), raw_chunk_buf_->size()); | 372 base::StringPiece(), chunk_buf_->data(), chunk_buf_->capacity()); |
| 374 chunk_buf_ = new DrainableIOBuffer(raw_chunk_buf_, chunk_length); | 373 chunk_buf_->DidAppend(chunk_length); |
| 375 sent_last_chunk_ = true; | 374 sent_last_chunk_ = true; |
| 376 } else if (request_body_->buf_len() > 0) { | 375 } else if (request_body_->buf_len() > 0) { |
| 377 // Encode and send the buffer as 1 chunk. | 376 // Encode and send the buffer as 1 chunk. |
| 378 const base::StringPiece payload(request_body_->buf()->data(), | 377 const base::StringPiece payload(request_body_->buf()->data(), |
| 379 request_body_->buf_len()); | 378 request_body_->buf_len()); |
| 379 chunk_buf_->Clear(); | |
| 380 const int chunk_length = EncodeChunk( | 380 const int chunk_length = EncodeChunk( |
| 381 payload, raw_chunk_buf_->data(), raw_chunk_buf_->size()); | 381 payload, chunk_buf_->data(), chunk_buf_->capacity()); |
| 382 chunk_buf_ = new DrainableIOBuffer(raw_chunk_buf_, chunk_length); | 382 chunk_buf_->DidAppend(chunk_length); |
| 383 chunk_length_without_encoding_ = payload.size(); | 383 chunk_length_without_encoding_ = payload.size(); |
| 384 } else { | 384 } else { |
| 385 // Nothing to send. More POST data is yet to come? | 385 // Nothing to send. More POST data is yet to come? |
| 386 return ERR_IO_PENDING; | 386 return ERR_IO_PENDING; |
| 387 } | 387 } |
| 388 | 388 |
| 389 return connection_->socket()->Write(chunk_buf_, chunk_buf_->BytesRemaining(), | 389 return connection_->socket()->Write(chunk_buf_, chunk_buf_->BytesRemaining(), |
| 390 io_callback_); | 390 io_callback_); |
| 391 } | 391 } |
| 392 | 392 |
| (...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 823 request_body->IsInMemory() && | 823 request_body->IsInMemory() && |
| 824 request_body->size() > 0) { | 824 request_body->size() > 0) { |
| 825 size_t merged_size = request_headers.size() + request_body->size(); | 825 size_t merged_size = request_headers.size() + request_body->size(); |
| 826 if (merged_size <= kMaxMergedHeaderAndBodySize) | 826 if (merged_size <= kMaxMergedHeaderAndBodySize) |
| 827 return true; | 827 return true; |
| 828 } | 828 } |
| 829 return false; | 829 return false; |
| 830 } | 830 } |
| 831 | 831 |
| 832 } // namespace net | 832 } // namespace net |
| OLD | NEW |