[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 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_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_ |
| 6 | #define NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/basictypes.h" |
| 12 | #include "base/ref_counted.h" |
| 13 | #include "net/base/completion_callback.h" |
| 14 | #include "net/base/host_port_pair.h" |
| 15 | #include "net/base/net_log.h" |
| 16 | #include "net/http/http_auth_controller.h" |
| 17 | #include "net/http/http_request_headers.h" |
| 18 | #include "net/http/http_request_info.h" |
| 19 | #include "net/http/http_response_info.h" |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 20 | #include "net/http/proxy_client_socket.h" |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 21 | |
| 22 | class GURL; |
| 23 | |
| 24 | namespace net { |
| 25 | |
| 26 | class AddressList; |
| 27 | class ClientSocketHandle; |
[email protected] | 4d4a516 | 2010-09-21 22:44:04 | [diff] [blame] | 28 | class GrowableIOBuffer; |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 29 | class HttpAuthCache; |
| 30 | class HttpAuthHandleFactory; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 31 | class HttpStream; |
[email protected] | 4d4a516 | 2010-09-21 22:44:04 | [diff] [blame] | 32 | class HttpStreamParser; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 33 | class IOBuffer; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 34 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 35 | class HttpProxyClientSocket : public ProxyClientSocket { |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 36 | public: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 37 | // Takes ownership of |transport_socket|, which should already be connected |
| 38 | // by the time Connect() is called. If tunnel is true then on Connect() |
| 39 | // this socket will establish an Http tunnel. |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 40 | HttpProxyClientSocket(ClientSocketHandle* transport_socket, |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 41 | const GURL& request_url, |
| 42 | const std::string& user_agent, |
| 43 | const HostPortPair& endpoint, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 44 | const HostPortPair& proxy_server, |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 45 | HttpAuthCache* http_auth_cache, |
| 46 | HttpAuthHandlerFactory* http_auth_handler_factory, |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 47 | bool tunnel, |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 48 | bool using_spdy, |
| 49 | bool is_https_proxy); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 50 | |
| 51 | // On destruction Disconnect() is called. |
| 52 | virtual ~HttpProxyClientSocket(); |
| 53 | |
| 54 | // If Connect (or its callback) returns PROXY_AUTH_REQUESTED, then |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 55 | // credentials should be added to the HttpAuthController before calling |
| 56 | // RestartWithAuth. |
| 57 | int RestartWithAuth(CompletionCallback* callback); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 58 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 59 | const scoped_refptr<HttpAuthController>& auth_controller() { |
| 60 | return auth_; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 63 | bool using_spdy() { |
| 64 | return using_spdy_; |
| 65 | } |
| 66 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 67 | // ProxyClientSocket methods: |
| 68 | virtual const HttpResponseInfo* GetConnectResponseInfo() const; |
| 69 | virtual HttpStream* CreateConnectResponseStream(); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 70 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 71 | // ClientSocket methods: |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 72 | virtual int Connect(CompletionCallback* callback); |
| 73 | virtual void Disconnect(); |
| 74 | virtual bool IsConnected() const; |
| 75 | virtual bool IsConnectedAndIdle() const; |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 76 | virtual const BoundNetLog& NetLog() const; |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 77 | virtual void SetSubresourceSpeculation(); |
| 78 | virtual void SetOmniboxSpeculation(); |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 79 | virtual bool WasEverUsed() const; |
[email protected] | 7f7e9239 | 2010-10-26 18:29:29 | [diff] [blame] | 80 | virtual bool UsingTCPFastOpen() const; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 81 | |
| 82 | // Socket methods: |
| 83 | virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 84 | virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 85 | virtual bool SetReceiveBufferSize(int32 size); |
| 86 | virtual bool SetSendBufferSize(int32 size); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 87 | virtual int GetPeerAddress(AddressList* address) const; |
| 88 | |
| 89 | private: |
| 90 | enum State { |
| 91 | STATE_NONE, |
| 92 | STATE_GENERATE_AUTH_TOKEN, |
| 93 | STATE_GENERATE_AUTH_TOKEN_COMPLETE, |
| 94 | STATE_SEND_REQUEST, |
| 95 | STATE_SEND_REQUEST_COMPLETE, |
| 96 | STATE_READ_HEADERS, |
| 97 | STATE_READ_HEADERS_COMPLETE, |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 98 | STATE_DRAIN_BODY, |
| 99 | STATE_DRAIN_BODY_COMPLETE, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 100 | STATE_TCP_RESTART, |
| 101 | STATE_TCP_RESTART_COMPLETE, |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 102 | STATE_DONE, |
| 103 | }; |
| 104 | |
| 105 | // The size in bytes of the buffer we use to drain the response body that |
| 106 | // we want to throw away. The response body is typically a small error |
| 107 | // page just a few hundred bytes long. |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 108 | static const int kDrainBodyBufferSize = 1024; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 109 | |
| 110 | int PrepareForAuthRestart(); |
| 111 | int DidDrainBodyForAuthRestart(bool keep_alive); |
| 112 | |
| 113 | int HandleAuthChallenge(); |
| 114 | |
| 115 | void LogBlockedTunnelResponse(int response_code) const; |
| 116 | |
| 117 | void DoCallback(int result); |
| 118 | void OnIOComplete(int result); |
| 119 | |
| 120 | int DoLoop(int last_io_result); |
| 121 | int DoGenerateAuthToken(); |
| 122 | int DoGenerateAuthTokenComplete(int result); |
| 123 | int DoSendRequest(); |
| 124 | int DoSendRequestComplete(int result); |
| 125 | int DoReadHeaders(); |
| 126 | int DoReadHeadersComplete(int result); |
| 127 | int DoDrainBody(); |
| 128 | int DoDrainBodyComplete(int result); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 129 | int DoTCPRestart(); |
| 130 | int DoTCPRestartComplete(int result); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 131 | |
| 132 | CompletionCallbackImpl<HttpProxyClientSocket> io_callback_; |
| 133 | State next_state_; |
| 134 | |
| 135 | // Stores the callback to the layer above, called on completing Connect(). |
| 136 | CompletionCallback* user_callback_; |
| 137 | |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 138 | HttpRequestInfo request_; |
| 139 | HttpResponseInfo response_; |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 140 | |
[email protected] | 4d4a516 | 2010-09-21 22:44:04 | [diff] [blame] | 141 | scoped_refptr<GrowableIOBuffer> parser_buf_; |
| 142 | scoped_ptr<HttpStreamParser> http_stream_parser_; |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 143 | scoped_refptr<IOBuffer> drain_buf_; |
| 144 | |
| 145 | // Stores the underlying socket. |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 146 | scoped_ptr<ClientSocketHandle> transport_; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 147 | |
| 148 | // The hostname and port of the endpoint. This is not necessarily the one |
| 149 | // specified by the URL, due to Alternate-Protocol or fixed testing ports. |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 150 | const HostPortPair endpoint_; |
| 151 | scoped_refptr<HttpAuthController> auth_; |
| 152 | const bool tunnel_; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 153 | // If true, then the connection to the proxy is a SPDY connection. |
| 154 | const bool using_spdy_; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 155 | // If true, then SSL is used to communicate with this proxy |
| 156 | const bool is_https_proxy_; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 157 | |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 158 | std::string request_line_; |
| 159 | HttpRequestHeaders request_headers_; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 160 | |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 161 | const BoundNetLog net_log_; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocket); |
| 164 | }; |
| 165 | |
| 166 | } // namespace net |
| 167 | |
| 168 | #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_ |