Commit b64e8366 authored by Laurent Bachelier's avatar Laurent Bachelier

More URL building options

parent 7157f2bf
......@@ -38,7 +38,13 @@ class GetUrlCmd(Command):
@staticmethod
def configure_parser(parser):
parser.add_argument('path', nargs='+')
parser.add_argument('-u', '--user')
parser.add_argument('-u', '--user', metavar='USERNAME')
parser.add_argument('-K', '--no-key',
action='store_false', dest='use_key',
help="Don't use the user's key")
parser.add_argument('-H', '--http-auth',
action='store_true',
help="Use HTTP authentication (for downloaders for instance)")
def cmd(self, args):
root_url = build_root_url(self.storage)
......@@ -58,7 +64,7 @@ class GetUrlCmd(Command):
else:
user = Anonymous()
if user.exists and user.key is None:
if args.use_key and user.exists and user.key is None:
print >>sys.stderr, \
'Warning: user %s has no key.' % user.name, \
'Use contacts genkey %s to create one.' % user.name
......@@ -70,7 +76,8 @@ class GetUrlCmd(Command):
f = self.storage.get_file_from_realpath(os.path.realpath(path))
if not f:
print >>sys.stderr, 'Error: Path "%s" is not in working directory.' % path
print >>sys.stderr, \
'Error: Path "%s" is not in working directory.' % path
continue
if not user.has_perms(f, f.PERM_READ):
......@@ -83,7 +90,8 @@ class GetUrlCmd(Command):
print >>sys.stderr, \
'Warning: anonymous users cannot read %s.' % (f.path)
print quote_url(build_url(root_url, f, user))
print quote_url(build_url(root_url, f, user=user,
use_key=args.use_key, http_auth=args.http_auth))
class SendPlugin(Plugin):
......
......@@ -21,6 +21,7 @@ import os
from mako.lookup import TemplateLookup
from paste.url import URL
from urlparse import urlsplit, urlunsplit
from .storage import Storage
from .version import VERSION
......@@ -67,17 +68,28 @@ def build_root_url(storage):
return URL(root_url.encode('utf-8'))
def build_url(root_url, f, user=None):
def build_url(root_url, f, user=None, use_key=True, http_auth=False):
"""
Build an URL for a particular file and user if provided.
root_url: URL (paste.url)
f: File
user: User
use_key: Add the user key to the URL if possible
http_auth: Make an URL for HTTP authentication (for downloaders for instance)
"""
qs = {}
if user and user.key:
if http_auth:
qs['authby'] = 'http'
purl = urlsplit(root_url.url)
netloc = purl.netloc
if use_key and user and user.key:
netloc = '%s:%s@%s' % ('_key', user.key, netloc)
elif user and user.password:
netloc = '%s@%s' % (user.name, netloc)
root_url = URL(urlunsplit((purl.scheme, netloc, purl.path, purl.query, purl.fragment)))
elif use_key and user and user.key:
qs['authkey'] = user.key
path = f.path
if f.isdir():
path += '/'
return root_url.addpath(path).setvar(**qs)
return root_url.addpath(path).setvars(**qs)
......@@ -62,3 +62,31 @@ class BuildURLTest(TestCase):
self.storage.get_file('/penguin'),
self.storage.get_user('user2'))) \
== 'http://penguin:42/penguin'
# user.key, but don't want it
assert quote_url(build_url(self.root_url,
self.storage.get_file('/penguin'),
self.storage.get_user('user1'),
use_key=False)) \
== 'http://penguin:42/penguin'
def test_buildHttpAuthUrls(self):
assert quote_url(build_url(self.root_url,
self.storage.get_file('/penguin'),
self.storage.get_user('user1'),
http_auth=True)) \
== 'http://_key:fabf37d746da8a45df63489f642b3813@penguin:42/penguin?authby=http'
# no user.key
assert quote_url(build_url(self.root_url,
self.storage.get_file('/penguin'),
self.storage.get_user('user2'),
http_auth=True)) \
== 'http://user2@penguin:42/penguin?authby=http'
# user.key, but don't want it
assert quote_url(build_url(self.root_url,
self.storage.get_file('/penguin'),
self.storage.get_user('user1'),
http_auth=True, use_key=False)) \
== 'http://user1@penguin:42/penguin?authby=http'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment