Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: appengine/findit/common/http_client_appengine.py

Issue 2116073002: [Findit] Fix redirect bug and update template for waterfall/culprit. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Add one unittest. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | appengine/findit/common/rietveld.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import auth_util
11 from common.retry_http_client import RetryHttpClient 11 from common.retry_http_client import RetryHttpClient
12 12
13 13
14 # TODO(katesonia): Move this to config. 14 # TODO(katesonia): Move this to config.
15 _INTERNAL_HOSTS_TO_SCOPES = { 15 _INTERNAL_HOSTS_TO_SCOPES = {
16 'https://chrome-internal.googlesource.com/': ( 16 'https://chrome-internal.googlesource.com/': (
17 'https://www.googleapis.com/auth/gerritcodereview'), 17 'https://www.googleapis.com/auth/gerritcodereview'),
18 'https://codereview.chromium.org/': ( 18 'https://codereview.chromium.org/': (
19 'https://www.googleapis.com/auth/userinfo.email'), 19 'https://www.googleapis.com/auth/userinfo.email'),
20 } 20 }
21 21
22 22
23 class HttpClientAppengine(RetryHttpClient): # pragma: no cover 23 class HttpClientAppengine(RetryHttpClient): # pragma: no cover
24 """A http client for running on appengine.""" 24 """A http client for running on appengine."""
25 25
26 def __init__(self, follow_redirects=True, *args, **kwargs):
27 super(HttpClientAppengine, self).__init__(*args, **kwargs)
28 self.follow_redirects = follow_redirects
29
26 def _ExpandAuthorizationHeaders(self, headers, scope): 30 def _ExpandAuthorizationHeaders(self, headers, scope):
27 headers['Authorization'] = 'Bearer ' + auth_util.GetAuthToken(scope) 31 headers['Authorization'] = 'Bearer ' + auth_util.GetAuthToken(scope)
28 32
29 def _ShouldLogError(self, status_code): 33 def _ShouldLogError(self, status_code):
30 if not self.no_error_logging_statuses: 34 if not self.no_error_logging_statuses:
31 return True 35 return True
32 return status_code not in self.no_error_logging_statuses 36 return status_code not in self.no_error_logging_statuses
33 37
34 def _SendRequest(self, url, method, data, timeout, headers=None): 38 def _SendRequest(self, url, method, data, timeout, headers=None):
35 # We wanted to validate certificate to avoid the man in the middle. 39 # We wanted to validate certificate to avoid the man in the middle.
36 if not headers: 40 if not headers:
37 headers = {} 41 headers = {}
38 42
39 # For google internal hosts, expand Oauth2.0 token to headers to authorize 43 # For google internal hosts, expand Oauth2.0 token to headers to authorize
40 # the requests. 44 # the requests.
41 for host, scope in _INTERNAL_HOSTS_TO_SCOPES.iteritems(): 45 for host, scope in _INTERNAL_HOSTS_TO_SCOPES.iteritems():
42 if url.startswith(host): 46 if url.startswith(host):
43 self._ExpandAuthorizationHeaders(headers, scope) 47 self._ExpandAuthorizationHeaders(headers, scope)
44 logging.debug('Authorization header of scope %s was added for %s', 48 logging.debug('Authorization header of scope %s was added for %s',
45 scope, url) 49 scope, url)
46 break 50 break
47 51
48 if method in (urlfetch.POST, urlfetch.PUT): 52 if method in (urlfetch.POST, urlfetch.PUT):
49 result = urlfetch.fetch( 53 result = urlfetch.fetch(
50 url, payload=data, method=method, headers=headers, deadline=timeout, 54 url, payload=data, method=method, headers=headers, deadline=timeout,
51 follow_redirects=False, validate_certificate=True) 55 follow_redirects=self.follow_redirects, validate_certificate=True)
52 else: 56 else:
53 result = urlfetch.fetch( 57 result = urlfetch.fetch(
54 url, headers=headers, deadline=timeout, 58 url, headers=headers, deadline=timeout,
55 follow_redirects=False, validate_certificate=True) 59 follow_redirects=self.follow_redirects, validate_certificate=True)
56 60
57 if (result.status_code != 200 and self._ShouldLogError(result.status_code)): 61 if (result.status_code != 200 and self._ShouldLogError(result.status_code)):
58 logging.error('Request to %s resulted in %d, headers:%s', url, 62 logging.error('Request to %s resulted in %d, headers:%s', url,
59 result.status_code, json.dumps(result.headers.items())) 63 result.status_code, json.dumps(result.headers.items()))
60 64
61 return result.status_code, result.content 65 return result.status_code, result.content
62 66
63 def _Get(self, url, timeout, headers): 67 def _Get(self, url, timeout, headers):
64 return self._SendRequest(url, urlfetch.GET, None, timeout, headers) 68 return self._SendRequest(url, urlfetch.GET, None, timeout, headers)
65 69
66 def _Post(self, url, data, timeout, headers): 70 def _Post(self, url, data, timeout, headers):
67 return self._SendRequest(url, urlfetch.POST, data, timeout, headers) 71 return self._SendRequest(url, urlfetch.POST, data, timeout, headers)
68 72
69 def _Put(self, url, data, timeout, headers): 73 def _Put(self, url, data, timeout, headers):
70 return self._SendRequest(url, urlfetch.PUT, data, timeout, headers) 74 return self._SendRequest(url, urlfetch.PUT, data, timeout, headers)
OLDNEW
« no previous file with comments | « no previous file | appengine/findit/common/rietveld.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698