Home | History | Annotate | Download | only in http
      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.
      4 
      5 #include "net/http/http_stream_factory.h"
      6 
      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"
     14 #include "url/gurl.h"
     15 
     16 namespace net {
     17 
     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.
     20 
     21 // static
     22 bool HttpStreamFactory::spdy_enabled_ = true;
     23 
     24 HttpStreamFactory::~HttpStreamFactory() {}
     25 
     26 // static
     27 void HttpStreamFactory::ResetStaticSettingsToInit() {
     28   spdy_enabled_ = true;
     29 }
     30 
     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;
     37   int port = 0;
     38   double probability = 1;
     39   for (size_t i = 0; i < alternate_protocol_values.size(); ++i) {
     40     const std::string& alternate_protocol_str = alternate_protocol_values[i];
     41     if (StartsWithASCII(alternate_protocol_str, "p=", true)) {
     42       if (!base::StringToDouble(alternate_protocol_str.substr(2),
     43                                 &probability) ||
     44           probability < 0 || probability > 1) {
     45         DVLOG(1) << kAlternateProtocolHeader
     46                  << " header has unrecognizable probability: "
     47                  << alternate_protocol_values[i];
     48         return;
     49       }
     50       continue;
     51     }
     52 
     53     std::vector<std::string> port_protocol_vector;
     54     base::SplitString(alternate_protocol_str, ':', &port_protocol_vector);
     55     if (port_protocol_vector.size() != 2) {
     56       DVLOG(1) << kAlternateProtocolHeader
     57                << " header has too many tokens: "
     58                << alternate_protocol_str;
     59       return;
     60     }
     61 
     62     if (!base::StringToInt(port_protocol_vector[0], &port) ||
     63         port <= 0 || port >= 1 << 16) {
     64       DVLOG(1) << kAlternateProtocolHeader
     65                << " header has unrecognizable port: "
     66                << port_protocol_vector[0];
     67       return;
     68     }
     69 
     70     protocol =
     71         AlternateProtocolFromString(port_protocol_vector[1]);
     72     if (IsAlternateProtocolValid(protocol) &&
     73         !session.IsProtocolEnabled(protocol)) {
     74       protocol = ALTERNATE_PROTOCOL_BROKEN;
     75     }
     76 
     77     if (protocol == ALTERNATE_PROTOCOL_BROKEN) {
     78       DVLOG(1) << kAlternateProtocolHeader
     79                << " header has unrecognized protocol: "
     80                << port_protocol_vector[1];
     81       return;
     82     }
     83   }
     84 
     85   if (protocol == UNINITIALIZED_ALTERNATE_PROTOCOL)
     86     return;
     87 
     88   HostPortPair host_port(http_host_port_pair);
     89   const HostMappingRules* mapping_rules = GetHostMappingRules();
     90   if (mapping_rules)
     91     mapping_rules->RewriteHost(&host_port);
     92 
     93   if (http_server_properties->HasAlternateProtocol(host_port)) {
     94     const AlternateProtocolInfo existing_alternate =
     95         http_server_properties->GetAlternateProtocol(host_port);
     96     // If we think the alternate protocol is broken, don't change it.
     97     if (existing_alternate.protocol == ALTERNATE_PROTOCOL_BROKEN)
     98       return;
     99   }
    100 
    101   http_server_properties->SetAlternateProtocol(host_port, port, protocol,
    102                                                probability);
    103 }
    104 
    105 GURL HttpStreamFactory::ApplyHostMappingRules(const GURL& url,
    106                                               HostPortPair* endpoint) {
    107   const HostMappingRules* mapping_rules = GetHostMappingRules();
    108   if (mapping_rules && mapping_rules->RewriteHost(endpoint)) {
    109     url::Replacements<char> replacements;
    110     const std::string port_str = base::IntToString(endpoint->port());
    111     replacements.SetPort(port_str.c_str(), url::Component(0, port_str.size()));
    112     replacements.SetHost(endpoint->host().c_str(),
    113                          url::Component(0, endpoint->host().size()));
    114     return url.ReplaceComponents(replacements);
    115   }
    116   return url;
    117 }
    118 
    119 HttpStreamFactory::HttpStreamFactory() {}
    120 
    121 }  // namespace net
    122