summaryrefslogtreecommitdiff
path: root/paste/httpexceptions.py
blob: d5a7e07819d6638ae91ec228b1d91860a357f51f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

"""
WSGI middleware

Processes Python exceptions that relate to HTTP exceptions.  This
defines a set of extensions, all subclasses of HTTPException, and a
middleware (`middleware`) that catches these exceptions and turns them
into proper responses.
"""

import types

class HTTPException(Exception):
    code = None
    title = None
    message = None
    # @@: not currently used:
    required_headers = ()
    def __init__(self, message=None, headers=None):
        self.headers = headers
        if message is not None:
            self.message = message
        Exception.__init__(self, self.message)

    def html(self, environ):
        message = self.message
        args = environ.copy()
        if self.headers:
            environ.update(self.headers)
        message = message % args
        return ('<html><head><title>%(title)s</title></head>\n'
                '<body>\n'
                '<h1>%(title)s</h1>\n'
                '<p>%(message)s</p>\n'
                '<hr noshade>\n'
                '<div align="right">WSGI server</div>\n'
                '</body></html>\n'
                % {'title': self.title,
                   'code': self.code,
                   'message': message})

    def __repr__(self):
        return '<%s %s; code=%s>' % (self.__class__.__name__,
                                     self.title, self.code)

class _HTTPMove(HTTPException):
    required_headers = ('location',)
    message = ('The resource has been moved to <a href="%(location)s">'
               '%(location)s</a>; you should be redirected automatically.')

class HTTPMovedPermanently(_HTTPMove):
    code = 301
    title = 'Moved Permanently'

class HTTPFound(_HTTPMove):
    code = 302
    title = 'Found'

# This one is safe after a POST (the redirected location will be
# retrieved with GET):
class HTTPSeeOther(_HTTPMove):
    code = 303
    title = 'See Other'

class HTTPNotModified(HTTPException):
    # @@: but not always (HTTP section 14.18.1)...?
    required_headers = ('date',)
    code = 304
    title = 'Not Modified'
    message = ''
    # @@: should include date header, optionally other headers

class HTTPUserProxy(_HTTPMove):
    # @@: OK, not a move, but looks a little like one
    code = 305
    title = 'Use Proxy'
    message = ('This resource must be accessed through the proxy located '
               'at <a href="%(location)s">%(location)s</a>')

class HTTPTemporaryRedirect(_HTTPMove):
    code = 307
    title = 'Temporary Redirect'

class HTTPBadRequest(HTTPException):
    code = 400
    title = 'Bad Request'
    message = ('The server could not understand your request')

class HTTPUnauthorized(HTTPException):
    required_headers = ('WWW-Authenticate',)
    code = 401
    title = 'Unauthorized'
    # @@: should require WWW-Authenticate header
    message = ('Authorization is required to access this resource; '
               'you must login.')

class HTTPForbidden(HTTPException):
    code = 403
    title = 'Forbidden'
    message = ('Access was denied to this resource.')

class HTTPNotFound(HTTPException):
    code = 404
    title = 'Not Found'
    message = ('The resource could not be found.')

class HTTPMethodNotAllowed(HTTPException):
    required_headers = ('allowed',)
    code = 405
    title = 'Method Not Allowed'
    message = ('The method %(REQUEST_METHOD)s is not allowed for this '
               'resource.')

class HTTPNotAcceptable(HTTPException):
    code = 406
    title = 'Not Acceptable'
    message = ('The resource could not be generated that was acceptable '
               'to your browser (content of type %(HTTP_ACCEPT)s).')

class HTTPConfict(HTTPException):
    code = 409
    title = 'Conflict'
    message = ('There was a conflict when trying to complete your '
               'request.')

class HTTPGone(HTTPException):
    code = 410
    title = 'Gone'
    message = ('This resource is no longer available.  No forwarding '
               'address is aavailable.')

class HTTPLengthRequired(HTTPException):
    code = 411
    title = 'Length Required'
    message = ('Content-Length header required.')

class HTTPPreconditionFailed(HTTPException):
    code = 412
    title = 'Precondition Failed'
    message = ('Request precondition failed.')

class HTTPRequestEntityTooLarge(HTTPException):
    code = 413
    title = 'Request Entity Too Large'
    message = ('The body of your request was too large for this server.')

class HTTPRequestURITooLong(HTTPException):
    code = 414
    title = 'Request-URI Too Long'
    message = ('The request URI was too long for this server.')

class HTTPUnsupportedMediaType(HTTPException):
    code = 415
    title = 'Unsupported Media Type'
    message = ('The request media type %(CONTENT_TYPE)s is not '
               'supported by this server.')

class HTTPRequestRangeNotSatisfiable(HTTPException):
    code = 416
    title = 'Request Range Not Satisfiable'
    message = ('The Range requested is not available.')

class HTTPExpectationFailed(HTTPException):
    code = 417
    title = 'Expectation Failed'
    message = ('Expectation failed.')

class HTTPServerError(HTTPException):
    code = 500
    title = 'Internal Server Error'
    message = ('An internal server error occurred.')

class HTTPNotImplemented(HTTPException):
    coded = 501
    title = 'Not Implemented'
    message = ('The request method %(REQUEST_METHOD)s is not implemented '
               'for this server.')

class HTTPBadGateway(HTTPException):
    code = 502
    title = 'Bad Gateway'
    message = ('Bad gateway.')

class HTTPServiceUnavailable(HTTPException):
    code = 503
    title = 'Service Unavailable'
    message = ('The server is currently unavailable.  Please try again '
               'at a later time.')

class HTTPGatewayTimeout(HTTPException):
    code = 504
    title = 'Gateway Timeout'
    message = ('The gateway has timed out.')

class HTTPHttpVersionNotSupported(HTTPException):
    code = 505
    title = 'HTTP Version Not Supported'
    message = ('The HTTP version is not supported.')

__all__ = []
_exceptions = {}
for name, value in globals().items():
    if (isinstance(value, (type, types.ClassType)) and
        issubclass(value, HTTPException) and
        value.code):
        _exceptions[value.code] = value
        __all__.append(name)
def get_exception(code):
    return _exceptions[code]

############################################################
## Middleware implementation:
############################################################

def middleware(application, global_conf=None):

    """
    This middleware catches any exceptions (which are subclasses of
    `HTTPException`) and turns them into proper HTTP responses.
    """

    def start_application(environ, start_response):
        app_started = []
        def checked_start_response(status, headers, exc_info=None):
            app_started.append(None)
            return start_response(status, headers, exc_info)
        
        try:
            return application(environ, checked_start_response)
        except HTTPException, e:
            if app_started:
                # They've already started the response, so we can't
                # do the right thing anymore.
                raise
            headers = {'content-type': 'text/html'}
            if e.headers:
                headers.update(e.headers)
            start_response('%s %s' % (e.code, e.title),
                           headers.items())
            return [e.html(environ)]
        
    return start_application

__all__.extend(['middleware', 'get_exception'])