1
2
3
4 """
5 This file is part of the web2py Web Framework
6 Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
7 License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
8 """
9
10 import re
11
12 __all__ = ['HTTP', 'redirect']
13
14 defined_status = {
15 200: 'OK',
16 201: 'CREATED',
17 202: 'ACCEPTED',
18 203: 'NON-AUTHORITATIVE INFORMATION',
19 204: 'NO CONTENT',
20 205: 'RESET CONTENT',
21 206: 'PARTIAL CONTENT',
22 301: 'MOVED PERMANENTLY',
23 302: 'FOUND',
24 303: 'SEE OTHER',
25 304: 'NOT MODIFIED',
26 305: 'USE PROXY',
27 307: 'TEMPORARY REDIRECT',
28 400: 'BAD REQUEST',
29 401: 'UNAUTHORIZED',
30 403: 'FORBIDDEN',
31 404: 'NOT FOUND',
32 405: 'METHOD NOT ALLOWED',
33 406: 'NOT ACCEPTABLE',
34 407: 'PROXY AUTHENTICATION REQUIRED',
35 408: 'REQUEST TIMEOUT',
36 409: 'CONFLICT',
37 410: 'GONE',
38 411: 'LENGTH REQUIRED',
39 412: 'PRECONDITION FAILED',
40 413: 'REQUEST ENTITY TOO LARGE',
41 414: 'REQUEST-URI TOO LONG',
42 415: 'UNSUPPORTED MEDIA TYPE',
43 416: 'REQUESTED RANGE NOT SATISFIABLE',
44 417: 'EXPECTATION FAILED',
45 422: 'UNPROCESSABLE ENTITY',
46 500: 'INTERNAL SERVER ERROR',
47 501: 'NOT IMPLEMENTED',
48 502: 'BAD GATEWAY',
49 503: 'SERVICE UNAVAILABLE',
50 504: 'GATEWAY TIMEOUT',
51 505: 'HTTP VERSION NOT SUPPORTED',
52 }
53
54
55
56
57 try:
58 BaseException
59 except NameError:
60 BaseException = Exception
61
62 regex_status = re.compile('^\d{3} \w+$')
63
64
65 -class HTTP(BaseException):
66
67 - def __init__(
68 self,
69 status,
70 body='',
71 cookies=None,
72 **headers
73 ):
74 self.status = status
75 self.body = body
76 self.headers = headers
77 self.cookies2headers(cookies)
78
80 if cookies and len(cookies) > 0:
81 self.headers['Set-Cookie'] = [
82 str(cookie)[11:] for cookie in cookies.values()]
83
84 - def to(self, responder, env=None):
85 env = env or {}
86 status = self.status
87 headers = self.headers
88 if status in defined_status:
89 status = '%d %s' % (status, defined_status[status])
90 else:
91 status = str(status)
92 if not regex_status.match(status):
93 status = '500 %s' % (defined_status[500])
94 headers.setdefault('Content-Type', 'text/html; charset=UTF-8')
95 body = self.body
96 if status[:1] == '4':
97 if not body:
98 body = status
99 if isinstance(body, str):
100 headers['Content-Length'] = len(body)
101 rheaders = []
102 for k, v in headers.iteritems():
103 if isinstance(v, list):
104 rheaders += [(k, str(item)) for item in v]
105 elif not v is None:
106 rheaders.append((k, str(v)))
107 responder(status, rheaders)
108 if env.get('request_method', '') == 'HEAD':
109 return ['']
110 elif isinstance(body, str):
111 return [body]
112 elif hasattr(body, '__iter__'):
113 return body
114 else:
115 return [str(body)]
116
117 @property
119 """
120 compose a message describing this exception
121
122 "status defined_status [web2py_error]"
123
124 message elements that are not defined are omitted
125 """
126 msg = '%(status)s'
127 if self.status in defined_status:
128 msg = '%(status)s %(defined_status)s'
129 if 'web2py_error' in self.headers:
130 msg += ' [%(web2py_error)s]'
131 return msg % dict(
132 status=self.status,
133 defined_status=defined_status.get(self.status),
134 web2py_error=self.headers.get('web2py_error'))
135
137 "stringify me"
138 return self.message
139
140
141 -def redirect(location='', how=303, client_side=False):
142 if location:
143 from gluon import current
144 loc = location.replace('\r', '%0D').replace('\n', '%0A')
145 if client_side and current.request.ajax:
146 raise HTTP(200, **{'web2py-redirect-location': loc})
147 else:
148 raise HTTP(how,
149 'You are being redirected <a href="%s">here</a>' % loc,
150 Location=loc)
151 else:
152 from gluon import current
153 if client_side and current.request.ajax:
154 raise HTTP(200, **{'web2py-component-command': 'window.location.reload(true)'})
155