| 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 "base/strings/string_util.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 HttpStreamFactory::~HttpStreamFactory() {} | 29 HttpStreamFactory::~HttpStreamFactory() {} |
| 30 | 30 |
| 31 // static | 31 // static |
| 32 void HttpStreamFactory::ResetStaticSettingsToInit() { | 32 void HttpStreamFactory::ResetStaticSettingsToInit() { |
| 33 spdy_enabled_ = true; | 33 spdy_enabled_ = true; |
| 34 } | 34 } |
| 35 | 35 |
| 36 void HttpStreamFactory::ProcessAlternativeServices( | 36 void HttpStreamFactory::ProcessAlternativeServices( |
| 37 HttpNetworkSession* session, | 37 HttpNetworkSession* session, |
| 38 const HttpResponseHeaders* headers, | 38 const HttpResponseHeaders* headers, |
| 39 const HostPortPair& http_host_port_pair) { | 39 const url::SchemeHostPort& http_server) { |
| 40 if (session->params().parse_alternative_services) { | 40 if (session->params().parse_alternative_services) { |
| 41 if (headers->HasHeader(kAlternativeServiceHeader)) { | 41 if (headers->HasHeader(kAlternativeServiceHeader)) { |
| 42 std::string alternative_service_str; | 42 std::string alternative_service_str; |
| 43 headers->GetNormalizedHeader(kAlternativeServiceHeader, | 43 headers->GetNormalizedHeader(kAlternativeServiceHeader, |
| 44 &alternative_service_str); | 44 &alternative_service_str); |
| 45 ProcessAlternativeService(session->http_server_properties(), | 45 ProcessAlternativeService(session->http_server_properties(), |
| 46 alternative_service_str, http_host_port_pair, | 46 alternative_service_str, http_server, *session); |
| 47 *session); | |
| 48 } | 47 } |
| 49 // If "Alt-Svc" is enabled, then ignore "Alternate-Protocol". | 48 // If "Alt-Svc" is enabled, then ignore "Alternate-Protocol". |
| 50 return; | 49 return; |
| 51 } | 50 } |
| 52 | 51 |
| 53 if (!headers->HasHeader(kAlternateProtocolHeader)) | 52 if (!headers->HasHeader(kAlternateProtocolHeader)) |
| 54 return; | 53 return; |
| 55 | 54 |
| 56 std::vector<std::string> alternate_protocol_values; | 55 std::vector<std::string> alternate_protocol_values; |
| 57 size_t iter = 0; | 56 size_t iter = 0; |
| 58 std::string alternate_protocol_str; | 57 std::string alternate_protocol_str; |
| 59 while (headers->EnumerateHeader(&iter, kAlternateProtocolHeader, | 58 while (headers->EnumerateHeader(&iter, kAlternateProtocolHeader, |
| 60 &alternate_protocol_str)) { | 59 &alternate_protocol_str)) { |
| 61 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL, | 60 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL, |
| 62 &alternate_protocol_str); | 61 &alternate_protocol_str); |
| 63 if (!alternate_protocol_str.empty()) { | 62 if (!alternate_protocol_str.empty()) { |
| 64 alternate_protocol_values.push_back(alternate_protocol_str); | 63 alternate_protocol_values.push_back(alternate_protocol_str); |
| 65 } | 64 } |
| 66 } | 65 } |
| 67 | 66 |
| 68 ProcessAlternateProtocol(session->http_server_properties(), | 67 ProcessAlternateProtocol(session->http_server_properties(), |
| 69 alternate_protocol_values, http_host_port_pair, | 68 alternate_protocol_values, http_server, *session); |
| 70 *session); | |
| 71 } | 69 } |
| 72 | 70 |
| 73 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url, | 71 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url, |
| 74 HostPortPair* endpoint) { | 72 HostPortPair* endpoint) { |
| 75 const HostMappingRules* mapping_rules = GetHostMappingRules(); | 73 const HostMappingRules* mapping_rules = GetHostMappingRules(); |
| 76 if (mapping_rules && mapping_rules->RewriteHost(endpoint)) { | 74 if (mapping_rules && mapping_rules->RewriteHost(endpoint)) { |
| 77 url::Replacements<char> replacements; | 75 url::Replacements<char> replacements; |
| 78 const std::string port_str = base::UintToString(endpoint->port()); | 76 const std::string port_str = base::UintToString(endpoint->port()); |
| 79 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.size())); | 77 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.size())); |
| 80 replacements.SetHost(endpoint->host().c_str(), | 78 replacements.SetHost(endpoint->host().c_str(), |
| 81 url::Component(0, endpoint->host().size())); | 79 url::Component(0, endpoint->host().size())); |
| 82 return url.ReplaceComponents(replacements); | 80 return url.ReplaceComponents(replacements); |
| 83 } | 81 } |
| 84 return url; | 82 return url; |
| 85 } | 83 } |
| 86 | 84 |
| 87 HttpStreamFactory::HttpStreamFactory() {} | 85 HttpStreamFactory::HttpStreamFactory() {} |
| 88 | 86 |
| 89 void HttpStreamFactory::ProcessAlternativeService( | 87 void HttpStreamFactory::ProcessAlternativeService( |
| 90 const base::WeakPtr<HttpServerProperties>& http_server_properties, | 88 const base::WeakPtr<HttpServerProperties>& http_server_properties, |
| 91 base::StringPiece alternative_service_str, | 89 base::StringPiece alternative_service_str, |
| 92 const HostPortPair& http_host_port_pair, | 90 const url::SchemeHostPort& http_server, |
| 93 const HttpNetworkSession& session) { | 91 const HttpNetworkSession& session) { |
| 94 SpdyAltSvcWireFormat::AlternativeServiceVector alternative_service_vector; | 92 SpdyAltSvcWireFormat::AlternativeServiceVector alternative_service_vector; |
| 95 if (!SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 93 if (!SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 96 alternative_service_str, &alternative_service_vector)) { | 94 alternative_service_str, &alternative_service_vector)) { |
| 97 return; | 95 return; |
| 98 } | 96 } |
| 99 | 97 |
| 100 // Convert SpdyAltSvcWireFormat::AlternativeService entries | 98 // Convert SpdyAltSvcWireFormat::AlternativeService entries |
| 101 // to net::AlternativeServiceInfo. | 99 // to net::AlternativeServiceInfo. |
| 102 AlternativeServiceInfoVector alternative_service_info_vector; | 100 AlternativeServiceInfoVector alternative_service_info_vector; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 132 alternative_service_entry.port); | 130 alternative_service_entry.port); |
| 133 base::Time expiration = | 131 base::Time expiration = |
| 134 base::Time::Now() + | 132 base::Time::Now() + |
| 135 base::TimeDelta::FromSeconds(alternative_service_entry.max_age); | 133 base::TimeDelta::FromSeconds(alternative_service_entry.max_age); |
| 136 AlternativeServiceInfo alternative_service_info(alternative_service, | 134 AlternativeServiceInfo alternative_service_info(alternative_service, |
| 137 expiration); | 135 expiration); |
| 138 alternative_service_info_vector.push_back(alternative_service_info); | 136 alternative_service_info_vector.push_back(alternative_service_info); |
| 139 } | 137 } |
| 140 | 138 |
| 141 http_server_properties->SetAlternativeServices( | 139 http_server_properties->SetAlternativeServices( |
| 142 RewriteHost(http_host_port_pair), alternative_service_info_vector); | 140 RewriteHost(http_server), alternative_service_info_vector); |
| 143 } | 141 } |
| 144 | 142 |
| 145 void HttpStreamFactory::ProcessAlternateProtocol( | 143 void HttpStreamFactory::ProcessAlternateProtocol( |
| 146 const base::WeakPtr<HttpServerProperties>& http_server_properties, | 144 const base::WeakPtr<HttpServerProperties>& http_server_properties, |
| 147 const std::vector<std::string>& alternate_protocol_values, | 145 const std::vector<std::string>& alternate_protocol_values, |
| 148 const HostPortPair& http_host_port_pair, | 146 const url::SchemeHostPort& http_server, |
| 149 const HttpNetworkSession& session) { | 147 const HttpNetworkSession& session) { |
| 150 AlternateProtocol protocol = UNINITIALIZED_ALTERNATE_PROTOCOL; | 148 AlternateProtocol protocol = UNINITIALIZED_ALTERNATE_PROTOCOL; |
| 151 int port = 0; | 149 int port = 0; |
| 152 bool is_valid = true; | 150 bool is_valid = true; |
| 153 for (size_t i = 0; i < alternate_protocol_values.size(); ++i) { | 151 for (size_t i = 0; i < alternate_protocol_values.size(); ++i) { |
| 154 base::StringPiece alternate_protocol_str = alternate_protocol_values[i]; | 152 base::StringPiece alternate_protocol_str = alternate_protocol_values[i]; |
| 155 if (base::StartsWith(alternate_protocol_str, "p=", | 153 if (base::StartsWith(alternate_protocol_str, "p=", |
| 156 base::CompareCase::SENSITIVE)) { | 154 base::CompareCase::SENSITIVE)) { |
| 157 // Ignore deprecated probability. | 155 // Ignore deprecated probability. |
| 158 continue; | 156 continue; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 183 !session.IsProtocolEnabled(protocol)) { | 181 !session.IsProtocolEnabled(protocol)) { |
| 184 DVLOG(1) << kAlternateProtocolHeader | 182 DVLOG(1) << kAlternateProtocolHeader |
| 185 << " header has unrecognized protocol: " | 183 << " header has unrecognized protocol: " |
| 186 << port_protocol_vector[1]; | 184 << port_protocol_vector[1]; |
| 187 is_valid = false; | 185 is_valid = false; |
| 188 break; | 186 break; |
| 189 } | 187 } |
| 190 } | 188 } |
| 191 | 189 |
| 192 if (!is_valid || protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) { | 190 if (!is_valid || protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) { |
| 193 http_server_properties->ClearAlternativeServices(http_host_port_pair); | 191 http_server_properties->ClearAlternativeServices(http_server); |
| 194 return; | 192 return; |
| 195 } | 193 } |
| 196 | 194 |
| 197 http_server_properties->SetAlternativeService( | 195 http_server_properties->SetAlternativeService( |
| 198 RewriteHost(http_host_port_pair), | 196 RewriteHost(http_server), |
| 199 AlternativeService(protocol, "", static_cast<uint16_t>(port)), | 197 AlternativeService(protocol, "", static_cast<uint16_t>(port)), |
| 200 base::Time::Now() + base::TimeDelta::FromDays(30)); | 198 base::Time::Now() + base::TimeDelta::FromDays(30)); |
| 201 } | 199 } |
| 202 | 200 |
| 203 HostPortPair HttpStreamFactory::RewriteHost(HostPortPair host_port_pair) { | 201 url::SchemeHostPort HttpStreamFactory::RewriteHost( |
| 202 const url::SchemeHostPort& server) { |
| 203 HostPortPair host_port_pair(server.host(), server.port()); |
| 204 const HostMappingRules* mapping_rules = GetHostMappingRules(); | 204 const HostMappingRules* mapping_rules = GetHostMappingRules(); |
| 205 if (mapping_rules) | 205 if (mapping_rules) |
| 206 mapping_rules->RewriteHost(&host_port_pair); | 206 mapping_rules->RewriteHost(&host_port_pair); |
| 207 return host_port_pair; | 207 return url::SchemeHostPort(server.scheme(), host_port_pair.host(), |
| 208 host_port_pair.port()); |
| 208 } | 209 } |
| 209 | 210 |
| 210 } // namespace net | 211 } // namespace net |
| OLD | NEW |