blob: ce78bb3d894f8d9ed8717beb2526fd8b234536f1 [file] [log] [blame]
[email protected]f702d572012-12-04 15:56:201// 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]0b2294d32013-08-02 00:46:3613#include "net/quic/quic_client_session.h"
[email protected]f702d572012-12-04 15:56:2014#include "net/quic/quic_reliable_client_stream.h"
15
16namespace net {
17
[email protected]24e5bc52013-09-18 15:36:5818namespace test {
19class QuicHttpStreamPeer;
20} // namespace test
21
[email protected]f702d572012-12-04 15:56:2022// 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.
25class NET_EXPORT_PRIVATE QuicHttpStream :
[email protected]4d283b32013-10-17 12:57:2726 public QuicClientSession::Observer,
[email protected]f702d572012-12-04 15:56:2027 public QuicReliableClientStream::Delegate,
28 public HttpStream {
29 public:
[email protected]4d283b32013-10-17 12:57:2730 explicit QuicHttpStream(const base::WeakPtr<QuicClientSession>& session);
[email protected]f702d572012-12-04 15:56:2031
32 virtual ~QuicHttpStream();
33
34 // HttpStream implementation.
35 virtual int InitializeStream(const HttpRequestInfo* request_info,
[email protected]262eec82013-03-19 21:01:3636 RequestPriority priority,
[email protected]f702d572012-12-04 15:56:2037 const BoundNetLog& net_log,
38 const CompletionCallback& callback) OVERRIDE;
39 virtual int SendRequest(const HttpRequestHeaders& request_headers,
40 HttpResponseInfo* response,
41 const CompletionCallback& callback) OVERRIDE;
42 virtual UploadProgress GetUploadProgress() const OVERRIDE;
43 virtual int ReadResponseHeaders(const CompletionCallback& callback) OVERRIDE;
44 virtual const HttpResponseInfo* GetResponseInfo() const OVERRIDE;
45 virtual int ReadResponseBody(IOBuffer* buf,
46 int buf_len,
47 const CompletionCallback& callback) OVERRIDE;
48 virtual void Close(bool not_reusable) OVERRIDE;
49 virtual HttpStream* RenewStreamForAuth() OVERRIDE;
50 virtual bool IsResponseBodyComplete() const OVERRIDE;
51 virtual bool CanFindEndOfResponse() const OVERRIDE;
[email protected]f702d572012-12-04 15:56:2052 virtual bool IsConnectionReused() const OVERRIDE;
53 virtual void SetConnectionReused() OVERRIDE;
54 virtual bool IsConnectionReusable() const OVERRIDE;
[email protected]bc92bc972013-12-13 08:32:5955 virtual int64 GetTotalReceivedBytes() const OVERRIDE;
[email protected]b258e0792013-01-12 07:11:5956 virtual bool GetLoadTimingInfo(
57 LoadTimingInfo* load_timing_info) const OVERRIDE;
[email protected]f702d572012-12-04 15:56:2058 virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
59 virtual void GetSSLCertRequestInfo(
60 SSLCertRequestInfo* cert_request_info) OVERRIDE;
61 virtual bool IsSpdyHttpStream() const OVERRIDE;
[email protected]f702d572012-12-04 15:56:2062 virtual void Drain(HttpNetworkSession* session) OVERRIDE;
[email protected]e86839fd2013-08-14 18:29:0363 virtual void SetPriority(RequestPriority priority) OVERRIDE;
[email protected]f702d572012-12-04 15:56:2064
65 // QuicReliableClientStream::Delegate implementation
[email protected]f702d572012-12-04 15:56:2066 virtual int OnDataReceived(const char* data, int length) OVERRIDE;
67 virtual void OnClose(QuicErrorCode error) OVERRIDE;
[email protected]56dfb902013-01-03 23:17:5568 virtual void OnError(int error) OVERRIDE;
[email protected]24e5bc52013-09-18 15:36:5869 virtual bool HasSendHeadersComplete() OVERRIDE;
[email protected]f702d572012-12-04 15:56:2070
[email protected]4d283b32013-10-17 12:57:2771 // QuicClientSession::Observer implementation
72 virtual void OnCryptoHandshakeConfirmed() OVERRIDE;
73 virtual void OnSessionClosed(int error) OVERRIDE;
74
[email protected]f702d572012-12-04 15:56:2075 private:
[email protected]24e5bc52013-09-18 15:36:5876 friend class test::QuicHttpStreamPeer;
77
[email protected]f702d572012-12-04 15:56:2078 enum State {
79 STATE_NONE,
80 STATE_SEND_HEADERS,
81 STATE_SEND_HEADERS_COMPLETE,
82 STATE_READ_REQUEST_BODY,
83 STATE_READ_REQUEST_BODY_COMPLETE,
84 STATE_SEND_BODY,
85 STATE_SEND_BODY_COMPLETE,
86 STATE_OPEN,
87 };
88
[email protected]0b2294d32013-08-02 00:46:3689 void OnStreamReady(int rv);
[email protected]f702d572012-12-04 15:56:2090 void OnIOComplete(int rv);
91 void DoCallback(int rv);
92
93 int DoLoop(int);
94 int DoSendHeaders();
95 int DoSendHeadersComplete(int rv);
96 int DoReadRequestBody();
97 int DoReadRequestBodyComplete(int rv);
98 int DoSendBody();
99 int DoSendBodyComplete(int rv);
100 int DoReadResponseHeaders();
101 int DoReadResponseHeadersComplete(int rv);
102
103 int ParseResponseHeaders();
104
105 void BufferResponseBody(const char* data, int length);
106
[email protected]c9e49a02013-02-26 05:56:47107 State next_state_;
[email protected]f702d572012-12-04 15:56:20108
[email protected]4d283b32013-10-17 12:57:27109 base::WeakPtr<QuicClientSession> session_;
110 int session_error_; // Error code from the connection shutdown.
111 bool was_handshake_confirmed_; // True if the crypto handshake succeeded.
[email protected]0b2294d32013-08-02 00:46:36112 QuicClientSession::StreamRequest stream_request_;
[email protected]f702d572012-12-04 15:56:20113 QuicReliableClientStream* stream_; // Non-owning.
114
115 // The following three fields are all owned by the caller and must
116 // outlive this object, according to the HttpStream contract.
117
118 // The request to send.
119 const HttpRequestInfo* request_info_;
120 // The request body to send, if any, owned by the caller.
121 UploadDataStream* request_body_stream_;
[email protected]24e5bc52013-09-18 15:36:58122 // The priority of the request.
123 RequestPriority priority_;
[email protected]f702d572012-12-04 15:56:20124 // |response_info_| is the HTTP response data object which is filled in
125 // when a the response headers are read. It is not owned by this stream.
126 HttpResponseInfo* response_info_;
[email protected]56dfb902013-01-03 23:17:55127 // Because response data is buffered, also buffer the response status if the
128 // stream is explicitly closed via OnError or OnClose with an error.
129 // Once all buffered data has been returned, this will be used as the final
130 // response.
131 int response_status_;
[email protected]f702d572012-12-04 15:56:20132
[email protected]13428d42013-11-26 18:50:36133 // Serialized request headers.
134 SpdyHeaderBlock request_headers_;
135
[email protected]f702d572012-12-04 15:56:20136 bool response_headers_received_;
137
[email protected]f702d572012-12-04 15:56:20138 // Serialized HTTP request.
139 std::string request_;
140
141 // Buffer into which response header data is read.
142 scoped_refptr<GrowableIOBuffer> read_buf_;
143
144 // We buffer the response body as it arrives asynchronously from the stream.
145 // TODO(rch): This is infinite buffering, which is bad.
146 std::list<scoped_refptr<IOBufferWithSize> > response_body_;
147
[email protected]14cfbd62014-01-23 22:59:15148 // Number of bytes received when the stream was closed.
149 int64 closed_stream_received_bytes_;
150
[email protected]f702d572012-12-04 15:56:20151 // The caller's callback to be used for asynchronous operations.
152 CompletionCallback callback_;
153
154 // Caller provided buffer for the ReadResponseBody() response.
155 scoped_refptr<IOBuffer> user_buffer_;
156 int user_buffer_len_;
157
158 // Temporary buffer used to read the request body from UploadDataStream.
159 scoped_refptr<IOBufferWithSize> raw_request_body_buf_;
160 // Wraps raw_request_body_buf_ to read the remaining data progressively.
161 scoped_refptr<DrainableIOBuffer> request_body_buf_;
162
[email protected]0d10b592013-02-14 16:09:26163 BoundNetLog stream_net_log_;
164
[email protected]f702d572012-12-04 15:56:20165 base::WeakPtrFactory<QuicHttpStream> weak_factory_;
166};
167
168} // namespace net
169
170#endif // NET_QUIC_QUIC_HTTP_STREAM_H_