1 // Copyright (c) 2011 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/string_number_conversions.h"
9 #include "base/string_split.h"
10 #include "googleurl/src/gurl.h"
11 #include "net/base/host_mapping_rules.h"
12 #include "net/base/host_port_pair.h"
13 #include "net/http/http_alternate_protocols.h"
14
15 namespace net {
16
17 // static
18 const HostMappingRules* HttpStreamFactory::host_mapping_rules_ = NULL;
19 // static
20 const std::string* HttpStreamFactory::next_protos_ = NULL;
21 // static
22 bool HttpStreamFactory::spdy_enabled_ = true;
23 // static
24 bool HttpStreamFactory::use_alternate_protocols_ = false;
25 // static
26 bool HttpStreamFactory::force_spdy_over_ssl_ = true;
27 // static
28 bool HttpStreamFactory::force_spdy_always_ = false;
29 // static
30 std::list<HostPortPair>* HttpStreamFactory::forced_spdy_exclusions_ = NULL;
31 // static
32 bool HttpStreamFactory::ignore_certificate_errors_ = false;
33
~HttpStreamFactory()34 HttpStreamFactory::~HttpStreamFactory() {}
35
ProcessAlternateProtocol(HttpAlternateProtocols * alternate_protocols,const std::string & alternate_protocol_str,const HostPortPair & http_host_port_pair)36 void HttpStreamFactory::ProcessAlternateProtocol(
37 HttpAlternateProtocols* alternate_protocols,
38 const std::string& alternate_protocol_str,
39 const HostPortPair& http_host_port_pair) {
40 std::vector<std::string> port_protocol_vector;
41 base::SplitString(alternate_protocol_str, ':', &port_protocol_vector);
42 if (port_protocol_vector.size() != 2) {
43 DLOG(WARNING) << HttpAlternateProtocols::kHeader
44 << " header has too many tokens: "
45 << alternate_protocol_str;
46 return;
47 }
48
49 int port;
50 if (!base::StringToInt(port_protocol_vector[0], &port) ||
51 port <= 0 || port >= 1 << 16) {
52 DLOG(WARNING) << HttpAlternateProtocols::kHeader
53 << " header has unrecognizable port: "
54 << port_protocol_vector[0];
55 return;
56 }
57
58 HttpAlternateProtocols::Protocol protocol = HttpAlternateProtocols::BROKEN;
59 // We skip NPN_SPDY_1 here, because we've rolled the protocol version to 2.
60 for (int i = HttpAlternateProtocols::NPN_SPDY_2;
61 i < HttpAlternateProtocols::NUM_ALTERNATE_PROTOCOLS; ++i) {
62 if (port_protocol_vector[1] == HttpAlternateProtocols::kProtocolStrings[i])
63 protocol = static_cast<HttpAlternateProtocols::Protocol>(i);
64 }
65
66 if (protocol == HttpAlternateProtocols::BROKEN) {
67 // Currently, we only recognize the npn-spdy protocol.
68 DLOG(WARNING) << HttpAlternateProtocols::kHeader
69 << " header has unrecognized protocol: "
70 << port_protocol_vector[1];
71 return;
72 }
73
74 HostPortPair host_port(http_host_port_pair);
75 host_mapping_rules().RewriteHost(&host_port);
76
77 if (alternate_protocols->HasAlternateProtocolFor(host_port)) {
78 const HttpAlternateProtocols::PortProtocolPair existing_alternate =
79 alternate_protocols->GetAlternateProtocolFor(host_port);
80 // If we think the alternate protocol is broken, don't change it.
81 if (existing_alternate.protocol == HttpAlternateProtocols::BROKEN)
82 return;
83 }
84
85 alternate_protocols->SetAlternateProtocolFor(host_port, port, protocol);
86 }
87
ApplyHostMappingRules(const GURL & url,HostPortPair * endpoint)88 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url,
89 HostPortPair* endpoint) {
90 if (host_mapping_rules().RewriteHost(endpoint)) {
91 url_canon::Replacements<char> replacements;
92 const std::string port_str = base::IntToString(endpoint->port());
93 replacements.SetPort(port_str.c_str(),
94 url_parse::Component(0, port_str.size()));
95 replacements.SetHost(endpoint->host().c_str(),
96 url_parse::Component(0, endpoint->host().size()));
97 return url.ReplaceComponents(replacements);
98 }
99 return url;
100 }
101
102 // static
add_forced_spdy_exclusion(const std::string & value)103 void HttpStreamFactory::add_forced_spdy_exclusion(const std::string& value) {
104 HostPortPair pair = HostPortPair::FromURL(GURL(value));
105 if (!forced_spdy_exclusions_)
106 forced_spdy_exclusions_ = new std::list<HostPortPair>();
107 forced_spdy_exclusions_->push_back(pair);
108 }
109
110 // static
HasSpdyExclusion(const HostPortPair & endpoint)111 bool HttpStreamFactory::HasSpdyExclusion(const HostPortPair& endpoint) {
112 std::list<HostPortPair>* exclusions = forced_spdy_exclusions_;
113 if (!exclusions)
114 return false;
115
116 std::list<HostPortPair>::const_iterator it;
117 for (it = exclusions->begin(); it != exclusions->end(); ++it)
118 if (it->Equals(endpoint))
119 return true;
120 return false;
121 }
122
123 // static
SetHostMappingRules(const std::string & rules)124 void HttpStreamFactory::SetHostMappingRules(const std::string& rules) {
125 HostMappingRules* host_mapping_rules = new HostMappingRules;
126 host_mapping_rules->SetRulesFromString(rules);
127 delete host_mapping_rules_;
128 host_mapping_rules_ = host_mapping_rules;
129 }
130
HttpStreamFactory()131 HttpStreamFactory::HttpStreamFactory() {}
132
133 // static
host_mapping_rules()134 const HostMappingRules& HttpStreamFactory::host_mapping_rules() {
135 if (!host_mapping_rules_)
136 host_mapping_rules_ = new HostMappingRules;
137 return *host_mapping_rules_;
138 }
139
140 } // namespace net
141