- add sources.
[platform/framework/web/crosswalk.git] / src / net / quic / quic_http_stream.h
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 namespace test {
19 class QuicHttpStreamPeer;
20 }  // namespace test
21
22 // 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.
25 class NET_EXPORT_PRIVATE QuicHttpStream :
26       public QuicClientSession::Observer,
27       public QuicReliableClientStream::Delegate,
28       public HttpStream {
29  public:
30   explicit QuicHttpStream(const base::WeakPtr<QuicClientSession>& session);
31
32   virtual ~QuicHttpStream();
33
34   // HttpStream implementation.
35   virtual int InitializeStream(const HttpRequestInfo* request_info,
36                                RequestPriority priority,
37                                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;
52   virtual bool IsConnectionReused() const OVERRIDE;
53   virtual void SetConnectionReused() OVERRIDE;
54   virtual bool IsConnectionReusable() const OVERRIDE;
55   virtual bool GetLoadTimingInfo(
56       LoadTimingInfo* load_timing_info) const OVERRIDE;
57   virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
58   virtual void GetSSLCertRequestInfo(
59       SSLCertRequestInfo* cert_request_info) OVERRIDE;
60   virtual bool IsSpdyHttpStream() const OVERRIDE;
61   virtual void Drain(HttpNetworkSession* session) OVERRIDE;
62   virtual void SetPriority(RequestPriority priority) OVERRIDE;
63
64   // QuicReliableClientStream::Delegate implementation
65   virtual int OnSendData() OVERRIDE;
66   virtual int OnSendDataComplete(int status, bool* eof) OVERRIDE;
67   virtual int OnDataReceived(const char* data, int length) OVERRIDE;
68   virtual void OnClose(QuicErrorCode error) OVERRIDE;
69   virtual void OnError(int error) OVERRIDE;
70   virtual bool HasSendHeadersComplete() OVERRIDE;
71
72   // QuicClientSession::Observer implementation
73   virtual void OnCryptoHandshakeConfirmed() OVERRIDE;
74   virtual void OnSessionClosed(int error) OVERRIDE;
75
76  private:
77   friend class test::QuicHttpStreamPeer;
78
79   enum State {
80     STATE_NONE,
81     STATE_SEND_HEADERS,
82     STATE_SEND_HEADERS_COMPLETE,
83     STATE_READ_REQUEST_BODY,
84     STATE_READ_REQUEST_BODY_COMPLETE,
85     STATE_SEND_BODY,
86     STATE_SEND_BODY_COMPLETE,
87     STATE_OPEN,
88   };
89
90   void OnStreamReady(int rv);
91   void OnIOComplete(int rv);
92   void DoCallback(int rv);
93
94   int DoLoop(int);
95   int DoSendHeaders();
96   int DoSendHeadersComplete(int rv);
97   int DoReadRequestBody();
98   int DoReadRequestBodyComplete(int rv);
99   int DoSendBody();
100   int DoSendBodyComplete(int rv);
101   int DoReadResponseHeaders();
102   int DoReadResponseHeadersComplete(int rv);
103
104   int ParseResponseHeaders();
105
106   void BufferResponseBody(const char* data, int length);
107
108   State next_state_;
109
110   base::WeakPtr<QuicClientSession> session_;
111   int session_error_;  // Error code from the connection shutdown.
112   bool was_handshake_confirmed_;  // True if the crypto handshake succeeded.
113   QuicClientSession::StreamRequest stream_request_;
114   QuicReliableClientStream* stream_;  // Non-owning.
115
116   // The following three fields are all owned by the caller and must
117   // outlive this object, according to the HttpStream contract.
118
119   // The request to send.
120   const HttpRequestInfo* request_info_;
121   // The request body to send, if any, owned by the caller.
122   UploadDataStream* request_body_stream_;
123   // The priority of the request.
124   RequestPriority priority_;
125   // |response_info_| is the HTTP response data object which is filled in
126   // when a the response headers are read.  It is not owned by this stream.
127   HttpResponseInfo* response_info_;
128   // Because response data is buffered, also buffer the response status if the
129   // stream is explicitly closed via OnError or OnClose with an error.
130   // Once all buffered data has been returned, this will be used as the final
131   // response.
132   int response_status_;
133
134   bool response_headers_received_;
135
136   // Serialized HTTP request.
137   std::string request_;
138
139   // Buffer into which response header data is read.
140   scoped_refptr<GrowableIOBuffer> read_buf_;
141
142   // We buffer the response body as it arrives asynchronously from the stream.
143   // TODO(rch): This is infinite buffering, which is bad.
144   std::list<scoped_refptr<IOBufferWithSize> > response_body_;
145
146   // The caller's callback to be used for asynchronous operations.
147   CompletionCallback callback_;
148
149   // Caller provided buffer for the ReadResponseBody() response.
150   scoped_refptr<IOBuffer> user_buffer_;
151   int user_buffer_len_;
152
153   // Temporary buffer used to read the request body from UploadDataStream.
154   scoped_refptr<IOBufferWithSize> raw_request_body_buf_;
155   // Wraps raw_request_body_buf_ to read the remaining data progressively.
156   scoped_refptr<DrainableIOBuffer> request_body_buf_;
157
158   BoundNetLog stream_net_log_;
159
160   base::WeakPtrFactory<QuicHttpStream> weak_factory_;
161 };
162
163 }  // namespace net
164
165 #endif  // NET_QUIC_QUIC_HTTP_STREAM_H_