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