Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / http / http_response_info.cc
blob18e7235ea1ac79242cbe0a662e4f848cce4c6357
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_response_info.h"
7 #include "base/logging.h"
8 #include "base/pickle.h"
9 #include "base/time/time.h"
10 #include "net/base/auth.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/cert/signed_certificate_timestamp.h"
14 #include "net/cert/x509_certificate.h"
15 #include "net/http/http_response_headers.h"
16 #include "net/ssl/ssl_cert_request_info.h"
18 using base::Time;
20 namespace net {
22 namespace {
24 X509Certificate::PickleType GetPickleTypeForVersion(int version) {
25 switch (version) {
26 case 1:
27 return X509Certificate::PICKLETYPE_SINGLE_CERTIFICATE;
28 case 2:
29 return X509Certificate::PICKLETYPE_CERTIFICATE_CHAIN_V2;
30 case 3:
31 default:
32 return X509Certificate::PICKLETYPE_CERTIFICATE_CHAIN_V3;
36 } // namespace
38 // These values can be bit-wise combined to form the flags field of the
39 // serialized HttpResponseInfo.
40 enum {
41 // The version of the response info used when persisting response info.
42 RESPONSE_INFO_VERSION = 3,
44 // The minimum version supported for deserializing response info.
45 RESPONSE_INFO_MINIMUM_VERSION = 1,
47 // We reserve up to 8 bits for the version number.
48 RESPONSE_INFO_VERSION_MASK = 0xFF,
50 // This bit is set if the response info has a cert at the end.
51 // Version 1 serialized only the end-entity certificate, while subsequent
52 // versions include the available certificate chain.
53 RESPONSE_INFO_HAS_CERT = 1 << 8,
55 // This bit is set if the response info has a security-bits field (security
56 // strength, in bits, of the SSL connection) at the end.
57 RESPONSE_INFO_HAS_SECURITY_BITS = 1 << 9,
59 // This bit is set if the response info has a cert status at the end.
60 RESPONSE_INFO_HAS_CERT_STATUS = 1 << 10,
62 // This bit is set if the response info has vary header data.
63 RESPONSE_INFO_HAS_VARY_DATA = 1 << 11,
65 // This bit is set if the request was cancelled before completion.
66 RESPONSE_INFO_TRUNCATED = 1 << 12,
68 // This bit is set if the response was received via SPDY.
69 RESPONSE_INFO_WAS_SPDY = 1 << 13,
71 // This bit is set if the request has NPN negotiated.
72 RESPONSE_INFO_WAS_NPN = 1 << 14,
74 // This bit is set if the request was fetched via an explicit proxy.
75 RESPONSE_INFO_WAS_PROXY = 1 << 15,
77 // This bit is set if the response info has an SSL connection status field.
78 // This contains the ciphersuite used to fetch the resource as well as the
79 // protocol version, compression method and whether SSLv3 fallback was used.
80 RESPONSE_INFO_HAS_SSL_CONNECTION_STATUS = 1 << 16,
82 // This bit is set if the response info has protocol version.
83 RESPONSE_INFO_HAS_NPN_NEGOTIATED_PROTOCOL = 1 << 17,
85 // This bit is set if the response info has connection info.
86 RESPONSE_INFO_HAS_CONNECTION_INFO = 1 << 18,
88 // This bit is set if the request has http authentication.
89 RESPONSE_INFO_USE_HTTP_AUTHENTICATION = 1 << 19,
91 // This bit is set if ssl_info has SCTs.
92 RESPONSE_INFO_HAS_SIGNED_CERTIFICATE_TIMESTAMPS = 1 << 20,
94 RESPONSE_INFO_UNUSED_SINCE_PREFETCH = 1 << 21,
96 // TODO(darin): Add other bits to indicate alternate request methods.
97 // For now, we don't support storing those.
100 HttpResponseInfo::HttpResponseInfo()
101 : was_cached(false),
102 server_data_unavailable(false),
103 network_accessed(false),
104 was_fetched_via_spdy(false),
105 was_npn_negotiated(false),
106 was_fetched_via_proxy(false),
107 did_use_http_auth(false),
108 unused_since_prefetch(false),
109 async_revalidation_required(false),
110 connection_info(CONNECTION_INFO_UNKNOWN) {}
112 HttpResponseInfo::HttpResponseInfo(const HttpResponseInfo& rhs)
113 : was_cached(rhs.was_cached),
114 server_data_unavailable(rhs.server_data_unavailable),
115 network_accessed(rhs.network_accessed),
116 was_fetched_via_spdy(rhs.was_fetched_via_spdy),
117 was_npn_negotiated(rhs.was_npn_negotiated),
118 was_fetched_via_proxy(rhs.was_fetched_via_proxy),
119 proxy_server(rhs.proxy_server),
120 did_use_http_auth(rhs.did_use_http_auth),
121 unused_since_prefetch(rhs.unused_since_prefetch),
122 async_revalidation_required(rhs.async_revalidation_required),
123 socket_address(rhs.socket_address),
124 npn_negotiated_protocol(rhs.npn_negotiated_protocol),
125 connection_info(rhs.connection_info),
126 request_time(rhs.request_time),
127 response_time(rhs.response_time),
128 auth_challenge(rhs.auth_challenge),
129 cert_request_info(rhs.cert_request_info),
130 ssl_info(rhs.ssl_info),
131 headers(rhs.headers),
132 vary_data(rhs.vary_data),
133 metadata(rhs.metadata) {}
135 HttpResponseInfo::~HttpResponseInfo() {
138 HttpResponseInfo& HttpResponseInfo::operator=(const HttpResponseInfo& rhs) {
139 was_cached = rhs.was_cached;
140 server_data_unavailable = rhs.server_data_unavailable;
141 network_accessed = rhs.network_accessed;
142 was_fetched_via_spdy = rhs.was_fetched_via_spdy;
143 proxy_server = rhs.proxy_server;
144 was_npn_negotiated = rhs.was_npn_negotiated;
145 was_fetched_via_proxy = rhs.was_fetched_via_proxy;
146 did_use_http_auth = rhs.did_use_http_auth;
147 unused_since_prefetch = rhs.unused_since_prefetch;
148 async_revalidation_required = rhs.async_revalidation_required;
149 socket_address = rhs.socket_address;
150 npn_negotiated_protocol = rhs.npn_negotiated_protocol;
151 connection_info = rhs.connection_info;
152 request_time = rhs.request_time;
153 response_time = rhs.response_time;
154 auth_challenge = rhs.auth_challenge;
155 cert_request_info = rhs.cert_request_info;
156 ssl_info = rhs.ssl_info;
157 headers = rhs.headers;
158 vary_data = rhs.vary_data;
159 metadata = rhs.metadata;
160 return *this;
163 bool HttpResponseInfo::InitFromPickle(const base::Pickle& pickle,
164 bool* response_truncated) {
165 base::PickleIterator iter(pickle);
167 // Read flags and verify version
168 int flags;
169 if (!iter.ReadInt(&flags))
170 return false;
171 int version = flags & RESPONSE_INFO_VERSION_MASK;
172 if (version < RESPONSE_INFO_MINIMUM_VERSION ||
173 version > RESPONSE_INFO_VERSION) {
174 DLOG(ERROR) << "unexpected response info version: " << version;
175 return false;
178 // Read request-time
179 int64 time_val;
180 if (!iter.ReadInt64(&time_val))
181 return false;
182 request_time = Time::FromInternalValue(time_val);
183 was_cached = true; // Set status to show cache resurrection.
185 // Read response-time
186 if (!iter.ReadInt64(&time_val))
187 return false;
188 response_time = Time::FromInternalValue(time_val);
190 // Read response-headers
191 headers = new HttpResponseHeaders(&iter);
192 if (headers->response_code() == -1)
193 return false;
195 // Read ssl-info
196 if (flags & RESPONSE_INFO_HAS_CERT) {
197 X509Certificate::PickleType type = GetPickleTypeForVersion(version);
198 ssl_info.cert = X509Certificate::CreateFromPickle(&iter, type);
199 if (!ssl_info.cert.get())
200 return false;
202 if (flags & RESPONSE_INFO_HAS_CERT_STATUS) {
203 CertStatus cert_status;
204 if (!iter.ReadUInt32(&cert_status))
205 return false;
206 ssl_info.cert_status = cert_status;
208 if (flags & RESPONSE_INFO_HAS_SECURITY_BITS) {
209 int security_bits;
210 if (!iter.ReadInt(&security_bits))
211 return false;
212 ssl_info.security_bits = security_bits;
215 if (flags & RESPONSE_INFO_HAS_SSL_CONNECTION_STATUS) {
216 int connection_status;
217 if (!iter.ReadInt(&connection_status))
218 return false;
219 ssl_info.connection_status = connection_status;
222 if (flags & RESPONSE_INFO_HAS_SIGNED_CERTIFICATE_TIMESTAMPS) {
223 int num_scts;
224 if (!iter.ReadInt(&num_scts))
225 return false;
226 for (int i = 0; i < num_scts; ++i) {
227 scoped_refptr<ct::SignedCertificateTimestamp> sct(
228 ct::SignedCertificateTimestamp::CreateFromPickle(&iter));
229 uint16 status;
230 if (!sct.get() || !iter.ReadUInt16(&status))
231 return false;
232 ssl_info.signed_certificate_timestamps.push_back(
233 SignedCertificateTimestampAndStatus(
234 sct, static_cast<ct::SCTVerifyStatus>(status)));
238 // Read vary-data
239 if (flags & RESPONSE_INFO_HAS_VARY_DATA) {
240 if (!vary_data.InitFromPickle(&iter))
241 return false;
244 // Read socket_address.
245 std::string socket_address_host;
246 if (iter.ReadString(&socket_address_host)) {
247 // If the host was written, we always expect the port to follow.
248 uint16 socket_address_port;
249 if (!iter.ReadUInt16(&socket_address_port))
250 return false;
251 socket_address = HostPortPair(socket_address_host, socket_address_port);
252 } else if (version > 1) {
253 // socket_address was not always present in version 1 of the response
254 // info, so we don't fail if it can't be read.
255 return false;
258 // Read protocol-version.
259 if (flags & RESPONSE_INFO_HAS_NPN_NEGOTIATED_PROTOCOL) {
260 if (!iter.ReadString(&npn_negotiated_protocol))
261 return false;
264 // Read connection info.
265 if (flags & RESPONSE_INFO_HAS_CONNECTION_INFO) {
266 int value;
267 if (!iter.ReadInt(&value))
268 return false;
270 if (value > static_cast<int>(CONNECTION_INFO_UNKNOWN) &&
271 value < static_cast<int>(NUM_OF_CONNECTION_INFOS)) {
272 connection_info = static_cast<ConnectionInfo>(value);
276 was_fetched_via_spdy = (flags & RESPONSE_INFO_WAS_SPDY) != 0;
278 was_npn_negotiated = (flags & RESPONSE_INFO_WAS_NPN) != 0;
280 was_fetched_via_proxy = (flags & RESPONSE_INFO_WAS_PROXY) != 0;
282 *response_truncated = (flags & RESPONSE_INFO_TRUNCATED) != 0;
284 did_use_http_auth = (flags & RESPONSE_INFO_USE_HTTP_AUTHENTICATION) != 0;
286 unused_since_prefetch = (flags & RESPONSE_INFO_UNUSED_SINCE_PREFETCH) != 0;
288 return true;
291 void HttpResponseInfo::Persist(base::Pickle* pickle,
292 bool skip_transient_headers,
293 bool response_truncated) const {
294 int flags = RESPONSE_INFO_VERSION;
295 if (ssl_info.is_valid()) {
296 flags |= RESPONSE_INFO_HAS_CERT;
297 flags |= RESPONSE_INFO_HAS_CERT_STATUS;
298 if (ssl_info.security_bits != -1)
299 flags |= RESPONSE_INFO_HAS_SECURITY_BITS;
300 if (ssl_info.connection_status != 0)
301 flags |= RESPONSE_INFO_HAS_SSL_CONNECTION_STATUS;
303 if (vary_data.is_valid())
304 flags |= RESPONSE_INFO_HAS_VARY_DATA;
305 if (response_truncated)
306 flags |= RESPONSE_INFO_TRUNCATED;
307 if (was_fetched_via_spdy)
308 flags |= RESPONSE_INFO_WAS_SPDY;
309 if (was_npn_negotiated) {
310 flags |= RESPONSE_INFO_WAS_NPN;
311 flags |= RESPONSE_INFO_HAS_NPN_NEGOTIATED_PROTOCOL;
313 if (was_fetched_via_proxy)
314 flags |= RESPONSE_INFO_WAS_PROXY;
315 if (connection_info != CONNECTION_INFO_UNKNOWN)
316 flags |= RESPONSE_INFO_HAS_CONNECTION_INFO;
317 if (did_use_http_auth)
318 flags |= RESPONSE_INFO_USE_HTTP_AUTHENTICATION;
319 if (unused_since_prefetch)
320 flags |= RESPONSE_INFO_UNUSED_SINCE_PREFETCH;
321 if (!ssl_info.signed_certificate_timestamps.empty())
322 flags |= RESPONSE_INFO_HAS_SIGNED_CERTIFICATE_TIMESTAMPS;
324 pickle->WriteInt(flags);
325 pickle->WriteInt64(request_time.ToInternalValue());
326 pickle->WriteInt64(response_time.ToInternalValue());
328 HttpResponseHeaders::PersistOptions persist_options =
329 HttpResponseHeaders::PERSIST_RAW;
331 if (skip_transient_headers) {
332 persist_options = HttpResponseHeaders::PERSIST_SANS_COOKIES |
333 HttpResponseHeaders::PERSIST_SANS_CHALLENGES |
334 HttpResponseHeaders::PERSIST_SANS_HOP_BY_HOP |
335 HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE |
336 HttpResponseHeaders::PERSIST_SANS_RANGES |
337 HttpResponseHeaders::PERSIST_SANS_SECURITY_STATE;
340 headers->Persist(pickle, persist_options);
342 if (ssl_info.is_valid()) {
343 ssl_info.cert->Persist(pickle);
344 pickle->WriteUInt32(ssl_info.cert_status);
345 if (ssl_info.security_bits != -1)
346 pickle->WriteInt(ssl_info.security_bits);
347 if (ssl_info.connection_status != 0)
348 pickle->WriteInt(ssl_info.connection_status);
349 if (!ssl_info.signed_certificate_timestamps.empty()) {
350 pickle->WriteInt(ssl_info.signed_certificate_timestamps.size());
351 for (SignedCertificateTimestampAndStatusList::const_iterator it =
352 ssl_info.signed_certificate_timestamps.begin(); it !=
353 ssl_info.signed_certificate_timestamps.end(); ++it) {
354 it->sct->Persist(pickle);
355 pickle->WriteUInt16(static_cast<uint16>(it->status));
360 if (vary_data.is_valid())
361 vary_data.Persist(pickle);
363 pickle->WriteString(socket_address.host());
364 pickle->WriteUInt16(socket_address.port());
366 if (was_npn_negotiated)
367 pickle->WriteString(npn_negotiated_protocol);
369 if (connection_info != CONNECTION_INFO_UNKNOWN)
370 pickle->WriteInt(static_cast<int>(connection_info));
373 HttpResponseInfo::ConnectionInfo HttpResponseInfo::ConnectionInfoFromNextProto(
374 NextProto next_proto) {
375 switch (next_proto) {
376 case kProtoDeprecatedSPDY2:
377 return CONNECTION_INFO_DEPRECATED_SPDY2;
378 case kProtoSPDY3:
379 case kProtoSPDY31:
380 return CONNECTION_INFO_SPDY3;
381 case kProtoHTTP2:
382 return CONNECTION_INFO_HTTP2;
383 case kProtoQUIC1SPDY3:
384 return CONNECTION_INFO_QUIC1_SPDY3;
386 case kProtoUnknown:
387 case kProtoHTTP11:
388 break;
391 NOTREACHED();
392 return CONNECTION_INFO_UNKNOWN;
395 // static
396 std::string HttpResponseInfo::ConnectionInfoToString(
397 ConnectionInfo connection_info) {
398 switch (connection_info) {
399 case CONNECTION_INFO_UNKNOWN:
400 return "unknown";
401 case CONNECTION_INFO_HTTP1:
402 return "http/1";
403 case CONNECTION_INFO_DEPRECATED_SPDY2:
404 return "spdy/2";
405 case CONNECTION_INFO_SPDY3:
406 return "spdy/3";
407 // Since ConnectionInfo is persisted to disk, deprecated values have to be
408 // handled. Note that h2-14 and h2-15 are essentially wire compatible with
409 // h2.
410 // Intentional fallthrough.
411 case CONNECTION_INFO_HTTP2_14:
412 case CONNECTION_INFO_HTTP2_15:
413 case CONNECTION_INFO_HTTP2:
414 return "h2";
415 case CONNECTION_INFO_QUIC1_SPDY3:
416 return "quic/1+spdy/3";
417 case NUM_OF_CONNECTION_INFOS:
418 break;
420 NOTREACHED();
421 return "";
424 } // namespace net