blob: 3803a7b031adcf23f2e9410e0640fc15eec77bcd [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;
49 virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
50 virtual void GetSSLCertRequestInfo(
51 SSLCertRequestInfo* cert_request_info) OVERRIDE;
52 virtual bool IsSpdyHttpStream() const OVERRIDE;
53 virtual void LogNumRttVsBytesMetrics() const OVERRIDE {}
54 virtual void Drain(HttpNetworkSession* session) OVERRIDE;
55
56 // QuicReliableClientStream::Delegate implementation
57 virtual int OnSendData() OVERRIDE;
58 virtual int OnSendDataComplete(int status, bool* eof) OVERRIDE;
59 virtual int OnDataReceived(const char* data, int length) OVERRIDE;
60 virtual void OnClose(QuicErrorCode error) OVERRIDE;
[email protected]56dfb902013-01-03 23:17:5561 virtual void OnError(int error) OVERRIDE;
[email protected]f702d572012-12-04 15:56:2062
63 private:
64 enum State {
65 STATE_NONE,
66 STATE_SEND_HEADERS,
67 STATE_SEND_HEADERS_COMPLETE,
68 STATE_READ_REQUEST_BODY,
69 STATE_READ_REQUEST_BODY_COMPLETE,
70 STATE_SEND_BODY,
71 STATE_SEND_BODY_COMPLETE,
72 STATE_OPEN,
73 };
74
75 void OnIOComplete(int rv);
76 void DoCallback(int rv);
77
78 int DoLoop(int);
79 int DoSendHeaders();
80 int DoSendHeadersComplete(int rv);
81 int DoReadRequestBody();
82 int DoReadRequestBodyComplete(int rv);
83 int DoSendBody();
84 int DoSendBodyComplete(int rv);
85 int DoReadResponseHeaders();
86 int DoReadResponseHeadersComplete(int rv);
87
88 int ParseResponseHeaders();
89
90 void BufferResponseBody(const char* data, int length);
91
92 State io_state_;
93
94 QuicReliableClientStream* stream_; // Non-owning.
95
96 // The following three fields are all owned by the caller and must
97 // outlive this object, according to the HttpStream contract.
98
99 // The request to send.
100 const HttpRequestInfo* request_info_;
101 // The request body to send, if any, owned by the caller.
102 UploadDataStream* request_body_stream_;
103 // |response_info_| is the HTTP response data object which is filled in
104 // when a the response headers are read. It is not owned by this stream.
105 HttpResponseInfo* response_info_;
[email protected]56dfb902013-01-03 23:17:55106 // Because response data is buffered, also buffer the response status if the
107 // stream is explicitly closed via OnError or OnClose with an error.
108 // Once all buffered data has been returned, this will be used as the final
109 // response.
110 int response_status_;
[email protected]f702d572012-12-04 15:56:20111
112 bool response_headers_received_;
113
114 // Serialized HTTP request.
115 std::string request_;
116
117 // Buffer into which response header data is read.
118 scoped_refptr<GrowableIOBuffer> read_buf_;
119
120 // We buffer the response body as it arrives asynchronously from the stream.
121 // TODO(rch): This is infinite buffering, which is bad.
122 std::list<scoped_refptr<IOBufferWithSize> > response_body_;
123
124 // The caller's callback to be used for asynchronous operations.
125 CompletionCallback callback_;
126
127 // Caller provided buffer for the ReadResponseBody() response.
128 scoped_refptr<IOBuffer> user_buffer_;
129 int user_buffer_len_;
130
131 // Temporary buffer used to read the request body from UploadDataStream.
132 scoped_refptr<IOBufferWithSize> raw_request_body_buf_;
133 // Wraps raw_request_body_buf_ to read the remaining data progressively.
134 scoped_refptr<DrainableIOBuffer> request_body_buf_;
135
136 base::WeakPtrFactory<QuicHttpStream> weak_factory_;
137};
138
139} // namespace net
140
141#endif // NET_QUIC_QUIC_HTTP_STREAM_H_