Chromium Code Reviews| 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 json | 5 import json |
| 6 import logging | 6 import logging |
| 7 | 7 |
| 8 from google.appengine.api import urlfetch | 8 from google.appengine.api import urlfetch |
| 9 | 9 |
| 10 from common import auth_util | |
| 10 from common.retry_http_client import RetryHttpClient | 11 from common.retry_http_client import RetryHttpClient |
| 11 | 12 |
| 12 | 13 |
| 14 #TODO(katesonia): Move this to config. | |
| 15 _INTERNAL_HOSTS = ['https://chrome-internal.googlesource.com'] | |
|
stgao
2016/05/06 20:35:44
To be safe, we may want to add a tailing "/", "...
Sharu Jiang
2016/05/06 22:23:03
Done.
| |
| 16 | |
| 17 | |
| 13 class HttpClientAppengine(RetryHttpClient): # pragma: no cover | 18 class HttpClientAppengine(RetryHttpClient): # pragma: no cover |
| 14 """A http client for running on appengine.""" | 19 """A http client for running on appengine.""" |
| 20 | |
| 21 def _ExpandAuthorizationHeaders(self, headers): | |
| 22 headers['Authorization'] = 'Bearer ' + auth_util.GetAuthToken() | |
| 23 | |
| 15 def _ShouldLogError(self, status_code): | 24 def _ShouldLogError(self, status_code): |
| 16 if not self.no_error_logging_statuses: | 25 if not self.no_error_logging_statuses: |
| 17 return True | 26 return True |
| 18 return status_code not in self.no_error_logging_statuses | 27 return status_code not in self.no_error_logging_statuses |
| 19 | 28 |
| 20 def _SendRequest(self, url, method, data, timeout, headers=None): | 29 def _SendRequest(self, url, method, data, timeout, headers=None): |
| 21 # We wanted to validate certificate to avoid the man in the middle. | 30 # We wanted to validate certificate to avoid the man in the middle. |
| 22 if not headers: | 31 if not headers: |
| 23 headers = {} | 32 headers = {} |
| 33 | |
| 34 # For google internal hosts, expand Oauth2.0 token to headers to autherize | |
| 35 # the requests. | |
| 36 for host in _INTERNAL_HOSTS: | |
| 37 if url.startswith(host): | |
| 38 self._ExpandAuthorizationHeaders(headers) | |
| 39 break | |
| 40 | |
| 24 if method in (urlfetch.POST, urlfetch.PUT): | 41 if method in (urlfetch.POST, urlfetch.PUT): |
| 25 result = urlfetch.fetch( | 42 result = urlfetch.fetch( |
| 26 url, payload=data, method=method, | 43 url, payload=data, method=method, |
| 27 headers=headers, deadline=timeout, validate_certificate=True) | 44 headers=headers, deadline=timeout, validate_certificate=True) |
| 28 else: | 45 else: |
| 29 result = urlfetch.fetch( | 46 result = urlfetch.fetch( |
| 30 url, headers=headers, deadline=timeout, validate_certificate=True) | 47 url, headers=headers, deadline=timeout, validate_certificate=True) |
| 31 | 48 |
| 32 if (result.status_code != 200 and self._ShouldLogError(result.status_code)): | 49 if (result.status_code != 200 and self._ShouldLogError(result.status_code)): |
| 33 logging.error('Request to %s resulted in %d, headers:%s', url, | 50 logging.error('Request to %s resulted in %d, headers:%s', url, |
| 34 result.status_code, json.dumps(result.headers.items())) | 51 result.status_code, json.dumps(result.headers.items())) |
| 35 | 52 |
| 36 return result.status_code, result.content | 53 return result.status_code, result.content |
| 37 | 54 |
| 38 def _Get(self, url, timeout, headers): | 55 def _Get(self, url, timeout, headers): |
| 39 return self._SendRequest(url, urlfetch.GET, None, timeout, headers) | 56 return self._SendRequest(url, urlfetch.GET, None, timeout, headers) |
| 40 | 57 |
| 41 def _Post(self, url, data, timeout, headers): | 58 def _Post(self, url, data, timeout, headers): |
| 42 return self._SendRequest(url, urlfetch.POST, data, timeout, headers) | 59 return self._SendRequest(url, urlfetch.POST, data, timeout, headers) |
| 43 | 60 |
| 44 def _Put(self, url, data, timeout, headers): | 61 def _Put(self, url, data, timeout, headers): |
| 45 return self._SendRequest(url, urlfetch.PUT, data, timeout, headers) | 62 return self._SendRequest(url, urlfetch.PUT, data, timeout, headers) |
| OLD | NEW |