| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_stream_factory.h" | 5 #include "net/http/http_stream_factory.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" |
| 10 #include "net/base/host_mapping_rules.h" | 11 #include "net/base/host_mapping_rules.h" |
| 11 #include "net/base/host_port_pair.h" | 12 #include "net/base/host_port_pair.h" |
| 12 #include "net/http/http_network_session.h" | 13 #include "net/http/http_network_session.h" |
| 13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 // WARNING: If you modify or add any static flags, you must keep them in sync | 18 // 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 // with |ResetStaticSettingsToInit|. This is critical for unit test isolation. |
| 19 | 20 |
| 20 // static | 21 // static |
| 21 bool HttpStreamFactory::spdy_enabled_ = true; | 22 bool HttpStreamFactory::spdy_enabled_ = true; |
| 22 | 23 |
| 23 HttpStreamFactory::~HttpStreamFactory() {} | 24 HttpStreamFactory::~HttpStreamFactory() {} |
| 24 | 25 |
| 25 // static | 26 // static |
| 26 void HttpStreamFactory::ResetStaticSettingsToInit() { | 27 void HttpStreamFactory::ResetStaticSettingsToInit() { |
| 27 spdy_enabled_ = true; | 28 spdy_enabled_ = true; |
| 28 } | 29 } |
| 29 | 30 |
| 30 void HttpStreamFactory::ProcessAlternateProtocol( | 31 void HttpStreamFactory::ProcessAlternateProtocol( |
| 31 const base::WeakPtr<HttpServerProperties>& http_server_properties, | 32 const base::WeakPtr<HttpServerProperties>& http_server_properties, |
| 32 const std::string& alternate_protocol_str, | 33 const std::vector<std::string>& alternate_protocol_values, |
| 33 const HostPortPair& http_host_port_pair, | 34 const HostPortPair& http_host_port_pair, |
| 34 const HttpNetworkSession& session) { | 35 const HttpNetworkSession& session) { |
| 35 std::vector<std::string> port_protocol_vector; | 36 AlternateProtocol protocol = UNINITIALIZED_ALTERNATE_PROTOCOL; |
| 36 base::SplitString(alternate_protocol_str, ':', &port_protocol_vector); | 37 int port = 0; |
| 37 if (port_protocol_vector.size() != 2) { | 38 double probability = 1; |
| 38 DVLOG(1) << kAlternateProtocolHeader | 39 for (size_t i = 0; i < alternate_protocol_values.size(); ++i) { |
| 39 << " header has too many tokens: " | 40 const std::string& alternate_protocol_str = alternate_protocol_values[i]; |
| 40 << alternate_protocol_str; | 41 if (StartsWithASCII(alternate_protocol_str, "p=", true)) { |
| 41 return; | 42 if (!base::StringToDouble(alternate_protocol_str.substr(2), |
| 43 &probability) || |
| 44 probability < 0 || probability > 1) { |
| 45 DVLOG(1) << kAlternateProtocolHeader |
| 46 << " header has unrecognizable probability: " |
| 47 << alternate_protocol_values[i]; |
| 48 return; |
| 49 } |
| 50 continue; |
| 51 } |
| 52 |
| 53 std::vector<std::string> port_protocol_vector; |
| 54 base::SplitString(alternate_protocol_str, ':', &port_protocol_vector); |
| 55 if (port_protocol_vector.size() != 2) { |
| 56 DVLOG(1) << kAlternateProtocolHeader |
| 57 << " header has too many tokens: " |
| 58 << alternate_protocol_str; |
| 59 return; |
| 60 } |
| 61 |
| 62 if (!base::StringToInt(port_protocol_vector[0], &port) || |
| 63 port <= 0 || port >= 1 << 16) { |
| 64 DVLOG(1) << kAlternateProtocolHeader |
| 65 << " header has unrecognizable port: " |
| 66 << port_protocol_vector[0]; |
| 67 return; |
| 68 } |
| 69 |
| 70 protocol = |
| 71 AlternateProtocolFromString(port_protocol_vector[1]); |
| 72 if (IsAlternateProtocolValid(protocol) && |
| 73 !session.IsProtocolEnabled(protocol)) { |
| 74 protocol = ALTERNATE_PROTOCOL_BROKEN; |
| 75 } |
| 76 |
| 77 if (protocol == ALTERNATE_PROTOCOL_BROKEN) { |
| 78 DVLOG(1) << kAlternateProtocolHeader |
| 79 << " header has unrecognized protocol: " |
| 80 << port_protocol_vector[1]; |
| 81 return; |
| 82 } |
| 42 } | 83 } |
| 43 | 84 |
| 44 int port; | 85 if (protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) |
| 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; | 86 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 | 87 |
| 67 HostPortPair host_port(http_host_port_pair); | 88 HostPortPair host_port(http_host_port_pair); |
| 68 const HostMappingRules* mapping_rules = GetHostMappingRules(); | 89 const HostMappingRules* mapping_rules = GetHostMappingRules(); |
| 69 if (mapping_rules) | 90 if (mapping_rules) |
| 70 mapping_rules->RewriteHost(&host_port); | 91 mapping_rules->RewriteHost(&host_port); |
| 71 | 92 |
| 72 if (http_server_properties->HasAlternateProtocol(host_port)) { | 93 if (http_server_properties->HasAlternateProtocol(host_port)) { |
| 73 const PortAlternateProtocolPair existing_alternate = | 94 const AlternateProtocolInfo existing_alternate = |
| 74 http_server_properties->GetAlternateProtocol(host_port); | 95 http_server_properties->GetAlternateProtocol(host_port); |
| 75 // If we think the alternate protocol is broken, don't change it. | 96 // If we think the alternate protocol is broken, don't change it. |
| 76 if (existing_alternate.protocol == ALTERNATE_PROTOCOL_BROKEN) | 97 if (existing_alternate.protocol == ALTERNATE_PROTOCOL_BROKEN) |
| 77 return; | 98 return; |
| 78 } | 99 } |
| 79 | 100 |
| 80 http_server_properties->SetAlternateProtocol(host_port, port, protocol); | 101 http_server_properties->SetAlternateProtocol(host_port, port, protocol, |
| 102 probability); |
| 81 } | 103 } |
| 82 | 104 |
| 83 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url, | 105 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url, |
| 84 HostPortPair* endpoint) { | 106 HostPortPair* endpoint) { |
| 85 const HostMappingRules* mapping_rules = GetHostMappingRules(); | 107 const HostMappingRules* mapping_rules = GetHostMappingRules(); |
| 86 if (mapping_rules && mapping_rules->RewriteHost(endpoint)) { | 108 if (mapping_rules && mapping_rules->RewriteHost(endpoint)) { |
| 87 url::Replacements<char> replacements; | 109 url::Replacements<char> replacements; |
| 88 const std::string port_str = base::IntToString(endpoint->port()); | 110 const std::string port_str = base::IntToString(endpoint->port()); |
| 89 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.size())); | 111 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.size())); |
| 90 replacements.SetHost(endpoint->host().c_str(), | 112 replacements.SetHost(endpoint->host().c_str(), |
| 91 url::Component(0, endpoint->host().size())); | 113 url::Component(0, endpoint->host().size())); |
| 92 return url.ReplaceComponents(replacements); | 114 return url.ReplaceComponents(replacements); |
| 93 } | 115 } |
| 94 return url; | 116 return url; |
| 95 } | 117 } |
| 96 | 118 |
| 97 HttpStreamFactory::HttpStreamFactory() {} | 119 HttpStreamFactory::HttpStreamFactory() {} |
| 98 | 120 |
| 99 } // namespace net | 121 } // namespace net |
| OLD | NEW |