| OLD | NEW |
| 1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 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 """ | 5 """ |
| 6 A http client with support for https connections with certificate verification. | 6 A http client with support for https connections with certificate verification. |
| 7 | 7 |
| 8 The verification is based on http://tools.ietf.org/html/rfc6125#section-6.4.3 | 8 The verification is based on http://tools.ietf.org/html/rfc6125#section-6.4.3 |
| 9 and the code is from Lib/ssl.py in python3: | 9 and the code is from Lib/ssl.py in python3: |
| 10 http://hg.python.org/cpython/file/4dac45f88d45/Lib/ssl.py | 10 http://hg.python.org/cpython/file/4dac45f88d45/Lib/ssl.py |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 content = None | 211 content = None |
| 212 | 212 |
| 213 try: | 213 try: |
| 214 response = url_opener.open(url, timeout=timeout) | 214 response = url_opener.open(url, timeout=timeout) |
| 215 | 215 |
| 216 status_code = response.code | 216 status_code = response.code |
| 217 content = response.read() | 217 content = response.read() |
| 218 except urllib2.HTTPError as e: | 218 except urllib2.HTTPError as e: |
| 219 status_code = e.code | 219 status_code = e.code |
| 220 content = None | 220 content = None |
| 221 except (ssl.SSLError, httplib.BadStatusLine, IOError): |
| 222 status_code = -1 |
| 223 content = None |
| 221 | 224 |
| 222 return status_code, content | 225 return status_code, content |
| 223 | 226 |
| 224 | 227 |
| 225 class HttpClientLocal(http_client.HttpClient): | 228 class HttpClientLocal(http_client.HttpClient): |
| 226 """This http client is used locally in a workstation, GCE VMs, etc.""" | 229 """This http client is used locally in a workstation, GCE VMs, etc.""" |
| 227 | 230 |
| 228 @staticmethod | 231 @staticmethod |
| 229 def Get(url, params={}, timeout=60, retries=5, retry_interval=0.5, | 232 def Get(url, params={}, timeout=120, retries=5, retry_interval=0.5, |
| 230 retry_if_not=None): | 233 retry_if_not=None): |
| 231 if params: | 234 if params: |
| 232 url = '%s?%s' % (url, urllib.urlencode(params)) | 235 url = '%s?%s' % (url, urllib.urlencode(params)) |
| 233 | 236 |
| 234 count = 0 | 237 count = 0 |
| 235 while True: | 238 while True: |
| 236 count += 1 | 239 count += 1 |
| 237 | 240 |
| 238 status_code, content = _SendRequest(url, timeout=timeout) | 241 status_code, content = _SendRequest(url, timeout=timeout) |
| 239 if status_code == 200: | 242 if status_code == 200: |
| 240 return status_code, content | 243 return status_code, content |
| 241 if retry_if_not and status_code == retry_if_not: | 244 if retry_if_not and status_code == retry_if_not: |
| 242 return status_code, content | 245 return status_code, content |
| 243 | 246 |
| 244 if count < retries: | 247 if count < retries: |
| 245 time.sleep(retry_interval) | 248 time.sleep(retry_interval) |
| 246 else: | 249 else: |
| 247 return status_code, content | 250 return status_code, content |
| 248 | 251 |
| 249 # Should never be reached. | 252 # Should never be reached. |
| 250 return status_code, content | 253 return status_code, content |
| OLD | NEW |