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