| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_SOCKET_STREAM_SOCKET_STREAM_H_ | 5 #ifndef NET_SOCKET_STREAM_SOCKET_STREAM_H_ |
| 6 #define NET_SOCKET_STREAM_SOCKET_STREAM_H_ | 6 #define NET_SOCKET_STREAM_SOCKET_STREAM_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/linked_ptr.h" | 13 #include "base/linked_ptr.h" |
| 13 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
| 14 #include "base/scoped_ptr.h" | 15 #include "base/scoped_ptr.h" |
| 15 #include "base/task.h" | 16 #include "base/task.h" |
| 16 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
| 17 #include "net/base/completion_callback.h" | 18 #include "net/base/completion_callback.h" |
| 18 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 20 #include "net/http/http_auth.h" |
| 21 #include "net/http/http_auth_cache.h" |
| 22 #include "net/http/http_auth_handler.h" |
| 19 #include "net/proxy/proxy_service.h" | 23 #include "net/proxy/proxy_service.h" |
| 20 #include "net/socket/tcp_client_socket.h" | 24 #include "net/socket/tcp_client_socket.h" |
| 21 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| 22 | 26 |
| 23 namespace net { | 27 namespace net { |
| 24 | 28 |
| 29 class AuthChallengeInfo; |
| 25 class ClientSocketFactory; | 30 class ClientSocketFactory; |
| 26 class HostResolver; | 31 class HostResolver; |
| 27 class SSLConfigService; | 32 class SSLConfigService; |
| 28 class SingleRequestHostResolver; | 33 class SingleRequestHostResolver; |
| 29 | 34 |
| 35 // SocketStream is used to implement Web Sockets. |
| 36 // It provides plain full-duplex stream with proxy and SSL support. |
| 37 // For proxy authentication, only basic mechanisum is supported. It will try |
| 38 // authentication identity for proxy URL first. If server requires proxy |
| 39 // authentication, it will try authentication identity for realm that server |
| 40 // requests. |
| 30 class SocketStream : public base::RefCountedThreadSafe<SocketStream> { | 41 class SocketStream : public base::RefCountedThreadSafe<SocketStream> { |
| 31 public: | 42 public: |
| 32 // Derive from this class and add your own data members to associate extra | 43 // Derive from this class and add your own data members to associate extra |
| 33 // information with a SocketStream. Use GetUserData(key) and | 44 // information with a SocketStream. Use GetUserData(key) and |
| 34 // SetUserData(key, data). | 45 // SetUserData(key, data). |
| 35 class UserData { | 46 class UserData { |
| 36 public: | 47 public: |
| 37 UserData() {} | 48 UserData() {} |
| 38 virtual ~UserData() {} | 49 virtual ~UserData() {} |
| 39 }; | 50 }; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 52 // Called when |amount_sent| bytes of data are sent. | 63 // Called when |amount_sent| bytes of data are sent. |
| 53 virtual void OnSentData(SocketStream* socket, | 64 virtual void OnSentData(SocketStream* socket, |
| 54 int amount_sent) = 0; | 65 int amount_sent) = 0; |
| 55 | 66 |
| 56 // Called when |len| bytes of |data| are received. | 67 // Called when |len| bytes of |data| are received. |
| 57 virtual void OnReceivedData(SocketStream* socket, | 68 virtual void OnReceivedData(SocketStream* socket, |
| 58 const char* data, int len) = 0; | 69 const char* data, int len) = 0; |
| 59 | 70 |
| 60 // Called when the socket stream has been closed. | 71 // Called when the socket stream has been closed. |
| 61 virtual void OnClose(SocketStream* socket) = 0; | 72 virtual void OnClose(SocketStream* socket) = 0; |
| 73 |
| 74 // Called when proxy authentication required. |
| 75 // The delegate should call RestartWithAuth() if credential for |auth_info| |
| 76 // is found in password database, or call Close() to close the connection. |
| 77 virtual void OnAuthRequired(SocketStream* socket, |
| 78 AuthChallengeInfo* auth_info) { |
| 79 // By default, no credential is available and close the connection. |
| 80 socket->Close(); |
| 81 } |
| 62 }; | 82 }; |
| 63 | 83 |
| 64 SocketStream(const GURL& url, Delegate* delegate); | 84 SocketStream(const GURL& url, Delegate* delegate); |
| 65 | 85 |
| 66 // The user data allows the clients to associate data with this job. | 86 // The user data allows the clients to associate data with this job. |
| 67 // Multiple user data values can be stored under different keys. | 87 // Multiple user data values can be stored under different keys. |
| 68 // This job will TAKE OWNERSHIP of the given data pointer, and will | 88 // This job will TAKE OWNERSHIP of the given data pointer, and will |
| 69 // delete the object if it is changed or the job is destroyed. | 89 // delete the object if it is changed or the job is destroyed. |
| 70 UserData* GetUserData(const void* key) const; | 90 UserData* GetUserData(const void* key) const; |
| 71 void SetUserData(const void* key, UserData* data); | 91 void SetUserData(const void* key, UserData* data); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 84 // Requests to send |len| bytes of |data| on the connection. | 104 // Requests to send |len| bytes of |data| on the connection. |
| 85 // Returns true if |data| is buffered in the job. | 105 // Returns true if |data| is buffered in the job. |
| 86 // Returns false if size of buffered data would exceeds | 106 // Returns false if size of buffered data would exceeds |
| 87 // |max_pending_send_allowed_| and |data| is not sent at all. | 107 // |max_pending_send_allowed_| and |data| is not sent at all. |
| 88 bool SendData(const char* data, int len); | 108 bool SendData(const char* data, int len); |
| 89 | 109 |
| 90 // Requests to close the connection. | 110 // Requests to close the connection. |
| 91 // Once the connection is closed, calls delegate's OnClose. | 111 // Once the connection is closed, calls delegate's OnClose. |
| 92 void Close(); | 112 void Close(); |
| 93 | 113 |
| 114 // Restarts with authentication info. |
| 115 // Should be used for response of OnAuthRequired. |
| 116 void RestartWithAuth( |
| 117 const std::wstring& username, |
| 118 const std::wstring& password); |
| 119 |
| 94 // Detach delegate. Call before delegate is deleted. | 120 // Detach delegate. Call before delegate is deleted. |
| 95 // Once delegate is detached, close the socket stream and never call delegate | 121 // Once delegate is detached, close the socket stream and never call delegate |
| 96 // back. | 122 // back. |
| 97 void DetachDelegate(); | 123 void DetachDelegate(); |
| 98 | 124 |
| 99 // Sets an alternative HostResolver. For testing purposes only. | 125 // Sets an alternative HostResolver. For testing purposes only. |
| 100 void SetHostResolver(HostResolver* host_resolver); | 126 void SetHostResolver(HostResolver* host_resolver); |
| 101 | 127 |
| 102 // Sets an alternative ClientSocketFactory. Doesn't take ownership of | 128 // Sets an alternative ClientSocketFactory. Doesn't take ownership of |
| 103 // |factory|. For testing purposes only. | 129 // |factory|. For testing purposes only. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 STATE_TCP_CONNECT, | 164 STATE_TCP_CONNECT, |
| 139 STATE_TCP_CONNECT_COMPLETE, | 165 STATE_TCP_CONNECT_COMPLETE, |
| 140 STATE_WRITE_TUNNEL_HEADERS, | 166 STATE_WRITE_TUNNEL_HEADERS, |
| 141 STATE_WRITE_TUNNEL_HEADERS_COMPLETE, | 167 STATE_WRITE_TUNNEL_HEADERS_COMPLETE, |
| 142 STATE_READ_TUNNEL_HEADERS, | 168 STATE_READ_TUNNEL_HEADERS, |
| 143 STATE_READ_TUNNEL_HEADERS_COMPLETE, | 169 STATE_READ_TUNNEL_HEADERS_COMPLETE, |
| 144 STATE_SOCKS_CONNECT, | 170 STATE_SOCKS_CONNECT, |
| 145 STATE_SOCKS_CONNECT_COMPLETE, | 171 STATE_SOCKS_CONNECT_COMPLETE, |
| 146 STATE_SSL_CONNECT, | 172 STATE_SSL_CONNECT, |
| 147 STATE_SSL_CONNECT_COMPLETE, | 173 STATE_SSL_CONNECT_COMPLETE, |
| 148 STATE_CONNECTION_ESTABLISHED, | |
| 149 STATE_READ_WRITE | 174 STATE_READ_WRITE |
| 150 }; | 175 }; |
| 151 | 176 |
| 152 enum ProxyMode { | 177 enum ProxyMode { |
| 153 kDirectConnection, // If using a direct connection | 178 kDirectConnection, // If using a direct connection |
| 154 kTunnelProxy, // If using a tunnel (CONNECT method as HTTPS) | 179 kTunnelProxy, // If using a tunnel (CONNECT method as HTTPS) |
| 155 kSOCKSProxy, // If using a SOCKS proxy | 180 kSOCKSProxy, // If using a SOCKS proxy |
| 156 }; | 181 }; |
| 157 | 182 |
| 158 typedef std::deque< scoped_refptr<IOBufferWithSize> > PendingDataQueue; | 183 typedef std::deque< scoped_refptr<IOBufferWithSize> > PendingDataQueue; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 182 int DoWriteTunnelHeaders(); | 207 int DoWriteTunnelHeaders(); |
| 183 int DoWriteTunnelHeadersComplete(int result); | 208 int DoWriteTunnelHeadersComplete(int result); |
| 184 int DoReadTunnelHeaders(); | 209 int DoReadTunnelHeaders(); |
| 185 int DoReadTunnelHeadersComplete(int result); | 210 int DoReadTunnelHeadersComplete(int result); |
| 186 int DoSOCKSConnect(); | 211 int DoSOCKSConnect(); |
| 187 int DoSOCKSConnectComplete(int result); | 212 int DoSOCKSConnectComplete(int result); |
| 188 int DoSSLConnect(); | 213 int DoSSLConnect(); |
| 189 int DoSSLConnectComplete(int result); | 214 int DoSSLConnectComplete(int result); |
| 190 int DoReadWrite(int result); | 215 int DoReadWrite(int result); |
| 191 | 216 |
| 217 GURL ProxyAuthOrigin() const; |
| 218 int HandleAuthChallenge(const HttpResponseHeaders* headers); |
| 219 void DoAuthRequired(); |
| 220 void DoRestartWithAuth(); |
| 221 |
| 192 int HandleCertificateError(int result); | 222 int HandleCertificateError(int result); |
| 193 | 223 |
| 194 bool is_secure() const; | 224 bool is_secure() const; |
| 195 SSLConfigService* ssl_config_service() const; | 225 SSLConfigService* ssl_config_service() const; |
| 196 ProxyService* proxy_service() const; | 226 ProxyService* proxy_service() const; |
| 197 | 227 |
| 198 GURL url_; | 228 GURL url_; |
| 199 Delegate* delegate_; | 229 Delegate* delegate_; |
| 200 int max_pending_send_allowed_; | 230 int max_pending_send_allowed_; |
| 201 scoped_refptr<URLRequestContext> context_; | 231 scoped_refptr<URLRequestContext> context_; |
| 202 | 232 |
| 203 typedef std::map<const void*, linked_ptr<UserData> > UserDataMap; | 233 typedef std::map<const void*, linked_ptr<UserData> > UserDataMap; |
| 204 UserDataMap user_data_; | 234 UserDataMap user_data_; |
| 205 | 235 |
| 206 State next_state_; | 236 State next_state_; |
| 207 scoped_refptr<HostResolver> host_resolver_; | 237 scoped_refptr<HostResolver> host_resolver_; |
| 208 ClientSocketFactory* factory_; | 238 ClientSocketFactory* factory_; |
| 209 | 239 |
| 210 ProxyMode proxy_mode_; | 240 ProxyMode proxy_mode_; |
| 211 | 241 |
| 212 ProxyService::PacRequest* pac_request_; | 242 ProxyService::PacRequest* pac_request_; |
| 213 ProxyInfo proxy_info_; | 243 ProxyInfo proxy_info_; |
| 214 | 244 |
| 245 HttpAuthCache auth_cache_; |
| 246 scoped_refptr<HttpAuthHandler> auth_handler_; |
| 247 HttpAuth::Identity auth_identity_; |
| 248 scoped_refptr<AuthChallengeInfo> auth_info_; |
| 249 |
| 215 scoped_refptr<RequestHeaders> tunnel_request_headers_; | 250 scoped_refptr<RequestHeaders> tunnel_request_headers_; |
| 216 size_t tunnel_request_headers_bytes_sent_; | 251 size_t tunnel_request_headers_bytes_sent_; |
| 217 scoped_refptr<ResponseHeaders> tunnel_response_headers_; | 252 scoped_refptr<ResponseHeaders> tunnel_response_headers_; |
| 218 int tunnel_response_headers_capacity_; | 253 int tunnel_response_headers_capacity_; |
| 219 int tunnel_response_headers_len_; | 254 int tunnel_response_headers_len_; |
| 220 | 255 |
| 221 // Use the same number as HttpNetworkTransaction::kMaxHeaderBufSize. | 256 // Use the same number as HttpNetworkTransaction::kMaxHeaderBufSize. |
| 222 enum { kMaxTunnelResponseHeadersSize = 32768 }; // 32 kilobytes. | 257 enum { kMaxTunnelResponseHeadersSize = 32768 }; // 32 kilobytes. |
| 223 | 258 |
| 224 scoped_ptr<SingleRequestHostResolver> resolver_; | 259 scoped_ptr<SingleRequestHostResolver> resolver_; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 245 int write_buf_offset_; | 280 int write_buf_offset_; |
| 246 int write_buf_size_; | 281 int write_buf_size_; |
| 247 PendingDataQueue pending_write_bufs_; | 282 PendingDataQueue pending_write_bufs_; |
| 248 | 283 |
| 249 DISALLOW_COPY_AND_ASSIGN(SocketStream); | 284 DISALLOW_COPY_AND_ASSIGN(SocketStream); |
| 250 }; | 285 }; |
| 251 | 286 |
| 252 } // namespace net | 287 } // namespace net |
| 253 | 288 |
| 254 #endif // NET_SOCKET_STREAM_SOCKET_STREAM_H_ | 289 #endif // NET_SOCKET_STREAM_SOCKET_STREAM_H_ |
| OLD | NEW |