15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HttpStreamBase is an interface for reading and writing data to an
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HTTP-like stream that keeps the client agnostic of the actual underlying
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// transport layer.  This provides an abstraction for HttpStream and
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// WebSocketHandshakeStreamBase.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_HTTP_HTTP_STREAM_BASE_H_
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_HTTP_HTTP_STREAM_BASE_H_
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/completion_callback.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/request_priority.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/upload_progress.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BoundNetLog;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HttpNetworkSession;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HttpRequestHeaders;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct HttpRequestInfo;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HttpResponseInfo;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IOBuffer;
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct LoadTimingInfo;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SSLCertRequestInfo;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SSLInfo;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT_PRIVATE HttpStreamBase {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HttpStreamBase() {}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~HttpStreamBase() {}
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialize stream.  Must be called before calling SendRequest().
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |request_info| must outlive the HttpStreamBase.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a net error code, possibly ERR_IO_PENDING.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int InitializeStream(const HttpRequestInfo* request_info,
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               RequestPriority priority,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const BoundNetLog& net_log,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const CompletionCallback& callback) = 0;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Writes the headers and uploads body data to the underlying socket.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ERR_IO_PENDING is returned if the operation could not be completed
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // synchronously, in which case the result will be passed to the callback
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when available. Returns OK on success.
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The callback will only be invoked once the first full set of headers have
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // been received, at which point |response| will have been populated with that
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // set of headers, and is safe to read, until/unless ReadResponseHeaders is
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // called.
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |response| must remain valid until all sets of headers has been read, or
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // the HttpStreamBase is destroyed. There's typically only one set of
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // headers, except in the case of 1xx responses (See ReadResponseHeaders).
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int SendRequest(const HttpRequestHeaders& request_headers,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          HttpResponseInfo* response,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const CompletionCallback& callback) = 0;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Reads from the underlying socket until the next set of response headers
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // have been completely received.  This may only be called on 1xx responses
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // after SendRequest has completed successfully, to read the next set of
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // headers.
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // ERR_IO_PENDING is returned if the operation could not be completed
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // synchronously, in which case the result will be passed to the callback when
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // available. Returns OK on success. The response headers are available in
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // the HttpResponseInfo passed in to original call to SendRequest.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadResponseHeaders(const CompletionCallback& callback) = 0;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reads response body data, up to |buf_len| bytes. |buf_len| should be a
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // reasonable size (<2MB). The number of bytes read is returned, or an
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // error is returned upon failure.  0 indicates that the request has been
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // fully satisfied and there is no more data to read.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ERR_CONNECTION_CLOSED is returned when the connection has been closed
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // prematurely.  ERR_IO_PENDING is returned if the operation could not be
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // completed synchronously, in which case the result will be passed to the
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback when available. If the operation is not completed immediately,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the socket acquires a reference to the provided buffer until the callback
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is invoked or the socket is destroyed.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadResponseBody(IOBuffer* buf, int buf_len,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const CompletionCallback& callback) = 0;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Closes the stream.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |not_reusable| indicates if the stream can be used for further requests.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In the case of HTTP, where we re-use the byte-stream (e.g. the connection)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this means we need to close the connection; in the case of SPDY, where the
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // underlying stream is never reused, it has no effect.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(mbelshe): We should figure out how to fold the not_reusable flag
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //                into the stream implementation itself so that the caller
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //                does not need to pass it at all.  We might also be able to
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //                eliminate the SetConnectionReused() below.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Close(bool not_reusable) = 0;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Indicates if the response body has been completely read.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsResponseBodyComplete() const = 0;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Indicates that the end of the response is detectable. This means that
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the response headers indicate either chunked encoding or content length.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If neither is sent, the server must close the connection for us to detect
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the end of the response.
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(rch): Rename this method, so that it is clear why it exists
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // particularly as it applies to QUIC and SPDY for which the end of the
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // response is always findable.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanFindEndOfResponse() const = 0;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A stream exists on top of a connection.  If the connection has been used
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to successfully exchange data in the past, error handling for the
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // stream is done differently.  This method returns true if the underlying
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // connection is reused or has been connected and idle for some time.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsConnectionReused() const = 0;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetConnectionReused() = 0;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Checks whether the current state of the underlying connection
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // allows it to be reused.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsConnectionReusable() const = 0;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Get the total number of bytes received from network for this stream.
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual int64 GetTotalReceivedBytes() const = 0;
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Populates the connection establishment part of |load_timing_info|, and
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // socket ID.  |load_timing_info| must have all null times when called.
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns false and does nothing if there is no underlying connection, either
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // because one has yet to be assigned to the stream, or because the underlying
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // socket has been closed.
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In practice, this means that this function will always succeed any time
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // between when the full headers have been received and the stream has been
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // closed.
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0;
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the SSLInfo associated with this stream's connection.  This should
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only be called for streams over SSL sockets, otherwise the behavior is
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // undefined.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetSSLInfo(SSLInfo* ssl_info) = 0;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the SSLCertRequestInfo associated with this stream's connection.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should only be called for streams over SSL sockets, otherwise the
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // behavior is undefined.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) = 0;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HACK(willchan): Really, we should move the HttpResponseDrainer logic into
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the HttpStream implementation. This is just a quick hack.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsSpdyHttpStream() const = 0;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In the case of an HTTP error or redirect, flush the response body (usually
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a simple error or "this page has moved") so that we can re-use the
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // underlying connection. This stream is responsible for deleting itself when
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // draining is complete.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Drain(HttpNetworkSession* session) = 0;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the priority of the parent transaction changes.
1573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void SetPriority(RequestPriority priority) = 0;
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(HttpStreamBase);
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_HTTP_HTTP_STREAM_BASE_H_
166