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/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/values.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/upload_data_stream.h"
15 #include "net/http/http_chunked_decoder.h"
16 #include "net/http/http_request_headers.h"
17 #include "net/http/http_request_info.h"
18 #include "net/http/http_response_headers.h"
19 #include "net/http/http_util.h"
20 #include "net/socket/client_socket_handle.h"
21 #include "net/socket/ssl_client_socket.h"
27 const size_t kMaxMergedHeaderAndBodySize = 1400;
28 const size_t kRequestBodyBufferSize = 1 << 14; // 16KB
30 std::string GetResponseHeaderLines(const HttpResponseHeaders& headers) {
31 std::string raw_headers = headers.raw_headers();
32 const char* null_separated_headers = raw_headers.c_str();
33 const char* header_line = null_separated_headers;
34 std::string cr_separated_headers;
35 while (header_line[0] != 0) {
36 cr_separated_headers += header_line;
37 cr_separated_headers += "\n";
38 header_line += strlen(header_line) + 1;
40 return cr_separated_headers;
43 // Return true if |headers| contain multiple |field_name| fields with different
45 bool HeadersContainMultipleCopiesOfField(const HttpResponseHeaders& headers,
46 const std::string& field_name) {
48 std::string field_value;
49 if (!headers.EnumerateHeader(&it, field_name, &field_value))
51 // There's at least one |field_name| header. Check if there are any more
52 // such headers, and if so, return true if they have different values.
53 std::string field_value2;
54 while (headers.EnumerateHeader(&it, field_name, &field_value2)) {
55 if (field_value != field_value2)
61 base::Value* NetLogSendRequestBodyCallback(int length,
64 NetLog::LogLevel /* log_level */) {
65 base::DictionaryValue* dict = new base::DictionaryValue();
66 dict->SetInteger("length", length);
67 dict->SetBoolean("is_chunked", is_chunked);
68 dict->SetBoolean("did_merge", did_merge);
72 // Returns true if |error_code| is an error for which we give the server a
73 // chance to send a body containing error information, if the error was received
74 // while trying to upload a request body.
75 bool ShouldTryReadingOnUploadError(int error_code) {
76 return (error_code == ERR_CONNECTION_RESET);
81 // Similar to DrainableIOBuffer(), but this version comes with its own
82 // storage. The motivation is to avoid repeated allocations of
87 // scoped_refptr<SeekableIOBuffer> buf = new SeekableIOBuffer(1024);
88 // // capacity() == 1024. size() == BytesRemaining() == BytesConsumed() == 0.
89 // // data() points to the beginning of the buffer.
91 // // Read() takes an IOBuffer.
92 // int bytes_read = some_reader->Read(buf, buf->capacity());
93 // buf->DidAppend(bytes_read);
94 // // size() == BytesRemaining() == bytes_read. data() is unaffected.
96 // while (buf->BytesRemaining() > 0) {
97 // // Write() takes an IOBuffer. If it takes const char*, we could
98 /// // simply use the regular IOBuffer like buf->data() + offset.
99 // int bytes_written = Write(buf, buf->BytesRemaining());
100 // buf->DidConsume(bytes_written);
102 // // BytesRemaining() == 0. BytesConsumed() == size().
103 // // data() points to the end of the consumed bytes (exclusive).
105 // // If you want to reuse the buffer, be sure to clear the buffer.
107 // // size() == BytesRemaining() == BytesConsumed() == 0.
108 // // data() points to the beginning of the buffer.
110 class HttpStreamParser::SeekableIOBuffer : public IOBuffer {
112 explicit SeekableIOBuffer(int capacity)
113 : IOBuffer(capacity),
120 // DidConsume() changes the |data_| pointer so that |data_| always points
121 // to the first unconsumed byte.
122 void DidConsume(int bytes) {
123 SetOffset(used_ + bytes);
126 // Returns the number of unconsumed bytes.
127 int BytesRemaining() const {
128 return size_ - used_;
131 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed
132 // and remaining are updated appropriately.
133 void SetOffset(int bytes) {
135 DCHECK_LE(bytes, size_);
137 data_ = real_data_ + used_;
140 // Called after data is added to the buffer. Adds |bytes| added to
141 // |size_|. data() is unaffected.
142 void DidAppend(int bytes) {
144 DCHECK_GE(size_ + bytes, 0);
145 DCHECK_LE(size_ + bytes, capacity_);
149 // Changes the logical size to 0, and the offset to 0.
155 // Returns the logical size of the buffer (i.e the number of bytes of data
157 int size() const { return size_; }
159 // Returns the capacity of the buffer. The capacity is the size used when
160 // the object is created.
161 int capacity() const { return capacity_; };
164 virtual ~SeekableIOBuffer() {
165 // data_ will be deleted in IOBuffer::~IOBuffer().
175 // 2 CRLFs + max of 8 hex chars.
176 const size_t HttpStreamParser::kChunkHeaderFooterSize = 12;
178 HttpStreamParser::HttpStreamParser(ClientSocketHandle* connection,
179 const HttpRequestInfo* request,
180 GrowableIOBuffer* read_buffer,
181 const BoundNetLog& net_log)
182 : io_state_(STATE_NONE),
184 request_headers_(NULL),
185 request_headers_length_(0),
186 read_buf_(read_buffer),
187 read_buf_unused_offset_(0),
188 response_header_start_offset_(-1),
190 response_body_length_(-1),
191 response_body_read_(0),
192 user_read_buf_(NULL),
193 user_read_buf_len_(0),
194 connection_(connection),
196 sent_last_chunk_(false),
198 weak_ptr_factory_(this) {
199 io_callback_ = base::Bind(&HttpStreamParser::OnIOComplete,
200 weak_ptr_factory_.GetWeakPtr());
203 HttpStreamParser::~HttpStreamParser() {
206 int HttpStreamParser::SendRequest(const std::string& request_line,
207 const HttpRequestHeaders& headers,
208 HttpResponseInfo* response,
209 const CompletionCallback& callback) {
210 DCHECK_EQ(STATE_NONE, io_state_);
211 DCHECK(callback_.is_null());
212 DCHECK(!callback.is_null());
216 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS,
217 base::Bind(&HttpRequestHeaders::NetLogCallback,
218 base::Unretained(&headers),
221 DVLOG(1) << __FUNCTION__ << "()"
222 << " request_line = \"" << request_line << "\""
223 << " headers = \"" << headers.ToString() << "\"";
224 response_ = response;
226 // Put the peer's IP address and port into the response.
227 IPEndPoint ip_endpoint;
228 int result = connection_->socket()->GetPeerAddress(&ip_endpoint);
231 response_->socket_address = HostPortPair::FromIPEndPoint(ip_endpoint);
233 std::string request = request_line + headers.ToString();
234 request_headers_length_ = request.size();
236 if (request_->upload_data_stream != NULL) {
237 request_body_send_buf_ = new SeekableIOBuffer(kRequestBodyBufferSize);
238 if (request_->upload_data_stream->is_chunked()) {
239 // Read buffer is adjusted to guarantee that |request_body_send_buf_| is
240 // large enough to hold the encoded chunk.
241 request_body_read_buf_ =
242 new SeekableIOBuffer(kRequestBodyBufferSize - kChunkHeaderFooterSize);
244 // No need to encode request body, just send the raw data.
245 request_body_read_buf_ = request_body_send_buf_;
249 io_state_ = STATE_SEND_HEADERS;
251 // If we have a small request body, then we'll merge with the headers into a
253 bool did_merge = false;
254 if (ShouldMergeRequestHeadersAndBody(request, request_->upload_data_stream)) {
256 request_headers_length_ + request_->upload_data_stream->size();
257 scoped_refptr<IOBuffer> merged_request_headers_and_body(
258 new IOBuffer(merged_size));
259 // We'll repurpose |request_headers_| to store the merged headers and
261 request_headers_ = new DrainableIOBuffer(
262 merged_request_headers_and_body.get(), merged_size);
264 memcpy(request_headers_->data(), request.data(), request_headers_length_);
265 request_headers_->DidConsume(request_headers_length_);
267 size_t todo = request_->upload_data_stream->size();
269 int consumed = request_->upload_data_stream
270 ->Read(request_headers_.get(), todo, CompletionCallback());
271 DCHECK_GT(consumed, 0); // Read() won't fail if not chunked.
272 request_headers_->DidConsume(consumed);
275 DCHECK(request_->upload_data_stream->IsEOF());
276 // Reset the offset, so the buffer can be read from the beginning.
277 request_headers_->SetOffset(0);
281 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY,
282 base::Bind(&NetLogSendRequestBodyCallback,
283 request_->upload_data_stream->size(),
284 false, /* not chunked */
289 // If we didn't merge the body with the headers, then |request_headers_|
290 // contains just the HTTP headers.
291 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request));
293 new DrainableIOBuffer(headers_io_buf.get(), headers_io_buf->size());
297 if (result == ERR_IO_PENDING)
298 callback_ = callback;
300 return result > 0 ? OK : result;
303 int HttpStreamParser::ReadResponseHeaders(const CompletionCallback& callback) {
304 DCHECK(io_state_ == STATE_NONE || io_state_ == STATE_DONE);
305 DCHECK(callback_.is_null());
306 DCHECK(!callback.is_null());
307 DCHECK_EQ(0, read_buf_unused_offset_);
309 // This function can be called with io_state_ == STATE_DONE if the
310 // connection is closed after seeing just a 1xx response code.
311 if (io_state_ == STATE_DONE)
312 return ERR_CONNECTION_CLOSED;
315 io_state_ = STATE_READ_HEADERS;
317 if (read_buf_->offset() > 0) {
318 // Simulate the state where the data was just read from the socket.
319 result = read_buf_->offset();
320 read_buf_->set_offset(0);
323 io_state_ = STATE_READ_HEADERS_COMPLETE;
325 result = DoLoop(result);
326 if (result == ERR_IO_PENDING)
327 callback_ = callback;
329 return result > 0 ? OK : result;
332 void HttpStreamParser::Close(bool not_reusable) {
333 if (not_reusable && connection_->socket())
334 connection_->socket()->Disconnect();
335 connection_->Reset();
338 int HttpStreamParser::ReadResponseBody(IOBuffer* buf, int buf_len,
339 const CompletionCallback& callback) {
340 DCHECK(io_state_ == STATE_NONE || io_state_ == STATE_DONE);
341 DCHECK(callback_.is_null());
342 DCHECK(!callback.is_null());
343 DCHECK_LE(buf_len, kMaxBufSize);
345 if (io_state_ == STATE_DONE)
348 // Must have response headers with a non-1xx error code.
349 DCHECK_NE(1, response_->headers->response_code() / 100);
351 user_read_buf_ = buf;
352 user_read_buf_len_ = buf_len;
353 io_state_ = STATE_READ_BODY;
355 int result = DoLoop(OK);
356 if (result == ERR_IO_PENDING)
357 callback_ = callback;
362 void HttpStreamParser::OnIOComplete(int result) {
363 result = DoLoop(result);
365 // The client callback can do anything, including destroying this class,
366 // so any pending callback must be issued after everything else is done.
367 if (result != ERR_IO_PENDING && !callback_.is_null()) {
368 CompletionCallback c = callback_;
374 int HttpStreamParser::DoLoop(int result) {
376 DCHECK_NE(ERR_IO_PENDING, result);
377 DCHECK_NE(STATE_DONE, io_state_);
378 DCHECK_NE(STATE_NONE, io_state_);
379 State state = io_state_;
380 io_state_ = STATE_NONE;
382 case STATE_SEND_HEADERS:
383 DCHECK_EQ(OK, result);
384 result = DoSendHeaders();
386 case STATE_SEND_HEADERS_COMPLETE:
387 result = DoSendHeadersComplete(result);
389 case STATE_SEND_BODY:
390 DCHECK_EQ(OK, result);
391 result = DoSendBody();
393 case STATE_SEND_BODY_COMPLETE:
394 result = DoSendBodyComplete(result);
396 case STATE_SEND_REQUEST_READ_BODY_COMPLETE:
397 result = DoSendRequestReadBodyComplete(result);
399 case STATE_READ_HEADERS:
400 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS);
401 DCHECK_GE(result, 0);
402 result = DoReadHeaders();
404 case STATE_READ_HEADERS_COMPLETE:
405 result = DoReadHeadersComplete(result);
406 net_log_.EndEventWithNetErrorCode(
407 NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS, result);
409 case STATE_READ_BODY:
410 DCHECK_GE(result, 0);
411 result = DoReadBody();
413 case STATE_READ_BODY_COMPLETE:
414 result = DoReadBodyComplete(result);
420 } while (result != ERR_IO_PENDING &&
421 (io_state_ != STATE_DONE && io_state_ != STATE_NONE));
426 int HttpStreamParser::DoSendHeaders() {
427 int bytes_remaining = request_headers_->BytesRemaining();
428 DCHECK_GT(bytes_remaining, 0);
430 // Record our best estimate of the 'request time' as the time when we send
431 // out the first bytes of the request headers.
432 if (bytes_remaining == request_headers_->size())
433 response_->request_time = base::Time::Now();
435 io_state_ = STATE_SEND_HEADERS_COMPLETE;
436 return connection_->socket()
437 ->Write(request_headers_.get(), bytes_remaining, io_callback_);
440 int HttpStreamParser::DoSendHeadersComplete(int result) {
442 // In the unlikely case that the headers and body were merged, all the
443 // the headers were sent, but not all of the body way, and |result| is
444 // an error that this should try reading after, stash the error for now and
445 // act like the request was successfully sent.
446 if (request_headers_->BytesConsumed() >= request_headers_length_ &&
447 ShouldTryReadingOnUploadError(result)) {
448 upload_error_ = result;
454 request_headers_->DidConsume(result);
455 if (request_headers_->BytesRemaining() > 0) {
456 io_state_ = STATE_SEND_HEADERS;
460 if (request_->upload_data_stream != NULL &&
461 (request_->upload_data_stream->is_chunked() ||
462 // !IsEOF() indicates that the body wasn't merged.
463 (request_->upload_data_stream->size() > 0 &&
464 !request_->upload_data_stream->IsEOF()))) {
466 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY,
467 base::Bind(&NetLogSendRequestBodyCallback,
468 request_->upload_data_stream->size(),
469 request_->upload_data_stream->is_chunked(),
470 false /* not merged */));
471 io_state_ = STATE_SEND_BODY;
475 // Finished sending the request.
479 int HttpStreamParser::DoSendBody() {
480 if (request_body_send_buf_->BytesRemaining() > 0) {
481 io_state_ = STATE_SEND_BODY_COMPLETE;
482 return connection_->socket()
483 ->Write(request_body_send_buf_.get(),
484 request_body_send_buf_->BytesRemaining(),
488 if (request_->upload_data_stream->is_chunked() && sent_last_chunk_) {
489 // Finished sending the request.
493 request_body_read_buf_->Clear();
494 io_state_ = STATE_SEND_REQUEST_READ_BODY_COMPLETE;
495 return request_->upload_data_stream->Read(request_body_read_buf_.get(),
496 request_body_read_buf_->capacity(),
500 int HttpStreamParser::DoSendBodyComplete(int result) {
502 // If |result| is an error that this should try reading after, stash the
503 // error for now and act like the request was successfully sent.
504 if (ShouldTryReadingOnUploadError(result)) {
505 upload_error_ = result;
511 request_body_send_buf_->DidConsume(result);
513 io_state_ = STATE_SEND_BODY;
517 int HttpStreamParser::DoSendRequestReadBodyComplete(int result) {
518 // |result| is the result of read from the request body from the last call to
520 DCHECK_GE(result, 0); // There won't be errors.
522 // Chunked data needs to be encoded.
523 if (request_->upload_data_stream->is_chunked()) {
524 if (result == 0) { // Reached the end.
525 DCHECK(request_->upload_data_stream->IsEOF());
526 sent_last_chunk_ = true;
528 // Encode the buffer as 1 chunk.
529 const base::StringPiece payload(request_body_read_buf_->data(), result);
530 request_body_send_buf_->Clear();
531 result = EncodeChunk(payload,
532 request_body_send_buf_->data(),
533 request_body_send_buf_->capacity());
536 if (result == 0) { // Reached the end.
537 // Reaching EOF means we can finish sending request body unless the data is
538 // chunked. (i.e. No need to send the terminal chunk.)
539 DCHECK(request_->upload_data_stream->IsEOF());
540 DCHECK(!request_->upload_data_stream->is_chunked());
541 // Finished sending the request.
542 } else if (result > 0) {
543 request_body_send_buf_->DidAppend(result);
545 io_state_ = STATE_SEND_BODY;
550 int HttpStreamParser::DoReadHeaders() {
551 io_state_ = STATE_READ_HEADERS_COMPLETE;
553 // Grow the read buffer if necessary.
554 if (read_buf_->RemainingCapacity() == 0)
555 read_buf_->SetCapacity(read_buf_->capacity() + kHeaderBufInitialSize);
557 // http://crbug.com/16371: We're seeing |user_buf_->data()| return NULL.
558 // See if the user is passing in an IOBuffer with a NULL |data_|.
559 CHECK(read_buf_->data());
561 return connection_->socket()
562 ->Read(read_buf_.get(), read_buf_->RemainingCapacity(), io_callback_);
565 int HttpStreamParser::DoReadHeadersComplete(int result) {
566 result = HandleReadHeaderResult(result);
568 // TODO(mmenke): The code below is ugly and hacky. A much better and more
569 // flexible long term solution would be to separate out the read and write
570 // loops, though this would involve significant changes, both here and
571 // elsewhere (WebSockets, for instance).
573 // If still reading the headers, or there was no error uploading the request
574 // body, just return the result.
575 if (io_state_ == STATE_READ_HEADERS || upload_error_ == OK)
578 // If the result is ERR_IO_PENDING, |io_state_| should be STATE_READ_HEADERS.
579 DCHECK_NE(ERR_IO_PENDING, result);
581 // On errors, use the original error received when sending the request.
582 // The main cases where these are different is when there's a header-related
583 // error code, or when there's an ERR_CONNECTION_CLOSED, which can result in
584 // special handling of partial responses and HTTP/0.9 responses.
586 // Nothing else to do. In the HTTP/0.9 or only partial headers received
587 // cases, can normally go to other states after an error reading headers.
588 io_state_ = STATE_DONE;
589 // Don't let caller see the headers.
590 response_->headers = NULL;
591 return upload_error_;
594 // Skip over 1xx responses as usual, and allow 4xx/5xx error responses to
595 // override the error received while uploading the body.
596 int response_code_class = response_->headers->response_code() / 100;
597 if (response_code_class == 1 || response_code_class == 4 ||
598 response_code_class == 5) {
602 // All other status codes are not allowed after an error during upload, to
603 // make sure the consumer has some indication there was an error.
605 // Nothing else to do.
606 io_state_ = STATE_DONE;
607 // Don't let caller see the headers.
608 response_->headers = NULL;
609 return upload_error_;
612 int HttpStreamParser::DoReadBody() {
613 io_state_ = STATE_READ_BODY_COMPLETE;
615 // There may be some data left over from reading the response headers.
616 if (read_buf_->offset()) {
617 int available = read_buf_->offset() - read_buf_unused_offset_;
619 CHECK_GT(available, 0);
620 int bytes_from_buffer = std::min(available, user_read_buf_len_);
621 memcpy(user_read_buf_->data(),
622 read_buf_->StartOfBuffer() + read_buf_unused_offset_,
624 read_buf_unused_offset_ += bytes_from_buffer;
625 if (bytes_from_buffer == available) {
626 read_buf_->SetCapacity(0);
627 read_buf_unused_offset_ = 0;
629 return bytes_from_buffer;
631 read_buf_->SetCapacity(0);
632 read_buf_unused_offset_ = 0;
636 // Check to see if we're done reading.
637 if (IsResponseBodyComplete())
640 DCHECK_EQ(0, read_buf_->offset());
641 return connection_->socket()
642 ->Read(user_read_buf_.get(), user_read_buf_len_, io_callback_);
645 int HttpStreamParser::DoReadBodyComplete(int result) {
646 // When the connection is closed, there are numerous ways to interpret it.
648 // - If a Content-Length header is present and the body contains exactly that
649 // number of bytes at connection close, the response is successful.
651 // - If a Content-Length header is present and the body contains fewer bytes
652 // than promised by the header at connection close, it may indicate that
653 // the connection was closed prematurely, or it may indicate that the
654 // server sent an invalid Content-Length header. Unfortunately, the invalid
655 // Content-Length header case does occur in practice and other browsers are
656 // tolerant of it. We choose to treat it as an error for now, but the
657 // download system treats it as a non-error, and URLRequestHttpJob also
658 // treats it as OK if the Content-Length is the post-decoded body content
661 // - If chunked encoding is used and the terminating chunk has been processed
662 // when the connection is closed, the response is successful.
664 // - If chunked encoding is used and the terminating chunk has not been
665 // processed when the connection is closed, it may indicate that the
666 // connection was closed prematurely or it may indicate that the server
667 // sent an invalid chunked encoding. We choose to treat it as
668 // an invalid chunked encoding.
670 // - If a Content-Length is not present and chunked encoding is not used,
671 // connection close is the only way to signal that the response is
672 // complete. Unfortunately, this also means that there is no way to detect
673 // early close of a connection. No error is returned.
674 if (result == 0 && !IsResponseBodyComplete() && CanFindEndOfResponse()) {
675 if (chunked_decoder_.get())
676 result = ERR_INCOMPLETE_CHUNKED_ENCODING;
678 result = ERR_CONTENT_LENGTH_MISMATCH;
682 received_bytes_ += result;
684 // Filter incoming data if appropriate. FilterBuf may return an error.
685 if (result > 0 && chunked_decoder_.get()) {
686 result = chunked_decoder_->FilterBuf(user_read_buf_->data(), result);
687 if (result == 0 && !chunked_decoder_->reached_eof()) {
688 // Don't signal completion of the Read call yet or else it'll look like
689 // we received end-of-file. Wait for more data.
690 io_state_ = STATE_READ_BODY;
696 response_body_read_ += result;
698 if (result <= 0 || IsResponseBodyComplete()) {
699 io_state_ = STATE_DONE;
701 // Save the overflow data, which can be in two places. There may be
702 // some left over in |user_read_buf_|, plus there may be more
703 // in |read_buf_|. But the part left over in |user_read_buf_| must have
704 // come from the |read_buf_|, so there's room to put it back at the
706 int additional_save_amount = read_buf_->offset() - read_buf_unused_offset_;
708 if (chunked_decoder_.get()) {
709 save_amount = chunked_decoder_->bytes_after_eof();
710 } else if (response_body_length_ >= 0) {
711 int64 extra_data_read = response_body_read_ - response_body_length_;
712 if (extra_data_read > 0) {
713 save_amount = static_cast<int>(extra_data_read);
715 result -= save_amount;
719 CHECK_LE(save_amount + additional_save_amount, kMaxBufSize);
720 if (read_buf_->capacity() < save_amount + additional_save_amount) {
721 read_buf_->SetCapacity(save_amount + additional_save_amount);
725 received_bytes_ -= save_amount;
726 memcpy(read_buf_->StartOfBuffer(), user_read_buf_->data() + result,
729 read_buf_->set_offset(save_amount);
730 if (additional_save_amount) {
731 memmove(read_buf_->data(),
732 read_buf_->StartOfBuffer() + read_buf_unused_offset_,
733 additional_save_amount);
734 read_buf_->set_offset(save_amount + additional_save_amount);
736 read_buf_unused_offset_ = 0;
738 // Now waiting for more of the body to be read.
739 user_read_buf_ = NULL;
740 user_read_buf_len_ = 0;
746 int HttpStreamParser::HandleReadHeaderResult(int result) {
747 DCHECK_EQ(0, read_buf_unused_offset_);
750 result = ERR_CONNECTION_CLOSED;
752 if (result < 0 && result != ERR_CONNECTION_CLOSED) {
753 io_state_ = STATE_DONE;
756 // If we've used the connection before, then we know it is not a HTTP/0.9
757 // response and return ERR_CONNECTION_CLOSED.
758 if (result == ERR_CONNECTION_CLOSED && read_buf_->offset() == 0 &&
759 connection_->is_reused()) {
760 io_state_ = STATE_DONE;
764 // Record our best estimate of the 'response time' as the time when we read
765 // the first bytes of the response headers.
766 if (read_buf_->offset() == 0 && result != ERR_CONNECTION_CLOSED)
767 response_->response_time = base::Time::Now();
769 if (result == ERR_CONNECTION_CLOSED) {
770 // The connection closed before we detected the end of the headers.
771 if (read_buf_->offset() == 0) {
772 // The connection was closed before any data was sent. Likely an error
773 // rather than empty HTTP/0.9 response.
774 io_state_ = STATE_DONE;
775 return ERR_EMPTY_RESPONSE;
776 } else if (request_->url.SchemeIsSecure()) {
777 // The connection was closed in the middle of the headers. For HTTPS we
778 // don't parse partial headers. Return a different error code so that we
779 // know that we shouldn't attempt to retry the request.
780 io_state_ = STATE_DONE;
781 return ERR_RESPONSE_HEADERS_TRUNCATED;
783 // Parse things as well as we can and let the caller decide what to do.
785 if (response_header_start_offset_ >= 0) {
786 io_state_ = STATE_READ_BODY_COMPLETE;
787 end_offset = read_buf_->offset();
789 // Now waiting for the body to be read.
792 int rv = DoParseResponseHeaders(end_offset);
798 read_buf_->set_offset(read_buf_->offset() + result);
799 DCHECK_LE(read_buf_->offset(), read_buf_->capacity());
800 DCHECK_GE(result, 0);
802 int end_of_header_offset = ParseResponseHeaders();
804 // Note: -1 is special, it indicates we haven't found the end of headers.
805 // Anything less than -1 is a net::Error, so we bail out.
806 if (end_of_header_offset < -1)
807 return end_of_header_offset;
809 if (end_of_header_offset == -1) {
810 io_state_ = STATE_READ_HEADERS;
811 // Prevent growing the headers buffer indefinitely.
812 if (read_buf_->offset() >= kMaxHeaderBufSize) {
813 io_state_ = STATE_DONE;
814 return ERR_RESPONSE_HEADERS_TOO_BIG;
817 CalculateResponseBodySize();
818 // If the body is zero length, the caller may not call ReadResponseBody,
819 // which is where any extra data is copied to read_buf_, so we move the
821 if (response_body_length_ == 0) {
822 int extra_bytes = read_buf_->offset() - end_of_header_offset;
824 CHECK_GT(extra_bytes, 0);
825 memmove(read_buf_->StartOfBuffer(),
826 read_buf_->StartOfBuffer() + end_of_header_offset,
829 read_buf_->SetCapacity(extra_bytes);
830 if (response_->headers->response_code() / 100 == 1) {
831 // After processing a 1xx response, the caller will ask for the next
832 // header, so reset state to support that. We don't completely ignore a
833 // 1xx response because it cannot be returned in reply to a CONNECT
834 // request so we return OK here, which lets the caller inspect the
835 // response and reject it in the event that we're setting up a CONNECT
837 response_header_start_offset_ = -1;
838 response_body_length_ = -1;
839 // Now waiting for the second set of headers to be read.
841 io_state_ = STATE_DONE;
846 // Note where the headers stop.
847 read_buf_unused_offset_ = end_of_header_offset;
848 // Now waiting for the body to be read.
853 int HttpStreamParser::ParseResponseHeaders() {
855 DCHECK_EQ(0, read_buf_unused_offset_);
857 // Look for the start of the status line, if it hasn't been found yet.
858 if (response_header_start_offset_ < 0) {
859 response_header_start_offset_ = HttpUtil::LocateStartOfStatusLine(
860 read_buf_->StartOfBuffer(), read_buf_->offset());
863 if (response_header_start_offset_ >= 0) {
864 end_offset = HttpUtil::LocateEndOfHeaders(read_buf_->StartOfBuffer(),
866 response_header_start_offset_);
867 } else if (read_buf_->offset() >= 8) {
868 // Enough data to decide that this is an HTTP/0.9 response.
869 // 8 bytes = (4 bytes of junk) + "http".length()
873 if (end_offset == -1)
876 int rv = DoParseResponseHeaders(end_offset);
882 int HttpStreamParser::DoParseResponseHeaders(int end_offset) {
883 scoped_refptr<HttpResponseHeaders> headers;
884 DCHECK_EQ(0, read_buf_unused_offset_);
886 if (response_header_start_offset_ >= 0) {
887 received_bytes_ += end_offset;
888 headers = new HttpResponseHeaders(HttpUtil::AssembleRawHeaders(
889 read_buf_->StartOfBuffer(), end_offset));
891 // Enough data was read -- there is no status line.
892 headers = new HttpResponseHeaders(std::string("HTTP/0.9 200 OK"));
895 // Check for multiple Content-Length headers with no Transfer-Encoding header.
896 // If they exist, and have distinct values, it's a potential response
898 if (!headers->HasHeader("Transfer-Encoding")) {
899 if (HeadersContainMultipleCopiesOfField(*headers.get(), "Content-Length"))
900 return ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH;
903 // Check for multiple Content-Disposition or Location headers. If they exist,
904 // it's also a potential response smuggling attack.
905 if (HeadersContainMultipleCopiesOfField(*headers.get(),
906 "Content-Disposition"))
907 return ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION;
908 if (HeadersContainMultipleCopiesOfField(*headers.get(), "Location"))
909 return ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION;
911 response_->headers = headers;
912 response_->connection_info = HttpResponseInfo::CONNECTION_INFO_HTTP1;
913 response_->vary_data.Init(*request_, *response_->headers.get());
914 DVLOG(1) << __FUNCTION__ << "()"
915 << " content_length = \"" << response_->headers->GetContentLength()
918 << GetResponseHeaderLines(*response_->headers.get()) << "\"";
922 void HttpStreamParser::CalculateResponseBodySize() {
923 // Figure how to determine EOF:
925 // For certain responses, we know the content length is always 0. From
926 // RFC 2616 Section 4.3 Message Body:
928 // For response messages, whether or not a message-body is included with
929 // a message is dependent on both the request method and the response
930 // status code (section 6.1.1). All responses to the HEAD request method
931 // MUST NOT include a message-body, even though the presence of entity-
932 // header fields might lead one to believe they do. All 1xx
933 // (informational), 204 (no content), and 304 (not modified) responses
934 // MUST NOT include a message-body. All other responses do include a
935 // message-body, although it MAY be of zero length.
936 if (response_->headers->response_code() / 100 == 1) {
937 response_body_length_ = 0;
939 switch (response_->headers->response_code()) {
940 case 204: // No Content
941 case 205: // Reset Content
942 case 304: // Not Modified
943 response_body_length_ = 0;
947 if (request_->method == "HEAD")
948 response_body_length_ = 0;
950 if (response_body_length_ == -1) {
951 // "Transfer-Encoding: chunked" trumps "Content-Length: N"
952 if (response_->headers->IsChunkEncoded()) {
953 chunked_decoder_.reset(new HttpChunkedDecoder());
955 response_body_length_ = response_->headers->GetContentLength();
956 // If response_body_length_ is still -1, then we have to wait
957 // for the server to close the connection.
962 UploadProgress HttpStreamParser::GetUploadProgress() const {
963 if (!request_->upload_data_stream)
964 return UploadProgress();
966 return UploadProgress(request_->upload_data_stream->position(),
967 request_->upload_data_stream->size());
970 HttpResponseInfo* HttpStreamParser::GetResponseInfo() {
974 bool HttpStreamParser::IsResponseBodyComplete() const {
975 if (chunked_decoder_.get())
976 return chunked_decoder_->reached_eof();
977 if (response_body_length_ != -1)
978 return response_body_read_ >= response_body_length_;
980 return false; // Must read to EOF.
983 bool HttpStreamParser::CanFindEndOfResponse() const {
984 return chunked_decoder_.get() || response_body_length_ >= 0;
987 bool HttpStreamParser::IsMoreDataBuffered() const {
988 return read_buf_->offset() > read_buf_unused_offset_;
991 bool HttpStreamParser::IsConnectionReused() const {
992 ClientSocketHandle::SocketReuseType reuse_type = connection_->reuse_type();
993 return connection_->is_reused() ||
994 reuse_type == ClientSocketHandle::UNUSED_IDLE;
997 void HttpStreamParser::SetConnectionReused() {
998 connection_->set_reuse_type(ClientSocketHandle::REUSED_IDLE);
1001 bool HttpStreamParser::IsConnectionReusable() const {
1002 return connection_->socket() && connection_->socket()->IsConnectedAndIdle();
1005 void HttpStreamParser::GetSSLInfo(SSLInfo* ssl_info) {
1006 if (request_->url.SchemeIsSecure() && connection_->socket()) {
1007 SSLClientSocket* ssl_socket =
1008 static_cast<SSLClientSocket*>(connection_->socket());
1009 ssl_socket->GetSSLInfo(ssl_info);
1013 void HttpStreamParser::GetSSLCertRequestInfo(
1014 SSLCertRequestInfo* cert_request_info) {
1015 if (request_->url.SchemeIsSecure() && connection_->socket()) {
1016 SSLClientSocket* ssl_socket =
1017 static_cast<SSLClientSocket*>(connection_->socket());
1018 ssl_socket->GetSSLCertRequestInfo(cert_request_info);
1022 int HttpStreamParser::EncodeChunk(const base::StringPiece& payload,
1024 size_t output_size) {
1025 if (output_size < payload.size() + kChunkHeaderFooterSize)
1026 return ERR_INVALID_ARGUMENT;
1028 char* cursor = output;
1030 const int num_chars = base::snprintf(output, output_size,
1032 static_cast<int>(payload.size()));
1033 cursor += num_chars;
1034 // Add the payload if any.
1035 if (payload.size() > 0) {
1036 memcpy(cursor, payload.data(), payload.size());
1037 cursor += payload.size();
1039 // Add the trailing CRLF.
1040 memcpy(cursor, "\r\n", 2);
1043 return cursor - output;
1047 bool HttpStreamParser::ShouldMergeRequestHeadersAndBody(
1048 const std::string& request_headers,
1049 const UploadDataStream* request_body) {
1050 if (request_body != NULL &&
1051 // IsInMemory() ensures that the request body is not chunked.
1052 request_body->IsInMemory() &&
1053 request_body->size() > 0) {
1054 size_t merged_size = request_headers.size() + request_body->size();
1055 if (merged_size <= kMaxMergedHeaderAndBodySize)