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
3 // found in the LICENSE file.
5 #include "net/http/http_stream_factory.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "net/base/host_mapping_rules.h"
12 #include "net/base/host_port_pair.h"
13 #include "net/http/http_network_session.h"
18 // WARNING: If you modify or add any static flags, you must keep them in sync
19 // with |ResetStaticSettingsToInit|. This is critical for unit test isolation.
22 bool HttpStreamFactory::spdy_enabled_
= true;
24 HttpStreamFactory::~HttpStreamFactory() {}
27 void HttpStreamFactory::ResetStaticSettingsToInit() {
31 void HttpStreamFactory::ProcessAlternateProtocol(
32 const base::WeakPtr
<HttpServerProperties
>& http_server_properties
,
33 const std::vector
<std::string
>& alternate_protocol_values
,
34 const HostPortPair
& http_host_port_pair
,
35 const HttpNetworkSession
& session
) {
36 AlternateProtocol protocol
= UNINITIALIZED_ALTERNATE_PROTOCOL
;
38 double probability
= 1;
40 for (size_t i
= 0; i
< alternate_protocol_values
.size(); ++i
) {
41 const std::string
& alternate_protocol_str
= alternate_protocol_values
[i
];
42 if (StartsWithASCII(alternate_protocol_str
, "p=", true)) {
43 if (!base::StringToDouble(alternate_protocol_str
.substr(2),
45 probability
< 0 || probability
> 1) {
46 DVLOG(1) << kAlternateProtocolHeader
47 << " header has unrecognizable probability: "
48 << alternate_protocol_values
[i
];
55 std::vector
<std::string
> port_protocol_vector
;
56 base::SplitString(alternate_protocol_str
, ':', &port_protocol_vector
);
57 if (port_protocol_vector
.size() != 2) {
58 DVLOG(1) << kAlternateProtocolHeader
59 << " header has too many tokens: "
60 << alternate_protocol_str
;
65 if (!base::StringToInt(port_protocol_vector
[0], &port
) ||
66 port
== 0 || !IsPortValid(port
)) {
67 DVLOG(1) << kAlternateProtocolHeader
68 << " header has unrecognizable port: "
69 << port_protocol_vector
[0];
74 protocol
= AlternateProtocolFromString(port_protocol_vector
[1]);
76 if (IsAlternateProtocolValid(protocol
) &&
77 !session
.IsProtocolEnabled(protocol
)) {
78 DVLOG(1) << kAlternateProtocolHeader
79 << " header has unrecognized protocol: "
80 << port_protocol_vector
[1];
86 if (!is_valid
|| protocol
== UNINITIALIZED_ALTERNATE_PROTOCOL
) {
87 http_server_properties
->ClearAlternativeService(http_host_port_pair
);
91 HostPortPair
host_port(http_host_port_pair
);
92 const HostMappingRules
* mapping_rules
= GetHostMappingRules();
94 mapping_rules
->RewriteHost(&host_port
);
96 http_server_properties
->SetAlternativeService(
97 host_port
, AlternativeService(protocol
, "", static_cast<uint16
>(port
)),
101 GURL
HttpStreamFactory::ApplyHostMappingRules(const GURL
& url
,
102 HostPortPair
* endpoint
) {
103 const HostMappingRules
* mapping_rules
= GetHostMappingRules();
104 if (mapping_rules
&& mapping_rules
->RewriteHost(endpoint
)) {
105 url::Replacements
<char> replacements
;
106 const std::string port_str
= base::IntToString(endpoint
->port());
107 replacements
.SetPort(port_str
.c_str(), url::Component(0, port_str
.size()));
108 replacements
.SetHost(endpoint
->host().c_str(),
109 url::Component(0, endpoint
->host().size()));
110 return url
.ReplaceComponents(replacements
);
115 HttpStreamFactory::HttpStreamFactory() {}