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/base/port_util.h"
14 #include "net/http/http_network_session.h"
19 // WARNING: If you modify or add any static flags, you must keep them in sync
20 // with |ResetStaticSettingsToInit|. This is critical for unit test isolation.
23 bool HttpStreamFactory::spdy_enabled_
= true;
25 HttpStreamFactory::~HttpStreamFactory() {}
28 void HttpStreamFactory::ResetStaticSettingsToInit() {
32 void HttpStreamFactory::ProcessAlternateProtocol(
33 const base::WeakPtr
<HttpServerProperties
>& http_server_properties
,
34 const std::vector
<std::string
>& alternate_protocol_values
,
35 const HostPortPair
& http_host_port_pair
,
36 const HttpNetworkSession
& session
) {
37 AlternateProtocol protocol
= UNINITIALIZED_ALTERNATE_PROTOCOL
;
39 double probability
= 1;
41 for (size_t i
= 0; i
< alternate_protocol_values
.size(); ++i
) {
42 base::StringPiece alternate_protocol_str
= alternate_protocol_values
[i
];
43 if (base::StartsWith(alternate_protocol_str
, "p=",
44 base::CompareCase::SENSITIVE
)) {
45 if (!base::StringToDouble(alternate_protocol_str
.substr(2).as_string(),
47 probability
< 0 || probability
> 1) {
48 DVLOG(1) << kAlternateProtocolHeader
49 << " header has unrecognizable probability: "
50 << alternate_protocol_values
[i
];
57 std::vector
<base::StringPiece
> port_protocol_vector
=
58 base::SplitStringPiece(alternate_protocol_str
, ":",
59 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
60 if (port_protocol_vector
.size() != 2) {
61 DVLOG(1) << kAlternateProtocolHeader
62 << " header has too many tokens: "
63 << alternate_protocol_str
;
68 if (!base::StringToInt(port_protocol_vector
[0], &port
) ||
69 port
== 0 || !IsPortValid(port
)) {
70 DVLOG(1) << kAlternateProtocolHeader
71 << " header has unrecognizable port: "
72 << port_protocol_vector
[0];
77 protocol
= AlternateProtocolFromString(port_protocol_vector
[1].as_string());
79 if (IsAlternateProtocolValid(protocol
) &&
80 !session
.IsProtocolEnabled(protocol
)) {
81 DVLOG(1) << kAlternateProtocolHeader
82 << " header has unrecognized protocol: "
83 << port_protocol_vector
[1];
89 if (!is_valid
|| protocol
== UNINITIALIZED_ALTERNATE_PROTOCOL
) {
90 http_server_properties
->ClearAlternativeServices(http_host_port_pair
);
94 HostPortPair
host_port(http_host_port_pair
);
95 const HostMappingRules
* mapping_rules
= GetHostMappingRules();
97 mapping_rules
->RewriteHost(&host_port
);
99 http_server_properties
->SetAlternativeService(
100 host_port
, AlternativeService(protocol
, "", static_cast<uint16
>(port
)),
104 GURL
HttpStreamFactory::ApplyHostMappingRules(const GURL
& url
,
105 HostPortPair
* endpoint
) {
106 const HostMappingRules
* mapping_rules
= GetHostMappingRules();
107 if (mapping_rules
&& mapping_rules
->RewriteHost(endpoint
)) {
108 url::Replacements
<char> replacements
;
109 const std::string port_str
= base::UintToString(endpoint
->port());
110 replacements
.SetPort(port_str
.c_str(), url::Component(0, port_str
.size()));
111 replacements
.SetHost(endpoint
->host().c_str(),
112 url::Component(0, endpoint
->host().size()));
113 return url
.ReplaceComponents(replacements
);
118 HttpStreamFactory::HttpStreamFactory() {}