From db3746d122fb9b435d423fb6a476eb53745b5be2 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 20 Oct 2012 20:23:32 +0200 Subject: [PATCH] Add support for authentication scheme. This commit closes http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=690879 Signed-off-by: James McCoy --- src/utils/web.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/utils/web.py b/src/utils/web.py index b0cf22759..fe0e939de 100644 --- a/src/utils/web.py +++ b/src/utils/web.py @@ -29,6 +29,7 @@ ### import re +import base64 import socket import urllib import urllib2 @@ -58,10 +59,11 @@ _ipAddr = r'%s(?:\.%s){3}' % (_octet, _octet) # Base domain regex off RFC 1034 and 1738 _label = r'[0-9a-z][-0-9a-z]*[0-9a-z]?' _domain = r'%s(?:\.%s)*\.[0-9a-z][-0-9a-z]+' % (_label, _label) -_urlRe = r'(\w+://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' % (_domain, _ipAddr) +_urlRe = r'(\w+://(?:\S+@)?(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' % (_domain, + _ipAddr) urlRe = re.compile(_urlRe, re.I) -_httpUrlRe = r'(https?://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' % (_domain, - _ipAddr) +_httpUrlRe = r'(https?://(?:\S+@)?(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' % \ + (_domain, _ipAddr) httpUrlRe = re.compile(_httpUrlRe, re.I) REFUSED = 'Connection refused.' @@ -108,6 +110,12 @@ def getUrlFd(url, headers=None, data=None, timeout=None): if '#' in url: url = url[:url.index('#')] request = urllib2.Request(url, headers=headers, data=data) + if '@' in url: + scheme, url = url.split('://', 2) + auth, url = url.split('@') + url = scheme + '://' + url + request.add_header('Authorization', + 'Basic ' + base64.b64encode(auth)) else: request = url request.add_data(data)