spdy_proxy_client_socket.h revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2010 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_PROXY_CLIENT_SOCKET_H_
6#define NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_
7#pragma once
8
9#include <string>
10#include <list>
11
12#include "base/basictypes.h"
13#include "base/ref_counted.h"
14#include "net/base/completion_callback.h"
15#include "net/base/host_port_pair.h"
16#include "net/base/net_log.h"
17#include "net/http/http_auth_controller.h"
18#include "net/http/http_request_headers.h"
19#include "net/http/http_request_info.h"
20#include "net/http/http_response_info.h"
21#include "net/http/proxy_client_socket.h"
22#include "net/spdy/spdy_http_stream.h"
23#include "net/spdy/spdy_protocol.h"
24#include "net/spdy/spdy_session.h"
25#include "net/spdy/spdy_stream.h"
26
27
28class GURL;
29
30namespace net {
31
32class AddressList;
33class ClientSocketHandle;
34class HttpStream;
35class IOBuffer;
36class SpdySession;
37class SpdyStream;
38
39class SpdyProxyClientSocket : public ProxyClientSocket,
40                              public SpdyStream::Delegate {
41 public:
42  // Create a socket on top of the |spdy_stream| by sending a SYN_STREAM
43  // CONNECT frame for |endpoint|.  After the SYN_REPLY is received,
44  // any data read/written to the socket will be transferred in data
45  // frames.
46  SpdyProxyClientSocket(SpdyStream* spdy_stream,
47                        const std::string& user_agent,
48                        const HostPortPair& endpoint,
49                        const GURL& url,
50                        const HostPortPair& proxy_server,
51                        HttpAuthCache* auth_cache,
52                        HttpAuthHandlerFactory* auth_handler_factory);
53
54
55  // On destruction Disconnect() is called.
56  virtual ~SpdyProxyClientSocket();
57
58  const scoped_refptr<HttpAuthController>& auth_controller() {
59    return auth_;
60  }
61
62  // ProxyClientSocket methods:
63  virtual const HttpResponseInfo* GetConnectResponseInfo() const;
64
65  // In the event of a non-200 response to the CONNECT request, this
66  // method may be called to return an HttpStream in order to read
67  // the response body.
68  virtual HttpStream* CreateConnectResponseStream();
69
70  // ClientSocket methods:
71#ifdef ANDROID
72  virtual int Connect(CompletionCallback* callback, bool wait_for_connect);
73#else
74  virtual int Connect(CompletionCallback* callback);
75#endif
76  virtual void Disconnect();
77  virtual bool IsConnected() const;
78  virtual bool IsConnectedAndIdle() const;
79  virtual const BoundNetLog& NetLog() const;
80  virtual void SetSubresourceSpeculation();
81  virtual void SetOmniboxSpeculation();
82  virtual bool WasEverUsed() const;
83  virtual bool UsingTCPFastOpen() const;
84
85  // Socket methods:
86  virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
87  virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
88  virtual bool SetReceiveBufferSize(int32 size);
89  virtual bool SetSendBufferSize(int32 size);
90  virtual int GetPeerAddress(AddressList* address) const;
91
92  // SpdyStream::Delegate methods:
93  virtual bool OnSendHeadersComplete(int status);
94  virtual int OnSendBody();
95  virtual int OnSendBodyComplete(int status, bool* eof);
96  virtual int OnResponseReceived(const spdy::SpdyHeaderBlock& response,
97                                 base::Time response_time,
98                                 int status);
99  virtual void OnDataReceived(const char* data, int length);
100  virtual void OnDataSent(int length);
101  virtual void OnClose(int status);
102  virtual void set_chunk_callback(ChunkCallback* /*callback*/);
103
104 private:
105  enum State {
106    STATE_DISCONNECTED,
107    STATE_GENERATE_AUTH_TOKEN,
108    STATE_GENERATE_AUTH_TOKEN_COMPLETE,
109    STATE_SEND_REQUEST,
110    STATE_SEND_REQUEST_COMPLETE,
111    STATE_READ_REPLY_COMPLETE,
112    STATE_OPEN,
113    STATE_CLOSED
114  };
115
116  void OnIOComplete(int result);
117
118  int DoLoop(int last_io_result);
119  int DoGenerateAuthToken();
120  int DoGenerateAuthTokenComplete(int result);
121  int DoSendRequest();
122  int DoSendRequestComplete(int result);
123  int DoReadReplyComplete(int result);
124
125  // Populates |user_buffer_| with as much read data as possible
126  // and returns the number of bytes read.
127  int PopulateUserReadBuffer();
128
129  CompletionCallbackImpl<SpdyProxyClientSocket> io_callback_;
130  State next_state_;
131
132  // Pointer to the SPDY Stream that this sits on top of.
133  scoped_refptr<SpdyStream> spdy_stream_;
134
135  // Stores the callback to the layer above, called on completing Read() or
136  // Connect().
137  CompletionCallback* read_callback_;
138  // Stores the callback to the layer above, called on completing Write().
139  CompletionCallback* write_callback_;
140
141  // CONNECT request and response.
142  HttpRequestInfo request_;
143  HttpResponseInfo response_;
144
145  // The hostname and port of the endpoint.  This is not necessarily the one
146  // specified by the URL, due to Alternate-Protocol or fixed testing ports.
147  const HostPortPair endpoint_;
148  scoped_refptr<HttpAuthController> auth_;
149
150  // We buffer the response body as it arrives asynchronously from the stream.
151  std::list<scoped_refptr<DrainableIOBuffer> > read_buffer_;
152
153  // User provided buffer for the Read() response.
154  scoped_refptr<DrainableIOBuffer> user_buffer_;
155
156  // User specified number of bytes to be written.
157  int write_buffer_len_;
158  // Number of bytes written which have not been confirmed
159  int write_bytes_outstanding_;
160
161  // True if read has ever returned zero for eof.
162  bool eof_has_been_read_;
163  // True if the transport socket has ever sent data.
164  bool was_ever_used_;
165
166  scoped_ptr<SpdyHttpStream> response_stream_;
167
168  const BoundNetLog net_log_;
169
170  DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket);
171};
172
173}  // namespace net
174
175#endif  // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_
176