blob: 71fb5153352e0386f218068db158de2bd53e8731 [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 :
26 public QuicReliableClientStream::Delegate,
27 public HttpStream {
28 public:
[email protected]0b2294d32013-08-02 00:46:3629 explicit QuicHttpStream(const base::WeakPtr<QuicClientSession> session);
[email protected]f702d572012-12-04 15:56:2030
31 virtual ~QuicHttpStream();
32
33 // HttpStream implementation.
34 virtual int InitializeStream(const HttpRequestInfo* request_info,
[email protected]262eec82013-03-19 21:01:3635 RequestPriority priority,
[email protected]f702d572012-12-04 15:56:2036 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]f702d572012-12-04 15:56:2051 virtual bool IsConnectionReused() const OVERRIDE;
52 virtual void SetConnectionReused() OVERRIDE;
53 virtual bool IsConnectionReusable() const OVERRIDE;
[email protected]b258e0792013-01-12 07:11:5954 virtual bool GetLoadTimingInfo(
55 LoadTimingInfo* load_timing_info) const OVERRIDE;
[email protected]f702d572012-12-04 15:56:2056 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]f702d572012-12-04 15:56:2060 virtual void Drain(HttpNetworkSession* session) OVERRIDE;
[email protected]e86839fd2013-08-14 18:29:0361 virtual void SetPriority(RequestPriority priority) OVERRIDE;
[email protected]f702d572012-12-04 15:56:2062
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]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
71 private:
[email protected]24e5bc52013-09-18 15:36:5872 friend class test::QuicHttpStreamPeer;
73
[email protected]f702d572012-12-04 15:56:2074 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]0b2294d32013-08-02 00:46:3685 void OnStreamReady(int rv);
[email protected]f702d572012-12-04 15:56:2086 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]c9e49a02013-02-26 05:56:47103 State next_state_;
[email protected]f702d572012-12-04 15:56:20104
[email protected]0b2294d32013-08-02 00:46:36105 const base::WeakPtr<QuicClientSession> session_;
106 QuicClientSession::StreamRequest stream_request_;
[email protected]f702d572012-12-04 15:56:20107 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]24e5bc52013-09-18 15:36:58116 // The priority of the request.
117 RequestPriority priority_;
[email protected]f702d572012-12-04 15:56:20118 // |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]56dfb902013-01-03 23:17:55121 // 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]f702d572012-12-04 15:56:20126
127 bool response_headers_received_;
128
[email protected]f702d572012-12-04 15:56:20129 // 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]0d10b592013-02-14 16:09:26151 BoundNetLog stream_net_log_;
152
[email protected]f702d572012-12-04 15:56:20153 base::WeakPtrFactory<QuicHttpStream> weak_factory_;
154};
155
156} // namespace net
157
158#endif // NET_QUIC_QUIC_HTTP_STREAM_H_