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 // HttpStream provides an abstraction for a basic http streams, SPDY, and QUIC.
6 // The HttpStream subtype is expected to manage the underlying transport
7 // appropriately. For example, a basic http stream will return the transport
8 // socket to the pool for reuse. SPDY streams on the other hand leave the
9 // transport socket management to the SpdySession.
11 #ifndef NET_HTTP_HTTP_STREAM_H_
12 #define NET_HTTP_HTTP_STREAM_H_
14 #include "base/basictypes.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/net_export.h"
18 #include "net/base/request_priority.h"
19 #include "net/base/upload_progress.h"
24 class HttpNetworkSession
;
25 class HttpRequestHeaders
;
26 struct HttpRequestInfo
;
27 class HttpResponseInfo
;
29 struct LoadTimingInfo
;
30 class SSLCertRequestInfo
;
33 class NET_EXPORT_PRIVATE HttpStream
{
36 virtual ~HttpStream() {}
38 // Initialize stream. Must be called before calling SendRequest().
39 // |request_info| must outlive the HttpStream.
40 // Returns a net error code, possibly ERR_IO_PENDING.
41 virtual int InitializeStream(const HttpRequestInfo
* request_info
,
42 RequestPriority priority
,
43 const BoundNetLog
& net_log
,
44 const CompletionCallback
& callback
) = 0;
46 // Writes the headers and uploads body data to the underlying socket.
47 // ERR_IO_PENDING is returned if the operation could not be completed
48 // synchronously, in which case the result will be passed to the callback
49 // when available. Returns OK on success.
51 // The callback will only be invoked once the first full set of headers have
52 // been received, at which point |response| will have been populated with that
53 // set of headers, and is safe to read, until/unless ReadResponseHeaders is
56 // |response| must remain valid until all sets of headers has been read, or
57 // the HttpStream is destroyed. There's typically only one set of
58 // headers, except in the case of 1xx responses (See ReadResponseHeaders).
59 virtual int SendRequest(const HttpRequestHeaders
& request_headers
,
60 HttpResponseInfo
* response
,
61 const CompletionCallback
& callback
) = 0;
63 // Reads from the underlying socket until the next set of response headers
64 // have been completely received. This may only be called on 1xx responses
65 // after SendRequest has completed successfully, to read the next set of
68 // ERR_IO_PENDING is returned if the operation could not be completed
69 // synchronously, in which case the result will be passed to the callback when
70 // available. Returns OK on success. The response headers are available in
71 // the HttpResponseInfo passed in to original call to SendRequest.
72 virtual int ReadResponseHeaders(const CompletionCallback
& callback
) = 0;
74 // Reads response body data, up to |buf_len| bytes. |buf_len| should be a
75 // reasonable size (<2MB). The number of bytes read is returned, or an
76 // error is returned upon failure. 0 indicates that the request has been
77 // fully satisfied and there is no more data to read.
78 // ERR_CONNECTION_CLOSED is returned when the connection has been closed
79 // prematurely. ERR_IO_PENDING is returned if the operation could not be
80 // completed synchronously, in which case the result will be passed to the
81 // callback when available. If the operation is not completed immediately,
82 // the socket acquires a reference to the provided buffer until the callback
83 // is invoked or the socket is destroyed.
84 virtual int ReadResponseBody(IOBuffer
* buf
, int buf_len
,
85 const CompletionCallback
& callback
) = 0;
88 // |not_reusable| indicates if the stream can be used for further requests.
89 // In the case of HTTP, where we re-use the byte-stream (e.g. the connection)
90 // this means we need to close the connection; in the case of SPDY, where the
91 // underlying stream is never reused, it has no effect.
92 // TODO(mbelshe): We should figure out how to fold the not_reusable flag
93 // into the stream implementation itself so that the caller
94 // does not need to pass it at all. We might also be able to
95 // eliminate the SetConnectionReused() below.
96 virtual void Close(bool not_reusable
) = 0;
98 // Indicates if the response body has been completely read.
99 virtual bool IsResponseBodyComplete() const = 0;
101 // Indicates that the end of the response is detectable. This means that
102 // the response headers indicate either chunked encoding or content length.
103 // If neither is sent, the server must close the connection for us to detect
104 // the end of the response.
105 // TODO(rch): Rename this method, so that it is clear why it exists
106 // particularly as it applies to QUIC and SPDY for which the end of the
107 // response is always findable.
108 virtual bool CanFindEndOfResponse() const = 0;
110 // A stream exists on top of a connection. If the connection has been used
111 // to successfully exchange data in the past, error handling for the
112 // stream is done differently. This method returns true if the underlying
113 // connection is reused or has been connected and idle for some time.
114 virtual bool IsConnectionReused() const = 0;
115 virtual void SetConnectionReused() = 0;
117 // Checks whether the current state of the underlying connection
118 // allows it to be reused.
119 virtual bool IsConnectionReusable() const = 0;
121 // Get the total number of bytes received from network for this stream.
122 virtual int64
GetTotalReceivedBytes() const = 0;
124 // Populates the connection establishment part of |load_timing_info|, and
125 // socket ID. |load_timing_info| must have all null times when called.
126 // Returns false and does nothing if there is no underlying connection, either
127 // because one has yet to be assigned to the stream, or because the underlying
128 // socket has been closed.
130 // In practice, this means that this function will always succeed any time
131 // between when the full headers have been received and the stream has been
133 virtual bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const = 0;
135 // Get the SSLInfo associated with this stream's connection. This should
136 // only be called for streams over SSL sockets, otherwise the behavior is
138 virtual void GetSSLInfo(SSLInfo
* ssl_info
) = 0;
140 // Get the SSLCertRequestInfo associated with this stream's connection.
141 // This should only be called for streams over SSL sockets, otherwise the
142 // behavior is undefined.
143 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo
* cert_request_info
) = 0;
145 // HACK(willchan): Really, we should move the HttpResponseDrainer logic into
146 // the HttpStream implementation. This is just a quick hack.
147 virtual bool IsSpdyHttpStream() const = 0;
149 // In the case of an HTTP error or redirect, flush the response body (usually
150 // a simple error or "this page has moved") so that we can re-use the
151 // underlying connection. This stream is responsible for deleting itself when
152 // draining is complete.
153 virtual void Drain(HttpNetworkSession
* session
) = 0;
155 // Called when the priority of the parent transaction changes.
156 virtual void SetPriority(RequestPriority priority
) = 0;
158 // Queries the UploadDataStream for its progress (bytes sent).
159 virtual UploadProgress
GetUploadProgress() const = 0;
161 // Returns a new (not initialized) stream using the same underlying
162 // connection and invalidates the old stream - no further methods should be
163 // called on the old stream. The caller should ensure that the response body
164 // from the previous request is drained before calling this method. If the
165 // subclass does not support renewing the stream, NULL is returned.
166 virtual HttpStream
* RenewStreamForAuth() = 0;
169 DISALLOW_COPY_AND_ASSIGN(HttpStream
);
174 #endif // NET_HTTP_HTTP_STREAM_H_