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
3 // found in the LICENSE file.
5 #include "net/http/http_stream_parser.h"
8 #include "base/compiler_specific.h"
9 #include "base/strings/string_util.h"
10 #include "base/values.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/upload_data_stream.h"
14 #include "net/http/http_chunked_decoder.h"
15 #include "net/http/http_request_headers.h"
16 #include "net/http/http_request_info.h"
17 #include "net/http/http_response_headers.h"
18 #include "net/http/http_util.h"
19 #include "net/socket/client_socket_handle.h"
20 #include "net/socket/ssl_client_socket.h"
24 const size_t kMaxMergedHeaderAndBodySize = 1400;
25 const size_t kRequestBodyBufferSize = 1 << 14; // 16KB
27 std::string GetResponseHeaderLines(const net::HttpResponseHeaders& headers) {
28 std::string raw_headers = headers.raw_headers();
29 const char* null_separated_headers = raw_headers.c_str();
30 const char* header_line = null_separated_headers;
31 std::string cr_separated_headers;
32 while (header_line[0] != 0) {
33 cr_separated_headers += header_line;
34 cr_separated_headers += "\n";
35 header_line += strlen(header_line) + 1;
37 return cr_separated_headers;
40 // Return true if |headers| contain multiple |field_name| fields with different
42 bool HeadersContainMultipleCopiesOfField(
43 const net::HttpResponseHeaders& headers,
44 const std::string& field_name) {
46 std::string field_value;
47 if (!headers.EnumerateHeader(&it, field_name, &field_value))
49 // There's at least one |field_name| header. Check if there are any more
50 // such headers, and if so, return true if they have different values.
51 std::string field_value2;
52 while (headers.EnumerateHeader(&it, field_name, &field_value2)) {
53 if (field_value != field_value2)
59 base::Value* NetLogSendRequestBodyCallback(
63 net::NetLog::LogLevel /* log_level */) {
64 base::DictionaryValue* dict = new base::DictionaryValue();
65 dict->SetInteger("length", length);
66 dict->SetBoolean("is_chunked", is_chunked);
67 dict->SetBoolean("did_merge", did_merge);
75 // Similar to DrainableIOBuffer(), but this version comes with its own
76 // storage. The motivation is to avoid repeated allocations of
81 // scoped_refptr<SeekableIOBuffer> buf = new SeekableIOBuffer(1024);
82 // // capacity() == 1024. size() == BytesRemaining() == BytesConsumed() == 0.
83 // // data() points to the beginning of the buffer.
85 // // Read() takes an IOBuffer.
86 // int bytes_read = some_reader->Read(buf, buf->capacity());
87 // buf->DidAppend(bytes_read);
88 // // size() == BytesRemaining() == bytes_read. data() is unaffected.
90 // while (buf->BytesRemaining() > 0) {
91 // // Write() takes an IOBuffer. If it takes const char*, we could
92 /// // simply use the regular IOBuffer like buf->data() + offset.
93 // int bytes_written = Write(buf, buf->BytesRemaining());
94 // buf->DidConsume(bytes_written);
96 // // BytesRemaining() == 0. BytesConsumed() == size().
97 // // data() points to the end of the consumed bytes (exclusive).
99 // // If you want to reuse the buffer, be sure to clear the buffer.
101 // // size() == BytesRemaining() == BytesConsumed() == 0.
102 // // data() points to the beginning of the buffer.
104 class HttpStreamParser::SeekableIOBuffer : public net::IOBuffer {
106 explicit SeekableIOBuffer(int capacity)
107 : IOBuffer(capacity),
114 // DidConsume() changes the |data_| pointer so that |data_| always points
115 // to the first unconsumed byte.
116 void DidConsume(int bytes) {
117 SetOffset(used_ + bytes);
120 // Returns the number of unconsumed bytes.
121 int BytesRemaining() const {
122 return size_ - used_;
125 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed
126 // and remaining are updated appropriately.
127 void SetOffset(int bytes) {
129 DCHECK_LE(bytes, size_);
131 data_ = real_data_ + used_;
134 // Called after data is added to the buffer. Adds |bytes| added to
135 // |size_|. data() is unaffected.
136 void DidAppend(int bytes) {
138 DCHECK_GE(size_ + bytes, 0);
139 DCHECK_LE(size_ + bytes, capacity_);
143 // Changes the logical size to 0, and the offset to 0.
149 // Returns the logical size of the buffer (i.e the number of bytes of data
151 int size() const { return size_; }
153 // Returns the capacity of the buffer. The capacity is the size used when
154 // the object is created.
155 int capacity() const { return capacity_; };
158 virtual ~SeekableIOBuffer() {
159 // data_ will be deleted in IOBuffer::~IOBuffer().
169 // 2 CRLFs + max of 8 hex chars.
170 const size_t HttpStreamParser::kChunkHeaderFooterSize = 12;
172 HttpStreamParser::HttpStreamParser(ClientSocketHandle* connection,
173 const HttpRequestInfo* request,
174 GrowableIOBuffer* read_buffer,
175 const BoundNetLog& net_log)
176 : io_state_(STATE_NONE),
178 request_headers_(NULL),
179 read_buf_(read_buffer),
180 read_buf_unused_offset_(0),
181 response_header_start_offset_(-1),
182 response_body_length_(-1),
183 response_body_read_(0),
184 user_read_buf_(NULL),
185 user_read_buf_len_(0),
186 connection_(connection),
188 sent_last_chunk_(false),
189 weak_ptr_factory_(this) {
190 io_callback_ = base::Bind(&HttpStreamParser::OnIOComplete,
191 weak_ptr_factory_.GetWeakPtr());
194 HttpStreamParser::~HttpStreamParser() {
197 int HttpStreamParser::SendRequest(const std::string& request_line,
198 const HttpRequestHeaders& headers,
199 HttpResponseInfo* response,
200 const CompletionCallback& callback) {
201 DCHECK_EQ(STATE_NONE, io_state_);
202 DCHECK(callback_.is_null());
203 DCHECK(!callback.is_null());
207 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS,
208 base::Bind(&HttpRequestHeaders::NetLogCallback,
209 base::Unretained(&headers),
212 DVLOG(1) << __FUNCTION__ << "()"
213 << " request_line = \"" << request_line << "\""
214 << " headers = \"" << headers.ToString() << "\"";
215 response_ = response;
217 // Put the peer's IP address and port into the response.
218 IPEndPoint ip_endpoint;
219 int result = connection_->socket()->GetPeerAddress(&ip_endpoint);
222 response_->socket_address = HostPortPair::FromIPEndPoint(ip_endpoint);
224 std::string request = request_line + headers.ToString();
226 if (request_->upload_data_stream != NULL) {
227 request_body_send_buf_ = new SeekableIOBuffer(kRequestBodyBufferSize);
228 if (request_->upload_data_stream->is_chunked()) {
229 // Read buffer is adjusted to guarantee that |request_body_send_buf_| is
230 // large enough to hold the encoded chunk.
231 request_body_read_buf_ =
232 new SeekableIOBuffer(kRequestBodyBufferSize - kChunkHeaderFooterSize);
234 // No need to encode request body, just send the raw data.
235 request_body_read_buf_ = request_body_send_buf_;
239 io_state_ = STATE_SENDING_HEADERS;
241 // If we have a small request body, then we'll merge with the headers into a
243 bool did_merge = false;
244 if (ShouldMergeRequestHeadersAndBody(request, request_->upload_data_stream)) {
245 size_t merged_size = request.size() + request_->upload_data_stream->size();
246 scoped_refptr<IOBuffer> merged_request_headers_and_body(
247 new IOBuffer(merged_size));
248 // We'll repurpose |request_headers_| to store the merged headers and
250 request_headers_ = new DrainableIOBuffer(
251 merged_request_headers_and_body.get(), merged_size);
253 memcpy(request_headers_->data(), request.data(), request.size());
254 request_headers_->DidConsume(request.size());
256 size_t todo = request_->upload_data_stream->size();
258 int consumed = request_->upload_data_stream
259 ->Read(request_headers_.get(), todo, CompletionCallback());
260 DCHECK_GT(consumed, 0); // Read() won't fail if not chunked.
261 request_headers_->DidConsume(consumed);
264 DCHECK(request_->upload_data_stream->IsEOF());
265 // Reset the offset, so the buffer can be read from the beginning.
266 request_headers_->SetOffset(0);
270 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY,
271 base::Bind(&NetLogSendRequestBodyCallback,
272 request_->upload_data_stream->size(),
273 false, /* not chunked */
278 // If we didn't merge the body with the headers, then |request_headers_|
279 // contains just the HTTP headers.
280 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request));
282 new DrainableIOBuffer(headers_io_buf.get(), headers_io_buf->size());
286 if (result == ERR_IO_PENDING)
287 callback_ = callback;
289 return result > 0 ? OK : result;
292 int HttpStreamParser::ReadResponseHeaders(const CompletionCallback& callback) {
293 DCHECK(io_state_ == STATE_REQUEST_SENT || io_state_ == STATE_DONE);
294 DCHECK(callback_.is_null());
295 DCHECK(!callback.is_null());
296 DCHECK_EQ(0, read_buf_unused_offset_);
298 // This function can be called with io_state_ == STATE_DONE if the
299 // connection is closed after seeing just a 1xx response code.
300 if (io_state_ == STATE_DONE)
301 return ERR_CONNECTION_CLOSED;
304 io_state_ = STATE_READ_HEADERS;
306 if (read_buf_->offset() > 0) {
307 // Simulate the state where the data was just read from the socket.
308 result = read_buf_->offset();
309 read_buf_->set_offset(0);
312 io_state_ = STATE_READ_HEADERS_COMPLETE;
314 result = DoLoop(result);
315 if (result == ERR_IO_PENDING)
316 callback_ = callback;
318 return result > 0 ? OK : result;
321 void HttpStreamParser::Close(bool not_reusable) {
322 if (not_reusable && connection_->socket())
323 connection_->socket()->Disconnect();
324 connection_->Reset();
327 int HttpStreamParser::ReadResponseBody(IOBuffer* buf, int buf_len,
328 const CompletionCallback& callback) {
329 DCHECK(io_state_ == STATE_BODY_PENDING || io_state_ == STATE_DONE);
330 DCHECK(callback_.is_null());
331 DCHECK(!callback.is_null());
332 DCHECK_LE(buf_len, kMaxBufSize);
334 if (io_state_ == STATE_DONE)
337 user_read_buf_ = buf;
338 user_read_buf_len_ = buf_len;
339 io_state_ = STATE_READ_BODY;
341 int result = DoLoop(OK);
342 if (result == ERR_IO_PENDING)
343 callback_ = callback;
348 void HttpStreamParser::OnIOComplete(int result) {
349 result = DoLoop(result);
351 // The client callback can do anything, including destroying this class,
352 // so any pending callback must be issued after everything else is done.
353 if (result != ERR_IO_PENDING && !callback_.is_null()) {
354 CompletionCallback c = callback_;
360 int HttpStreamParser::DoLoop(int result) {
361 bool can_do_more = true;
364 case STATE_SENDING_HEADERS:
368 result = DoSendHeaders(result);
370 case STATE_SENDING_BODY:
374 result = DoSendBody(result);
376 case STATE_SEND_REQUEST_READING_BODY:
377 result = DoSendRequestReadingBody(result);
379 case STATE_REQUEST_SENT:
380 DCHECK(result != ERR_IO_PENDING);
383 case STATE_READ_HEADERS:
384 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS);
385 result = DoReadHeaders();
387 case STATE_READ_HEADERS_COMPLETE:
388 result = DoReadHeadersComplete(result);
389 net_log_.EndEventWithNetErrorCode(
390 NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS, result);
392 case STATE_BODY_PENDING:
393 DCHECK(result != ERR_IO_PENDING);
396 case STATE_READ_BODY:
397 result = DoReadBody();
398 // DoReadBodyComplete handles error conditions.
400 case STATE_READ_BODY_COMPLETE:
401 result = DoReadBodyComplete(result);
404 DCHECK(result != ERR_IO_PENDING);
412 } while (result != ERR_IO_PENDING && can_do_more);
417 int HttpStreamParser::DoSendHeaders(int result) {
418 request_headers_->DidConsume(result);
419 int bytes_remaining = request_headers_->BytesRemaining();
420 if (bytes_remaining > 0) {
421 // Record our best estimate of the 'request time' as the time when we send
422 // out the first bytes of the request headers.
423 if (bytes_remaining == request_headers_->size()) {
424 response_->request_time = base::Time::Now();
426 result = connection_->socket()
427 ->Write(request_headers_.get(), bytes_remaining, io_callback_);
428 } else if (request_->upload_data_stream != NULL &&
429 (request_->upload_data_stream->is_chunked() ||
430 // !IsEOF() indicates that the body wasn't merged.
431 (request_->upload_data_stream->size() > 0 &&
432 !request_->upload_data_stream->IsEOF()))) {
434 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY,
435 base::Bind(&NetLogSendRequestBodyCallback,
436 request_->upload_data_stream->size(),
437 request_->upload_data_stream->is_chunked(),
438 false /* not merged */));
439 io_state_ = STATE_SENDING_BODY;
442 io_state_ = STATE_REQUEST_SENT;
447 int HttpStreamParser::DoSendBody(int result) {
448 // |result| is the number of bytes sent from the last call to
449 // DoSendBody(), or 0 (i.e. OK).
451 // Send the remaining data in the request body buffer.
452 request_body_send_buf_->DidConsume(result);
453 if (request_body_send_buf_->BytesRemaining() > 0) {
454 return connection_->socket()
455 ->Write(request_body_send_buf_.get(),
456 request_body_send_buf_->BytesRemaining(),
460 if (request_->upload_data_stream->is_chunked() && sent_last_chunk_) {
461 io_state_ = STATE_REQUEST_SENT;
465 request_body_read_buf_->Clear();
466 io_state_ = STATE_SEND_REQUEST_READING_BODY;
467 return request_->upload_data_stream->Read(request_body_read_buf_.get(),
468 request_body_read_buf_->capacity(),
472 int HttpStreamParser::DoSendRequestReadingBody(int result) {
473 // |result| is the result of read from the request body from the last call to
475 DCHECK_GE(result, 0); // There won't be errors.
477 // Chunked data needs to be encoded.
478 if (request_->upload_data_stream->is_chunked()) {
479 if (result == 0) { // Reached the end.
480 DCHECK(request_->upload_data_stream->IsEOF());
481 sent_last_chunk_ = true;
483 // Encode the buffer as 1 chunk.
484 const base::StringPiece payload(request_body_read_buf_->data(), result);
485 request_body_send_buf_->Clear();
486 result = EncodeChunk(payload,
487 request_body_send_buf_->data(),
488 request_body_send_buf_->capacity());
491 if (result == 0) { // Reached the end.
492 // Reaching EOF means we can finish sending request body unless the data is
493 // chunked. (i.e. No need to send the terminal chunk.)
494 DCHECK(request_->upload_data_stream->IsEOF());
495 DCHECK(!request_->upload_data_stream->is_chunked());
496 io_state_ = STATE_REQUEST_SENT;
497 } else if (result > 0) {
498 request_body_send_buf_->DidAppend(result);
500 io_state_ = STATE_SENDING_BODY;
505 int HttpStreamParser::DoReadHeaders() {
506 io_state_ = STATE_READ_HEADERS_COMPLETE;
508 // Grow the read buffer if necessary.
509 if (read_buf_->RemainingCapacity() == 0)
510 read_buf_->SetCapacity(read_buf_->capacity() + kHeaderBufInitialSize);
512 // http://crbug.com/16371: We're seeing |user_buf_->data()| return NULL.
513 // See if the user is passing in an IOBuffer with a NULL |data_|.
514 CHECK(read_buf_->data());
516 return connection_->socket()
517 ->Read(read_buf_.get(), read_buf_->RemainingCapacity(), io_callback_);
520 int HttpStreamParser::DoReadHeadersComplete(int result) {
521 DCHECK_EQ(0, read_buf_unused_offset_);
524 result = ERR_CONNECTION_CLOSED;
526 if (result < 0 && result != ERR_CONNECTION_CLOSED) {
527 io_state_ = STATE_DONE;
530 // If we've used the connection before, then we know it is not a HTTP/0.9
531 // response and return ERR_CONNECTION_CLOSED.
532 if (result == ERR_CONNECTION_CLOSED && read_buf_->offset() == 0 &&
533 connection_->is_reused()) {
534 io_state_ = STATE_DONE;
538 // Record our best estimate of the 'response time' as the time when we read
539 // the first bytes of the response headers.
540 if (read_buf_->offset() == 0 && result != ERR_CONNECTION_CLOSED)
541 response_->response_time = base::Time::Now();
543 if (result == ERR_CONNECTION_CLOSED) {
544 // The connection closed before we detected the end of the headers.
545 if (read_buf_->offset() == 0) {
546 // The connection was closed before any data was sent. Likely an error
547 // rather than empty HTTP/0.9 response.
548 io_state_ = STATE_DONE;
549 return ERR_EMPTY_RESPONSE;
550 } else if (request_->url.SchemeIsSecure()) {
551 // The connection was closed in the middle of the headers. For HTTPS we
552 // don't parse partial headers. Return a different error code so that we
553 // know that we shouldn't attempt to retry the request.
554 io_state_ = STATE_DONE;
555 return ERR_RESPONSE_HEADERS_TRUNCATED;
557 // Parse things as well as we can and let the caller decide what to do.
559 if (response_header_start_offset_ >= 0) {
560 io_state_ = STATE_READ_BODY_COMPLETE;
561 end_offset = read_buf_->offset();
563 io_state_ = STATE_BODY_PENDING;
566 int rv = DoParseResponseHeaders(end_offset);
572 read_buf_->set_offset(read_buf_->offset() + result);
573 DCHECK_LE(read_buf_->offset(), read_buf_->capacity());
574 DCHECK_GE(result, 0);
576 int end_of_header_offset = ParseResponseHeaders();
578 // Note: -1 is special, it indicates we haven't found the end of headers.
579 // Anything less than -1 is a net::Error, so we bail out.
580 if (end_of_header_offset < -1)
581 return end_of_header_offset;
583 if (end_of_header_offset == -1) {
584 io_state_ = STATE_READ_HEADERS;
585 // Prevent growing the headers buffer indefinitely.
586 if (read_buf_->offset() >= kMaxHeaderBufSize) {
587 io_state_ = STATE_DONE;
588 return ERR_RESPONSE_HEADERS_TOO_BIG;
591 CalculateResponseBodySize();
592 // If the body is zero length, the caller may not call ReadResponseBody,
593 // which is where any extra data is copied to read_buf_, so we move the
595 if (response_body_length_ == 0) {
596 int extra_bytes = read_buf_->offset() - end_of_header_offset;
598 CHECK_GT(extra_bytes, 0);
599 memmove(read_buf_->StartOfBuffer(),
600 read_buf_->StartOfBuffer() + end_of_header_offset,
603 read_buf_->SetCapacity(extra_bytes);
604 if (response_->headers->response_code() / 100 == 1) {
605 // After processing a 1xx response, the caller will ask for the next
606 // header, so reset state to support that. We don't completely ignore a
607 // 1xx response because it cannot be returned in reply to a CONNECT
608 // request so we return OK here, which lets the caller inspect the
609 // response and reject it in the event that we're setting up a CONNECT
611 response_header_start_offset_ = -1;
612 response_body_length_ = -1;
613 io_state_ = STATE_REQUEST_SENT;
615 io_state_ = STATE_DONE;
620 // Note where the headers stop.
621 read_buf_unused_offset_ = end_of_header_offset;
622 io_state_ = STATE_BODY_PENDING;
627 int HttpStreamParser::DoReadBody() {
628 io_state_ = STATE_READ_BODY_COMPLETE;
630 // There may be some data left over from reading the response headers.
631 if (read_buf_->offset()) {
632 int available = read_buf_->offset() - read_buf_unused_offset_;
634 CHECK_GT(available, 0);
635 int bytes_from_buffer = std::min(available, user_read_buf_len_);
636 memcpy(user_read_buf_->data(),
637 read_buf_->StartOfBuffer() + read_buf_unused_offset_,
639 read_buf_unused_offset_ += bytes_from_buffer;
640 if (bytes_from_buffer == available) {
641 read_buf_->SetCapacity(0);
642 read_buf_unused_offset_ = 0;
644 return bytes_from_buffer;
646 read_buf_->SetCapacity(0);
647 read_buf_unused_offset_ = 0;
651 // Check to see if we're done reading.
652 if (IsResponseBodyComplete())
655 DCHECK_EQ(0, read_buf_->offset());
656 return connection_->socket()
657 ->Read(user_read_buf_.get(), user_read_buf_len_, io_callback_);
660 int HttpStreamParser::DoReadBodyComplete(int result) {
661 // When the connection is closed, there are numerous ways to interpret it.
663 // - If a Content-Length header is present and the body contains exactly that
664 // number of bytes at connection close, the response is successful.
666 // - If a Content-Length header is present and the body contains fewer bytes
667 // than promised by the header at connection close, it may indicate that
668 // the connection was closed prematurely, or it may indicate that the
669 // server sent an invalid Content-Length header. Unfortunately, the invalid
670 // Content-Length header case does occur in practice and other browsers are
671 // tolerant of it. We choose to treat it as an error for now, but the
672 // download system treats it as a non-error, and URLRequestHttpJob also
673 // treats it as OK if the Content-Length is the post-decoded body content
676 // - If chunked encoding is used and the terminating chunk has been processed
677 // when the connection is closed, the response is successful.
679 // - If chunked encoding is used and the terminating chunk has not been
680 // processed when the connection is closed, it may indicate that the
681 // connection was closed prematurely or it may indicate that the server
682 // sent an invalid chunked encoding. We choose to treat it as
683 // an invalid chunked encoding.
685 // - If a Content-Length is not present and chunked encoding is not used,
686 // connection close is the only way to signal that the response is
687 // complete. Unfortunately, this also means that there is no way to detect
688 // early close of a connection. No error is returned.
689 if (result == 0 && !IsResponseBodyComplete() && CanFindEndOfResponse()) {
690 if (chunked_decoder_.get())
691 result = ERR_INCOMPLETE_CHUNKED_ENCODING;
693 result = ERR_CONTENT_LENGTH_MISMATCH;
696 // Filter incoming data if appropriate. FilterBuf may return an error.
697 if (result > 0 && chunked_decoder_.get()) {
698 result = chunked_decoder_->FilterBuf(user_read_buf_->data(), result);
699 if (result == 0 && !chunked_decoder_->reached_eof()) {
700 // Don't signal completion of the Read call yet or else it'll look like
701 // we received end-of-file. Wait for more data.
702 io_state_ = STATE_READ_BODY;
708 response_body_read_ += result;
710 if (result <= 0 || IsResponseBodyComplete()) {
711 io_state_ = STATE_DONE;
713 // Save the overflow data, which can be in two places. There may be
714 // some left over in |user_read_buf_|, plus there may be more
715 // in |read_buf_|. But the part left over in |user_read_buf_| must have
716 // come from the |read_buf_|, so there's room to put it back at the
718 int additional_save_amount = read_buf_->offset() - read_buf_unused_offset_;
720 if (chunked_decoder_.get()) {
721 save_amount = chunked_decoder_->bytes_after_eof();
722 } else if (response_body_length_ >= 0) {
723 int64 extra_data_read = response_body_read_ - response_body_length_;
724 if (extra_data_read > 0) {
725 save_amount = static_cast<int>(extra_data_read);
727 result -= save_amount;
731 CHECK_LE(save_amount + additional_save_amount, kMaxBufSize);
732 if (read_buf_->capacity() < save_amount + additional_save_amount) {
733 read_buf_->SetCapacity(save_amount + additional_save_amount);
737 memcpy(read_buf_->StartOfBuffer(), user_read_buf_->data() + result,
740 read_buf_->set_offset(save_amount);
741 if (additional_save_amount) {
742 memmove(read_buf_->data(),
743 read_buf_->StartOfBuffer() + read_buf_unused_offset_,
744 additional_save_amount);
745 read_buf_->set_offset(save_amount + additional_save_amount);
747 read_buf_unused_offset_ = 0;
749 io_state_ = STATE_BODY_PENDING;
750 user_read_buf_ = NULL;
751 user_read_buf_len_ = 0;
757 int HttpStreamParser::ParseResponseHeaders() {
759 DCHECK_EQ(0, read_buf_unused_offset_);
761 // Look for the start of the status line, if it hasn't been found yet.
762 if (response_header_start_offset_ < 0) {
763 response_header_start_offset_ = HttpUtil::LocateStartOfStatusLine(
764 read_buf_->StartOfBuffer(), read_buf_->offset());
767 if (response_header_start_offset_ >= 0) {
768 end_offset = HttpUtil::LocateEndOfHeaders(read_buf_->StartOfBuffer(),
770 response_header_start_offset_);
771 } else if (read_buf_->offset() >= 8) {
772 // Enough data to decide that this is an HTTP/0.9 response.
773 // 8 bytes = (4 bytes of junk) + "http".length()
777 if (end_offset == -1)
780 int rv = DoParseResponseHeaders(end_offset);
786 int HttpStreamParser::DoParseResponseHeaders(int end_offset) {
787 scoped_refptr<HttpResponseHeaders> headers;
788 DCHECK_EQ(0, read_buf_unused_offset_);
790 if (response_header_start_offset_ >= 0) {
791 headers = new HttpResponseHeaders(HttpUtil::AssembleRawHeaders(
792 read_buf_->StartOfBuffer(), end_offset));
794 // Enough data was read -- there is no status line.
795 headers = new HttpResponseHeaders(std::string("HTTP/0.9 200 OK"));
798 // Check for multiple Content-Length headers with no Transfer-Encoding header.
799 // If they exist, and have distinct values, it's a potential response
801 if (!headers->HasHeader("Transfer-Encoding")) {
802 if (HeadersContainMultipleCopiesOfField(*headers.get(), "Content-Length"))
803 return ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH;
806 // Check for multiple Content-Disposition or Location headers. If they exist,
807 // it's also a potential response smuggling attack.
808 if (HeadersContainMultipleCopiesOfField(*headers.get(),
809 "Content-Disposition"))
810 return ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION;
811 if (HeadersContainMultipleCopiesOfField(*headers.get(), "Location"))
812 return ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION;
814 response_->headers = headers;
815 response_->connection_info = HttpResponseInfo::CONNECTION_INFO_HTTP1;
816 response_->vary_data.Init(*request_, *response_->headers.get());
817 DVLOG(1) << __FUNCTION__ << "()"
818 << " content_length = \"" << response_->headers->GetContentLength()
821 << GetResponseHeaderLines(*response_->headers.get()) << "\"";
825 void HttpStreamParser::CalculateResponseBodySize() {
826 // Figure how to determine EOF:
828 // For certain responses, we know the content length is always 0. From
829 // RFC 2616 Section 4.3 Message Body:
831 // For response messages, whether or not a message-body is included with
832 // a message is dependent on both the request method and the response
833 // status code (section 6.1.1). All responses to the HEAD request method
834 // MUST NOT include a message-body, even though the presence of entity-
835 // header fields might lead one to believe they do. All 1xx
836 // (informational), 204 (no content), and 304 (not modified) responses
837 // MUST NOT include a message-body. All other responses do include a
838 // message-body, although it MAY be of zero length.
839 if (response_->headers->response_code() / 100 == 1) {
840 response_body_length_ = 0;
842 switch (response_->headers->response_code()) {
843 case 204: // No Content
844 case 205: // Reset Content
845 case 304: // Not Modified
846 response_body_length_ = 0;
850 if (request_->method == "HEAD")
851 response_body_length_ = 0;
853 if (response_body_length_ == -1) {
854 // "Transfer-Encoding: chunked" trumps "Content-Length: N"
855 if (response_->headers->IsChunkEncoded()) {
856 chunked_decoder_.reset(new HttpChunkedDecoder());
858 response_body_length_ = response_->headers->GetContentLength();
859 // If response_body_length_ is still -1, then we have to wait
860 // for the server to close the connection.
865 UploadProgress HttpStreamParser::GetUploadProgress() const {
866 if (!request_->upload_data_stream)
867 return UploadProgress();
869 return UploadProgress(request_->upload_data_stream->position(),
870 request_->upload_data_stream->size());
873 HttpResponseInfo* HttpStreamParser::GetResponseInfo() {
877 bool HttpStreamParser::IsResponseBodyComplete() const {
878 if (chunked_decoder_.get())
879 return chunked_decoder_->reached_eof();
880 if (response_body_length_ != -1)
881 return response_body_read_ >= response_body_length_;
883 return false; // Must read to EOF.
886 bool HttpStreamParser::CanFindEndOfResponse() const {
887 return chunked_decoder_.get() || response_body_length_ >= 0;
890 bool HttpStreamParser::IsMoreDataBuffered() const {
891 return read_buf_->offset() > read_buf_unused_offset_;
894 bool HttpStreamParser::IsConnectionReused() const {
895 ClientSocketHandle::SocketReuseType reuse_type = connection_->reuse_type();
896 return connection_->is_reused() ||
897 reuse_type == ClientSocketHandle::UNUSED_IDLE;
900 void HttpStreamParser::SetConnectionReused() {
901 connection_->set_is_reused(true);
904 bool HttpStreamParser::IsConnectionReusable() const {
905 return connection_->socket() && connection_->socket()->IsConnectedAndIdle();
908 void HttpStreamParser::GetSSLInfo(SSLInfo* ssl_info) {
909 if (request_->url.SchemeIsSecure() && connection_->socket()) {
910 SSLClientSocket* ssl_socket =
911 static_cast<SSLClientSocket*>(connection_->socket());
912 ssl_socket->GetSSLInfo(ssl_info);
916 void HttpStreamParser::GetSSLCertRequestInfo(
917 SSLCertRequestInfo* cert_request_info) {
918 if (request_->url.SchemeIsSecure() && connection_->socket()) {
919 SSLClientSocket* ssl_socket =
920 static_cast<SSLClientSocket*>(connection_->socket());
921 ssl_socket->GetSSLCertRequestInfo(cert_request_info);
925 int HttpStreamParser::EncodeChunk(const base::StringPiece& payload,
927 size_t output_size) {
928 if (output_size < payload.size() + kChunkHeaderFooterSize)
929 return ERR_INVALID_ARGUMENT;
931 char* cursor = output;
933 const int num_chars = base::snprintf(output, output_size,
935 static_cast<int>(payload.size()));
937 // Add the payload if any.
938 if (payload.size() > 0) {
939 memcpy(cursor, payload.data(), payload.size());
940 cursor += payload.size();
942 // Add the trailing CRLF.
943 memcpy(cursor, "\r\n", 2);
946 return cursor - output;
950 bool HttpStreamParser::ShouldMergeRequestHeadersAndBody(
951 const std::string& request_headers,
952 const UploadDataStream* request_body) {
953 if (request_body != NULL &&
954 // IsInMemory() ensures that the request body is not chunked.
955 request_body->IsInMemory() &&
956 request_body->size() > 0) {
957 size_t merged_size = request_headers.size() + request_body->size();
958 if (merged_size <= kMaxMergedHeaderAndBodySize)