Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / http / http_stream_parser.cc
blob58bbf9271f99af8dbfd9cb1fd2f3b7a3f336a076
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"
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "base/strings/string_util.h"
13 #include "base/values.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/ip_endpoint.h"
16 #include "net/base/upload_data_stream.h"
17 #include "net/http/http_chunked_decoder.h"
18 #include "net/http/http_request_headers.h"
19 #include "net/http/http_request_info.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/http/http_status_line_validator.h"
22 #include "net/http/http_util.h"
23 #include "net/socket/client_socket_handle.h"
24 #include "net/socket/ssl_client_socket.h"
26 namespace net {
28 namespace {
30 enum HttpHeaderParserEvent {
31 HEADER_PARSER_INVOKED = 0,
32 // Obsolete: HEADER_HTTP_09_RESPONSE = 1,
33 HEADER_ALLOWED_TRUNCATED_HEADERS = 2,
34 HEADER_SKIPPED_WS_PREFIX = 3,
35 HEADER_SKIPPED_NON_WS_PREFIX = 4,
36 HEADER_HTTP_09_RESPONSE_OVER_HTTP = 5,
37 HEADER_HTTP_09_RESPONSE_OVER_SSL = 6,
38 HEADER_HTTP_09_ON_REUSED_SOCKET = 7,
39 NUM_HEADER_EVENTS
42 void RecordHeaderParserEvent(HttpHeaderParserEvent header_event) {
43 UMA_HISTOGRAM_ENUMERATION("Net.HttpHeaderParserEvent", header_event,
44 NUM_HEADER_EVENTS);
47 const uint64 kMaxMergedHeaderAndBodySize = 1400;
48 const size_t kRequestBodyBufferSize = 1 << 14; // 16KB
50 std::string GetResponseHeaderLines(const HttpResponseHeaders& headers) {
51 std::string raw_headers = headers.raw_headers();
52 const char* null_separated_headers = raw_headers.c_str();
53 const char* header_line = null_separated_headers;
54 std::string cr_separated_headers;
55 while (header_line[0] != 0) {
56 cr_separated_headers += header_line;
57 cr_separated_headers += "\n";
58 header_line += strlen(header_line) + 1;
60 return cr_separated_headers;
63 // Return true if |headers| contain multiple |field_name| fields with different
64 // values.
65 bool HeadersContainMultipleCopiesOfField(const HttpResponseHeaders& headers,
66 const std::string& field_name) {
67 void* it = NULL;
68 std::string field_value;
69 if (!headers.EnumerateHeader(&it, field_name, &field_value))
70 return false;
71 // There's at least one |field_name| header. Check if there are any more
72 // such headers, and if so, return true if they have different values.
73 std::string field_value2;
74 while (headers.EnumerateHeader(&it, field_name, &field_value2)) {
75 if (field_value != field_value2)
76 return true;
78 return false;
81 scoped_ptr<base::Value> NetLogSendRequestBodyCallback(
82 uint64 length,
83 bool is_chunked,
84 bool did_merge,
85 NetLogCaptureMode /* capture_mode */) {
86 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
87 dict->SetInteger("length", static_cast<int>(length));
88 dict->SetBoolean("is_chunked", is_chunked);
89 dict->SetBoolean("did_merge", did_merge);
90 return dict.Pass();
93 // Returns true if |error_code| is an error for which we give the server a
94 // chance to send a body containing error information, if the error was received
95 // while trying to upload a request body.
96 bool ShouldTryReadingOnUploadError(int error_code) {
97 return (error_code == ERR_CONNECTION_RESET);
100 } // namespace
102 // Similar to DrainableIOBuffer(), but this version comes with its own
103 // storage. The motivation is to avoid repeated allocations of
104 // DrainableIOBuffer.
106 // Example:
108 // scoped_refptr<SeekableIOBuffer> buf = new SeekableIOBuffer(1024);
109 // // capacity() == 1024. size() == BytesRemaining() == BytesConsumed() == 0.
110 // // data() points to the beginning of the buffer.
112 // // Read() takes an IOBuffer.
113 // int bytes_read = some_reader->Read(buf, buf->capacity());
114 // buf->DidAppend(bytes_read);
115 // // size() == BytesRemaining() == bytes_read. data() is unaffected.
117 // while (buf->BytesRemaining() > 0) {
118 // // Write() takes an IOBuffer. If it takes const char*, we could
119 /// // simply use the regular IOBuffer like buf->data() + offset.
120 // int bytes_written = Write(buf, buf->BytesRemaining());
121 // buf->DidConsume(bytes_written);
122 // }
123 // // BytesRemaining() == 0. BytesConsumed() == size().
124 // // data() points to the end of the consumed bytes (exclusive).
126 // // If you want to reuse the buffer, be sure to clear the buffer.
127 // buf->Clear();
128 // // size() == BytesRemaining() == BytesConsumed() == 0.
129 // // data() points to the beginning of the buffer.
131 class HttpStreamParser::SeekableIOBuffer : public IOBuffer {
132 public:
133 explicit SeekableIOBuffer(int capacity)
134 : IOBuffer(capacity),
135 real_data_(data_),
136 capacity_(capacity),
137 size_(0),
138 used_(0) {
141 // DidConsume() changes the |data_| pointer so that |data_| always points
142 // to the first unconsumed byte.
143 void DidConsume(int bytes) {
144 SetOffset(used_ + bytes);
147 // Returns the number of unconsumed bytes.
148 int BytesRemaining() const {
149 return size_ - used_;
152 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed
153 // and remaining are updated appropriately.
154 void SetOffset(int bytes) {
155 DCHECK_GE(bytes, 0);
156 DCHECK_LE(bytes, size_);
157 used_ = bytes;
158 data_ = real_data_ + used_;
161 // Called after data is added to the buffer. Adds |bytes| added to
162 // |size_|. data() is unaffected.
163 void DidAppend(int bytes) {
164 DCHECK_GE(bytes, 0);
165 DCHECK_GE(size_ + bytes, 0);
166 DCHECK_LE(size_ + bytes, capacity_);
167 size_ += bytes;
170 // Changes the logical size to 0, and the offset to 0.
171 void Clear() {
172 size_ = 0;
173 SetOffset(0);
176 // Returns the logical size of the buffer (i.e the number of bytes of data
177 // in the buffer).
178 int size() const { return size_; }
180 // Returns the capacity of the buffer. The capacity is the size used when
181 // the object is created.
182 int capacity() const { return capacity_; };
184 private:
185 ~SeekableIOBuffer() override {
186 // data_ will be deleted in IOBuffer::~IOBuffer().
187 data_ = real_data_;
190 char* real_data_;
191 const int capacity_;
192 int size_;
193 int used_;
196 // 2 CRLFs + max of 8 hex chars.
197 const size_t HttpStreamParser::kChunkHeaderFooterSize = 12;
199 HttpStreamParser::HttpStreamParser(ClientSocketHandle* connection,
200 const HttpRequestInfo* request,
201 GrowableIOBuffer* read_buffer,
202 const BoundNetLog& net_log)
203 : io_state_(STATE_NONE),
204 request_(request),
205 request_headers_(NULL),
206 request_headers_length_(0),
207 read_buf_(read_buffer),
208 read_buf_unused_offset_(0),
209 response_header_start_offset_(-1),
210 received_bytes_(0),
211 response_body_length_(-1),
212 response_body_read_(0),
213 user_read_buf_(NULL),
214 user_read_buf_len_(0),
215 connection_(connection),
216 net_log_(net_log),
217 sent_last_chunk_(false),
218 upload_error_(OK),
219 weak_ptr_factory_(this) {
220 io_callback_ = base::Bind(&HttpStreamParser::OnIOComplete,
221 weak_ptr_factory_.GetWeakPtr());
224 HttpStreamParser::~HttpStreamParser() {
227 int HttpStreamParser::SendRequest(const std::string& request_line,
228 const HttpRequestHeaders& headers,
229 HttpResponseInfo* response,
230 const CompletionCallback& callback) {
231 DCHECK_EQ(STATE_NONE, io_state_);
232 DCHECK(callback_.is_null());
233 DCHECK(!callback.is_null());
234 DCHECK(response);
236 net_log_.AddEvent(
237 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS,
238 base::Bind(&HttpRequestHeaders::NetLogCallback,
239 base::Unretained(&headers),
240 &request_line));
242 DVLOG(1) << __FUNCTION__ << "()"
243 << " request_line = \"" << request_line << "\""
244 << " headers = \"" << headers.ToString() << "\"";
245 response_ = response;
247 // Put the peer's IP address and port into the response.
248 IPEndPoint ip_endpoint;
249 int result = connection_->socket()->GetPeerAddress(&ip_endpoint);
250 if (result != OK)
251 return result;
252 response_->socket_address = HostPortPair::FromIPEndPoint(ip_endpoint);
254 std::string request = request_line + headers.ToString();
255 request_headers_length_ = request.size();
257 if (request_->upload_data_stream != NULL) {
258 request_body_send_buf_ = new SeekableIOBuffer(kRequestBodyBufferSize);
259 if (request_->upload_data_stream->is_chunked()) {
260 // Read buffer is adjusted to guarantee that |request_body_send_buf_| is
261 // large enough to hold the encoded chunk.
262 request_body_read_buf_ =
263 new SeekableIOBuffer(kRequestBodyBufferSize - kChunkHeaderFooterSize);
264 } else {
265 // No need to encode request body, just send the raw data.
266 request_body_read_buf_ = request_body_send_buf_;
270 io_state_ = STATE_SEND_HEADERS;
272 // If we have a small request body, then we'll merge with the headers into a
273 // single write.
274 bool did_merge = false;
275 if (ShouldMergeRequestHeadersAndBody(request, request_->upload_data_stream)) {
276 int merged_size = static_cast<int>(
277 request_headers_length_ + request_->upload_data_stream->size());
278 scoped_refptr<IOBuffer> merged_request_headers_and_body(
279 new IOBuffer(merged_size));
280 // We'll repurpose |request_headers_| to store the merged headers and
281 // body.
282 request_headers_ = new DrainableIOBuffer(
283 merged_request_headers_and_body.get(), merged_size);
285 memcpy(request_headers_->data(), request.data(), request_headers_length_);
286 request_headers_->DidConsume(request_headers_length_);
288 uint64 todo = request_->upload_data_stream->size();
289 while (todo) {
290 int consumed = request_->upload_data_stream->Read(
291 request_headers_.get(), static_cast<int>(todo), CompletionCallback());
292 DCHECK_GT(consumed, 0); // Read() won't fail if not chunked.
293 request_headers_->DidConsume(consumed);
294 todo -= consumed;
296 DCHECK(request_->upload_data_stream->IsEOF());
297 // Reset the offset, so the buffer can be read from the beginning.
298 request_headers_->SetOffset(0);
299 did_merge = true;
301 net_log_.AddEvent(
302 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY,
303 base::Bind(&NetLogSendRequestBodyCallback,
304 request_->upload_data_stream->size(),
305 false, /* not chunked */
306 true /* merged */));
309 if (!did_merge) {
310 // If we didn't merge the body with the headers, then |request_headers_|
311 // contains just the HTTP headers.
312 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request));
313 request_headers_ =
314 new DrainableIOBuffer(headers_io_buf.get(), headers_io_buf->size());
317 result = DoLoop(OK);
318 if (result == ERR_IO_PENDING)
319 callback_ = callback;
321 return result > 0 ? OK : result;
324 int HttpStreamParser::ReadResponseHeaders(const CompletionCallback& callback) {
325 DCHECK(io_state_ == STATE_NONE || io_state_ == STATE_DONE);
326 DCHECK(callback_.is_null());
327 DCHECK(!callback.is_null());
328 DCHECK_EQ(0, read_buf_unused_offset_);
330 // This function can be called with io_state_ == STATE_DONE if the
331 // connection is closed after seeing just a 1xx response code.
332 if (io_state_ == STATE_DONE)
333 return ERR_CONNECTION_CLOSED;
335 int result = OK;
336 io_state_ = STATE_READ_HEADERS;
338 if (read_buf_->offset() > 0) {
339 // Simulate the state where the data was just read from the socket.
340 result = read_buf_->offset();
341 read_buf_->set_offset(0);
343 if (result > 0)
344 io_state_ = STATE_READ_HEADERS_COMPLETE;
346 result = DoLoop(result);
347 if (result == ERR_IO_PENDING)
348 callback_ = callback;
350 return result > 0 ? OK : result;
353 void HttpStreamParser::Close(bool not_reusable) {
354 if (not_reusable && connection_->socket())
355 connection_->socket()->Disconnect();
356 connection_->Reset();
359 int HttpStreamParser::ReadResponseBody(IOBuffer* buf, int buf_len,
360 const CompletionCallback& callback) {
361 DCHECK(io_state_ == STATE_NONE || io_state_ == STATE_DONE);
362 DCHECK(callback_.is_null());
363 DCHECK(!callback.is_null());
364 DCHECK_LE(buf_len, kMaxBufSize);
366 if (io_state_ == STATE_DONE)
367 return OK;
369 user_read_buf_ = buf;
370 user_read_buf_len_ = buf_len;
371 io_state_ = STATE_READ_BODY;
373 int result = DoLoop(OK);
374 if (result == ERR_IO_PENDING)
375 callback_ = callback;
377 return result;
380 void HttpStreamParser::OnIOComplete(int result) {
381 result = DoLoop(result);
383 // The client callback can do anything, including destroying this class,
384 // so any pending callback must be issued after everything else is done.
385 if (result != ERR_IO_PENDING && !callback_.is_null()) {
386 CompletionCallback c = callback_;
387 callback_.Reset();
388 c.Run(result);
392 int HttpStreamParser::DoLoop(int result) {
393 do {
394 DCHECK_NE(ERR_IO_PENDING, result);
395 DCHECK_NE(STATE_DONE, io_state_);
396 DCHECK_NE(STATE_NONE, io_state_);
397 State state = io_state_;
398 io_state_ = STATE_NONE;
399 switch (state) {
400 case STATE_SEND_HEADERS:
401 DCHECK_EQ(OK, result);
402 result = DoSendHeaders();
403 break;
404 case STATE_SEND_HEADERS_COMPLETE:
405 result = DoSendHeadersComplete(result);
406 break;
407 case STATE_SEND_BODY:
408 DCHECK_EQ(OK, result);
409 result = DoSendBody();
410 break;
411 case STATE_SEND_BODY_COMPLETE:
412 result = DoSendBodyComplete(result);
413 break;
414 case STATE_SEND_REQUEST_READ_BODY_COMPLETE:
415 result = DoSendRequestReadBodyComplete(result);
416 break;
417 case STATE_READ_HEADERS:
418 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS);
419 DCHECK_GE(result, 0);
420 result = DoReadHeaders();
421 break;
422 case STATE_READ_HEADERS_COMPLETE:
423 result = DoReadHeadersComplete(result);
424 net_log_.EndEventWithNetErrorCode(
425 NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS, result);
426 break;
427 case STATE_READ_BODY:
428 DCHECK_GE(result, 0);
429 result = DoReadBody();
430 break;
431 case STATE_READ_BODY_COMPLETE:
432 result = DoReadBodyComplete(result);
433 break;
434 default:
435 NOTREACHED();
436 break;
438 } while (result != ERR_IO_PENDING &&
439 (io_state_ != STATE_DONE && io_state_ != STATE_NONE));
441 return result;
444 int HttpStreamParser::DoSendHeaders() {
445 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
446 tracked_objects::ScopedTracker tracking_profile(
447 FROM_HERE_WITH_EXPLICIT_FUNCTION(
448 "424359 HttpStreamParser::DoSendHeaders"));
450 int bytes_remaining = request_headers_->BytesRemaining();
451 DCHECK_GT(bytes_remaining, 0);
453 // Record our best estimate of the 'request time' as the time when we send
454 // out the first bytes of the request headers.
455 if (bytes_remaining == request_headers_->size())
456 response_->request_time = base::Time::Now();
458 io_state_ = STATE_SEND_HEADERS_COMPLETE;
459 return connection_->socket()
460 ->Write(request_headers_.get(), bytes_remaining, io_callback_);
463 int HttpStreamParser::DoSendHeadersComplete(int result) {
464 if (result < 0) {
465 // In the unlikely case that the headers and body were merged, all the
466 // the headers were sent, but not all of the body way, and |result| is
467 // an error that this should try reading after, stash the error for now and
468 // act like the request was successfully sent.
469 if (request_headers_->BytesConsumed() >= request_headers_length_ &&
470 ShouldTryReadingOnUploadError(result)) {
471 upload_error_ = result;
472 return OK;
474 return result;
477 request_headers_->DidConsume(result);
478 if (request_headers_->BytesRemaining() > 0) {
479 io_state_ = STATE_SEND_HEADERS;
480 return OK;
483 if (request_->upload_data_stream != NULL &&
484 (request_->upload_data_stream->is_chunked() ||
485 // !IsEOF() indicates that the body wasn't merged.
486 (request_->upload_data_stream->size() > 0 &&
487 !request_->upload_data_stream->IsEOF()))) {
488 net_log_.AddEvent(
489 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY,
490 base::Bind(&NetLogSendRequestBodyCallback,
491 request_->upload_data_stream->size(),
492 request_->upload_data_stream->is_chunked(),
493 false /* not merged */));
494 io_state_ = STATE_SEND_BODY;
495 return OK;
498 // Finished sending the request.
499 return OK;
502 int HttpStreamParser::DoSendBody() {
503 if (request_body_send_buf_->BytesRemaining() > 0) {
504 io_state_ = STATE_SEND_BODY_COMPLETE;
505 return connection_->socket()
506 ->Write(request_body_send_buf_.get(),
507 request_body_send_buf_->BytesRemaining(),
508 io_callback_);
511 if (request_->upload_data_stream->is_chunked() && sent_last_chunk_) {
512 // Finished sending the request.
513 return OK;
516 request_body_read_buf_->Clear();
517 io_state_ = STATE_SEND_REQUEST_READ_BODY_COMPLETE;
518 return request_->upload_data_stream->Read(request_body_read_buf_.get(),
519 request_body_read_buf_->capacity(),
520 io_callback_);
523 int HttpStreamParser::DoSendBodyComplete(int result) {
524 if (result < 0) {
525 // If |result| is an error that this should try reading after, stash the
526 // error for now and act like the request was successfully sent.
527 if (ShouldTryReadingOnUploadError(result)) {
528 upload_error_ = result;
529 return OK;
531 return result;
534 request_body_send_buf_->DidConsume(result);
536 io_state_ = STATE_SEND_BODY;
537 return OK;
540 int HttpStreamParser::DoSendRequestReadBodyComplete(int result) {
541 // |result| is the result of read from the request body from the last call to
542 // DoSendBody().
543 DCHECK_GE(result, 0); // There won't be errors.
545 // Chunked data needs to be encoded.
546 if (request_->upload_data_stream->is_chunked()) {
547 if (result == 0) { // Reached the end.
548 DCHECK(request_->upload_data_stream->IsEOF());
549 sent_last_chunk_ = true;
551 // Encode the buffer as 1 chunk.
552 const base::StringPiece payload(request_body_read_buf_->data(), result);
553 request_body_send_buf_->Clear();
554 result = EncodeChunk(payload,
555 request_body_send_buf_->data(),
556 request_body_send_buf_->capacity());
559 if (result == 0) { // Reached the end.
560 // Reaching EOF means we can finish sending request body unless the data is
561 // chunked. (i.e. No need to send the terminal chunk.)
562 DCHECK(request_->upload_data_stream->IsEOF());
563 DCHECK(!request_->upload_data_stream->is_chunked());
564 // Finished sending the request.
565 } else if (result > 0) {
566 request_body_send_buf_->DidAppend(result);
567 result = 0;
568 io_state_ = STATE_SEND_BODY;
570 return result;
573 int HttpStreamParser::DoReadHeaders() {
574 io_state_ = STATE_READ_HEADERS_COMPLETE;
576 // Grow the read buffer if necessary.
577 if (read_buf_->RemainingCapacity() == 0)
578 read_buf_->SetCapacity(read_buf_->capacity() + kHeaderBufInitialSize);
580 // http://crbug.com/16371: We're seeing |user_buf_->data()| return NULL.
581 // See if the user is passing in an IOBuffer with a NULL |data_|.
582 CHECK(read_buf_->data());
584 return connection_->socket()
585 ->Read(read_buf_.get(), read_buf_->RemainingCapacity(), io_callback_);
588 int HttpStreamParser::DoReadHeadersComplete(int result) {
589 result = HandleReadHeaderResult(result);
591 // TODO(mmenke): The code below is ugly and hacky. A much better and more
592 // flexible long term solution would be to separate out the read and write
593 // loops, though this would involve significant changes, both here and
594 // elsewhere (WebSockets, for instance).
596 // If still reading the headers, or there was no error uploading the request
597 // body, just return the result.
598 if (io_state_ == STATE_READ_HEADERS || upload_error_ == OK)
599 return result;
601 // If the result is ERR_IO_PENDING, |io_state_| should be STATE_READ_HEADERS.
602 DCHECK_NE(ERR_IO_PENDING, result);
604 // On errors, use the original error received when sending the request.
605 // The main cases where these are different is when there's a header-related
606 // error code, or when there's an ERR_CONNECTION_CLOSED, which can result in
607 // special handling of partial responses and HTTP/0.9 responses.
608 if (result < 0) {
609 // Nothing else to do. In the HTTP/0.9 or only partial headers received
610 // cases, can normally go to other states after an error reading headers.
611 io_state_ = STATE_DONE;
612 // Don't let caller see the headers.
613 response_->headers = NULL;
614 return upload_error_;
617 // Skip over 1xx responses as usual, and allow 4xx/5xx error responses to
618 // override the error received while uploading the body.
619 int response_code_class = response_->headers->response_code() / 100;
620 if (response_code_class == 1 || response_code_class == 4 ||
621 response_code_class == 5) {
622 return result;
625 // All other status codes are not allowed after an error during upload, to
626 // make sure the consumer has some indication there was an error.
628 // Nothing else to do.
629 io_state_ = STATE_DONE;
630 // Don't let caller see the headers.
631 response_->headers = NULL;
632 return upload_error_;
635 int HttpStreamParser::DoReadBody() {
636 io_state_ = STATE_READ_BODY_COMPLETE;
638 // There may be some data left over from reading the response headers.
639 if (read_buf_->offset()) {
640 int available = read_buf_->offset() - read_buf_unused_offset_;
641 if (available) {
642 CHECK_GT(available, 0);
643 int bytes_from_buffer = std::min(available, user_read_buf_len_);
644 memcpy(user_read_buf_->data(),
645 read_buf_->StartOfBuffer() + read_buf_unused_offset_,
646 bytes_from_buffer);
647 read_buf_unused_offset_ += bytes_from_buffer;
648 if (bytes_from_buffer == available) {
649 read_buf_->SetCapacity(0);
650 read_buf_unused_offset_ = 0;
652 return bytes_from_buffer;
653 } else {
654 read_buf_->SetCapacity(0);
655 read_buf_unused_offset_ = 0;
659 // Check to see if we're done reading.
660 if (IsResponseBodyComplete())
661 return 0;
663 DCHECK_EQ(0, read_buf_->offset());
664 return connection_->socket()
665 ->Read(user_read_buf_.get(), user_read_buf_len_, io_callback_);
668 int HttpStreamParser::DoReadBodyComplete(int result) {
669 // When the connection is closed, there are numerous ways to interpret it.
671 // - If a Content-Length header is present and the body contains exactly that
672 // number of bytes at connection close, the response is successful.
674 // - If a Content-Length header is present and the body contains fewer bytes
675 // than promised by the header at connection close, it may indicate that
676 // the connection was closed prematurely, or it may indicate that the
677 // server sent an invalid Content-Length header. Unfortunately, the invalid
678 // Content-Length header case does occur in practice and other browsers are
679 // tolerant of it. We choose to treat it as an error for now, but the
680 // download system treats it as a non-error, and URLRequestHttpJob also
681 // treats it as OK if the Content-Length is the post-decoded body content
682 // length.
684 // - If chunked encoding is used and the terminating chunk has been processed
685 // when the connection is closed, the response is successful.
687 // - If chunked encoding is used and the terminating chunk has not been
688 // processed when the connection is closed, it may indicate that the
689 // connection was closed prematurely or it may indicate that the server
690 // sent an invalid chunked encoding. We choose to treat it as
691 // an invalid chunked encoding.
693 // - If a Content-Length is not present and chunked encoding is not used,
694 // connection close is the only way to signal that the response is
695 // complete. Unfortunately, this also means that there is no way to detect
696 // early close of a connection. No error is returned.
697 if (result == 0 && !IsResponseBodyComplete() && CanFindEndOfResponse()) {
698 if (chunked_decoder_.get())
699 result = ERR_INCOMPLETE_CHUNKED_ENCODING;
700 else
701 result = ERR_CONTENT_LENGTH_MISMATCH;
704 if (result > 0)
705 received_bytes_ += result;
707 // Filter incoming data if appropriate. FilterBuf may return an error.
708 if (result > 0 && chunked_decoder_.get()) {
709 result = chunked_decoder_->FilterBuf(user_read_buf_->data(), result);
710 if (result == 0 && !chunked_decoder_->reached_eof()) {
711 // Don't signal completion of the Read call yet or else it'll look like
712 // we received end-of-file. Wait for more data.
713 io_state_ = STATE_READ_BODY;
714 return OK;
718 if (result > 0)
719 response_body_read_ += result;
721 if (result <= 0 || IsResponseBodyComplete()) {
722 io_state_ = STATE_DONE;
724 // Save the overflow data, which can be in two places. There may be
725 // some left over in |user_read_buf_|, plus there may be more
726 // in |read_buf_|. But the part left over in |user_read_buf_| must have
727 // come from the |read_buf_|, so there's room to put it back at the
728 // start first.
729 int additional_save_amount = read_buf_->offset() - read_buf_unused_offset_;
730 int save_amount = 0;
731 if (chunked_decoder_.get()) {
732 save_amount = chunked_decoder_->bytes_after_eof();
733 } else if (response_body_length_ >= 0) {
734 int64 extra_data_read = response_body_read_ - response_body_length_;
735 if (extra_data_read > 0) {
736 save_amount = static_cast<int>(extra_data_read);
737 if (result > 0)
738 result -= save_amount;
742 CHECK_LE(save_amount + additional_save_amount, kMaxBufSize);
743 if (read_buf_->capacity() < save_amount + additional_save_amount) {
744 read_buf_->SetCapacity(save_amount + additional_save_amount);
747 if (save_amount) {
748 received_bytes_ -= save_amount;
749 memcpy(read_buf_->StartOfBuffer(), user_read_buf_->data() + result,
750 save_amount);
752 read_buf_->set_offset(save_amount);
753 if (additional_save_amount) {
754 memmove(read_buf_->data(),
755 read_buf_->StartOfBuffer() + read_buf_unused_offset_,
756 additional_save_amount);
757 read_buf_->set_offset(save_amount + additional_save_amount);
759 read_buf_unused_offset_ = 0;
760 } else {
761 // Now waiting for more of the body to be read.
762 user_read_buf_ = NULL;
763 user_read_buf_len_ = 0;
766 return result;
769 int HttpStreamParser::HandleReadHeaderResult(int result) {
770 DCHECK_EQ(0, read_buf_unused_offset_);
772 if (result == 0)
773 result = ERR_CONNECTION_CLOSED;
775 if (result == ERR_CONNECTION_CLOSED) {
776 // The connection closed without getting any more data.
777 if (read_buf_->offset() == 0) {
778 io_state_ = STATE_DONE;
779 // If the connection has not been reused, it may have been a 0-length
780 // HTTP/0.9 responses, but it was most likely an error, so just return
781 // ERR_EMPTY_RESPONSE instead. If the connection was reused, just pass
782 // on the original connection close error, as rather than being an
783 // empty HTTP/0.9 response it's much more likely the server closed the
784 // socket before it received the request.
785 if (!connection_->is_reused())
786 return ERR_EMPTY_RESPONSE;
787 return result;
790 // Accepting truncated headers over HTTPS is a potential security
791 // vulnerability, so just return an error in that case.
793 // If response_header_start_offset_ is -1, this may be a < 8 byte HTTP/0.9
794 // response. However, accepting such a response over HTTPS would allow a
795 // MITM to truncate an HTTP/1.x status line to look like a short HTTP/0.9
796 // response if the peer put a record boundary at the first 8 bytes. To
797 // ensure that all response headers received over HTTPS are pristine, treat
798 // such responses as errors.
800 // TODO(mmenke): Returning ERR_RESPONSE_HEADERS_TRUNCATED when a response
801 // looks like an HTTP/0.9 response is weird. Should either come up with
802 // another error code, or, better, disable HTTP/0.9 over HTTPS (and give
803 // that a new error code).
804 if (request_->url.SchemeIsCryptographic()) {
805 io_state_ = STATE_DONE;
806 return ERR_RESPONSE_HEADERS_TRUNCATED;
809 // Parse things as well as we can and let the caller decide what to do.
810 int end_offset;
811 if (response_header_start_offset_ >= 0) {
812 // The response looks to be a truncated set of HTTP headers.
813 io_state_ = STATE_READ_BODY_COMPLETE;
814 end_offset = read_buf_->offset();
815 RecordHeaderParserEvent(HEADER_ALLOWED_TRUNCATED_HEADERS);
816 } else {
817 // The response is apparently using HTTP/0.9. Treat the entire response
818 // as the body.
819 end_offset = 0;
821 int rv = ParseResponseHeaders(end_offset);
822 if (rv < 0)
823 return rv;
824 return result;
827 if (result < 0) {
828 io_state_ = STATE_DONE;
829 return result;
832 // Record our best estimate of the 'response time' as the time when we read
833 // the first bytes of the response headers.
834 if (read_buf_->offset() == 0)
835 response_->response_time = base::Time::Now();
837 read_buf_->set_offset(read_buf_->offset() + result);
838 DCHECK_LE(read_buf_->offset(), read_buf_->capacity());
839 DCHECK_GE(result, 0);
841 int end_of_header_offset = FindAndParseResponseHeaders();
843 // Note: -1 is special, it indicates we haven't found the end of headers.
844 // Anything less than -1 is a net::Error, so we bail out.
845 if (end_of_header_offset < -1)
846 return end_of_header_offset;
848 if (end_of_header_offset == -1) {
849 io_state_ = STATE_READ_HEADERS;
850 // Prevent growing the headers buffer indefinitely.
851 if (read_buf_->offset() >= kMaxHeaderBufSize) {
852 io_state_ = STATE_DONE;
853 return ERR_RESPONSE_HEADERS_TOO_BIG;
855 } else {
856 CalculateResponseBodySize();
857 // If the body is zero length, the caller may not call ReadResponseBody,
858 // which is where any extra data is copied to read_buf_, so we move the
859 // data here.
860 if (response_body_length_ == 0) {
861 int extra_bytes = read_buf_->offset() - end_of_header_offset;
862 if (extra_bytes) {
863 CHECK_GT(extra_bytes, 0);
864 memmove(read_buf_->StartOfBuffer(),
865 read_buf_->StartOfBuffer() + end_of_header_offset,
866 extra_bytes);
868 read_buf_->SetCapacity(extra_bytes);
869 if (response_->headers->response_code() / 100 == 1) {
870 // After processing a 1xx response, the caller will ask for the next
871 // header, so reset state to support that. We don't completely ignore a
872 // 1xx response because it cannot be returned in reply to a CONNECT
873 // request so we return OK here, which lets the caller inspect the
874 // response and reject it in the event that we're setting up a CONNECT
875 // tunnel.
876 response_header_start_offset_ = -1;
877 response_body_length_ = -1;
878 // Now waiting for the second set of headers to be read.
879 } else {
880 io_state_ = STATE_DONE;
882 return OK;
885 // Note where the headers stop.
886 read_buf_unused_offset_ = end_of_header_offset;
887 // Now waiting for the body to be read.
889 return result;
892 int HttpStreamParser::FindAndParseResponseHeaders() {
893 int end_offset = -1;
894 DCHECK_EQ(0, read_buf_unused_offset_);
896 // Look for the start of the status line, if it hasn't been found yet.
897 if (response_header_start_offset_ < 0) {
898 response_header_start_offset_ = HttpUtil::LocateStartOfStatusLine(
899 read_buf_->StartOfBuffer(), read_buf_->offset());
902 if (response_header_start_offset_ >= 0) {
903 end_offset = HttpUtil::LocateEndOfHeaders(read_buf_->StartOfBuffer(),
904 read_buf_->offset(),
905 response_header_start_offset_);
906 } else if (read_buf_->offset() >= 8) {
907 // Enough data to decide that this is an HTTP/0.9 response.
908 // 8 bytes = (4 bytes of junk) + "http".length()
909 end_offset = 0;
912 if (end_offset == -1)
913 return -1;
915 int rv = ParseResponseHeaders(end_offset);
916 if (rv < 0)
917 return rv;
918 return end_offset;
921 int HttpStreamParser::ParseResponseHeaders(int end_offset) {
922 scoped_refptr<HttpResponseHeaders> headers;
923 DCHECK_EQ(0, read_buf_unused_offset_);
925 RecordHeaderParserEvent(HEADER_PARSER_INVOKED);
927 if (response_header_start_offset_ > 0) {
928 bool has_non_whitespace_in_prefix = false;
929 for (int i = 0; i < response_header_start_offset_; ++i) {
930 if (!strchr(" \t\r\n", read_buf_->StartOfBuffer()[i])) {
931 has_non_whitespace_in_prefix = true;
932 break;
935 if (has_non_whitespace_in_prefix) {
936 RecordHeaderParserEvent(HEADER_SKIPPED_NON_WS_PREFIX);
937 } else {
938 RecordHeaderParserEvent(HEADER_SKIPPED_WS_PREFIX);
942 if (response_header_start_offset_ >= 0) {
943 received_bytes_ += end_offset;
944 std::string raw_headers =
945 HttpUtil::AssembleRawHeaders(read_buf_->StartOfBuffer(), end_offset);
946 ValidateStatusLine(
947 std::string(read_buf_->StartOfBuffer(), raw_headers.find('\0')));
948 headers = new HttpResponseHeaders(raw_headers);
949 } else {
950 // Enough data was read -- there is no status line.
951 headers = new HttpResponseHeaders(std::string("HTTP/0.9 200 OK"));
953 if (request_->url.SchemeIsCryptographic()) {
954 RecordHeaderParserEvent(HEADER_HTTP_09_RESPONSE_OVER_SSL);
955 } else {
956 RecordHeaderParserEvent(HEADER_HTTP_09_RESPONSE_OVER_HTTP);
958 if (connection_->is_reused())
959 RecordHeaderParserEvent(HEADER_HTTP_09_ON_REUSED_SOCKET);
962 // Check for multiple Content-Length headers when the response is not
963 // chunked-encoded. If they exist, and have distinct values, it's a potential
964 // response smuggling attack.
965 if (!headers->IsChunkEncoded()) {
966 if (HeadersContainMultipleCopiesOfField(*headers, "Content-Length"))
967 return ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH;
970 // Check for multiple Content-Disposition or Location headers. If they exist,
971 // it's also a potential response smuggling attack.
972 if (HeadersContainMultipleCopiesOfField(*headers, "Content-Disposition"))
973 return ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION;
974 if (HeadersContainMultipleCopiesOfField(*headers, "Location"))
975 return ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION;
977 response_->headers = headers;
978 response_->connection_info = HttpResponseInfo::CONNECTION_INFO_HTTP1;
979 response_->vary_data.Init(*request_, *response_->headers);
980 DVLOG(1) << __FUNCTION__ << "()"
981 << " content_length = \"" << response_->headers->GetContentLength()
982 << "\n\""
983 << " headers = \"" << GetResponseHeaderLines(*response_->headers)
984 << "\"";
985 return OK;
988 void HttpStreamParser::CalculateResponseBodySize() {
989 // Figure how to determine EOF:
991 // For certain responses, we know the content length is always 0. From
992 // RFC 7230 Section 3.3 Message Body:
994 // The presence of a message body in a response depends on both the
995 // request method to which it is responding and the response status code
996 // (Section 3.1.2). Responses to the HEAD request method (Section 4.3.2
997 // of [RFC7231]) never include a message body because the associated
998 // response header fields (e.g., Transfer-Encoding, Content-Length,
999 // etc.), if present, indicate only what their values would have been if
1000 // the request method had been GET (Section 4.3.1 of [RFC7231]). 2xx
1001 // (Successful) responses to a CONNECT request method (Section 4.3.6 of
1002 // [RFC7231]) switch to tunnel mode instead of having a message body.
1003 // All 1xx (Informational), 204 (No Content), and 304 (Not Modified)
1004 // responses do not include a message body. All other responses do
1005 // include a message body, although the body might be of zero length.
1007 // From RFC 7231 Section 6.3.6 205 Reset Content:
1009 // Since the 205 status code implies that no additional content will be
1010 // provided, a server MUST NOT generate a payload in a 205 response.
1011 if (response_->headers->response_code() / 100 == 1) {
1012 response_body_length_ = 0;
1013 } else {
1014 switch (response_->headers->response_code()) {
1015 case 204: // No Content
1016 case 205: // Reset Content
1017 case 304: // Not Modified
1018 response_body_length_ = 0;
1019 break;
1022 if (request_->method == "HEAD")
1023 response_body_length_ = 0;
1025 if (response_body_length_ == -1) {
1026 // "Transfer-Encoding: chunked" trumps "Content-Length: N"
1027 if (response_->headers->IsChunkEncoded()) {
1028 chunked_decoder_.reset(new HttpChunkedDecoder());
1029 } else {
1030 response_body_length_ = response_->headers->GetContentLength();
1031 // If response_body_length_ is still -1, then we have to wait
1032 // for the server to close the connection.
1037 UploadProgress HttpStreamParser::GetUploadProgress() const {
1038 if (!request_->upload_data_stream)
1039 return UploadProgress();
1041 return UploadProgress(request_->upload_data_stream->position(),
1042 request_->upload_data_stream->size());
1045 bool HttpStreamParser::IsResponseBodyComplete() const {
1046 if (chunked_decoder_.get())
1047 return chunked_decoder_->reached_eof();
1048 if (response_body_length_ != -1)
1049 return response_body_read_ >= response_body_length_;
1051 return false; // Must read to EOF.
1054 bool HttpStreamParser::CanFindEndOfResponse() const {
1055 return chunked_decoder_.get() || response_body_length_ >= 0;
1058 bool HttpStreamParser::IsMoreDataBuffered() const {
1059 return read_buf_->offset() > read_buf_unused_offset_;
1062 bool HttpStreamParser::IsConnectionReused() const {
1063 ClientSocketHandle::SocketReuseType reuse_type = connection_->reuse_type();
1064 return connection_->is_reused() ||
1065 reuse_type == ClientSocketHandle::UNUSED_IDLE;
1068 void HttpStreamParser::SetConnectionReused() {
1069 connection_->set_reuse_type(ClientSocketHandle::REUSED_IDLE);
1072 bool HttpStreamParser::IsConnectionReusable() const {
1073 return connection_->socket() && connection_->socket()->IsConnectedAndIdle();
1076 void HttpStreamParser::GetSSLInfo(SSLInfo* ssl_info) {
1077 if (request_->url.SchemeIsCryptographic() && connection_->socket()) {
1078 SSLClientSocket* ssl_socket =
1079 static_cast<SSLClientSocket*>(connection_->socket());
1080 ssl_socket->GetSSLInfo(ssl_info);
1084 void HttpStreamParser::GetSSLCertRequestInfo(
1085 SSLCertRequestInfo* cert_request_info) {
1086 if (request_->url.SchemeIsCryptographic() && connection_->socket()) {
1087 SSLClientSocket* ssl_socket =
1088 static_cast<SSLClientSocket*>(connection_->socket());
1089 ssl_socket->GetSSLCertRequestInfo(cert_request_info);
1093 int HttpStreamParser::EncodeChunk(const base::StringPiece& payload,
1094 char* output,
1095 size_t output_size) {
1096 if (output_size < payload.size() + kChunkHeaderFooterSize)
1097 return ERR_INVALID_ARGUMENT;
1099 char* cursor = output;
1100 // Add the header.
1101 const int num_chars = base::snprintf(output, output_size,
1102 "%X\r\n",
1103 static_cast<int>(payload.size()));
1104 cursor += num_chars;
1105 // Add the payload if any.
1106 if (payload.size() > 0) {
1107 memcpy(cursor, payload.data(), payload.size());
1108 cursor += payload.size();
1110 // Add the trailing CRLF.
1111 memcpy(cursor, "\r\n", 2);
1112 cursor += 2;
1114 return cursor - output;
1117 // static
1118 bool HttpStreamParser::ShouldMergeRequestHeadersAndBody(
1119 const std::string& request_headers,
1120 const UploadDataStream* request_body) {
1121 if (request_body != NULL &&
1122 // IsInMemory() ensures that the request body is not chunked.
1123 request_body->IsInMemory() &&
1124 request_body->size() > 0) {
1125 uint64 merged_size = request_headers.size() + request_body->size();
1126 if (merged_size <= kMaxMergedHeaderAndBodySize)
1127 return true;
1129 return false;
1132 void HttpStreamParser::ValidateStatusLine(const std::string& status_line) {
1133 HttpStatusLineValidator::StatusLineStatus status =
1134 HttpStatusLineValidator::ValidateStatusLine(status_line);
1135 UMA_HISTOGRAM_ENUMERATION("Net.HttpStatusLineStatus", status,
1136 HttpStatusLineValidator::STATUS_LINE_MAX);
1139 } // namespace net