[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 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 | #ifndef NET_QUIC_QUIC_HTTP_STREAM_H_ |
| 6 | #define NET_QUIC_QUIC_HTTP_STREAM_H_ |
| 7 | |
| 8 | #include <list> |
| 9 | |
| 10 | #include "base/memory/weak_ptr.h" |
| 11 | #include "net/base/io_buffer.h" |
| 12 | #include "net/http/http_stream.h" |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 13 | #include "net/quic/quic_client_session.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 14 | #include "net/quic/quic_reliable_client_stream.h" |
| 15 | |
| 16 | namespace net { |
| 17 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 18 | namespace test { |
| 19 | class QuicHttpStreamPeer; |
| 20 | } // namespace test |
| 21 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 22 | // The QuicHttpStream is a QUIC-specific HttpStream subclass. It holds a |
| 23 | // non-owning pointer to a QuicReliableClientStream which it uses to |
| 24 | // send and receive data. |
| 25 | class NET_EXPORT_PRIVATE QuicHttpStream : |
| 26 | public QuicReliableClientStream::Delegate, |
| 27 | public HttpStream { |
| 28 | public: |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 29 | explicit QuicHttpStream(const base::WeakPtr<QuicClientSession> session); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 30 | |
| 31 | virtual ~QuicHttpStream(); |
| 32 | |
| 33 | // HttpStream implementation. |
| 34 | virtual int InitializeStream(const HttpRequestInfo* request_info, |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 35 | RequestPriority priority, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 36 | const BoundNetLog& net_log, |
| 37 | const CompletionCallback& callback) OVERRIDE; |
| 38 | virtual int SendRequest(const HttpRequestHeaders& request_headers, |
| 39 | HttpResponseInfo* response, |
| 40 | const CompletionCallback& callback) OVERRIDE; |
| 41 | virtual UploadProgress GetUploadProgress() const OVERRIDE; |
| 42 | virtual int ReadResponseHeaders(const CompletionCallback& callback) OVERRIDE; |
| 43 | virtual const HttpResponseInfo* GetResponseInfo() const OVERRIDE; |
| 44 | virtual int ReadResponseBody(IOBuffer* buf, |
| 45 | int buf_len, |
| 46 | const CompletionCallback& callback) OVERRIDE; |
| 47 | virtual void Close(bool not_reusable) OVERRIDE; |
| 48 | virtual HttpStream* RenewStreamForAuth() OVERRIDE; |
| 49 | virtual bool IsResponseBodyComplete() const OVERRIDE; |
| 50 | virtual bool CanFindEndOfResponse() const OVERRIDE; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 51 | virtual bool IsConnectionReused() const OVERRIDE; |
| 52 | virtual void SetConnectionReused() OVERRIDE; |
| 53 | virtual bool IsConnectionReusable() const OVERRIDE; |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 54 | virtual bool GetLoadTimingInfo( |
| 55 | LoadTimingInfo* load_timing_info) const OVERRIDE; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 56 | virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; |
| 57 | virtual void GetSSLCertRequestInfo( |
| 58 | SSLCertRequestInfo* cert_request_info) OVERRIDE; |
| 59 | virtual bool IsSpdyHttpStream() const OVERRIDE; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 60 | virtual void Drain(HttpNetworkSession* session) OVERRIDE; |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 61 | virtual void SetPriority(RequestPriority priority) OVERRIDE; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 62 | |
| 63 | // QuicReliableClientStream::Delegate implementation |
| 64 | virtual int OnSendData() OVERRIDE; |
| 65 | virtual int OnSendDataComplete(int status, bool* eof) OVERRIDE; |
| 66 | virtual int OnDataReceived(const char* data, int length) OVERRIDE; |
| 67 | virtual void OnClose(QuicErrorCode error) OVERRIDE; |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 68 | virtual void OnError(int error) OVERRIDE; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 69 | virtual bool HasSendHeadersComplete() OVERRIDE; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 70 | |
| 71 | private: |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 72 | friend class test::QuicHttpStreamPeer; |
| 73 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 74 | enum State { |
| 75 | STATE_NONE, |
| 76 | STATE_SEND_HEADERS, |
| 77 | STATE_SEND_HEADERS_COMPLETE, |
| 78 | STATE_READ_REQUEST_BODY, |
| 79 | STATE_READ_REQUEST_BODY_COMPLETE, |
| 80 | STATE_SEND_BODY, |
| 81 | STATE_SEND_BODY_COMPLETE, |
| 82 | STATE_OPEN, |
| 83 | }; |
| 84 | |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 85 | void OnStreamReady(int rv); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 86 | void OnIOComplete(int rv); |
| 87 | void DoCallback(int rv); |
| 88 | |
| 89 | int DoLoop(int); |
| 90 | int DoSendHeaders(); |
| 91 | int DoSendHeadersComplete(int rv); |
| 92 | int DoReadRequestBody(); |
| 93 | int DoReadRequestBodyComplete(int rv); |
| 94 | int DoSendBody(); |
| 95 | int DoSendBodyComplete(int rv); |
| 96 | int DoReadResponseHeaders(); |
| 97 | int DoReadResponseHeadersComplete(int rv); |
| 98 | |
| 99 | int ParseResponseHeaders(); |
| 100 | |
| 101 | void BufferResponseBody(const char* data, int length); |
| 102 | |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 103 | State next_state_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 104 | |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 105 | const base::WeakPtr<QuicClientSession> session_; |
| 106 | QuicClientSession::StreamRequest stream_request_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 107 | QuicReliableClientStream* stream_; // Non-owning. |
| 108 | |
| 109 | // The following three fields are all owned by the caller and must |
| 110 | // outlive this object, according to the HttpStream contract. |
| 111 | |
| 112 | // The request to send. |
| 113 | const HttpRequestInfo* request_info_; |
| 114 | // The request body to send, if any, owned by the caller. |
| 115 | UploadDataStream* request_body_stream_; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 116 | // The priority of the request. |
| 117 | RequestPriority priority_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 118 | // |response_info_| is the HTTP response data object which is filled in |
| 119 | // when a the response headers are read. It is not owned by this stream. |
| 120 | HttpResponseInfo* response_info_; |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 121 | // Because response data is buffered, also buffer the response status if the |
| 122 | // stream is explicitly closed via OnError or OnClose with an error. |
| 123 | // Once all buffered data has been returned, this will be used as the final |
| 124 | // response. |
| 125 | int response_status_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 126 | |
| 127 | bool response_headers_received_; |
| 128 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 129 | // Serialized HTTP request. |
| 130 | std::string request_; |
| 131 | |
| 132 | // Buffer into which response header data is read. |
| 133 | scoped_refptr<GrowableIOBuffer> read_buf_; |
| 134 | |
| 135 | // We buffer the response body as it arrives asynchronously from the stream. |
| 136 | // TODO(rch): This is infinite buffering, which is bad. |
| 137 | std::list<scoped_refptr<IOBufferWithSize> > response_body_; |
| 138 | |
| 139 | // The caller's callback to be used for asynchronous operations. |
| 140 | CompletionCallback callback_; |
| 141 | |
| 142 | // Caller provided buffer for the ReadResponseBody() response. |
| 143 | scoped_refptr<IOBuffer> user_buffer_; |
| 144 | int user_buffer_len_; |
| 145 | |
| 146 | // Temporary buffer used to read the request body from UploadDataStream. |
| 147 | scoped_refptr<IOBufferWithSize> raw_request_body_buf_; |
| 148 | // Wraps raw_request_body_buf_ to read the remaining data progressively. |
| 149 | scoped_refptr<DrainableIOBuffer> request_body_buf_; |
| 150 | |
[email protected] | 0d10b59 | 2013-02-14 16:09:26 | [diff] [blame] | 151 | BoundNetLog stream_net_log_; |
| 152 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 153 | base::WeakPtrFactory<QuicHttpStream> weak_factory_; |
| 154 | }; |
| 155 | |
| 156 | } // namespace net |
| 157 | |
| 158 | #endif // NET_QUIC_QUIC_HTTP_STREAM_H_ |