lib/http.py
author Tero Marttila <terom@fixme.fi>
Fri, 06 Feb 2009 21:31:02 +0200
changeset 7 d6a8258bd90e
child 8 0ce1f471e9d7
permissions -rw-r--r--
YES YES MOAR WSGI - Hello World
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    WSGI HTTP utility code
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
# for utility functions
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
import cgi
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
# for header handling
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
import wsgiref.headers
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
class Request (object) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
        HTTP Request with associated metadata
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    def __init__ (self, env) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
            Parse env data
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
        # store env
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        self.env = env
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        # get the querystring
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        self.arg_str = env.get('QUERY_STRING', '')
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        # parse query args
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        self.arg_dict = cgi.parse_qs(self.arg_str, True)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
class Response (object) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        HTTP Response with headers and data
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    def __init__ (self, data, content_type='text/html', status='200 OK', charset='utf8') :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
            Create the response. The Content-type header is built from the given values. The given \a data must be
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
            either a str (which is sent plain), an unicode object (which is encoded with the relevant charset), or
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
            None, whereupon an empty response body is sent. The content_type argument can also be forced to None to
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
            not send a Content-type header (e.g. for redirects)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        # store info
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        self.status = status
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        self.data = data
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        self.charset = charset
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        # headers
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        self.headers = wsgiref.headers.Headers([])
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
        # add Content-type header?
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        if content_type :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
            self.add_header('Content-type', content_type, charset=charset)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    def add_header (self, name, value, **params) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
            Add response header with the given name/value, plus option params
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
            XXX: uses the wsgiref.headers code, not sure how that behaves re multiple headers with the same name, etc
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
        self.headers.add_header(name, value, **params)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    def get_status (self) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
            Returns response status string (XXX Foo)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        return self.status
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
    
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
    def get_headers (self) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
            Returns the list of header (name, value) pairs
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
        return self.headers.items()
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    def get_data (self) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
            Returns the response data - as an encoded string
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
        if self.data :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
            return self.data.encode(self.charset)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
        else :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
            return ''
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
class ErrorResponse (Response) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        A response with an error code / message
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    def __init__ (self, status, message) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
            Build a plain error message response with the given status/message
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
        data = """\
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
<html><head><title>%(title)s</title></head><body>
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
<h1>%(title)s</h1>
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
<p>%(message)s</p>
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
</body></html>
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
""" % dict(
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
            title       = status, 
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
            message     = message
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
        )
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
            
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
        super(ErrorResponse, self).__init__(data, status=status)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
class ResponseError (Exception) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
        An exception that results in a specfic 4xx ErrorResponse message to the client
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    def __init__ (self, message, status='400 Bad Request') :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
        self.status = status
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
        self.message = message
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
        super(ResponseError, self).__init__(message)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
    def get_response (self) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
        return ErrorResponse(self.status, self.message)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
class Redirect (Response) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
        Redirect response
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
    def __init__ (self, url) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
            Redirect to given *absolute* URL
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
        
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
        # no content-type or data
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
        super(Redirect, self).__init__(None, content_type=None, status='302 Found')
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
        # add Location: header
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
        self.add_header("Location", url)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141