• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 <stddef.h>
9 
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <string>
14 
15 #include "base/containers/unique_ptr_adapters.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/raw_ptr.h"
18 #include "net/base/host_port_pair.h"
19 #include "net/base/load_states.h"
20 #include "net/base/net_export.h"
21 #include "net/base/privacy_mode.h"
22 #include "net/base/proxy_server.h"
23 #include "net/base/request_priority.h"
24 #include "net/http/http_request_info.h"
25 #include "net/http/http_server_properties.h"
26 #include "net/http/http_stream_request.h"
27 #include "net/log/net_log_source.h"
28 #include "net/log/net_log_with_source.h"
29 #include "net/proxy_resolution/proxy_info.h"
30 #include "net/socket/ssl_client_socket.h"
31 #include "net/spdy/spdy_session_key.h"
32 #include "net/ssl/ssl_config.h"
33 #include "net/websockets/websocket_handshake_stream_base.h"
34 
35 namespace net {
36 
37 class HostMappingRules;
38 class HttpNetworkSession;
39 class HttpResponseHeaders;
40 
41 class NET_EXPORT HttpStreamFactory {
42  public:
43   class NET_EXPORT_PRIVATE Job;
44   class NET_EXPORT_PRIVATE JobController;
45   class NET_EXPORT_PRIVATE JobFactory;
46 
47   enum JobType {
48     // Job that will connect via HTTP/1 or HTTP/2. This may be paused for a
49     // while when ALTERNATIVE or DNS_ALPN_H3 job was created.
50     MAIN,
51     // Job that will connect via HTTP/3 iff Chrome has received an Alt-Svc
52     // header from the origin.
53     ALTERNATIVE,
54     // Job that will connect via HTTP/3 iff an "h3" value was found in the ALPN
55     // list of an HTTPS DNS record.
56     DNS_ALPN_H3,
57     // Job that will preconnect. This uses HTTP/3 iff Chrome has received an
58     // Alt-Svc header from the origin. Otherwise, it use HTTP/1 or HTTP/2.
59     PRECONNECT,
60     // Job that will preconnect via HTTP/3 iff an "h3" value was found in the
61     // ALPN list of an HTTPS DNS record.
62     PRECONNECT_DNS_ALPN_H3,
63   };
64 
65   explicit HttpStreamFactory(HttpNetworkSession* session);
66 
67   HttpStreamFactory(const HttpStreamFactory&) = delete;
68   HttpStreamFactory& operator=(const HttpStreamFactory&) = delete;
69 
70   virtual ~HttpStreamFactory();
71 
72   void ProcessAlternativeServices(
73       HttpNetworkSession* session,
74       const net::NetworkAnonymizationKey& network_anonymization_key,
75       const HttpResponseHeaders* headers,
76       const url::SchemeHostPort& http_server);
77 
78   // Request a stream.
79   // Will call delegate->OnStreamReady on successful completion.
80   std::unique_ptr<HttpStreamRequest> RequestStream(
81       const HttpRequestInfo& info,
82       RequestPriority priority,
83       const SSLConfig& server_ssl_config,
84       const SSLConfig& proxy_ssl_config,
85       HttpStreamRequest::Delegate* delegate,
86       bool enable_ip_based_pooling,
87       bool enable_alternative_services,
88       const NetLogWithSource& net_log);
89 
90   // Request a WebSocket handshake stream.
91   // Will call delegate->OnWebSocketHandshakeStreamReady on successful
92   // completion.
93   std::unique_ptr<HttpStreamRequest> RequestWebSocketHandshakeStream(
94       const HttpRequestInfo& info,
95       RequestPriority priority,
96       const SSLConfig& server_ssl_config,
97       const SSLConfig& proxy_ssl_config,
98       HttpStreamRequest::Delegate* delegate,
99       WebSocketHandshakeStreamBase::CreateHelper* create_helper,
100       bool enable_ip_based_pooling,
101       bool enable_alternative_services,
102       const NetLogWithSource& net_log);
103 
104   // Request a BidirectionalStreamImpl.
105   // Will call delegate->OnBidirectionalStreamImplReady on successful
106   // completion.
107   // TODO(https://crbug.com/836823): This method is virtual to avoid cronet_test
108   // failure on iOS that is caused by Network Thread TLS getting the wrong slot.
109   virtual std::unique_ptr<HttpStreamRequest> RequestBidirectionalStreamImpl(
110       const HttpRequestInfo& info,
111       RequestPriority priority,
112       const SSLConfig& server_ssl_config,
113       const SSLConfig& proxy_ssl_config,
114       HttpStreamRequest::Delegate* delegate,
115       bool enable_ip_based_pooling,
116       bool enable_alternative_services,
117       const NetLogWithSource& net_log);
118 
119   // Requests that enough connections for |num_streams| be opened.
120   void PreconnectStreams(int num_streams, HttpRequestInfo& info);
121 
122   const HostMappingRules* GetHostMappingRules() const;
123 
124  private:
125   FRIEND_TEST_ALL_PREFIXES(HttpStreamRequestTest, SetPriority);
126 
127   friend class HttpStreamFactoryPeer;
128 
129   using JobControllerSet =
130       std::set<std::unique_ptr<JobController>, base::UniquePtrComparator>;
131 
132   url::SchemeHostPort RewriteHost(const url::SchemeHostPort& server);
133 
134   // Values must not be changed or reused.  Keep in sync with identically named
135   // enum in histograms.xml.
136   enum AlternativeServiceType {
137     NO_ALTERNATIVE_SERVICE = 0,
138     QUIC_SAME_DESTINATION = 1,
139     QUIC_DIFFERENT_DESTINATION = 2,
140     NOT_QUIC_SAME_DESTINATION = 3,
141     NOT_QUIC_DIFFERENT_DESTINATION = 4,
142     MAX_ALTERNATIVE_SERVICE_TYPE
143   };
144 
145   std::unique_ptr<HttpStreamRequest> RequestStreamInternal(
146       const HttpRequestInfo& info,
147       RequestPriority priority,
148       const SSLConfig& server_ssl_config,
149       const SSLConfig& proxy_ssl_config,
150       HttpStreamRequest::Delegate* delegate,
151       WebSocketHandshakeStreamBase::CreateHelper* create_helper,
152       HttpStreamRequest::StreamType stream_type,
153       bool is_websocket,
154       bool enable_ip_based_pooling,
155       bool enable_alternative_services,
156       const NetLogWithSource& net_log);
157 
158   // Called when the Preconnect completes. Used for testing.
OnPreconnectsCompleteInternal()159   virtual void OnPreconnectsCompleteInternal() {}
160 
161   // Called when the JobController finishes service. Delete the JobController
162   // from |job_controller_set_|.
163   void OnJobControllerComplete(JobController* controller);
164 
165   const raw_ptr<HttpNetworkSession> session_;
166 
167   // All Requests/Preconnects are assigned with a JobController to manage
168   // serving Job(s). JobController might outlive Request when Request
169   // is served while there's some working Job left. JobController will be
170   // deleted from |job_controller_set_| when it determines the completion of
171   // its work.
172   JobControllerSet job_controller_set_;
173 
174   // Factory used by job controllers for creating jobs.
175   std::unique_ptr<JobFactory> job_factory_;
176 };
177 
178 }  // namespace net
179 
180 #endif  // NET_HTTP_HTTP_STREAM_FACTORY_H_
181