Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / http / http_stream_parser.cc
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.
4
5 #include "net/http/http_stream_parser.h"
6
7 #include "base/bind.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"
22
23 namespace net {
24
25 namespace {
26
27 const size_t kMaxMergedHeaderAndBodySize = 1400;
28 const size_t kRequestBodyBufferSize = 1 << 14;  // 16KB
29
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;
39   }
40   return cr_separated_headers;
41 }
42
43 // Return true if |headers| contain multiple |field_name| fields with different
44 // values.
45 bool HeadersContainMultipleCopiesOfField(const HttpResponseHeaders& headers,
46                                          const std::string& field_name) {
47   void* it = NULL;
48   std::string field_value;
49   if (!headers.EnumerateHeader(&it, field_name, &field_value))
50     return false;
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)
56       return true;
57   }
58   return false;
59 }
60
61 base::Value* NetLogSendRequestBodyCallback(int length,
62                                            bool is_chunked,
63                                            bool did_merge,
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);
69   return dict;
70 }
71
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);
77 }
78
79 }  // namespace
80
81 // Similar to DrainableIOBuffer(), but this version comes with its own
82 // storage. The motivation is to avoid repeated allocations of
83 // DrainableIOBuffer.
84 //
85 // Example:
86 //
87 // scoped_refptr<SeekableIOBuffer> buf = new SeekableIOBuffer(1024);
88 // // capacity() == 1024. size() == BytesRemaining() == BytesConsumed() == 0.
89 // // data() points to the beginning of the buffer.
90 //
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.
95 //
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);
101 // }
102 // // BytesRemaining() == 0. BytesConsumed() == size().
103 // // data() points to the end of the consumed bytes (exclusive).
104 //
105 // // If you want to reuse the buffer, be sure to clear the buffer.
106 // buf->Clear();
107 // // size() == BytesRemaining() == BytesConsumed() == 0.
108 // // data() points to the beginning of the buffer.
109 //
110 class HttpStreamParser::SeekableIOBuffer : public IOBuffer {
111  public:
112   explicit SeekableIOBuffer(int capacity)
113     : IOBuffer(capacity),
114       real_data_(data_),
115       capacity_(capacity),
116       size_(0),
117       used_(0) {
118   }
119
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);
124   }
125
126   // Returns the number of unconsumed bytes.
127   int BytesRemaining() const {
128     return size_ - used_;
129   }
130
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) {
134     DCHECK_GE(bytes, 0);
135     DCHECK_LE(bytes, size_);
136     used_ = bytes;
137     data_ = real_data_ + used_;
138   }
139
140   // Called after data is added to the buffer. Adds |bytes| added to
141   // |size_|. data() is unaffected.
142   void DidAppend(int bytes) {
143     DCHECK_GE(bytes, 0);
144     DCHECK_GE(size_ + bytes, 0);
145     DCHECK_LE(size_ + bytes, capacity_);
146     size_ += bytes;
147   }
148
149   // Changes the logical size to 0, and the offset to 0.
150   void Clear() {
151     size_ = 0;
152     SetOffset(0);
153   }
154
155   // Returns the logical size of the buffer (i.e the number of bytes of data
156   // in the buffer).
157   int size() const { return size_; }
158
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_; };
162
163  private:
164   virtual ~SeekableIOBuffer() {
165     // data_ will be deleted in IOBuffer::~IOBuffer().
166     data_ = real_data_;
167   }
168
169   char* real_data_;
170   const int capacity_;
171   int size_;
172   int used_;
173 };
174
175 // 2 CRLFs + max of 8 hex chars.
176 const size_t HttpStreamParser::kChunkHeaderFooterSize = 12;
177
178 HttpStreamParser::HttpStreamParser(ClientSocketHandle* connection,
179                                    const HttpRequestInfo* request,
180                                    GrowableIOBuffer* read_buffer,
181                                    const BoundNetLog& net_log)
182     : io_state_(STATE_NONE),
183       request_(request),
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),
189       received_bytes_(0),
190       response_body_length_(-1),
191       response_body_read_(0),
192       user_read_buf_(NULL),
193       user_read_buf_len_(0),
194       connection_(connection),
195       net_log_(net_log),
196       sent_last_chunk_(false),
197       upload_error_(OK),
198       weak_ptr_factory_(this) {
199   io_callback_ = base::Bind(&HttpStreamParser::OnIOComplete,
200                             weak_ptr_factory_.GetWeakPtr());
201 }
202
203 HttpStreamParser::~HttpStreamParser() {
204 }
205
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());
213   DCHECK(response);
214
215   net_log_.AddEvent(
216       NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS,
217       base::Bind(&HttpRequestHeaders::NetLogCallback,
218                  base::Unretained(&headers),
219                  &request_line));
220
221   DVLOG(1) << __FUNCTION__ << "()"
222            << " request_line = \"" << request_line << "\""
223            << " headers = \"" << headers.ToString() << "\"";
224   response_ = response;
225
226   // Put the peer's IP address and port into the response.
227   IPEndPoint ip_endpoint;
228   int result = connection_->socket()->GetPeerAddress(&ip_endpoint);
229   if (result != OK)
230     return result;
231   response_->socket_address = HostPortPair::FromIPEndPoint(ip_endpoint);
232
233   std::string request = request_line + headers.ToString();
234   request_headers_length_ = request.size();
235
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);
243     } else {
244       // No need to encode request body, just send the raw data.
245       request_body_read_buf_ = request_body_send_buf_;
246     }
247   }
248
249   io_state_ = STATE_SEND_HEADERS;
250
251   // If we have a small request body, then we'll merge with the headers into a
252   // single write.
253   bool did_merge = false;
254   if (ShouldMergeRequestHeadersAndBody(request, request_->upload_data_stream)) {
255     size_t merged_size =
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
260     // body.
261     request_headers_ = new DrainableIOBuffer(
262         merged_request_headers_and_body.get(), merged_size);
263
264     memcpy(request_headers_->data(), request.data(), request_headers_length_);
265     request_headers_->DidConsume(request_headers_length_);
266
267     size_t todo = request_->upload_data_stream->size();
268     while (todo) {
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);
273       todo -= consumed;
274     }
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);
278     did_merge = true;
279
280     net_log_.AddEvent(
281         NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY,
282         base::Bind(&NetLogSendRequestBodyCallback,
283                    request_->upload_data_stream->size(),
284                    false, /* not chunked */
285                    true /* merged */));
286   }
287
288   if (!did_merge) {
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));
292     request_headers_ =
293         new DrainableIOBuffer(headers_io_buf.get(), headers_io_buf->size());
294   }
295
296   result = DoLoop(OK);
297   if (result == ERR_IO_PENDING)
298     callback_ = callback;
299
300   return result > 0 ? OK : result;
301 }
302
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_);
308
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;
313
314   int result = OK;
315   io_state_ = STATE_READ_HEADERS;
316
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);
321   }
322   if (result > 0)
323     io_state_ = STATE_READ_HEADERS_COMPLETE;
324
325   result = DoLoop(result);
326   if (result == ERR_IO_PENDING)
327     callback_ = callback;
328
329   return result > 0 ? OK : result;
330 }
331
332 void HttpStreamParser::Close(bool not_reusable) {
333   if (not_reusable && connection_->socket())
334     connection_->socket()->Disconnect();
335   connection_->Reset();
336 }
337
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);
344
345   if (io_state_ == STATE_DONE)
346     return OK;
347
348   // Must have response headers with a non-1xx error code.
349   DCHECK_NE(1, response_->headers->response_code() / 100);
350
351   user_read_buf_ = buf;
352   user_read_buf_len_ = buf_len;
353   io_state_ = STATE_READ_BODY;
354
355   int result = DoLoop(OK);
356   if (result == ERR_IO_PENDING)
357     callback_ = callback;
358
359   return result;
360 }
361
362 void HttpStreamParser::OnIOComplete(int result) {
363   result = DoLoop(result);
364
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_;
369     callback_.Reset();
370     c.Run(result);
371   }
372 }
373
374 int HttpStreamParser::DoLoop(int result) {
375   do {
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;
381     switch (state) {
382       case STATE_SEND_HEADERS:
383         DCHECK_EQ(OK, result);
384         result = DoSendHeaders();
385         break;
386       case STATE_SEND_HEADERS_COMPLETE:
387         result = DoSendHeadersComplete(result);
388         break;
389       case STATE_SEND_BODY:
390         DCHECK_EQ(OK, result);
391         result = DoSendBody();
392         break;
393       case STATE_SEND_BODY_COMPLETE:
394         result = DoSendBodyComplete(result);
395         break;
396       case STATE_SEND_REQUEST_READ_BODY_COMPLETE:
397         result = DoSendRequestReadBodyComplete(result);
398         break;
399       case STATE_READ_HEADERS:
400         net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS);
401         DCHECK_GE(result, 0);
402         result = DoReadHeaders();
403         break;
404       case STATE_READ_HEADERS_COMPLETE:
405         result = DoReadHeadersComplete(result);
406         net_log_.EndEventWithNetErrorCode(
407             NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS, result);
408         break;
409       case STATE_READ_BODY:
410         DCHECK_GE(result, 0);
411         result = DoReadBody();
412         break;
413       case STATE_READ_BODY_COMPLETE:
414         result = DoReadBodyComplete(result);
415         break;
416       default:
417         NOTREACHED();
418         break;
419     }
420   } while (result != ERR_IO_PENDING &&
421            (io_state_ != STATE_DONE && io_state_ != STATE_NONE));
422
423   return result;
424 }
425
426 int HttpStreamParser::DoSendHeaders() {
427   int bytes_remaining = request_headers_->BytesRemaining();
428   DCHECK_GT(bytes_remaining, 0);
429
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();
434
435   io_state_ = STATE_SEND_HEADERS_COMPLETE;
436   return connection_->socket()
437       ->Write(request_headers_.get(), bytes_remaining, io_callback_);
438 }
439
440 int HttpStreamParser::DoSendHeadersComplete(int result) {
441   if (result < 0) {
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;
449       return OK;
450     }
451     return result;
452   }
453
454   request_headers_->DidConsume(result);
455   if (request_headers_->BytesRemaining() > 0) {
456     io_state_ = STATE_SEND_HEADERS;
457     return OK;
458   }
459
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()))) {
465     net_log_.AddEvent(
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;
472     return OK;
473   }
474
475   // Finished sending the request.
476   return OK;
477 }
478
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(),
485                 io_callback_);
486   }
487
488   if (request_->upload_data_stream->is_chunked() && sent_last_chunk_) {
489     // Finished sending the request.
490     return OK;
491   }
492
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(),
497                                             io_callback_);
498 }
499
500 int HttpStreamParser::DoSendBodyComplete(int result) {
501   if (result < 0) {
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;
506       return OK;
507     }
508     return result;
509   }
510
511   request_body_send_buf_->DidConsume(result);
512
513   io_state_ = STATE_SEND_BODY;
514   return OK;
515 }
516
517 int HttpStreamParser::DoSendRequestReadBodyComplete(int result) {
518   // |result| is the result of read from the request body from the last call to
519   // DoSendBody().
520   DCHECK_GE(result, 0);  // There won't be errors.
521
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;
527     }
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());
534   }
535
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);
544     result = 0;
545     io_state_ = STATE_SEND_BODY;
546   }
547   return result;
548 }
549
550 int HttpStreamParser::DoReadHeaders() {
551   io_state_ = STATE_READ_HEADERS_COMPLETE;
552
553   // Grow the read buffer if necessary.
554   if (read_buf_->RemainingCapacity() == 0)
555     read_buf_->SetCapacity(read_buf_->capacity() + kHeaderBufInitialSize);
556
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());
560
561   return connection_->socket()
562       ->Read(read_buf_.get(), read_buf_->RemainingCapacity(), io_callback_);
563 }
564
565 int HttpStreamParser::DoReadHeadersComplete(int result) {
566   result = HandleReadHeaderResult(result);
567
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).
572
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)
576     return result;
577
578   // If the result is ERR_IO_PENDING, |io_state_| should be STATE_READ_HEADERS.
579   DCHECK_NE(ERR_IO_PENDING, result);
580
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.
585   if (result < 0) {
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_;
592   }
593
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) {
599     return result;
600   }
601
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.
604
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_;
610 }
611
612 int HttpStreamParser::DoReadBody() {
613   io_state_ = STATE_READ_BODY_COMPLETE;
614
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_;
618     if (available) {
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_,
623              bytes_from_buffer);
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;
628       }
629       return bytes_from_buffer;
630     } else {
631       read_buf_->SetCapacity(0);
632       read_buf_unused_offset_ = 0;
633     }
634   }
635
636   // Check to see if we're done reading.
637   if (IsResponseBodyComplete())
638     return 0;
639
640   DCHECK_EQ(0, read_buf_->offset());
641   return connection_->socket()
642       ->Read(user_read_buf_.get(), user_read_buf_len_, io_callback_);
643 }
644
645 int HttpStreamParser::DoReadBodyComplete(int result) {
646   // When the connection is closed, there are numerous ways to interpret it.
647   //
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.
650   //
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
659   //    length.
660   //
661   //  - If chunked encoding is used and the terminating chunk has been processed
662   //    when the connection is closed, the response is successful.
663   //
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.
669   //
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;
677     else
678       result = ERR_CONTENT_LENGTH_MISMATCH;
679   }
680
681   if (result > 0)
682     received_bytes_ += result;
683
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;
691       return OK;
692     }
693   }
694
695   if (result > 0)
696     response_body_read_ += result;
697
698   if (result <= 0 || IsResponseBodyComplete()) {
699     io_state_ = STATE_DONE;
700
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
705     // start first.
706     int additional_save_amount = read_buf_->offset() - read_buf_unused_offset_;
707     int save_amount = 0;
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);
714         if (result > 0)
715           result -= save_amount;
716       }
717     }
718
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);
722     }
723
724     if (save_amount) {
725       received_bytes_ -= save_amount;
726       memcpy(read_buf_->StartOfBuffer(), user_read_buf_->data() + result,
727              save_amount);
728     }
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);
735     }
736     read_buf_unused_offset_ = 0;
737   } else {
738     // Now waiting for more of the body to be read.
739     user_read_buf_ = NULL;
740     user_read_buf_len_ = 0;
741   }
742
743   return result;
744 }
745
746 int HttpStreamParser::HandleReadHeaderResult(int result) {
747   DCHECK_EQ(0, read_buf_unused_offset_);
748
749   if (result == 0)
750     result = ERR_CONNECTION_CLOSED;
751
752   if (result < 0 && result != ERR_CONNECTION_CLOSED) {
753     io_state_ = STATE_DONE;
754     return result;
755   }
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;
761     return result;
762   }
763
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();
768
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;
782     }
783     // Parse things as well as we can and let the caller decide what to do.
784     int end_offset;
785     if (response_header_start_offset_ >= 0) {
786       io_state_ = STATE_READ_BODY_COMPLETE;
787       end_offset = read_buf_->offset();
788     } else {
789       // Now waiting for the body to be read.
790       end_offset = 0;
791     }
792     int rv = DoParseResponseHeaders(end_offset);
793     if (rv < 0)
794       return rv;
795     return result;
796   }
797
798   read_buf_->set_offset(read_buf_->offset() + result);
799   DCHECK_LE(read_buf_->offset(), read_buf_->capacity());
800   DCHECK_GE(result,  0);
801
802   int end_of_header_offset = ParseResponseHeaders();
803
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;
808
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;
815     }
816   } else {
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
820     // data here.
821     if (response_body_length_ == 0) {
822       int extra_bytes = read_buf_->offset() - end_of_header_offset;
823       if (extra_bytes) {
824         CHECK_GT(extra_bytes, 0);
825         memmove(read_buf_->StartOfBuffer(),
826                 read_buf_->StartOfBuffer() + end_of_header_offset,
827                 extra_bytes);
828       }
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
836         // tunnel.
837         response_header_start_offset_ = -1;
838         response_body_length_ = -1;
839         // Now waiting for the second set of headers to be read.
840       } else {
841         io_state_ = STATE_DONE;
842       }
843       return OK;
844     }
845
846     // Note where the headers stop.
847     read_buf_unused_offset_ = end_of_header_offset;
848     // Now waiting for the body to be read.
849   }
850   return result;
851 }
852
853 int HttpStreamParser::ParseResponseHeaders() {
854   int end_offset = -1;
855   DCHECK_EQ(0, read_buf_unused_offset_);
856
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());
861   }
862
863   if (response_header_start_offset_ >= 0) {
864     end_offset = HttpUtil::LocateEndOfHeaders(read_buf_->StartOfBuffer(),
865                                               read_buf_->offset(),
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()
870     end_offset = 0;
871   }
872
873   if (end_offset == -1)
874     return -1;
875
876   int rv = DoParseResponseHeaders(end_offset);
877   if (rv < 0)
878     return rv;
879   return end_offset;
880 }
881
882 int HttpStreamParser::DoParseResponseHeaders(int end_offset) {
883   scoped_refptr<HttpResponseHeaders> headers;
884   DCHECK_EQ(0, read_buf_unused_offset_);
885
886   if (response_header_start_offset_ >= 0) {
887     received_bytes_ += end_offset;
888     headers = new HttpResponseHeaders(HttpUtil::AssembleRawHeaders(
889         read_buf_->StartOfBuffer(), end_offset));
890   } else {
891     // Enough data was read -- there is no status line.
892     headers = new HttpResponseHeaders(std::string("HTTP/0.9 200 OK"));
893   }
894
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
897   // smuggling attack.
898   if (!headers->HasHeader("Transfer-Encoding")) {
899     if (HeadersContainMultipleCopiesOfField(*headers.get(), "Content-Length"))
900       return ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH;
901   }
902
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;
910
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()
916            << "\n\""
917            << " headers = \""
918            << GetResponseHeaderLines(*response_->headers.get()) << "\"";
919   return OK;
920 }
921
922 void HttpStreamParser::CalculateResponseBodySize() {
923   // Figure how to determine EOF:
924
925   // For certain responses, we know the content length is always 0. From
926   // RFC 2616 Section 4.3 Message Body:
927   //
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;
938   } else {
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;
944         break;
945     }
946   }
947   if (request_->method == "HEAD")
948     response_body_length_ = 0;
949
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());
954     } else {
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.
958     }
959   }
960 }
961
962 UploadProgress HttpStreamParser::GetUploadProgress() const {
963   if (!request_->upload_data_stream)
964     return UploadProgress();
965
966   return UploadProgress(request_->upload_data_stream->position(),
967                         request_->upload_data_stream->size());
968 }
969
970 HttpResponseInfo* HttpStreamParser::GetResponseInfo() {
971   return response_;
972 }
973
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_;
979
980   return false;  // Must read to EOF.
981 }
982
983 bool HttpStreamParser::CanFindEndOfResponse() const {
984   return chunked_decoder_.get() || response_body_length_ >= 0;
985 }
986
987 bool HttpStreamParser::IsMoreDataBuffered() const {
988   return read_buf_->offset() > read_buf_unused_offset_;
989 }
990
991 bool HttpStreamParser::IsConnectionReused() const {
992   ClientSocketHandle::SocketReuseType reuse_type = connection_->reuse_type();
993   return connection_->is_reused() ||
994          reuse_type == ClientSocketHandle::UNUSED_IDLE;
995 }
996
997 void HttpStreamParser::SetConnectionReused() {
998   connection_->set_reuse_type(ClientSocketHandle::REUSED_IDLE);
999 }
1000
1001 bool HttpStreamParser::IsConnectionReusable() const {
1002   return connection_->socket() && connection_->socket()->IsConnectedAndIdle();
1003 }
1004
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);
1010   }
1011 }
1012
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);
1019   }
1020 }
1021
1022 int HttpStreamParser::EncodeChunk(const base::StringPiece& payload,
1023                                   char* output,
1024                                   size_t output_size) {
1025   if (output_size < payload.size() + kChunkHeaderFooterSize)
1026     return ERR_INVALID_ARGUMENT;
1027
1028   char* cursor = output;
1029   // Add the header.
1030   const int num_chars = base::snprintf(output, output_size,
1031                                        "%X\r\n",
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();
1038   }
1039   // Add the trailing CRLF.
1040   memcpy(cursor, "\r\n", 2);
1041   cursor += 2;
1042
1043   return cursor - output;
1044 }
1045
1046 // static
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)
1056       return true;
1057   }
1058   return false;
1059 }
1060
1061 }  // namespace net