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_server_properties.h"
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
12 const char kAlternateProtocolHeader
[] = "Alternate-Protocol";
16 // The order of these strings much match the order of the enum definition
17 // for AlternateProtocol.
18 const char* const kAlternateProtocolStrings
[] = {
23 "npn-HTTP-draft-04/2.0",
26 const char kBrokenAlternateProtocol
[] = "Broken";
29 arraysize(kAlternateProtocolStrings
) == NUM_VALID_ALTERNATE_PROTOCOLS
,
30 kAlternateProtocolStringsSize_kNumValidAlternateProtocols_not_equal
);
34 bool IsAlternateProtocolValid(AlternateProtocol protocol
) {
35 return protocol
>= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
&&
36 protocol
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
;
39 const char* AlternateProtocolToString(AlternateProtocol protocol
) {
41 case DEPRECATED_NPN_SPDY_2
:
45 case NPN_HTTP2_DRAFT_04
:
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";
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
))
66 if (str
== kBrokenAlternateProtocol
)
67 return ALTERNATE_PROTOCOL_BROKEN
;
68 return UNINITIALIZED_ALTERNATE_PROTOCOL
;
71 AlternateProtocol
AlternateProtocolFromNextProto(NextProto next_proto
) {
73 case kProtoDeprecatedSPDY2
:
74 return DEPRECATED_NPN_SPDY_2
;
81 case kProtoHTTP2Draft04
:
82 return NPN_HTTP2_DRAFT_04
;
83 case kProtoQUIC1SPDY3
:
91 NOTREACHED() << "Invalid NextProto: " << next_proto
;
92 return UNINITIALIZED_ALTERNATE_PROTOCOL
;
95 std::string
PortAlternateProtocolPair::ToString() const {
96 return base::StringPrintf("%d:%s", port
,
97 AlternateProtocolToString(protocol
));