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 #ifndef NET_SPDY_SPDY_HTTP_STREAM_H_
6 #define NET_SPDY_SPDY_HTTP_STREAM_H_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "net/base/completion_callback.h"
16 #include "net/http/http_stream.h"
17 #include "net/log/net_log.h"
18 #include "net/spdy/spdy_read_queue.h"
19 #include "net/spdy/spdy_session.h"
20 #include "net/spdy/spdy_stream.h"
24 class DrainableIOBuffer
;
25 struct HttpRequestInfo
;
26 class HttpResponseInfo
;
29 class UploadDataStream
;
31 // The SpdyHttpStream is a HTTP-specific type of stream known to a SpdySession.
32 class NET_EXPORT_PRIVATE SpdyHttpStream
: public SpdyStream::Delegate
,
35 // |spdy_session| must not be NULL.
36 SpdyHttpStream(const base::WeakPtr
<SpdySession
>& spdy_session
, bool direct
);
37 ~SpdyHttpStream() override
;
39 SpdyStream
* stream() { return stream_
.get(); }
41 // Cancels any callbacks from being invoked and deletes the stream.
44 // HttpStream implementation.
46 int InitializeStream(const HttpRequestInfo
* request_info
,
47 RequestPriority priority
,
48 const BoundNetLog
& net_log
,
49 const CompletionCallback
& callback
) override
;
51 int SendRequest(const HttpRequestHeaders
& headers
,
52 HttpResponseInfo
* response
,
53 const CompletionCallback
& callback
) override
;
54 UploadProgress
GetUploadProgress() const override
;
55 int ReadResponseHeaders(const CompletionCallback
& callback
) override
;
56 int ReadResponseBody(IOBuffer
* buf
,
58 const CompletionCallback
& callback
) override
;
59 void Close(bool not_reusable
) override
;
60 HttpStream
* RenewStreamForAuth() override
;
61 bool IsResponseBodyComplete() const override
;
63 // Must not be called if a NULL SpdySession was pssed into the
65 bool IsConnectionReused() const override
;
67 void SetConnectionReused() override
;
68 bool CanReuseConnection() const override
;
69 // Total number of bytes received over the network of SPDY data, headers, and
70 // push_promise frames associated with this stream, including the size of
71 // frame headers, after SSL decryption and not including proxy overhead.
72 int64
GetTotalReceivedBytes() const override
;
73 // Total number of bytes sent over the network of SPDY frames associated with
74 // this stream, including the size of frame headers, before SSL encryption and
75 // not including proxy overhead. Note that some SPDY frames such as pings are
76 // not associated with any stream, and are not included in this value.
77 int64_t GetTotalSentBytes() const override
;
78 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
79 void GetSSLInfo(SSLInfo
* ssl_info
) override
;
80 void GetSSLCertRequestInfo(SSLCertRequestInfo
* cert_request_info
) override
;
81 void Drain(HttpNetworkSession
* session
) override
;
82 void SetPriority(RequestPriority priority
) override
;
84 // SpdyStream::Delegate implementation.
85 void OnRequestHeadersSent() override
;
86 SpdyResponseHeadersStatus
OnResponseHeadersUpdated(
87 const SpdyHeaderBlock
& response_headers
) override
;
88 void OnDataReceived(scoped_ptr
<SpdyBuffer
> buffer
) override
;
89 void OnDataSent() override
;
90 void OnTrailers(const SpdyHeaderBlock
& trailers
) override
;
91 void OnClose(int status
) override
;
94 // Must be called only when |request_info_| is non-NULL.
95 bool HasUploadData() const;
97 void OnStreamCreated(const CompletionCallback
& callback
, int rv
);
99 // Reads the remaining data (whether chunked or not) from the
100 // request body stream and sends it if there's any. The read and
101 // subsequent sending may happen asynchronously. Must be called only
102 // when HasUploadData() is true.
103 void ReadAndSendRequestBodyData();
105 // Called when data has just been read from the request body stream;
106 // does the actual sending of data.
107 void OnRequestBodyReadCompleted(int status
);
109 // Call the user callback.
110 void DoCallback(int rv
);
112 void ScheduleBufferedReadCallback();
114 // Returns true if the callback is invoked.
115 bool DoBufferedReadCallback();
116 bool ShouldWaitForMoreBufferedData() const;
118 const base::WeakPtr
<SpdySession
> spdy_session_
;
120 SpdyStreamRequest stream_request_
;
121 base::WeakPtr
<SpdyStream
> stream_
;
125 // Set only when |stream_closed_| is true.
126 int closed_stream_status_
;
127 SpdyStreamId closed_stream_id_
;
128 bool closed_stream_has_load_timing_info_
;
129 LoadTimingInfo closed_stream_load_timing_info_
;
130 // After |stream_| has been closed, this keeps track of the total number of
131 // bytes received over the network for |stream_| while it was open.
132 int64 closed_stream_received_bytes_
;
133 // After |stream_| has been closed, this keeps track of the total number of
134 // bytes sent over the network for |stream_| while it was open.
135 int64_t closed_stream_sent_bytes_
;
137 // The request to send.
138 const HttpRequestInfo
* request_info_
;
140 // |response_info_| is the HTTP response data object which is filled in
141 // when a SYN_REPLY comes in for the stream.
142 // It is not owned by this stream object, or point to |push_response_info_|.
143 HttpResponseInfo
* response_info_
;
145 scoped_ptr
<HttpResponseInfo
> push_response_info_
;
147 // We don't use SpdyStream's |response_header_status_| as we
148 // sometimes call back into our delegate before it is updated.
149 SpdyResponseHeadersStatus response_headers_status_
;
151 // We buffer the response body as it arrives asynchronously from the stream.
152 SpdyReadQueue response_body_queue_
;
154 CompletionCallback callback_
;
156 // User provided buffer for the ReadResponseBody() response.
157 scoped_refptr
<IOBuffer
> user_buffer_
;
158 int user_buffer_len_
;
160 // Temporary buffer used to read the request body from UploadDataStream.
161 scoped_refptr
<IOBufferWithSize
> request_body_buf_
;
162 int request_body_buf_size_
;
164 // Is there a scheduled read callback pending.
165 bool buffered_read_callback_pending_
;
166 // Has more data been received from the network during the wait for the
167 // scheduled read callback.
168 bool more_read_data_pending_
;
170 // Is this spdy stream direct to the origin server (or to a proxy).
173 base::WeakPtrFactory
<SpdyHttpStream
> weak_factory_
;
175 DISALLOW_COPY_AND_ASSIGN(SpdyHttpStream
);
180 #endif // NET_SPDY_SPDY_HTTP_STREAM_H_