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 #include "net/http/http_stream_factory.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_split.h"
10 #include "net/base/host_mapping_rules.h"
11 #include "net/base/host_port_pair.h"
12 #include "net/http/http_network_session.h"
13 #include "url/gurl.h"
14
15 namespace net {
16
17 // WARNING: If you modify or add any static flags, you must keep them in sync
18 // with |ResetStaticSettingsToInit|. This is critical for unit test isolation.
19
20 // static
21 bool HttpStreamFactory::spdy_enabled_ = true;
22
~HttpStreamFactory()23 HttpStreamFactory::~HttpStreamFactory() {}
24
25 // static
ResetStaticSettingsToInit()26 void HttpStreamFactory::ResetStaticSettingsToInit() {
27 spdy_enabled_ = true;
28 }
29
ProcessAlternateProtocol(const base::WeakPtr<HttpServerProperties> & http_server_properties,const std::string & alternate_protocol_str,const HostPortPair & http_host_port_pair,const HttpNetworkSession & session)30 void HttpStreamFactory::ProcessAlternateProtocol(
31 const base::WeakPtr<HttpServerProperties>& http_server_properties,
32 const std::string& alternate_protocol_str,
33 const HostPortPair& http_host_port_pair,
34 const HttpNetworkSession& session) {
35 std::vector<std::string> port_protocol_vector;
36 base::SplitString(alternate_protocol_str, ':', &port_protocol_vector);
37 if (port_protocol_vector.size() != 2) {
38 DVLOG(1) << kAlternateProtocolHeader
39 << " header has too many tokens: "
40 << alternate_protocol_str;
41 return;
42 }
43
44 int port;
45 if (!base::StringToInt(port_protocol_vector[0], &port) ||
46 port <= 0 || port >= 1 << 16) {
47 DVLOG(1) << kAlternateProtocolHeader
48 << " header has unrecognizable port: "
49 << port_protocol_vector[0];
50 return;
51 }
52
53 AlternateProtocol protocol =
54 AlternateProtocolFromString(port_protocol_vector[1]);
55 if (IsAlternateProtocolValid(protocol) &&
56 !session.IsProtocolEnabled(protocol)) {
57 protocol = ALTERNATE_PROTOCOL_BROKEN;
58 }
59
60 if (protocol == ALTERNATE_PROTOCOL_BROKEN) {
61 DVLOG(1) << kAlternateProtocolHeader
62 << " header has unrecognized protocol: "
63 << port_protocol_vector[1];
64 return;
65 }
66
67 HostPortPair host_port(http_host_port_pair);
68 const HostMappingRules* mapping_rules = GetHostMappingRules();
69 if (mapping_rules)
70 mapping_rules->RewriteHost(&host_port);
71
72 if (http_server_properties->HasAlternateProtocol(host_port)) {
73 const PortAlternateProtocolPair existing_alternate =
74 http_server_properties->GetAlternateProtocol(host_port);
75 // If we think the alternate protocol is broken, don't change it.
76 if (existing_alternate.protocol == ALTERNATE_PROTOCOL_BROKEN)
77 return;
78 }
79
80 http_server_properties->SetAlternateProtocol(host_port, port, protocol);
81 }
82
ApplyHostMappingRules(const GURL & url,HostPortPair * endpoint)83 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url,
84 HostPortPair* endpoint) {
85 const HostMappingRules* mapping_rules = GetHostMappingRules();
86 if (mapping_rules && mapping_rules->RewriteHost(endpoint)) {
87 url::Replacements<char> replacements;
88 const std::string port_str = base::IntToString(endpoint->port());
89 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.size()));
90 replacements.SetHost(endpoint->host().c_str(),
91 url::Component(0, endpoint->host().size()));
92 return url.ReplaceComponents(replacements);
93 }
94 return url;
95 }
96
HttpStreamFactory()97 HttpStreamFactory::HttpStreamFactory() {}
98
99 } // namespace net
100