[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 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" | ||||
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 8 | #include "base/strings/stringprintf.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 9 | |
10 | namespace net { | ||||
11 | |||||
12 | const char kAlternateProtocolHeader[] = "Alternate-Protocol"; | ||||
[email protected] | 88a33262 | 2013-07-30 07:13:32 | [diff] [blame] | 13 | |
14 | namespace { | ||||
15 | |||||
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 16 | // The order of these strings much match the order of the enum definition |
17 | // for AlternateProtocol. | ||||
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 18 | const char* const kAlternateProtocolStrings[] = { |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 19 | "npn-spdy/2", |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 20 | "npn-spdy/3", |
[email protected] | 63bf966 | 2013-03-05 20:46:01 | [diff] [blame] | 21 | "npn-spdy/3.1", |
[email protected] | d5b42bdd | 2013-05-15 20:42:36 | [diff] [blame] | 22 | "npn-spdy/4a2", |
[email protected] | 88a33262 | 2013-07-30 07:13:32 | [diff] [blame] | 23 | "npn-HTTP-draft-04/2.0", |
[email protected] | 7db54b8 | 2013-04-01 21:53:45 | [diff] [blame] | 24 | "quic" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 25 | }; |
[email protected] | 04af25f6 | 2012-04-10 19:11:39 | [diff] [blame] | 26 | const char kBrokenAlternateProtocol[] = "Broken"; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 27 | |
[email protected] | 13621d6 | 2013-10-04 18:39:21 | [diff] [blame^] | 28 | COMPILE_ASSERT( |
29 | arraysize(kAlternateProtocolStrings) == NUM_VALID_ALTERNATE_PROTOCOLS, | ||||
30 | kAlternateProtocolStringsSize_kNumValidAlternateProtocols_not_equal); | ||||
[email protected] | 88a33262 | 2013-07-30 07:13:32 | [diff] [blame] | 31 | |
32 | } // namespace | ||||
33 | |||||
[email protected] | 13621d6 | 2013-10-04 18:39:21 | [diff] [blame^] | 34 | bool IsAlternateProtocolValid(AlternateProtocol protocol) { |
35 | return protocol >= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION && | ||||
36 | protocol <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; | ||||
37 | } | ||||
38 | |||||
[email protected] | 04af25f6 | 2012-04-10 19:11:39 | [diff] [blame] | 39 | const char* AlternateProtocolToString(AlternateProtocol protocol) { |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 40 | switch (protocol) { |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 41 | case NPN_SPDY_2: |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 42 | case NPN_SPDY_3: |
[email protected] | 8a0fc82 | 2013-06-27 20:52:43 | [diff] [blame] | 43 | case NPN_SPDY_3_1: |
44 | case NPN_SPDY_4A2: | ||||
[email protected] | 88a33262 | 2013-07-30 07:13:32 | [diff] [blame] | 45 | case NPN_HTTP2_DRAFT_04: |
[email protected] | 7db54b8 | 2013-04-01 21:53:45 | [diff] [blame] | 46 | case QUIC: |
[email protected] | 13621d6 | 2013-10-04 18:39:21 | [diff] [blame^] | 47 | DCHECK(IsAlternateProtocolValid(protocol)); |
48 | return kAlternateProtocolStrings[ | ||||
49 | protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION]; | ||||
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 50 | case ALTERNATE_PROTOCOL_BROKEN: |
[email protected] | 04af25f6 | 2012-04-10 19:11:39 | [diff] [blame] | 51 | return kBrokenAlternateProtocol; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 52 | case UNINITIALIZED_ALTERNATE_PROTOCOL: |
53 | return "Uninitialized"; | ||||
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 54 | } |
[email protected] | 88a33262 | 2013-07-30 07:13:32 | [diff] [blame] | 55 | NOTREACHED(); |
56 | return ""; | ||||
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 57 | } |
58 | |||||
[email protected] | 13621d6 | 2013-10-04 18:39:21 | [diff] [blame^] | 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) | ||||
[email protected] | 04af25f6 | 2012-04-10 19:11:39 | [diff] [blame] | 67 | return ALTERNATE_PROTOCOL_BROKEN; |
68 | return UNINITIALIZED_ALTERNATE_PROTOCOL; | ||||
69 | } | ||||
70 | |||||
[email protected] | 0ce3af8 | 2013-07-22 16:17:16 | [diff] [blame] | 71 | AlternateProtocol AlternateProtocolFromNextProto(NextProto next_proto) { |
72 | switch (next_proto) { | ||||
73 | case kProtoSPDY2: | ||||
74 | return 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; | ||||
[email protected] | 88a33262 | 2013-07-30 07:13:32 | [diff] [blame] | 81 | case kProtoHTTP2Draft04: |
82 | return NPN_HTTP2_DRAFT_04; | ||||
[email protected] | 0ce3af8 | 2013-07-22 16:17:16 | [diff] [blame] | 83 | case kProtoQUIC1SPDY3: |
84 | return QUIC; | ||||
85 | |||||
86 | case kProtoUnknown: | ||||
87 | case kProtoHTTP11: | ||||
[email protected] | 0ce3af8 | 2013-07-22 16:17:16 | [diff] [blame] | 88 | break; |
89 | } | ||||
90 | |||||
91 | NOTREACHED() << "Invalid NextProto: " << next_proto; | ||||
92 | return UNINITIALIZED_ALTERNATE_PROTOCOL; | ||||
93 | } | ||||
[email protected] | 04af25f6 | 2012-04-10 19:11:39 | [diff] [blame] | 94 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 95 | std::string PortAlternateProtocolPair::ToString() const { |
96 | return base::StringPrintf("%d:%s", port, | ||||
97 | AlternateProtocolToString(protocol)); | ||||
98 | } | ||||
99 | |||||
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 100 | } // namespace net |