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

Unified Diff: tools/findit/common/http_client_local.py

Issue 465403004: [Findit] Support sending cookies for http requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/findit/common/http_client.py ('k') | tools/findit/common/http_client_local_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/findit/common/http_client_local.py
diff --git a/tools/findit/https.py b/tools/findit/common/http_client_local.py
similarity index 82%
rename from tools/findit/https.py
rename to tools/findit/common/http_client_local.py
index c382f0cf7f1cbf65250135164e189af1aa2e33cd..bf386f9a3968ddbc183ca3687ac570d2bbff9f56 100644
--- a/tools/findit/https.py
+++ b/tools/findit/common/http_client_local.py
@@ -1,8 +1,9 @@
# Copyright (c) 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+
"""
-Provides a utility function for https connections with certificate verification.
+A http client with support for https connections with certificate verification.
The verification is based on http://tools.ietf.org/html/rfc6125#section-6.4.3
and the code is from Lib/ssl.py in python3:
@@ -14,13 +15,17 @@ One use case is to download Chromium DEPS file in a secure way:
Notice: python 2.7 or newer is required.
"""
+import cookielib
import httplib
import os
import re
import socket
import ssl
+import urllib
import urllib2
+import http_client
+
_SCRIPT_DIR = os.path.dirname(__file__)
_TRUSTED_ROOT_CERTS = os.path.join(_SCRIPT_DIR, 'cacert.pem')
@@ -171,25 +176,47 @@ class HTTPSHandler(urllib2.HTTPSHandler):
return HTTPSConnection(host, **params)
-def SendRequest(https_url):
+def _SendRequest(url, timeout=None):
"""Send request to the given https url, and return the server response.
Args:
- https_url: The https url to send request to.
+ url: The https url to send request to.
Returns:
- A string that is the response from the server.
+ An integer: http code of the response.
+ A string: content of the response.
Raises:
- ValueError: Unexpected value is received during certificate verification.
CertificateError: Certificate verification fails.
"""
- if not https_url or not https_url.startswith('https://'):
- raise ValueError('Not a https request for url %s.' % str(https_url))
+ if not url:
+ return None, None
+
+ handlers = []
+ if url.startswith('https://'):
+ # HTTPSHandler has to go first, because we don't want to send secure cookies
+ # to a man in the middle.
+ handlers.append(HTTPSHandler())
+
+
+ cookie_file = os.environ.get('COOKIE_FILE')
+ if cookie_file and os.path.exists(cookie_file):
+ handlers.append(
+ urllib2.HTTPCookieProcessor(cookielib.MozillaCookieJar(cookie_file)))
+
+ url_opener = urllib2.build_opener(*handlers)
+ if timeout is not None:
+ response = url_opener.open(url, timeout=timeout)
+ else:
+ response = url_opener.open(url)
+ return response.code, response.read()
- url_opener = urllib2.build_opener(HTTPSHandler)
- return url_opener.open(https_url).read()
+class HttpClientLocal(http_client.HttpClient):
+ """This http client is used locally in a workstation, GCE VMs, etc."""
-if __name__ == '__main__':
- print SendRequest('https://src.chromium.org/chrome/trunk/src/DEPS')
+ @staticmethod
+ def Get(url, params={}, timeout=None):
+ if params:
+ url = '%s?%s' % (url, urllib.urlencode(params))
+ return _SendRequest(url, timeout=timeout)
« no previous file with comments | « tools/findit/common/http_client.py ('k') | tools/findit/common/http_client_local_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698