OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | 5 import logging |
6 import time | 6 import time |
7 | 7 |
8 from integration_tests import network_metrics | 8 from integration_tests import network_metrics |
9 from telemetry.page import page_test | 9 from telemetry.page import page_test |
10 from telemetry.value import scalar | 10 from telemetry.value import scalar |
11 | 11 |
12 | 12 |
13 class ChromeProxyMetricException(page_test.MeasurementFailure): | 13 class ChromeProxyMetricException(page_test.MeasurementFailure): |
14 pass | 14 pass |
15 | 15 |
16 | 16 |
17 CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy' | 17 CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy' |
18 CHROME_PROXY_VIA_HEADER_DEPRECATED = '1.1 Chrome Compression Proxy' | |
19 | 18 |
20 | 19 |
21 class ChromeProxyResponse(network_metrics.HTTPResponse): | 20 class ChromeProxyResponse(network_metrics.HTTPResponse): |
22 """ Represents an HTTP response from a timeleine event.""" | 21 """ Represents an HTTP response from a timeleine event.""" |
23 def __init__(self, event): | 22 def __init__(self, event): |
24 super(ChromeProxyResponse, self).__init__(event) | 23 super(ChromeProxyResponse, self).__init__(event) |
25 | 24 |
26 def ShouldHaveChromeProxyViaHeader(self): | 25 def ShouldHaveChromeProxyViaHeader(self): |
27 resp = self.response | 26 resp = self.response |
28 # Ignore https and data url | 27 # Ignore https and data url |
29 if resp.url.startswith('https') or resp.url.startswith('data:'): | 28 if resp.url.startswith('https') or resp.url.startswith('data:'): |
30 return False | 29 return False |
31 # Ignore 304 Not Modified and cache hit. | 30 # Ignore 304 Not Modified and cache hit. |
32 if resp.status == 304 or resp.served_from_cache: | 31 if resp.status == 304 or resp.served_from_cache: |
33 return False | 32 return False |
34 # Ignore invalid responses that don't have any header. Log a warning. | 33 # Ignore invalid responses that don't have any header. Log a warning. |
35 if not resp.headers: | 34 if not resp.headers: |
36 logging.warning('response for %s does not any have header ' | 35 logging.warning('response for %s does not any have header ' |
37 '(refer=%s, status=%s)', | 36 '(refer=%s, status=%s)', |
38 resp.url, resp.GetHeader('Referer'), resp.status) | 37 resp.url, resp.GetHeader('Referer'), resp.status) |
39 return False | 38 return False |
40 return True | 39 return True |
41 | 40 |
42 def HasChromeProxyViaHeader(self): | 41 def HasChromeProxyViaHeader(self): |
43 via_header = self.response.GetHeader('Via') | 42 via_header = self.response.GetHeader('Via') |
44 if not via_header: | 43 if not via_header: |
45 return False | 44 return False |
46 vias = [v.strip(' ') for v in via_header.split(',')] | 45 vias = [v.strip(' ') for v in via_header.split(',')] |
47 # The Via header is valid if it is the old format or the new format | 46 # The Via header is valid if it has a 4-character version prefix followed by |
48 # with 4-character version prefix, for example, | 47 # the proxy name, for example, "1.1 Chrome-Compression-Proxy". |
49 # "1.1 Chrome-Compression-Proxy". | 48 return any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias) |
50 return (CHROME_PROXY_VIA_HEADER_DEPRECATED in vias or | |
51 any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias)) | |
52 | 49 |
53 def IsValidByViaHeader(self): | 50 def IsValidByViaHeader(self): |
54 return (not self.ShouldHaveChromeProxyViaHeader() or | 51 return (not self.ShouldHaveChromeProxyViaHeader() or |
55 self.HasChromeProxyViaHeader()) | 52 self.HasChromeProxyViaHeader()) |
56 | 53 |
57 def GetChromeProxyClientType(self): | 54 def GetChromeProxyClientType(self): |
58 """Get the client type directive from the Chrome-Proxy request header. | 55 """Get the client type directive from the Chrome-Proxy request header. |
59 | 56 |
60 Returns: | 57 Returns: |
61 The client type directive from the Chrome-Proxy request header for the | 58 The client type directive from the Chrome-Proxy request header for the |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 # The first response(s) coming from fallback_response_host should be | 313 # The first response(s) coming from fallback_response_host should be |
317 # through the HTTP fallback proxy. | 314 # through the HTTP fallback proxy. |
318 resp = next(responses, None) | 315 resp = next(responses, None) |
319 while resp and fallback_response_host in resp.response.url: | 316 while resp and fallback_response_host in resp.response.url: |
320 if fallback_response_host in resp.response.url: | 317 if fallback_response_host in resp.response.url: |
321 if (not resp.HasChromeProxyViaHeader() or resp.remote_port != 80): | 318 if (not resp.HasChromeProxyViaHeader() or resp.remote_port != 80): |
322 r = resp.response | 319 r = resp.response |
323 raise ChromeProxyMetricException, ( | 320 raise ChromeProxyMetricException, ( |
324 'Response for %s should have come through the fallback proxy.\n' | 321 'Response for %s should have come through the fallback proxy.\n' |
325 'Response: remote_port=%s status=(%d, %s)\nHeaders:\n %s' % ( | 322 'Response: remote_port=%s status=(%d, %s)\nHeaders:\n %s' % ( |
326 r.url, str(fallback_resp.remote_port), r.status, r.status_text, | 323 r.url, str(resp.remote_port), r.status, r.status_text, |
327 r.headers)) | 324 r.headers)) |
328 else: | 325 else: |
329 via_fallback_count += 1 | 326 via_fallback_count += 1 |
330 resp = next(responses, None) | 327 resp = next(responses, None) |
331 | 328 |
332 # All other responses should be bypassed. | 329 # All other responses should be bypassed. |
333 while resp: | 330 while resp: |
334 if resp.HasChromeProxyViaHeader(): | 331 if resp.HasChromeProxyViaHeader(): |
335 r = resp.response | 332 r = resp.response |
336 raise ChromeProxyMetricException, ( | 333 raise ChromeProxyMetricException, ( |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 'Response for %s should have via header; proxy should no longer ' | 413 'Response for %s should have via header; proxy should no longer ' |
417 'be bypassed.\nReponse: status=(%d, %s)\nHeaders:\n %s' % ( | 414 'be bypassed.\nReponse: status=(%d, %s)\nHeaders:\n %s' % ( |
418 r.url, r.status, r.status_text, r.headers)) | 415 r.url, r.status, r.status_text, r.headers)) |
419 else: | 416 else: |
420 via_count += 1 | 417 via_count += 1 |
421 | 418 |
422 results.AddValue(scalar.ScalarValue( | 419 results.AddValue(scalar.ScalarValue( |
423 results.current_page, 'bypass', 'count', bypass_count)) | 420 results.current_page, 'bypass', 'count', bypass_count)) |
424 results.AddValue(scalar.ScalarValue( | 421 results.AddValue(scalar.ScalarValue( |
425 results.current_page, 'via', 'count', via_count)) | 422 results.current_page, 'via', 'count', via_count)) |
OLD | NEW |