OpenSecurity/install/web.py-0.37/build/lib/web/http.py
changeset 3 65432e6c6042
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenSecurity/install/web.py-0.37/build/lib/web/http.py	Mon Dec 02 14:02:05 2013 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +"""
     1.5 +HTTP Utilities
     1.6 +(from web.py)
     1.7 +"""
     1.8 +
     1.9 +__all__ = [
    1.10 +  "expires", "lastmodified", 
    1.11 +  "prefixurl", "modified", 
    1.12 +  "changequery", "url",
    1.13 +  "profiler",
    1.14 +]
    1.15 +
    1.16 +import sys, os, threading, urllib, urlparse
    1.17 +try: import datetime
    1.18 +except ImportError: pass
    1.19 +import net, utils, webapi as web
    1.20 +
    1.21 +def prefixurl(base=''):
    1.22 +    """
    1.23 +    Sorry, this function is really difficult to explain.
    1.24 +    Maybe some other time.
    1.25 +    """
    1.26 +    url = web.ctx.path.lstrip('/')
    1.27 +    for i in xrange(url.count('/')): 
    1.28 +        base += '../'
    1.29 +    if not base: 
    1.30 +        base = './'
    1.31 +    return base
    1.32 +
    1.33 +def expires(delta):
    1.34 +    """
    1.35 +    Outputs an `Expires` header for `delta` from now. 
    1.36 +    `delta` is a `timedelta` object or a number of seconds.
    1.37 +    """
    1.38 +    if isinstance(delta, (int, long)):
    1.39 +        delta = datetime.timedelta(seconds=delta)
    1.40 +    date_obj = datetime.datetime.utcnow() + delta
    1.41 +    web.header('Expires', net.httpdate(date_obj))
    1.42 +
    1.43 +def lastmodified(date_obj):
    1.44 +    """Outputs a `Last-Modified` header for `datetime`."""
    1.45 +    web.header('Last-Modified', net.httpdate(date_obj))
    1.46 +
    1.47 +def modified(date=None, etag=None):
    1.48 +    """
    1.49 +    Checks to see if the page has been modified since the version in the
    1.50 +    requester's cache.
    1.51 +    
    1.52 +    When you publish pages, you can include `Last-Modified` and `ETag`
    1.53 +    with the date the page was last modified and an opaque token for
    1.54 +    the particular version, respectively. When readers reload the page, 
    1.55 +    the browser sends along the modification date and etag value for
    1.56 +    the version it has in its cache. If the page hasn't changed, 
    1.57 +    the server can just return `304 Not Modified` and not have to 
    1.58 +    send the whole page again.
    1.59 +    
    1.60 +    This function takes the last-modified date `date` and the ETag `etag`
    1.61 +    and checks the headers to see if they match. If they do, it returns 
    1.62 +    `True`, or otherwise it raises NotModified error. It also sets 
    1.63 +    `Last-Modified` and `ETag` output headers.
    1.64 +    """
    1.65 +    try:
    1.66 +        from __builtin__ import set
    1.67 +    except ImportError:
    1.68 +        # for python 2.3
    1.69 +        from sets import Set as set
    1.70 +
    1.71 +    n = set([x.strip('" ') for x in web.ctx.env.get('HTTP_IF_NONE_MATCH', '').split(',')])
    1.72 +    m = net.parsehttpdate(web.ctx.env.get('HTTP_IF_MODIFIED_SINCE', '').split(';')[0])
    1.73 +    validate = False
    1.74 +    if etag:
    1.75 +        if '*' in n or etag in n:
    1.76 +            validate = True
    1.77 +    if date and m:
    1.78 +        # we subtract a second because 
    1.79 +        # HTTP dates don't have sub-second precision
    1.80 +        if date-datetime.timedelta(seconds=1) <= m:
    1.81 +            validate = True
    1.82 +    
    1.83 +    if date: lastmodified(date)
    1.84 +    if etag: web.header('ETag', '"' + etag + '"')
    1.85 +    if validate:
    1.86 +        raise web.notmodified()
    1.87 +    else:
    1.88 +        return True
    1.89 +
    1.90 +def urlencode(query, doseq=0):
    1.91 +    """
    1.92 +    Same as urllib.urlencode, but supports unicode strings.
    1.93 +    
    1.94 +        >>> urlencode({'text':'foo bar'})
    1.95 +        'text=foo+bar'
    1.96 +        >>> urlencode({'x': [1, 2]}, doseq=True)
    1.97 +        'x=1&x=2'
    1.98 +    """
    1.99 +    def convert(value, doseq=False):
   1.100 +        if doseq and isinstance(value, list):
   1.101 +            return [convert(v) for v in value]
   1.102 +        else:
   1.103 +            return utils.safestr(value)
   1.104 +        
   1.105 +    query = dict([(k, convert(v, doseq)) for k, v in query.items()])
   1.106 +    return urllib.urlencode(query, doseq=doseq)
   1.107 +
   1.108 +def changequery(query=None, **kw):
   1.109 +    """
   1.110 +    Imagine you're at `/foo?a=1&b=2`. Then `changequery(a=3)` will return
   1.111 +    `/foo?a=3&b=2` -- the same URL but with the arguments you requested
   1.112 +    changed.
   1.113 +    """
   1.114 +    if query is None:
   1.115 +        query = web.rawinput(method='get')
   1.116 +    for k, v in kw.iteritems():
   1.117 +        if v is None:
   1.118 +            query.pop(k, None)
   1.119 +        else:
   1.120 +            query[k] = v
   1.121 +    out = web.ctx.path
   1.122 +    if query:
   1.123 +        out += '?' + urlencode(query, doseq=True)
   1.124 +    return out
   1.125 +
   1.126 +def url(path=None, doseq=False, **kw):
   1.127 +    """
   1.128 +    Makes url by concatenating web.ctx.homepath and path and the 
   1.129 +    query string created using the arguments.
   1.130 +    """
   1.131 +    if path is None:
   1.132 +        path = web.ctx.path
   1.133 +    if path.startswith("/"):
   1.134 +        out = web.ctx.homepath + path
   1.135 +    else:
   1.136 +        out = path
   1.137 +
   1.138 +    if kw:
   1.139 +        out += '?' + urlencode(kw, doseq=doseq)
   1.140 +    
   1.141 +    return out
   1.142 +
   1.143 +def profiler(app):
   1.144 +    """Outputs basic profiling information at the bottom of each response."""
   1.145 +    from utils import profile
   1.146 +    def profile_internal(e, o):
   1.147 +        out, result = profile(app)(e, o)
   1.148 +        return list(out) + ['<pre>' + net.websafe(result) + '</pre>']
   1.149 +    return profile_internal
   1.150 +
   1.151 +if __name__ == "__main__":
   1.152 +    import doctest
   1.153 +    doctest.testmod()