• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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_SPDY_SPDY_HTTP_STREAM_H_
6 #define NET_SPDY_SPDY_HTTP_STREAM_H_
7 #pragma once
8 
9 #include <list>
10 #include <string>
11 
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/task.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_log.h"
17 #include "net/http/http_request_info.h"
18 #include "net/http/http_stream.h"
19 #include "net/spdy/spdy_protocol.h"
20 #include "net/spdy/spdy_session.h"
21 #include "net/spdy/spdy_stream.h"
22 
23 namespace net {
24 
25 class HttpResponseInfo;
26 class IOBuffer;
27 class SpdySession;
28 class UploadData;
29 class UploadDataStream;
30 
31 // The SpdyHttpStream is a HTTP-specific type of stream known to a SpdySession.
32 class SpdyHttpStream : public SpdyStream::Delegate, public HttpStream {
33  public:
34   SpdyHttpStream(SpdySession* spdy_session, bool direct);
35   virtual ~SpdyHttpStream();
36 
37   // Initializes this SpdyHttpStream by wraping an existing SpdyStream.
38   void InitializeWithExistingStream(SpdyStream* spdy_stream);
39 
stream()40   SpdyStream* stream() { return stream_.get(); }
41 
42   // Cancels any callbacks from being invoked and deletes the stream.
43   void Cancel();
44 
45   // HttpStream methods:
46   virtual int InitializeStream(const HttpRequestInfo* request_info,
47                                const BoundNetLog& net_log,
48                                CompletionCallback* callback) OVERRIDE;
49   virtual int SendRequest(const HttpRequestHeaders& headers,
50                           UploadDataStream* request_body,
51                           HttpResponseInfo* response,
52                           CompletionCallback* callback) OVERRIDE;
53   virtual uint64 GetUploadProgress() const OVERRIDE;
54   virtual int ReadResponseHeaders(CompletionCallback* callback) OVERRIDE;
55   virtual const HttpResponseInfo* GetResponseInfo() const;
56   virtual int ReadResponseBody(IOBuffer* buf,
57                                int buf_len,
58                                CompletionCallback* callback) OVERRIDE;
59   virtual void Close(bool not_reusable) OVERRIDE;
60   virtual HttpStream* RenewStreamForAuth() OVERRIDE;
61   virtual bool IsResponseBodyComplete() const OVERRIDE;
62   virtual bool CanFindEndOfResponse() const OVERRIDE;
63   virtual bool IsMoreDataBuffered() const OVERRIDE;
64   virtual bool IsConnectionReused() const OVERRIDE;
65   virtual void SetConnectionReused() OVERRIDE;
66   virtual bool IsConnectionReusable() const OVERRIDE;
67   virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
68   virtual void GetSSLCertRequestInfo(
69       SSLCertRequestInfo* cert_request_info) OVERRIDE;
70   virtual bool IsSpdyHttpStream() const OVERRIDE;
71 
72   // SpdyStream::Delegate methods:
73   virtual bool OnSendHeadersComplete(int status) OVERRIDE;
74   virtual int OnSendBody() OVERRIDE;
75   virtual int OnSendBodyComplete(int status, bool* eof) OVERRIDE;
76   virtual int OnResponseReceived(const spdy::SpdyHeaderBlock& response,
77                                  base::Time response_time,
78                                  int status) OVERRIDE;
79   virtual void OnDataReceived(const char* buffer, int bytes) OVERRIDE;
80   virtual void OnDataSent(int length) OVERRIDE;
81   virtual void OnClose(int status) OVERRIDE;
82   virtual void set_chunk_callback(ChunkCallback* callback) OVERRIDE;
83 
84  private:
85   FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, FlowControlStallResume);
86 
87   // Call the user callback.
88   void DoCallback(int rv);
89 
90   void ScheduleBufferedReadCallback();
91 
92   // Returns true if the callback is invoked.
93   bool DoBufferedReadCallback();
94   bool ShouldWaitForMoreBufferedData() const;
95 
96   ScopedRunnableMethodFactory<SpdyHttpStream> read_callback_factory_;
97   scoped_refptr<SpdyStream> stream_;
98   scoped_refptr<SpdySession> spdy_session_;
99 
100   // The request to send.
101   const HttpRequestInfo* request_info_;
102 
103   scoped_ptr<UploadDataStream> request_body_stream_;
104 
105   // |response_info_| is the HTTP response data object which is filled in
106   // when a SYN_REPLY comes in for the stream.
107   // It is not owned by this stream object, or point to |push_response_info_|.
108   HttpResponseInfo* response_info_;
109 
110   scoped_ptr<HttpResponseInfo> push_response_info_;
111 
112   bool download_finished_;
113   bool response_headers_received_;  // Indicates waiting for more HEADERS.
114 
115   // We buffer the response body as it arrives asynchronously from the stream.
116   // TODO(mbelshe):  is this infinite buffering?
117   std::list<scoped_refptr<IOBufferWithSize> > response_body_;
118 
119   CompletionCallback* user_callback_;
120 
121   // User provided buffer for the ReadResponseBody() response.
122   scoped_refptr<IOBuffer> user_buffer_;
123   int user_buffer_len_;
124 
125   // Is there a scheduled read callback pending.
126   bool buffered_read_callback_pending_;
127   // Has more data been received from the network during the wait for the
128   // scheduled read callback.
129   bool more_read_data_pending_;
130 
131   // Is this spdy stream direct to the origin server (or to a proxy).
132   bool direct_;
133 
134   bool send_last_chunk_;
135 
136   DISALLOW_COPY_AND_ASSIGN(SpdyHttpStream);
137 };
138 
139 }  // namespace net
140 
141 #endif  // NET_SPDY_SPDY_HTTP_STREAM_H_
142