summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Coldrick <adam.coldrick@codethink.co.uk>2014-03-17 16:20:48 +0000
committerAdam Coldrick <adam.coldrick@codethink.co.uk>2014-03-17 16:20:48 +0000
commite7332af8c97f369827599a63ce71084aa76ae8ee (patch)
treed4083ecac44151bf5d160c11aa0805e74d5cd391
parent13f45d374b52a449a56f4ccf669c6466a56c2c20 (diff)
downloadlorry-controller-adamcoldrick/https-fix.tar.gz
Fix how https requests are handled by adding an auth handleradamcoldrick/https-fix
-rwxr-xr-xlorry-controller16
1 files changed, 12 insertions, 4 deletions
diff --git a/lorry-controller b/lorry-controller
index d9dc1e0..0ae4ceb 100755
--- a/lorry-controller
+++ b/lorry-controller
@@ -285,10 +285,10 @@ class LorryController(cliapp.Application):
self.runcmd(['git']+args, cwd=os.path.join(self.settings['work-area'],
'git'))
- def maybe_http_request(self, url, dry=False):
+ def maybe_http_request(self, url, auth=None, dry=False):
"""If not a dry run, make an HTTP request and return its output."""
if (not self.settings['dry-run']) or dry:
- return self.http_request(url)
+ return self.http_request(url, auth)
else:
logging.debug('DRY-RUN: Not sending a request to %s' % url)
return 0, 'DRY-RUN', 'DRY-RUN'
@@ -300,7 +300,7 @@ class LorryController(cliapp.Application):
logging.debug("DRY-RUN: Not running %r" % cmdline)
return 0, 'DRY-RUN', 'DRY-RUN'
- def http_request(self, url):
+ def http_request(self, url, auth=None):
"""Make an HTTP request to the given url, return the output.
Make an HTTP request to `url`. If the request succeeds (response code
@@ -310,7 +310,15 @@ class LorryController(cliapp.Application):
"""
request = urllib2.Request(url, None, {})
- response = urllib2.urlopen(request)
+ if auth:
+ password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
+ password_mgr.add_password(
+ None, url, auth['username'], auth['password'])
+ auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
+ opener = urllib2.build_opener(auth_handler)
+ response = opener.open(url)
+ else:
+ response = urllib2.urlopen(request)
code = response.getcode()
if code == 200:
return 0, response.read(), '200'