| 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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 # to a man in the middle. | 198 # to a man in the middle. |
| 199 handlers.append(HTTPSHandler()) | 199 handlers.append(HTTPSHandler()) |
| 200 | 200 |
| 201 | 201 |
| 202 cookie_file = os.environ.get('COOKIE_FILE') | 202 cookie_file = os.environ.get('COOKIE_FILE') |
| 203 if cookie_file and os.path.exists(cookie_file): | 203 if cookie_file and os.path.exists(cookie_file): |
| 204 handlers.append( | 204 handlers.append( |
| 205 urllib2.HTTPCookieProcessor(cookielib.MozillaCookieJar(cookie_file))) | 205 urllib2.HTTPCookieProcessor(cookielib.MozillaCookieJar(cookie_file))) |
| 206 | 206 |
| 207 url_opener = urllib2.build_opener(*handlers) | 207 url_opener = urllib2.build_opener(*handlers) |
| 208 if timeout is not None: | 208 |
| 209 status_code = None |
| 210 content = None |
| 211 |
| 212 try: |
| 209 response = url_opener.open(url, timeout=timeout) | 213 response = url_opener.open(url, timeout=timeout) |
| 210 else: | 214 |
| 211 response = url_opener.open(url) | 215 status_code = response.code |
| 212 return response.code, response.read() | 216 content = response.read() |
| 217 except urllib2.HTTPError as e: |
| 218 status_code = e.code |
| 219 content = None |
| 220 |
| 221 return status_code, content |
| 213 | 222 |
| 214 | 223 |
| 215 class HttpClientLocal(http_client.HttpClient): | 224 class HttpClientLocal(http_client.HttpClient): |
| 216 """This http client is used locally in a workstation, GCE VMs, etc.""" | 225 """This http client is used locally in a workstation, GCE VMs, etc.""" |
| 217 | 226 |
| 218 @staticmethod | 227 @staticmethod |
| 219 def Get(url, params={}, timeout=None): | 228 def Get(url, params={}, timeout=None): |
| 220 if params: | 229 if params: |
| 221 url = '%s?%s' % (url, urllib.urlencode(params)) | 230 url = '%s?%s' % (url, urllib.urlencode(params)) |
| 222 return _SendRequest(url, timeout=timeout) | 231 return _SendRequest(url, timeout=timeout) |
| OLD | NEW |