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