| 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 20 matching lines...) Expand all Loading... |
| 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 url::SchemeHostPort& http_server) { | 40 const url::SchemeHostPort& http_server) { |
| 41 if (session->params().parse_alternative_services) { | 41 if (!headers->HasHeader(kAlternativeServiceHeader)) |
| 42 if (headers->HasHeader(kAlternativeServiceHeader)) { | |
| 43 std::string alternative_service_str; | |
| 44 headers->GetNormalizedHeader(kAlternativeServiceHeader, | |
| 45 &alternative_service_str); | |
| 46 ProcessAlternativeService(session->http_server_properties(), | |
| 47 alternative_service_str, http_server, *session); | |
| 48 } | |
| 49 // If "Alt-Svc" is enabled, then ignore "Alternate-Protocol". | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 if (!headers->HasHeader(kAlternateProtocolHeader)) | |
| 54 return; | 42 return; |
| 55 | 43 |
| 56 std::vector<std::string> alternate_protocol_values; | 44 std::string alternative_service_str; |
| 57 size_t iter = 0; | 45 headers->GetNormalizedHeader(kAlternativeServiceHeader, |
| 58 std::string alternate_protocol_str; | 46 &alternative_service_str); |
| 59 while (headers->EnumerateHeader(&iter, kAlternateProtocolHeader, | |
| 60 &alternate_protocol_str)) { | |
| 61 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL, | |
| 62 &alternate_protocol_str); | |
| 63 if (!alternate_protocol_str.empty()) { | |
| 64 alternate_protocol_values.push_back(alternate_protocol_str); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 ProcessAlternateProtocol(session->http_server_properties(), | |
| 69 alternate_protocol_values, http_server, *session); | |
| 70 } | |
| 71 | |
| 72 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url, | |
| 73 HostPortPair* endpoint) { | |
| 74 const HostMappingRules* mapping_rules = GetHostMappingRules(); | |
| 75 if (mapping_rules && mapping_rules->RewriteHost(endpoint)) { | |
| 76 url::Replacements<char> replacements; | |
| 77 const std::string port_str = base::UintToString(endpoint->port()); | |
| 78 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.size())); | |
| 79 replacements.SetHost(endpoint->host().c_str(), | |
| 80 url::Component(0, endpoint->host().size())); | |
| 81 return url.ReplaceComponents(replacements); | |
| 82 } | |
| 83 return url; | |
| 84 } | |
| 85 | |
| 86 HttpStreamFactory::HttpStreamFactory() {} | |
| 87 | |
| 88 void HttpStreamFactory::ProcessAlternativeService( | |
| 89 const base::WeakPtr<HttpServerProperties>& http_server_properties, | |
| 90 base::StringPiece alternative_service_str, | |
| 91 const url::SchemeHostPort& http_server, | |
| 92 const HttpNetworkSession& session) { | |
| 93 SpdyAltSvcWireFormat::AlternativeServiceVector alternative_service_vector; | 47 SpdyAltSvcWireFormat::AlternativeServiceVector alternative_service_vector; |
| 94 if (!SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 48 if (!SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 95 alternative_service_str, &alternative_service_vector)) { | 49 alternative_service_str, &alternative_service_vector)) { |
| 96 return; | 50 return; |
| 97 } | 51 } |
| 98 | 52 |
| 99 // Convert SpdyAltSvcWireFormat::AlternativeService entries | 53 // Convert SpdyAltSvcWireFormat::AlternativeService entries |
| 100 // to net::AlternativeServiceInfo. | 54 // to net::AlternativeServiceInfo. |
| 101 AlternativeServiceInfoVector alternative_service_info_vector; | 55 AlternativeServiceInfoVector alternative_service_info_vector; |
| 102 for (const SpdyAltSvcWireFormat::AlternativeService& | 56 for (const SpdyAltSvcWireFormat::AlternativeService& |
| 103 alternative_service_entry : alternative_service_vector) { | 57 alternative_service_entry : alternative_service_vector) { |
| 104 AlternateProtocol protocol = | 58 AlternateProtocol protocol = |
| 105 AlternateProtocolFromString(alternative_service_entry.protocol_id); | 59 AlternateProtocolFromString(alternative_service_entry.protocol_id); |
| 106 if (!IsAlternateProtocolValid(protocol) || | 60 if (!IsAlternateProtocolValid(protocol) || |
| 107 !session.IsProtocolEnabled(protocol) || | 61 !session->IsProtocolEnabled(protocol) || |
| 108 !IsPortValid(alternative_service_entry.port)) { | 62 !IsPortValid(alternative_service_entry.port)) { |
| 109 continue; | 63 continue; |
| 110 } | 64 } |
| 111 // Check if QUIC version is supported. | 65 // Check if QUIC version is supported. |
| 112 if (protocol == QUIC && !alternative_service_entry.version.empty()) { | 66 if (protocol == QUIC && !alternative_service_entry.version.empty()) { |
| 113 bool match_found = false; | 67 bool match_found = false; |
| 114 for (QuicVersion supported : session.params().quic_supported_versions) { | 68 for (QuicVersion supported : session->params().quic_supported_versions) { |
| 115 for (uint16_t advertised : alternative_service_entry.version) { | 69 for (uint16_t advertised : alternative_service_entry.version) { |
| 116 if (supported == advertised) { | 70 if (supported == advertised) { |
| 117 match_found = true; | 71 match_found = true; |
| 118 break; | 72 break; |
| 119 } | 73 } |
| 120 } | 74 } |
| 121 if (match_found) { | 75 if (match_found) { |
| 122 break; | 76 break; |
| 123 } | 77 } |
| 124 } | 78 } |
| 125 if (!match_found) { | 79 if (!match_found) { |
| 126 continue; | 80 continue; |
| 127 } | 81 } |
| 128 } | 82 } |
| 129 AlternativeService alternative_service(protocol, | 83 AlternativeService alternative_service(protocol, |
| 130 alternative_service_entry.host, | 84 alternative_service_entry.host, |
| 131 alternative_service_entry.port); | 85 alternative_service_entry.port); |
| 132 base::Time expiration = | 86 base::Time expiration = |
| 133 base::Time::Now() + | 87 base::Time::Now() + |
| 134 base::TimeDelta::FromSeconds(alternative_service_entry.max_age); | 88 base::TimeDelta::FromSeconds(alternative_service_entry.max_age); |
| 135 AlternativeServiceInfo alternative_service_info(alternative_service, | 89 AlternativeServiceInfo alternative_service_info(alternative_service, |
| 136 expiration); | 90 expiration); |
| 137 alternative_service_info_vector.push_back(alternative_service_info); | 91 alternative_service_info_vector.push_back(alternative_service_info); |
| 138 } | 92 } |
| 139 | 93 |
| 140 http_server_properties->SetAlternativeServices( | 94 session->http_server_properties()->SetAlternativeServices( |
| 141 RewriteHost(http_server), alternative_service_info_vector); | 95 RewriteHost(http_server), alternative_service_info_vector); |
| 142 } | 96 } |
| 143 | 97 |
| 144 void HttpStreamFactory::ProcessAlternateProtocol( | 98 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url, |
| 145 const base::WeakPtr<HttpServerProperties>& http_server_properties, | 99 HostPortPair* endpoint) { |
| 146 const std::vector<std::string>& alternate_protocol_values, | 100 const HostMappingRules* mapping_rules = GetHostMappingRules(); |
| 147 const url::SchemeHostPort& http_server, | 101 if (mapping_rules && mapping_rules->RewriteHost(endpoint)) { |
| 148 const HttpNetworkSession& session) { | 102 url::Replacements<char> replacements; |
| 149 AlternateProtocol protocol = UNINITIALIZED_ALTERNATE_PROTOCOL; | 103 const std::string port_str = base::UintToString(endpoint->port()); |
| 150 int port = 0; | 104 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.size())); |
| 151 bool is_valid = true; | 105 replacements.SetHost(endpoint->host().c_str(), |
| 152 for (size_t i = 0; i < alternate_protocol_values.size(); ++i) { | 106 url::Component(0, endpoint->host().size())); |
| 153 base::StringPiece alternate_protocol_str = alternate_protocol_values[i]; | 107 return url.ReplaceComponents(replacements); |
| 154 if (base::StartsWith(alternate_protocol_str, "p=", | 108 } |
| 155 base::CompareCase::SENSITIVE)) { | 109 return url; |
| 156 // Ignore deprecated probability. | 110 } |
| 157 continue; | |
| 158 } | |
| 159 std::vector<base::StringPiece> port_protocol_vector = | |
| 160 base::SplitStringPiece(alternate_protocol_str, ":", | |
| 161 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 162 if (port_protocol_vector.size() != 2) { | |
| 163 DVLOG(1) << kAlternateProtocolHeader | |
| 164 << " header has too many tokens: " | |
| 165 << alternate_protocol_str; | |
| 166 is_valid = false; | |
| 167 break; | |
| 168 } | |
| 169 | 111 |
| 170 if (!ParseInt32(port_protocol_vector[0], ParseIntFormat::NON_NEGATIVE, | 112 HttpStreamFactory::HttpStreamFactory() {} |
| 171 &port) || | |
| 172 port == 0 || !IsPortValid(port)) { | |
| 173 DVLOG(1) << kAlternateProtocolHeader | |
| 174 << " header has unrecognizable port: " | |
| 175 << port_protocol_vector[0]; | |
| 176 is_valid = false; | |
| 177 break; | |
| 178 } | |
| 179 | |
| 180 protocol = AlternateProtocolFromString(port_protocol_vector[1].as_string()); | |
| 181 | |
| 182 if (IsAlternateProtocolValid(protocol) && | |
| 183 !session.IsProtocolEnabled(protocol)) { | |
| 184 DVLOG(1) << kAlternateProtocolHeader | |
| 185 << " header has unrecognized protocol: " | |
| 186 << port_protocol_vector[1]; | |
| 187 is_valid = false; | |
| 188 break; | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 if (!is_valid || protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) { | |
| 193 http_server_properties->ClearAlternativeServices(http_server); | |
| 194 return; | |
| 195 } | |
| 196 | |
| 197 http_server_properties->SetAlternativeService( | |
| 198 RewriteHost(http_server), | |
| 199 AlternativeService(protocol, "", static_cast<uint16_t>(port)), | |
| 200 base::Time::Now() + base::TimeDelta::FromDays(30)); | |
| 201 } | |
| 202 | 113 |
| 203 url::SchemeHostPort HttpStreamFactory::RewriteHost( | 114 url::SchemeHostPort HttpStreamFactory::RewriteHost( |
| 204 const url::SchemeHostPort& server) { | 115 const url::SchemeHostPort& server) { |
| 205 HostPortPair host_port_pair(server.host(), server.port()); | 116 HostPortPair host_port_pair(server.host(), server.port()); |
| 206 const HostMappingRules* mapping_rules = GetHostMappingRules(); | 117 const HostMappingRules* mapping_rules = GetHostMappingRules(); |
| 207 if (mapping_rules) | 118 if (mapping_rules) |
| 208 mapping_rules->RewriteHost(&host_port_pair); | 119 mapping_rules->RewriteHost(&host_port_pair); |
| 209 return url::SchemeHostPort(server.scheme(), host_port_pair.host(), | 120 return url::SchemeHostPort(server.scheme(), host_port_pair.host(), |
| 210 host_port_pair.port()); | 121 host_port_pair.port()); |
| 211 } | 122 } |
| 212 | 123 |
| 213 } // namespace net | 124 } // namespace net |
| OLD | NEW |