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.
5 #ifndef NET_HTTP_HTTP_STREAM_FACTORY_H_
6 #define NET_HTTP_HTTP_STREAM_FACTORY_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/load_states.h"
16 #include "net/base/net_export.h"
17 #include "net/base/request_priority.h"
18 #include "net/http/http_server_properties.h"
19 #include "net/socket/connection_attempts.h"
20 #include "net/ssl/ssl_failure_state.h"
21 // This file can be included from net/http even though
22 // it is in net/websockets because it doesn't
23 // introduce any link dependency to net/websockets.
24 #include "net/websockets/websocket_handshake_stream_base.h"
34 class AuthCredentials
;
36 class HostMappingRules
;
38 class HttpAuthController
;
39 class HttpNetworkSession
;
40 class HttpResponseInfo
;
41 class HttpServerProperties
;
44 class SSLCertRequestInfo
;
46 struct HttpRequestInfo
;
49 // The HttpStreamRequest is the client's handle to the worker object which
50 // handles the creation of an HttpStream. While the HttpStream is being
51 // created, this object is the creator's handle for interacting with the
52 // HttpStream creation process. The request is cancelled by deleting it, after
53 // which no callbacks will be invoked.
54 class NET_EXPORT_PRIVATE HttpStreamRequest
{
56 // The HttpStreamRequest::Delegate is a set of callback methods for a
57 // HttpStreamRequestJob. Generally, only one of these methods will be
58 // called as a result of a stream request.
59 class NET_EXPORT_PRIVATE Delegate
{
61 virtual ~Delegate() {}
63 // This is the success case for RequestStream.
64 // |stream| is now owned by the delegate.
65 // |used_ssl_config| indicates the actual SSL configuration used for this
66 // stream, since the HttpStreamRequest may have modified the configuration
67 // during stream processing.
68 // |used_proxy_info| indicates the actual ProxyInfo used for this stream,
69 // since the HttpStreamRequest performs the proxy resolution.
70 virtual void OnStreamReady(
71 const SSLConfig
& used_ssl_config
,
72 const ProxyInfo
& used_proxy_info
,
73 HttpStream
* stream
) = 0;
75 // This is the success case for RequestWebSocketHandshakeStream.
76 // |stream| is now owned by the delegate.
77 // |used_ssl_config| indicates the actual SSL configuration used for this
78 // stream, since the HttpStreamRequest may have modified the configuration
79 // during stream processing.
80 // |used_proxy_info| indicates the actual ProxyInfo used for this stream,
81 // since the HttpStreamRequest performs the proxy resolution.
82 virtual void OnWebSocketHandshakeStreamReady(
83 const SSLConfig
& used_ssl_config
,
84 const ProxyInfo
& used_proxy_info
,
85 WebSocketHandshakeStreamBase
* stream
) = 0;
87 // This is the failure to create a stream case.
88 // |used_ssl_config| indicates the actual SSL configuration used for this
89 // stream, since the HttpStreamRequest may have modified the configuration
90 // during stream processing. If an SSL handshake failed, |ssl_failure_state|
91 // is the state the SSLClientSocket was in.
92 virtual void OnStreamFailed(int status
,
93 const SSLConfig
& used_ssl_config
,
94 SSLFailureState ssl_failure_state
) = 0;
96 // Called when we have a certificate error for the request.
97 // |used_ssl_config| indicates the actual SSL configuration used for this
98 // stream, since the HttpStreamRequest may have modified the configuration
99 // during stream processing.
100 virtual void OnCertificateError(int status
,
101 const SSLConfig
& used_ssl_config
,
102 const SSLInfo
& ssl_info
) = 0;
104 // This is the failure case where we need proxy authentication during
105 // proxy tunnel establishment. For the tunnel case, we were unable to
106 // create the HttpStream, so the caller provides the auth and then resumes
107 // the HttpStreamRequest.
109 // For the non-tunnel case, the caller will discover the authentication
110 // failure when reading response headers. At that point, he will handle the
111 // authentication failure and restart the HttpStreamRequest entirely.
113 // Ownership of |auth_controller| and |proxy_response| are owned
114 // by the HttpStreamRequest. |proxy_response| is not guaranteed to be usable
115 // after the lifetime of this callback. The delegate may take a reference
116 // to |auth_controller| if it is needed beyond the lifetime of this
119 // |used_ssl_config| indicates the actual SSL configuration used for this
120 // stream, since the HttpStreamRequest may have modified the configuration
121 // during stream processing.
122 virtual void OnNeedsProxyAuth(const HttpResponseInfo
& proxy_response
,
123 const SSLConfig
& used_ssl_config
,
124 const ProxyInfo
& used_proxy_info
,
125 HttpAuthController
* auth_controller
) = 0;
127 // This is the failure for SSL Client Auth
128 // Ownership of |cert_info| is retained by the HttpStreamRequest. The
129 // delegate may take a reference if it needs the cert_info beyond the
130 // lifetime of this callback.
131 virtual void OnNeedsClientAuth(const SSLConfig
& used_ssl_config
,
132 SSLCertRequestInfo
* cert_info
) = 0;
134 // This is the failure of the CONNECT request through an HTTPS proxy.
135 // Headers can be read from |response_info|, while the body can be read
138 // |used_ssl_config| indicates the actual SSL configuration used for this
139 // stream, since the HttpStreamRequest may have modified the configuration
140 // during stream processing.
142 // |used_proxy_info| indicates the actual ProxyInfo used for this stream,
143 // since the HttpStreamRequest performs the proxy resolution.
145 // Ownership of |stream| is transferred to the delegate.
146 virtual void OnHttpsProxyTunnelResponse(
147 const HttpResponseInfo
& response_info
,
148 const SSLConfig
& used_ssl_config
,
149 const ProxyInfo
& used_proxy_info
,
150 HttpStream
* stream
) = 0;
153 virtual ~HttpStreamRequest() {}
155 // When a HttpStream creation process is stalled due to necessity
156 // of Proxy authentication credentials, the delegate OnNeedsProxyAuth
157 // will have been called. It now becomes the delegate's responsibility
158 // to collect the necessary credentials, and then call this method to
159 // resume the HttpStream creation process.
160 virtual int RestartTunnelWithProxyAuth(
161 const AuthCredentials
& credentials
) = 0;
163 // Called when the priority of the parent transaction changes.
164 virtual void SetPriority(RequestPriority priority
) = 0;
166 // Returns the LoadState for the request.
167 virtual LoadState
GetLoadState() const = 0;
169 // Returns true if TLS/NPN was negotiated for this stream.
170 virtual bool was_npn_negotiated() const = 0;
172 // Protocol negotiated with the server.
173 virtual NextProto
protocol_negotiated() const = 0;
175 // Returns true if this stream is being fetched over SPDY.
176 virtual bool using_spdy() const = 0;
178 // Returns socket-layer connection attempts made for this stream request.
179 virtual const ConnectionAttempts
& connection_attempts() const = 0;
182 // The HttpStreamFactory defines an interface for creating usable HttpStreams.
183 class NET_EXPORT HttpStreamFactory
{
185 virtual ~HttpStreamFactory();
187 void ProcessAlternateProtocol(
188 const base::WeakPtr
<HttpServerProperties
>& http_server_properties
,
189 const std::vector
<std::string
>& alternate_protocol_values
,
190 const HostPortPair
& http_host_port_pair
,
191 const HttpNetworkSession
& session
);
193 GURL
ApplyHostMappingRules(const GURL
& url
, HostPortPair
* endpoint
);
195 // Virtual interface methods.
198 // Will call delegate->OnStreamReady on successful completion.
199 virtual HttpStreamRequest
* RequestStream(
200 const HttpRequestInfo
& info
,
201 RequestPriority priority
,
202 const SSLConfig
& server_ssl_config
,
203 const SSLConfig
& proxy_ssl_config
,
204 HttpStreamRequest::Delegate
* delegate
,
205 const BoundNetLog
& net_log
) = 0;
207 // Request a WebSocket handshake stream.
208 // Will call delegate->OnWebSocketHandshakeStreamReady on successful
210 virtual HttpStreamRequest
* RequestWebSocketHandshakeStream(
211 const HttpRequestInfo
& info
,
212 RequestPriority priority
,
213 const SSLConfig
& server_ssl_config
,
214 const SSLConfig
& proxy_ssl_config
,
215 HttpStreamRequest::Delegate
* delegate
,
216 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
,
217 const BoundNetLog
& net_log
) = 0;
219 // Requests that enough connections for |num_streams| be opened.
220 virtual void PreconnectStreams(int num_streams
,
221 const HttpRequestInfo
& info
,
222 RequestPriority priority
,
223 const SSLConfig
& server_ssl_config
,
224 const SSLConfig
& proxy_ssl_config
) = 0;
226 virtual const HostMappingRules
* GetHostMappingRules() const = 0;
230 // Reset all static settings to initialized values. Used to init test suite.
231 static void ResetStaticSettingsToInit();
233 // Turns spdy on or off.
234 // TODO(mmenke): Figure out if this can be made a property of the
235 // HttpNetworkSession.
236 static void set_spdy_enabled(bool value
) {
237 spdy_enabled_
= value
;
239 static bool spdy_enabled() { return spdy_enabled_
; }
245 static bool spdy_enabled_
;
247 DISALLOW_COPY_AND_ASSIGN(HttpStreamFactory
);
252 #endif // NET_HTTP_HTTP_STREAM_FACTORY_H_