lib/http.py
author Tero Marttila <terom@fixme.fi>
Fri, 06 Feb 2009 22:48:00 +0200
changeset 8 0ce1f471e9d7
parent 7 d6a8258bd90e
child 9 2a47b00f60b0
permissions -rw-r--r--
and it works, a lot better than before
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
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    11
# for path handling
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    12
import os.path
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    13
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
class Request (object) :
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
        HTTP Request with associated metadata
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
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    def __init__ (self, env) :
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
            Parse env data
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        """
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
        # store env
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        self.env = env
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
        # get the querystring
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        self.arg_str = env.get('QUERY_STRING', '')
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
        # parse query args
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
        self.arg_dict = cgi.parse_qs(self.arg_str, True)
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    32
    
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    33
    def get_script_dir (self) :
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    34
        """
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    35
            Returns the URL path to the requested script's directory with no trailing slash, i.e.
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    36
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    37
            /               -> 
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    38
            /foo.cgi        -> 
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    39
            /foo/bar.cgi    -> /foo
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    40
        """
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    41
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    42
        return os.path.dirname(self.env['SCRIPT_NAME'])
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    43
    
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    44
    def get_page_name (self) :
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    45
        """
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    46
            Returns the requested page path with no leading slash, i.e.
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    47
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    48
            /foo.cgi        -> 
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    49
            /foo.cgi/       -> 
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    50
            /foo.cgi/bar    -> bar
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    51
            /foo.cgi/quux/  -> quux/
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    52
        """
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    53
        
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    54
        return os.path.normpath(self.env['PATH_INFO']).lstrip('/')
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
class Response (object) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        HTTP Response with headers and data
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    """
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
    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
    62
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
            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
    64
            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
    65
            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
    66
            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
    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
        # store info
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
        self.status = status
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
        self.data = data
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        self.charset = charset
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
        # headers
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
        self.headers = wsgiref.headers.Headers([])
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
        
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
        # add Content-type header?
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
        if content_type :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
            self.add_header('Content-type', content_type, charset=charset)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
    def add_header (self, name, value, **params) :
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
            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
    84
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
            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
    86
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
        
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
        self.headers.add_header(name, value, **params)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    def get_status (self) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
            Returns response status string (XXX Foo)
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
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
        return self.status
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
    def get_headers (self) :
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
            Returns the list of header (name, value) pairs
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
        return self.headers.items()
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    def get_data (self) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
            Returns the response data - as an encoded string
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
        if self.data :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
            return self.data.encode(self.charset)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
        else :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
            return ''
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
class ErrorResponse (Response) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
        A response with an error code / message
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   120
    def __init__ (self, status, message, details=None) :
7
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
            Build a plain error message response with the given status/message
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   123
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   124
            @param status HTTP status code
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   125
            @param message short message to describe errors
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   126
            @param details optional details, plaintext
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
        """
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
        data = """\
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
<html><head><title>%(title)s</title></head><body>
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
<h1>%(title)s</h1>
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
<p>%(message)s</p>
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   133
%(details)s
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
</body></html>
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
""" % dict(
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
            title       = status, 
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   137
            message     = message,
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   138
            details     = '<pre>%s</pre>' % details if details else ''
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
        )
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
        super(ErrorResponse, self).__init__(data, status=status)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
class ResponseError (Exception) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
        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
   146
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   148
    def __init__ (self, message, status='400 Bad Request', details=None) :
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
        self.status = status
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
        self.message = message
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   151
        self.details = details
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
        super(ResponseError, self).__init__(message)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
    def get_response (self) :
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   156
        return ErrorResponse(self.status, self.message, self.details)
7
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
class Redirect (Response) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
        Redirect response
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   161
    """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   163
    def __init__ (self, url) :
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   164
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
            Redirect to given *absolute* URL
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
        """
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
        
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
        # no content-type or data
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
        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
   170
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
        # add Location: header
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
        self.add_header("Location", url)
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
d6a8258bd90e YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174