Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / http / http_server_properties.cc
blob1e3ccd234f0460a83a2b2efe6ac4f1bc139a1470
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/metrics/histogram_macros.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/socket/ssl_client_socket.h"
11 #include "net/ssl/ssl_config.h"
13 namespace net {
15 const char kAlternateProtocolHeader[] = "Alternate-Protocol";
16 const char kAlternativeServiceHeader[] = "Alt-Svc";
18 namespace {
20 // The order of these strings much match the order of the enum definition
21 // for AlternateProtocol.
22 const char* const kAlternateProtocolStrings[] = {
23 "npn-spdy/2",
24 "npn-spdy/3",
25 "npn-spdy/3.1",
26 "npn-h2",
27 "quic"};
29 static_assert(arraysize(kAlternateProtocolStrings) ==
30 NUM_VALID_ALTERNATE_PROTOCOLS,
31 "kAlternateProtocolStrings has incorrect size");
33 } // namespace
35 void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage) {
36 UMA_HISTOGRAM_ENUMERATION("Net.AlternateProtocolUsage", usage,
37 ALTERNATE_PROTOCOL_USAGE_MAX);
40 void HistogramBrokenAlternateProtocolLocation(
41 BrokenAlternateProtocolLocation location){
42 UMA_HISTOGRAM_ENUMERATION("Net.AlternateProtocolBrokenLocation", location,
43 BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX);
46 bool IsAlternateProtocolValid(AlternateProtocol protocol) {
47 return protocol >= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION &&
48 protocol <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION;
51 const char* AlternateProtocolToString(AlternateProtocol protocol) {
52 switch (protocol) {
53 case DEPRECATED_NPN_SPDY_2:
54 case NPN_SPDY_3:
55 case NPN_SPDY_3_1:
56 case NPN_HTTP_2:
57 case QUIC:
58 DCHECK(IsAlternateProtocolValid(protocol));
59 return kAlternateProtocolStrings[
60 protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION];
61 case UNINITIALIZED_ALTERNATE_PROTOCOL:
62 return "Uninitialized";
64 NOTREACHED();
65 return "";
68 AlternateProtocol AlternateProtocolFromString(const std::string& str) {
69 for (int i = ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION;
70 i <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; ++i) {
71 AlternateProtocol protocol = static_cast<AlternateProtocol>(i);
72 if (str == AlternateProtocolToString(protocol))
73 return protocol;
75 return UNINITIALIZED_ALTERNATE_PROTOCOL;
78 AlternateProtocol AlternateProtocolFromNextProto(NextProto next_proto) {
79 switch (next_proto) {
80 case kProtoDeprecatedSPDY2:
81 return DEPRECATED_NPN_SPDY_2;
82 case kProtoSPDY3:
83 return NPN_SPDY_3;
84 case kProtoSPDY31:
85 return NPN_SPDY_3_1;
86 case kProtoHTTP2:
87 return NPN_HTTP_2;
88 case kProtoQUIC1SPDY3:
89 return QUIC;
91 case kProtoUnknown:
92 case kProtoHTTP11:
93 break;
96 NOTREACHED() << "Invalid NextProto: " << next_proto;
97 return UNINITIALIZED_ALTERNATE_PROTOCOL;
100 std::string AlternativeService::ToString() const {
101 return base::StringPrintf("%s %s:%d", AlternateProtocolToString(protocol),
102 host.c_str(), port);
105 std::string AlternativeServiceInfo::ToString() const {
106 return base::StringPrintf("%s, p=%f", alternative_service.ToString().c_str(),
107 probability);
110 // static
111 void HttpServerProperties::ForceHTTP11(SSLConfig* ssl_config) {
112 ssl_config->next_protos.clear();
113 ssl_config->next_protos.push_back(kProtoHTTP11);
116 } // namespace net