OLD | NEW |
---|---|
(Empty) | |
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 from telemetry.page import page_measurement | |
6 from metrics import network | |
7 | |
8 | |
9 class ChromeProxyMetricException(page_measurement.MeasurementFailure): | |
10 pass | |
bengr
2014/03/26 22:45:55
Shouldn't this have an implementation?
bolian
2014/03/27 00:43:32
No, it will just take an error message when thrown
| |
11 | |
12 | |
13 CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy' | |
14 CHROME_PROXY_VIA_HEADER_OLD = '1.1 Chrome Compression Proxy' | |
bengr
2014/03/26 22:45:55
OLD -> DEPRECATED
bolian
2014/03/27 00:43:32
Done.
| |
15 | |
16 | |
17 class ChromeProxyResponse(network.HTTPResponse): | |
18 """ Represents an HTTP response from a timeleine event.""" | |
19 def __init__(self, event): | |
20 super(ChromeProxyResponse, self).__init__(event) | |
21 | |
22 def ShouldHaveChromeProxyViaHeader(self): | |
23 resp = self.response | |
24 # Ignore https and data url | |
25 if resp.url.startswith('https') or resp.url.startswith('data:'): | |
bengr
2014/03/26 22:45:55
Is there any way to do something similar for incog
bolian
2014/03/27 00:43:32
Not sure how to know it is incognito from Telemetr
| |
26 return False | |
27 # Ignore 304 Not Modified. | |
28 if resp.status == 304: | |
29 return False | |
30 return True | |
31 | |
32 def HasChromeProxyViaHeader(self): | |
33 via_header = self.response.GetHeader('Via') | |
34 if not via_header: | |
35 return False | |
36 vias = [v.strip(' ') for v in via_header.split(',')] | |
37 # The Via header is valid if it is the old format or the new format | |
38 # with 4-character version prefix, for example, | |
39 # "1.1 Chrome-Compression-Proxy". | |
40 return (CHROME_PROXY_VIA_HEADER_OLD in vias or | |
41 any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias)) | |
42 | |
43 def IsValidByViaHeader(self): | |
44 return (not self.ShouldHaveChromeProxyViaHeader() or | |
45 self.HasChromeProxyViaHeader()) | |
46 | |
47 def IsSafebrowsingResponse(self): | |
48 if (self.response.status == 307 and | |
49 self.response.GetHeader('X-Malware-Url') == '1' and | |
50 self.IsValidByViaHeader() and | |
51 self.response.GetHeader('Location') == self.response.url): | |
52 return True | |
53 return False | |
54 | |
55 | |
56 class ChromeProxyMetric(network.NetworkMetric): | |
57 """A Chrome proxy timeline metric.""" | |
58 | |
59 def __init__(self): | |
60 super(ChromeProxyMetric, self).__init__() | |
61 self.compute_data_saving = True | |
62 | |
63 def ResponseFromEvent(self, event): | |
64 return ChromeProxyResponse(event) | |
65 | |
66 def AddResults(self, tab, results): | |
67 raise NotImplementedError | |
68 | |
69 def AddResultsForDataSaving(self, tab, results): | |
70 resources_via_proxy = 0 | |
71 resources_from_cache = 0 | |
72 resources_direct = 0 | |
73 | |
74 super(ChromeProxyMetric, self).AddResults(tab, results) | |
75 for resp in self.IterResponses(tab): | |
76 if resp.response.served_from_cache: | |
77 resources_from_cache += 1 | |
78 if resp.HasChromeProxyViaHeader(): | |
79 resources_via_proxy += 1 | |
80 else: | |
81 resources_direct += 1 | |
82 | |
83 results.Add('resources_via_proxy', 'count', resources_via_proxy) | |
84 results.Add('resources_from_cache', 'count', resources_from_cache) | |
85 results.Add('resources_direct', 'count', resources_direct) | |
86 | |
87 def AddResultsForHeaderValidation(self, tab, results): | |
88 via_count = 0 | |
89 for resp in self.IterResponses(tab): | |
90 if resp.IsValidByViaHeader(): | |
91 via_count += 1 | |
92 else: | |
93 r = resp.response | |
94 raise ChromeProxyMetricException, ( | |
95 '%s: Via header (%s) is not valid (refer=%s, status=%d)' % ( | |
96 r.url, r.GetHeader('Via'), r.GetHeader('Referer'), r.status)) | |
97 results.Add('checked_via_header', 'count', via_count) | |
98 | |
99 def AddResultsForBypass(self, tab, results): | |
100 bypass_count = 0 | |
101 for resp in self.IterResponses(tab): | |
102 if resp.HasChromeProxyViaHeader(): | |
103 r = resp.response | |
104 raise ChromeProxyMetricException, ( | |
105 '%s: Should not have Via header (%s) (refer=%s, status=%d)' % ( | |
106 r.url, r.GetHeader('Via'), r.GetHeader('Referer'), r.status)) | |
107 bypass_count += 1 | |
108 results.Add('bypass', 'count', bypass_count) | |
109 | |
110 def AddResultsForSafebrowsing(self, tab, results): | |
111 count = 0 | |
112 safebrowsing_count = 0 | |
113 for resp in self.IterResponses(tab): | |
114 count += 1 | |
115 if resp.IsSafebrowsingResponse(): | |
116 safebrowsing_count += 1 | |
117 else: | |
118 r = resp.response | |
119 raise ChromeProxyMetricException, ( | |
120 '%s: Not a valid safe browsing response.\n' | |
121 'Reponse: status=(%d, %s)\nHeaders:\n %s' % ( | |
122 r.url, r.status, r.status_text, r.headers)) | |
123 if count == safebrowsing_count: | |
124 results.Add('safebrowsing', 'boolean', True) | |
125 else: | |
126 raise ChromeProxyMetricException, ( | |
127 'Safebrowsing failed (count=%d, safebrowsing_count=%d)\n' % ( | |
128 count, safebrowsing_count)) | |
OLD | NEW |