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_server_properties.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/strings/stringprintf.h"
      9 
     10 namespace net {
     11 
     12 const char kAlternateProtocolHeader[] = "Alternate-Protocol";
     13 
     14 namespace {
     15 
     16 // The order of these strings much match the order of the enum definition
     17 // for AlternateProtocol.
     18 const char* const kAlternateProtocolStrings[] = {
     19   "npn-spdy/2",
     20   "npn-spdy/3",
     21   "npn-spdy/3.1",
     22   "npn-spdy/4a2",
     23   "npn-HTTP-draft-04/2.0",
     24   "quic"
     25 };
     26 const char kBrokenAlternateProtocol[] = "Broken";
     27 
     28 COMPILE_ASSERT(
     29     arraysize(kAlternateProtocolStrings) == NUM_VALID_ALTERNATE_PROTOCOLS,
     30     kAlternateProtocolStringsSize_kNumValidAlternateProtocols_not_equal);
     31 
     32 }  // namespace
     33 
     34 bool IsAlternateProtocolValid(AlternateProtocol protocol) {
     35   return protocol >= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION &&
     36       protocol <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION;
     37 }
     38 
     39 const char* AlternateProtocolToString(AlternateProtocol protocol) {
     40   switch (protocol) {
     41     case DEPRECATED_NPN_SPDY_2:
     42     case NPN_SPDY_3:
     43     case NPN_SPDY_3_1:
     44     case NPN_SPDY_4A2:
     45     case NPN_HTTP2_DRAFT_04:
     46     case QUIC:
     47       DCHECK(IsAlternateProtocolValid(protocol));
     48       return kAlternateProtocolStrings[
     49           protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION];
     50     case ALTERNATE_PROTOCOL_BROKEN:
     51       return kBrokenAlternateProtocol;
     52     case UNINITIALIZED_ALTERNATE_PROTOCOL:
     53       return "Uninitialized";
     54   }
     55   NOTREACHED();
     56   return "";
     57 }
     58 
     59 AlternateProtocol AlternateProtocolFromString(const std::string& str) {
     60   for (int i = ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION;
     61        i <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; ++i) {
     62     AlternateProtocol protocol = static_cast<AlternateProtocol>(i);
     63     if (str == AlternateProtocolToString(protocol))
     64       return protocol;
     65   }
     66   if (str == kBrokenAlternateProtocol)
     67     return ALTERNATE_PROTOCOL_BROKEN;
     68   return UNINITIALIZED_ALTERNATE_PROTOCOL;
     69 }
     70 
     71 AlternateProtocol AlternateProtocolFromNextProto(NextProto next_proto) {
     72   switch (next_proto) {
     73     case kProtoDeprecatedSPDY2:
     74       return DEPRECATED_NPN_SPDY_2;
     75     case kProtoSPDY3:
     76       return NPN_SPDY_3;
     77     case kProtoSPDY31:
     78       return NPN_SPDY_3_1;
     79     case kProtoSPDY4a2:
     80       return NPN_SPDY_4A2;
     81     case kProtoHTTP2Draft04:
     82       return NPN_HTTP2_DRAFT_04;
     83     case kProtoQUIC1SPDY3:
     84       return QUIC;
     85 
     86     case kProtoUnknown:
     87     case kProtoHTTP11:
     88       break;
     89   }
     90 
     91   NOTREACHED() << "Invalid NextProto: " << next_proto;
     92   return UNINITIALIZED_ALTERNATE_PROTOCOL;
     93 }
     94 
     95 std::string PortAlternateProtocolPair::ToString() const {
     96   return base::StringPrintf("%d:%s", port,
     97                             AlternateProtocolToString(protocol));
     98 }
     99 
    100 }  // namespace net
    101