blob: 0ca1f81384c220d22af2273153ed99ed761a9e67 [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"
13#include "net/quic/quic_reliable_client_stream.h"
14
15namespace net {
16
17// The QuicHttpStream is a QUIC-specific HttpStream subclass. It holds a
18// non-owning pointer to a QuicReliableClientStream which it uses to
19// send and receive data.
20class NET_EXPORT_PRIVATE QuicHttpStream :
21 public QuicReliableClientStream::Delegate,
22 public HttpStream {
23 public:
24 explicit QuicHttpStream(QuicReliableClientStream* stream);
25
26 virtual ~QuicHttpStream();
27
28 // HttpStream implementation.
29 virtual int InitializeStream(const HttpRequestInfo* request_info,
30 const BoundNetLog& net_log,
31 const CompletionCallback& callback) OVERRIDE;
32 virtual int SendRequest(const HttpRequestHeaders& request_headers,
33 HttpResponseInfo* response,
34 const CompletionCallback& callback) OVERRIDE;
35 virtual UploadProgress GetUploadProgress() const OVERRIDE;
36 virtual int ReadResponseHeaders(const CompletionCallback& callback) OVERRIDE;
37 virtual const HttpResponseInfo* GetResponseInfo() const OVERRIDE;
38 virtual int ReadResponseBody(IOBuffer* buf,
39 int buf_len,
40 const CompletionCallback& callback) OVERRIDE;
41 virtual void Close(bool not_reusable) OVERRIDE;
42 virtual HttpStream* RenewStreamForAuth() OVERRIDE;
43 virtual bool IsResponseBodyComplete() const OVERRIDE;
44 virtual bool CanFindEndOfResponse() const OVERRIDE;
45 virtual bool IsMoreDataBuffered() const OVERRIDE;
46 virtual bool IsConnectionReused() const OVERRIDE;
47 virtual void SetConnectionReused() OVERRIDE;
48 virtual bool IsConnectionReusable() const OVERRIDE;
[email protected]b258e0792013-01-12 07:11:5949 virtual bool GetLoadTimingInfo(
50 LoadTimingInfo* load_timing_info) const OVERRIDE;
[email protected]f702d572012-12-04 15:56:2051 virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
52 virtual void GetSSLCertRequestInfo(
53 SSLCertRequestInfo* cert_request_info) OVERRIDE;
54 virtual bool IsSpdyHttpStream() const OVERRIDE;
55 virtual void LogNumRttVsBytesMetrics() 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;
[email protected]56dfb902013-01-03 23:17:5563 virtual void OnError(int error) OVERRIDE;
[email protected]f702d572012-12-04 15:56:2064
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 OnIOComplete(int rv);
78 void DoCallback(int rv);
79
80 int DoLoop(int);
81 int DoSendHeaders();
82 int DoSendHeadersComplete(int rv);
83 int DoReadRequestBody();
84 int DoReadRequestBodyComplete(int rv);
85 int DoSendBody();
86 int DoSendBodyComplete(int rv);
87 int DoReadResponseHeaders();
88 int DoReadResponseHeadersComplete(int rv);
89
90 int ParseResponseHeaders();
91
92 void BufferResponseBody(const char* data, int length);
93
94 State io_state_;
95
96 QuicReliableClientStream* stream_; // Non-owning.
97
98 // The following three fields are all owned by the caller and must
99 // outlive this object, according to the HttpStream contract.
100
101 // The request to send.
102 const HttpRequestInfo* request_info_;
103 // The request body to send, if any, owned by the caller.
104 UploadDataStream* request_body_stream_;
105 // |response_info_| is the HTTP response data object which is filled in
106 // when a the response headers are read. It is not owned by this stream.
107 HttpResponseInfo* response_info_;
[email protected]56dfb902013-01-03 23:17:55108 // Because response data is buffered, also buffer the response status if the
109 // stream is explicitly closed via OnError or OnClose with an error.
110 // Once all buffered data has been returned, this will be used as the final
111 // response.
112 int response_status_;
[email protected]f702d572012-12-04 15:56:20113
114 bool response_headers_received_;
115
116 // Serialized HTTP request.
117 std::string request_;
118
119 // Buffer into which response header data is read.
120 scoped_refptr<GrowableIOBuffer> read_buf_;
121
122 // We buffer the response body as it arrives asynchronously from the stream.
123 // TODO(rch): This is infinite buffering, which is bad.
124 std::list<scoped_refptr<IOBufferWithSize> > response_body_;
125
126 // The caller's callback to be used for asynchronous operations.
127 CompletionCallback callback_;
128
129 // Caller provided buffer for the ReadResponseBody() response.
130 scoped_refptr<IOBuffer> user_buffer_;
131 int user_buffer_len_;
132
133 // Temporary buffer used to read the request body from UploadDataStream.
134 scoped_refptr<IOBufferWithSize> raw_request_body_buf_;
135 // Wraps raw_request_body_buf_ to read the remaining data progressively.
136 scoped_refptr<DrainableIOBuffer> request_body_buf_;
137
138 base::WeakPtrFactory<QuicHttpStream> weak_factory_;
139};
140
141} // namespace net
142
143#endif // NET_QUIC_QUIC_HTTP_STREAM_H_