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/base/net_log.h"
17 #include "net/http/http_request_info.h"
18 #include "net/http/http_stream.h"
19 #include "net/spdy/spdy_protocol.h"
20 #include "net/spdy/spdy_session.h"
21 #include "net/spdy/spdy_stream.h"
25 class DrainableIOBuffer
;
26 class HttpResponseInfo
;
30 class UploadDataStream
;
32 // The SpdyHttpStream is a HTTP-specific type of stream known to a SpdySession.
33 class NET_EXPORT_PRIVATE SpdyHttpStream
: public SpdyStream::Delegate
,
36 SpdyHttpStream(SpdySession
* spdy_session
, bool direct
);
37 virtual ~SpdyHttpStream();
39 // Initializes this SpdyHttpStream by wrapping an existing SpdyStream.
40 void InitializeWithExistingStream(SpdyStream
* spdy_stream
);
42 SpdyStream
* stream() { return stream_
.get(); }
44 // Cancels any callbacks from being invoked and deletes the stream.
47 // HttpStream implementation.
48 virtual int InitializeStream(const HttpRequestInfo
* request_info
,
49 const BoundNetLog
& net_log
,
50 const CompletionCallback
& callback
) OVERRIDE
;
51 virtual int SendRequest(const HttpRequestHeaders
& headers
,
52 UploadDataStream
* request_body
,
53 HttpResponseInfo
* response
,
54 const CompletionCallback
& callback
) OVERRIDE
;
55 virtual uint64
GetUploadProgress() const OVERRIDE
;
56 virtual int ReadResponseHeaders(const CompletionCallback
& callback
) OVERRIDE
;
57 virtual const HttpResponseInfo
* GetResponseInfo() const OVERRIDE
;
58 virtual int ReadResponseBody(IOBuffer
* buf
,
60 const CompletionCallback
& callback
) OVERRIDE
;
61 virtual void Close(bool not_reusable
) OVERRIDE
;
62 virtual HttpStream
* RenewStreamForAuth() OVERRIDE
;
63 virtual bool IsResponseBodyComplete() const OVERRIDE
;
64 virtual bool CanFindEndOfResponse() const OVERRIDE
;
65 virtual bool IsMoreDataBuffered() const OVERRIDE
;
66 virtual bool IsConnectionReused() const OVERRIDE
;
67 virtual void SetConnectionReused() OVERRIDE
;
68 virtual bool IsConnectionReusable() const OVERRIDE
;
69 virtual void GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
70 virtual void GetSSLCertRequestInfo(
71 SSLCertRequestInfo
* cert_request_info
) OVERRIDE
;
72 virtual bool IsSpdyHttpStream() const OVERRIDE
;
73 virtual void LogNumRttVsBytesMetrics() const OVERRIDE
{}
74 virtual void Drain(HttpNetworkSession
* session
) OVERRIDE
;
76 // SpdyStream::Delegate implementation.
77 virtual bool OnSendHeadersComplete(int status
) OVERRIDE
;
78 virtual int OnSendBody() OVERRIDE
;
79 virtual int OnSendBodyComplete(int status
, bool* eof
) OVERRIDE
;
80 virtual int OnResponseReceived(const spdy::SpdyHeaderBlock
& response
,
81 base::Time response_time
,
83 virtual void OnDataReceived(const char* buffer
, int bytes
) OVERRIDE
;
84 virtual void OnDataSent(int length
) OVERRIDE
;
85 virtual void OnClose(int status
) OVERRIDE
;
86 virtual void set_chunk_callback(ChunkCallback
* callback
) OVERRIDE
;
89 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest
, FlowControlStallResume
);
91 // Call the user callback.
92 void DoCallback(int rv
);
94 void ScheduleBufferedReadCallback();
96 // Returns true if the callback is invoked.
97 bool DoBufferedReadCallback();
98 bool ShouldWaitForMoreBufferedData() const;
100 base::WeakPtrFactory
<SpdyHttpStream
> weak_factory_
;
101 scoped_refptr
<SpdyStream
> stream_
;
102 scoped_refptr
<SpdySession
> spdy_session_
;
104 // The request to send.
105 const HttpRequestInfo
* request_info_
;
107 scoped_ptr
<UploadDataStream
> request_body_stream_
;
109 // |response_info_| is the HTTP response data object which is filled in
110 // when a SYN_REPLY comes in for the stream.
111 // It is not owned by this stream object, or point to |push_response_info_|.
112 HttpResponseInfo
* response_info_
;
114 scoped_ptr
<HttpResponseInfo
> push_response_info_
;
116 bool download_finished_
;
117 bool response_headers_received_
; // Indicates waiting for more HEADERS.
119 // We buffer the response body as it arrives asynchronously from the stream.
120 // TODO(mbelshe): is this infinite buffering?
121 std::list
<scoped_refptr
<IOBufferWithSize
> > response_body_
;
123 CompletionCallback callback_
;
125 // User provided buffer for the ReadResponseBody() response.
126 scoped_refptr
<IOBuffer
> user_buffer_
;
127 int user_buffer_len_
;
129 // Temporary buffer used to read the request body from UploadDataStream.
130 scoped_refptr
<IOBufferWithSize
> raw_request_body_buf_
;
131 // Wraps raw_request_body_buf_ to read the remaining data progressively.
132 scoped_refptr
<DrainableIOBuffer
> request_body_buf_
;
134 // Is there a scheduled read callback pending.
135 bool buffered_read_callback_pending_
;
136 // Has more data been received from the network during the wait for the
137 // scheduled read callback.
138 bool more_read_data_pending_
;
140 // Is this spdy stream direct to the origin server (or to a proxy).
143 bool send_last_chunk_
;
145 DISALLOW_COPY_AND_ASSIGN(SpdyHttpStream
);
150 #endif // NET_SPDY_SPDY_HTTP_STREAM_H_