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.
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
):
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
):
27 # Ignore https and data url
28 if resp
.url
.startswith('https') or resp
.url
.startswith('data:'):
30 # Ignore 304 Not Modified and cache hit.
31 if resp
.status
== 304 or resp
.served_from_cache
:
33 # Ignore invalid responses that don't have any header. Log a warning.
35 logging
.warning('response for %s does not any have header '
36 '(refer=%s, status=%s)',
37 resp
.url
, resp
.GetHeader('Referer'), resp
.status
)
41 def HasChromeProxyViaHeader(self
):
42 via_header
= self
.response
.GetHeader('Via')
45 vias
= [v
.strip(' ') for v
in via_header
.split(',')]
46 # The Via header is valid if it has a 4-character version prefix followed by
47 # the proxy name, for example, "1.1 Chrome-Compression-Proxy".
48 return any(v
[4:] == CHROME_PROXY_VIA_HEADER
for v
in vias
)
50 def HasExtraViaHeader(self
, extra_header
):
51 via_header
= self
.response
.GetHeader('Via')
54 vias
= [v
.strip(' ') for v
in via_header
.split(',')]
55 return any(v
== extra_header
for v
in vias
)
57 def IsValidByViaHeader(self
):
58 return (not self
.ShouldHaveChromeProxyViaHeader() or
59 self
.HasChromeProxyViaHeader())
61 def GetChromeProxyClientType(self
):
62 """Get the client type directive from the Chrome-Proxy request header.
65 The client type directive from the Chrome-Proxy request header for the
66 request that lead to this response. For example, if the request header
67 "Chrome-Proxy: c=android" is present, then this method would return
68 "android". Returns None if no client type directive is present.
70 if 'Chrome-Proxy' not in self
.response
.request_headers
:
73 chrome_proxy_request_header
= self
.response
.request_headers
['Chrome-Proxy']
74 values
= [v
.strip() for v
in chrome_proxy_request_header
.split(',')]
76 kvp
= value
.split('=', 1)
77 if len(kvp
) == 2 and kvp
[0].strip() == 'c':
81 def HasChromeProxyLoFiRequest(self
):
82 if 'Chrome-Proxy' not in self
.response
.request_headers
:
84 chrome_proxy_request_header
= self
.response
.request_headers
['Chrome-Proxy']
85 values
= [v
.strip() for v
in chrome_proxy_request_header
.split(',')]
86 return any(v
== "q=low" for v
in values
)
88 def HasChromeProxyLoFiResponse(self
):
89 chrome_proxy_response_header
= self
.response
.GetHeader('Chrome-Proxy')
90 if not chrome_proxy_response_header
:
92 values
= [v
.strip() for v
in chrome_proxy_response_header
.split(',')]
93 return any(v
== "q=low" for v
in values
)