Add ICU message format support
[chromium-blink-merge.git] / tools / chrome_proxy / common / chrome_proxy_metrics.py
blob791d510041d7cd1a71fd2b294202fe53107a1a8f
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.
5 import logging
6 import time
8 from common import network_metrics
9 from telemetry.page import page_test
10 from telemetry.value import scalar
13 CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy'
16 class ChromeProxyMetricException(page_test.MeasurementFailure):
17 pass
20 class ChromeProxyResponse(network_metrics.HTTPResponse):
21 """ Represents an HTTP response from a timeline event."""
22 def __init__(self, event):
23 super(ChromeProxyResponse, self).__init__(event)
25 def ShouldHaveChromeProxyViaHeader(self):
26 resp = self.response
27 # Ignore https and data url
28 if resp.url.startswith('https') or resp.url.startswith('data:'):
29 return False
30 # Ignore 304 Not Modified and cache hit.
31 if resp.status == 304 or resp.served_from_cache:
32 return False
33 # Ignore invalid responses that don't have any header. Log a warning.
34 if not resp.headers:
35 logging.warning('response for %s does not any have header '
36 '(refer=%s, status=%s)',
37 resp.url, resp.GetHeader('Referer'), resp.status)
38 return False
39 return True
41 def HasResponseHeader(self, key, value):
42 response_header = self.response.GetHeader(key)
43 if not response_header:
44 return False
45 values = [v.strip() for v in response_header.split(',')]
46 return any(v == value for v in values)
48 def HasRequestHeader(self, key, value):
49 if key not in self.response.request_headers:
50 return False
51 request_header = self.response.request_headers[key]
52 values = [v.strip() for v in request_header.split(',')]
53 return any(v == value for v in values)
55 def HasChromeProxyViaHeader(self):
56 via_header = self.response.GetHeader('Via')
57 if not via_header:
58 return False
59 vias = [v.strip(' ') for v in via_header.split(',')]
60 # The Via header is valid if it has a 4-character version prefix followed by
61 # the proxy name, for example, "1.1 Chrome-Compression-Proxy".
62 return any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias)
64 def HasExtraViaHeader(self, extra_header):
65 return self.HasResponseHeader('Via', extra_header)
67 def IsValidByViaHeader(self):
68 return (not self.ShouldHaveChromeProxyViaHeader() or
69 self.HasChromeProxyViaHeader())
71 def GetChromeProxyRequestHeaderValue(self, key):
72 """Get a specific Chrome-Proxy request header value.
74 Returns:
75 The value for a specific Chrome-Proxy request header value for a
76 given key. Returns None if no such key is present.
77 """
78 if 'Chrome-Proxy' not in self.response.request_headers:
79 return None
81 chrome_proxy_request_header = self.response.request_headers['Chrome-Proxy']
82 values = [v.strip() for v in chrome_proxy_request_header.split(',')]
83 for value in values:
84 kvp = value.split('=', 1)
85 if len(kvp) == 2 and kvp[0].strip() == key:
86 return kvp[1].strip()
87 return None
89 def GetChromeProxyClientType(self):
90 """Get the client type directive from the Chrome-Proxy request header.
92 Returns:
93 The client type directive from the Chrome-Proxy request header for the
94 request that lead to this response. For example, if the request header
95 "Chrome-Proxy: c=android" is present, then this method would return
96 "android". Returns None if no client type directive is present.
97 """
98 return self.GetChromeProxyRequestHeaderValue('c')
100 def HasChromeProxyLoFiRequest(self):
101 return self.HasRequestHeader('Chrome-Proxy', "q=low")
103 def HasChromeProxyLoFiResponse(self):
104 return self.HasResponseHeader('Chrome-Proxy', "q=low")
106 def HasChromeProxyPassThroughRequest(self):
107 return self.HasRequestHeader('Chrome-Proxy', "pass-through")