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