Commits

Daniel Graña  committed e21d2d6

refactor basic authorization request signing and restore usenetrc flag name

  • Participants
  • Parent commits 8d4ed52

Comments (0)

Files changed (1)

 '''send diffs from Mercurial to various pastebin websites
 '''
 
-import base64
 import sys
 import urllib2
+from netrc import netrc
+from urlparse import urlparse
+from base64 import urlsafe_b64encode
 from mercurial import cmdutil, commands, help, util
 from urllib import urlencode
 
         url = pastebins['dpaste']['url']
 
     request = urllib2.Request(url, data)
-    if parameters['httpauth']:
-        request.add_header('Authorization', 'Basic %s' \
-                    % base64.encodestring(parameters['httpauth'])[:-1])
-    elif parameters['netrc']:
-        auth = _netrc_get_credentials(url)
-        if auth:
-            b64str = base64.urlsafe_b64encode('%s:%s' % (auth[0], auth[2]))
-            request.add_header('Authorization', 'Basic %s' % b64str)
+    _authorize_request(request, **parameters)
     response = urllib2.urlopen(request)
     return response.geturl()
 
         url = pastebins['dpaste.org']['url']
 
     request = urllib2.Request(url, data)
-    if parameters['httpauth']:
-        request.add_header('Authorization', 'Basic %s' \
-                    % base64.encodestring(parameters['httpauth'])[:-1])
-    elif parameters['netrc']:
-        auth = _netrc_get_credentials(url)
-        if auth:
-            request.add_header('Authorization', 'Basic %s' \
-                    % base64.encodestring('%s:%s' % (auth[0], auth[2]))[:-1])
+    _authorize_request(request, **parameters)
     response = urllib2.urlopen(request)
     return response.geturl()
 
-def _netrc_get_credentials(url):
-    import netrc, urlparse
-    credentials = netrc.netrc()
-    host = urlparse.urlparse(url).hostname
-    return credentials.authenticators(host)
-
 pastebins = {
     'dpaste': { 'url': 'http://dpaste.com/api/v1/',
                 'parameters': {
     },
 }
 
+def _authorize_request(request, httpauth=None, usenetrc=None, **kwargs):
+    """Sets basic authorization header using httpauth flag or netrc if enabled"""
+    if httpauth:
+        b64cred = urlsafe_b64encode(httpauth)
+        request.add_header('Authorization', 'Basic %s' % b64cred)
+    elif usenetrc:
+        host = urlparse(request.url).hostname
+        cred = netrc().authenticators(host)
+        if cred:
+            b64cred = urlsafe_b64encode('%s:%s' % (cred[0], cred[2]))
+            request.add_header('Authorization', 'Basic %s' % b64cred)
+
+
 def paste(ui, repo, *fnames, **opts):
     '''send diffs from Mercurial to various pastebin websites
     
         ('',  'dry-run', False, 'do not paste to the pastebin'),
         ('',  'url', '', 'perform request against this url'),
         ('',  'httpauth', '', 'http authorization (user:pass)'),
-        ('',  'netrc', False, 'use ~/.netrc for http authorization'),
+        ('',  'usenetrc', False, 'use ~/.netrc for http authorization'),
         ('',  'stdin', False, 'read content from standard input'),
         ('',  'syntax', '', 'choose syntax'),
     ] + commands.diffopts + commands.walkopts,