Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / http / http_stream_factory.cc
blob0f3f21ec9ce2b51b3580d7484103dd94749eb59c
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"
15 #include "url/gurl.h"
17 namespace net {
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.
22 // static
23 bool HttpStreamFactory::spdy_enabled_ = true;
25 HttpStreamFactory::~HttpStreamFactory() {}
27 // static
28 void HttpStreamFactory::ResetStaticSettingsToInit() {
29 spdy_enabled_ = true;
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;
38 int port = 0;
39 double probability = 1;
40 bool is_valid = true;
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(),
46 &probability) ||
47 probability < 0 || probability > 1) {
48 DVLOG(1) << kAlternateProtocolHeader
49 << " header has unrecognizable probability: "
50 << alternate_protocol_values[i];
51 is_valid = false;
52 break;
54 continue;
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;
64 is_valid = false;
65 break;
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];
73 is_valid = false;
74 break;
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];
84 is_valid = false;
85 break;
89 if (!is_valid || protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) {
90 http_server_properties->ClearAlternativeServices(http_host_port_pair);
91 return;
94 HostPortPair host_port(http_host_port_pair);
95 const HostMappingRules* mapping_rules = GetHostMappingRules();
96 if (mapping_rules)
97 mapping_rules->RewriteHost(&host_port);
99 http_server_properties->SetAlternativeService(
100 host_port, AlternativeService(protocol, "", static_cast<uint16>(port)),
101 probability);
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);
115 return url;
118 HttpStreamFactory::HttpStreamFactory() {}
120 } // namespace net