~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/util.py

Merged 416109-arbitrary-endpoints [r=therve,jkakar] [f=416109].

The primary change of this branch is support of arbitrary endpoints (needed for
the support of Eucalyptus). In addition, the following was also performed:
 * Added a parse utility function from Twisted
 * Created a testing subpackage for use by txAWS unit tests
 * Created a service module for abstracting regions and associated
   serices/credentials

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from base64 import b64encode
8
8
from hashlib import sha1, md5
9
9
import hmac
 
10
from urlparse import urlparse, urlunparse
10
11
import time
11
12
 
12
13
# Import XML from somwhere; here in one place to prevent duplication.
38
39
        return time.strftime("%Y-%m-%dT%H:%M:%SZ", time_tuple)
39
40
    else:
40
41
        return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
 
42
 
 
43
 
 
44
def parse(url, defaultPort=None):
 
45
    """
 
46
    Split the given URL into the scheme, host, port, and path.
 
47
 
 
48
    @type url: C{str}
 
49
    @param url: An URL to parse.
 
50
 
 
51
    @type defaultPort: C{int} or C{None}
 
52
    @param defaultPort: An alternate value to use as the port if the URL does
 
53
    not include one.
 
54
 
 
55
    @return: A four-tuple of the scheme, host, port, and path of the URL.  All
 
56
    of these are C{str} instances except for port, which is an C{int}.
 
57
    """
 
58
    url = url.strip()
 
59
    parsed = urlparse(url)
 
60
    scheme = parsed[0]
 
61
    path = urlunparse(('', '') + parsed[2:])
 
62
    if defaultPort is None:
 
63
        if scheme == 'https':
 
64
            defaultPort = 443
 
65
        else:
 
66
            defaultPort = 80
 
67
    host, port = parsed[1], defaultPort
 
68
    if ':' in host:
 
69
        host, port = host.split(':')
 
70
        try:
 
71
            port = int(port)
 
72
        except ValueError:
 
73
            port = defaultPort
 
74
    if path == '':
 
75
        path = '/'
 
76
    return scheme, host, port, path