1// Copyright 2014 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#ifndef COMPONENTS_DATA_REDUCTION_PROXY_COMMON_DATA_REDUCTION_PROXY_HEADERS_H_
6#define COMPONENTS_DATA_REDUCTION_PROXY_COMMON_DATA_REDUCTION_PROXY_HEADERS_H_
7
8#include <string>
9
10#include "base/macros.h"
11#include "base/time/time.h"
12#include "net/proxy/proxy_service.h"
13
14namespace net {
15
16class HttpResponseHeaders;
17
18}  // namespace net
19
20namespace data_reduction_proxy {
21
22// Values of the UMA DataReductionProxy.BypassType{Primary|Fallback}
23// and DataReductionProxy.BlockType{Primary|Fallback} histograms.
24// This enum must remain synchronized with the enum of the same
25// name in metrics/histograms/histograms.xml.
26enum DataReductionProxyBypassType {
27  // Bypass due to explicit instruction for the current request.
28  BYPASS_EVENT_TYPE_CURRENT = 0,
29
30  // Bypass the proxy for less than one minute.
31  BYPASS_EVENT_TYPE_SHORT = 1,
32
33  // Bypass the proxy for one to five minutes.
34  BYPASS_EVENT_TYPE_MEDIUM = 2,
35
36  // Bypass the proxy for more than five minutes.
37  BYPASS_EVENT_TYPE_LONG = 3,
38
39  // Bypass due to a 4xx missing via header.
40  BYPASS_EVENT_TYPE_MISSING_VIA_HEADER_4XX = 4,
41
42  // Bypass due to other missing via header, excluding 4xx errors.
43  BYPASS_EVENT_TYPE_MISSING_VIA_HEADER_OTHER = 5,
44
45  // Bypass due to 407 response from proxy without a challenge.
46  BYPASS_EVENT_TYPE_MALFORMED_407 = 6,
47
48  // Bypass due to a 500 internal server error
49  BYPASS_EVENT_TYPE_STATUS_500_HTTP_INTERNAL_SERVER_ERROR = 7,
50
51  // Bypass because the request URI was too long.
52  BYPASS_EVENT_TYPE_STATUS_502_HTTP_BAD_GATEWAY = 8,
53
54  // Bypass due to a 503 response.
55  BYPASS_EVENT_TYPE_STATUS_503_HTTP_SERVICE_UNAVAILABLE = 9,
56
57  // Bypass due to any network error.
58  BYPASS_EVENT_TYPE_NETWORK_ERROR = 10,
59
60  // This must always be last.
61  BYPASS_EVENT_TYPE_MAX = 11
62};
63
64// Contains instructions contained in the Chrome-Proxy header.
65struct DataReductionProxyInfo {
66  DataReductionProxyInfo()
67      : bypass_all(false), mark_proxies_as_bad(false) {}
68
69  // True if Chrome should bypass all available data reduction proxies. False
70  // if only the currently connected data reduction proxy should be bypassed.
71  bool bypass_all;
72
73  // True iff Chrome should mark the data reduction proxy or proxies as bad for
74  // the period of time specified in |bypass_duration|.
75  bool mark_proxies_as_bad;
76
77  // Amount of time to bypass the data reduction proxy or proxies. This value is
78  // ignored if |mark_proxies_as_bad| is false.
79  base::TimeDelta bypass_duration;
80};
81
82// Returns true if the Chrome-Proxy header is present and contains a bypass
83// delay. Sets |proxy_info->bypass_duration| to the specified delay if greater
84// than 0, and to 0 otherwise to indicate that the default proxy delay
85// (as specified in |ProxyList::UpdateRetryInfoOnFallback|) should be used.
86// If all available data reduction proxies should by bypassed, |bypass_all| is
87// set to true. |proxy_info| must be non-NULL.
88bool ParseHeadersAndSetProxyInfo(const net::HttpResponseHeaders* headers,
89                                 DataReductionProxyInfo* proxy_info);
90
91// Returns true if the response contains the data reduction proxy Via header
92// value. If non-NULL, sets |has_intermediary| to true if another server added
93// a Via header after the data reduction proxy, and to false otherwise. Used to
94// check the integrity of data reduction proxy responses and whether there are
95// other middleboxes between the data reduction proxy and the client.
96bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers,
97                                    bool* has_intermediary);
98
99// Returns the reason why the Chrome proxy should be bypassed or not, and
100// populates |proxy_info| with information on how long to bypass if
101// applicable.
102DataReductionProxyBypassType GetDataReductionProxyBypassType(
103    const net::HttpResponseHeaders* headers,
104    DataReductionProxyInfo* proxy_info);
105
106// Searches for the specified Chrome-Proxy action, and if present saves its
107// value as a string in |action_value|. Only returns the first one and ignores
108// the rest if multiple actions match |action_prefix|.
109bool GetDataReductionProxyActionValue(
110    const net::HttpResponseHeaders* headers,
111    const std::string& action_prefix,
112    std::string* action_value);
113
114// Searches for the specified Chrome-Proxy action, and if present interprets
115// its value as a duration in seconds.
116bool ParseHeadersAndSetBypassDuration(const net::HttpResponseHeaders* headers,
117                                      const std::string& action_prefix,
118                                      base::TimeDelta* bypass_duration);
119
120// Gets the fingerprint of the Chrome-Proxy header.
121bool GetDataReductionProxyActionFingerprintChromeProxy(
122    const net::HttpResponseHeaders* headers,
123    std::string* chrome_proxy_fingerprint);
124
125// Gets the fingerprint of the Via header.
126bool GetDataReductionProxyActionFingerprintVia(
127    const net::HttpResponseHeaders* headers,
128    std::string* via_fingerprint);
129
130// Gets the fingerprint of a list of headers.
131bool GetDataReductionProxyActionFingerprintOtherHeaders(
132    const net::HttpResponseHeaders* headers,
133    std::string* other_headers_fingerprint);
134
135// Gets the fingerprint of Content-Length header.
136bool GetDataReductionProxyActionFingerprintContentLength(
137    const net::HttpResponseHeaders* headers,
138    std::string* content_length_fingerprint);
139
140// Returns values of the Chrome-Proxy header, but with its fingerprint removed.
141void GetDataReductionProxyHeaderWithFingerprintRemoved(
142    const net::HttpResponseHeaders* headers,
143    std::vector<std::string>* values);
144
145}  // namespace data_reduction_proxy
146#endif  // COMPONENTS_DATA_REDUCTION_PROXY_COMMON_DATA_REDUCTION_PROXY_HEADERS_H_
147