This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
/
_webapp25.py
771 lines (581 loc) · 21.6 KB
/
_webapp25.py
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""An extremely simple WSGI web application framework for Python 2.5.
Note:
This framework is for Python 2.5 applications which have been deprecated.
The submodule `mail_handlers` is still valid for use with Python 2.7
applications.
"""
import cgi
import logging
import re
import StringIO
import sys
import traceback
import urlparse
import webob
import wsgiref.handlers
import wsgiref.headers
import wsgiref.util
wsgiref.handlers.BaseHandler.os_environ = {}
RE_FIND_GROUPS = re.compile('\(.*?\)')
_CHARSET_RE = re.compile(r';\s*charset=([^;\s]*)', re.I)
class Error(Exception):
"""Base of all exceptions in the webapp module."""
pass
class CannotReversePattern(Error):
"""Thrown when a url_pattern cannot be reversed."""
pass
class NoUrlFoundError(Error):
"""Thrown when RequestHandler.get_url() fails."""
pass
class Request(webob.Request):
"""Abstraction for an HTTP request.
Properties:
uri: the complete URI requested by the user
scheme: 'http' or 'https'
host: the host, including the port
path: the path up to the ';' or '?' in the URL
parameters: the part of the URL between the ';' and the '?', if any
query: the part of the URL after the '?'
You can access parsed query and POST values with the get() method; do not
parse the query string yourself.
"""
request_body_tempfile_limit = 0
uri = property(lambda self: self.url)
query = property(lambda self: self.query_string)
def __init__(self, environ):
"""Constructs a Request object from a WSGI environment.
If the charset isn't specified in the Content-Type header, defaults
to UTF-8.
Args:
environ: A WSGI-compliant environment dictionary.
"""
match = _CHARSET_RE.search(environ.get('CONTENT_TYPE', ''))
if match:
charset = match.group(1).lower()
else:
charset = 'utf-8'
webob.Request.__init__(self, environ, charset=charset,
unicode_errors= 'ignore', decode_param_names=True)
def get(self, argument_name, default_value='', allow_multiple=False):
"""Returns the query or POST argument with the given name.
We parse the query string and POST payload lazily, so this will be a
slower operation on the first call.
Args:
argument_name: the name of the query or POST argument
default_value: the value to return if the given argument is not present
allow_multiple: return a list of values with the given name (deprecated)
Returns:
If allow_multiple is False (which it is by default), we return the first
value with the given name given in the request. If it is True, we always
return a list.
"""
param_value = self.get_all(argument_name)
if allow_multiple:
logging.warning('allow_multiple is a deprecated param, please use the '
'Request.get_all() method instead.')
if len(param_value) > 0:
if allow_multiple:
return param_value
return param_value[0]
else:
if allow_multiple and not default_value:
return []
return default_value
def get_all(self, argument_name, default_value=None):
"""Returns a list of query or POST arguments with the given name.
We parse the query string and POST payload lazily, so this will be a
slower operation on the first call.
Args:
argument_name: the name of the query or POST argument
default_value: the value to return if the given argument is not present,
None may not be used as a default, if it is then an empty list will be
returned instead.
Returns:
A (possibly empty) list of values.
"""
if self.charset:
argument_name = argument_name.encode(self.charset)
if default_value is None:
default_value = []
param_value = self.params.getall(argument_name)
if param_value is None or len(param_value) == 0:
return default_value
for i in xrange(len(param_value)):
if isinstance(param_value[i], cgi.FieldStorage):
param_value[i] = param_value[i].value
return param_value
def arguments(self):
"""Returns a list of the arguments provided in the query and/or POST.
The return value is a list of strings.
"""
return list(set(self.params.keys()))
def get_range(self, name, min_value=None, max_value=None, default=0):
"""Parses the given int argument, limiting it to the given range.
Args:
name: the name of the argument
min_value: the minimum int value of the argument (if any)
max_value: the maximum int value of the argument (if any)
default: the default value of the argument if it is not given
Returns:
An int within the given range for the argument
"""
value = self.get(name, default)
if value is None:
return value
try:
value = int(value)
except ValueError:
value = default
if value is not None:
if max_value is not None:
value = min(value, max_value)
if min_value is not None:
value = max(value, min_value)
return value
class Response(object):
"""Abstraction for an HTTP response.
Properties:
out: file pointer for the output stream
headers: wsgiref.headers.Headers instance representing the output headers
"""
def __init__(self):
"""Constructs a response with the default settings."""
self.out = StringIO.StringIO()
self.__wsgi_headers = []
self.headers = wsgiref.headers.Headers(self.__wsgi_headers)
self.headers['Content-Type'] = 'text/html; charset=utf-8'
self.headers['Cache-Control'] = 'no-cache'
self.set_status(200)
@property
def status(self):
"""Returns current request status code."""
return self.__status[0]
@property
def status_message(self):
"""Returns current request status message."""
return self.__status[1]
def set_status(self, code, message=None):
"""Sets the HTTP status code of this response.
Args:
message: the HTTP status string to use
If no status string is given, we use the default from the HTTP/1.1
specification.
"""
if not message:
message = Response.http_status_message(code)
self.__status = (code, message)
def has_error(self):
"""Indicates whether the response was an error response."""
return self.__status[0] >= 400
def clear(self):
"""Clears all data written to the output stream so that it is empty."""
self.out.seek(0)
self.out.truncate(0)
def wsgi_write(self, start_response):
"""Writes this response using WSGI semantics with the given WSGI function.
Args:
start_response: the WSGI-compatible start_response function
"""
body = self.out.getvalue()
if isinstance(body, unicode):
body = body.encode('utf-8')
elif self.headers.get('Content-Type', '').endswith('; charset=utf-8'):
try:
body.decode('utf-8')
except UnicodeError, e:
logging.warning('Response written is not UTF-8: %s', e)
if (self.headers.get('Cache-Control') == 'no-cache' and
not self.headers.get('Expires')):
self.headers['Expires'] = 'Mon, 01 Jan 1990 00:00:00 GMT'
self.headers['Content-Length'] = str(len(body))
new_headers = []
for header, value in self.__wsgi_headers:
if not isinstance(value, basestring):
value = unicode(value)
if ('\n' in header or '\r' in header or
'\n' in value or '\r' in value):
logging.warning('Replacing newline in header: %s', repr((header,value)))
value = value.replace('\n','').replace('\r','')
header = header.replace('\n','').replace('\r','')
new_headers.append((header, value))
self.__wsgi_headers = new_headers
write = start_response('%d %s' % self.__status, self.__wsgi_headers)
write(body)
self.out.close()
def http_status_message(code):
"""Returns the default HTTP status message for the given code.
Args:
code: the HTTP code for which we want a message
"""
if not Response.__HTTP_STATUS_MESSAGES.has_key(code):
raise Error('Invalid HTTP status code: %d' % code)
return Response.__HTTP_STATUS_MESSAGES[code]
http_status_message = staticmethod(http_status_message)
__HTTP_STATUS_MESSAGES = {
100: 'Continue',
101: 'Switching Protocols',
200: 'OK',
201: 'Created',
202: 'Accepted',
203: 'Non-Authoritative Information',
204: 'No Content',
205: 'Reset Content',
206: 'Partial Content',
300: 'Multiple Choices',
301: 'Moved Permanently',
302: 'Moved Temporarily',
303: 'See Other',
304: 'Not Modified',
305: 'Use Proxy',
306: 'Unused',
307: 'Temporary Redirect',
400: 'Bad Request',
401: 'Unauthorized',
402: 'Payment Required',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Time-out',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Request Entity Too Large',
414: 'Request-URI Too Large',
415: 'Unsupported Media Type',
416: 'Requested Range Not Satisfiable',
417: 'Expectation Failed',
428: 'Precondition Required',
429: 'Too Many Requests',
431: 'Request Header Fields Too Large',
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Time-out',
505: 'HTTP Version not supported',
511: 'Network Authentication Required'
}
class RequestHandler(object):
"""Our base HTTP request handler. Clients should subclass this class.
Subclasses should override get(), post(), head(), options(), etc to handle
different HTTP methods.
"""
def initialize(self, request, response):
"""Initializes this request handler with the given Request and Response."""
self.request = request
self.response = response
def get(self, *args):
"""Handler method for GET requests."""
self.error(405)
def post(self, *args):
"""Handler method for POST requests."""
self.error(405)
def head(self, *args):
"""Handler method for HEAD requests."""
self.error(405)
def options(self, *args):
"""Handler method for OPTIONS requests."""
self.error(405)
def put(self, *args):
"""Handler method for PUT requests."""
self.error(405)
def delete(self, *args):
"""Handler method for DELETE requests."""
self.error(405)
def trace(self, *args):
"""Handler method for TRACE requests."""
self.error(405)
def error(self, code):
"""Clears the response output stream and sets the given HTTP error code.
Args:
code: the HTTP status error code (e.g., 501)
"""
self.response.set_status(code)
self.response.clear()
def redirect(self, uri, permanent=False):
"""Issues an HTTP redirect to the given relative URL.
Args:
uri: a relative or absolute URI (e.g., '../flowers.html')
permanent: if true, we use a 301 redirect instead of a 302 redirect
"""
if permanent:
self.response.set_status(301)
else:
self.response.set_status(302)
absolute_url = urlparse.urljoin(self.request.uri, uri)
self.response.headers['Location'] = str(absolute_url)
self.response.clear()
def handle_exception(self, exception, debug_mode):
"""Called if this handler throws an exception during execution.
The default behavior is to call self.error(500) and print a stack trace
if debug_mode is True.
Args:
exception: the exception that was thrown
debug_mode: True if the web application is running in debug mode
"""
self.error(500)
logging.exception(exception)
if debug_mode:
lines = ''.join(traceback.format_exception(*sys.exc_info()))
self.response.clear()
self.response.out.write('<pre>%s</pre>' % (cgi.escape(lines, quote=True)))
@classmethod
def new_factory(cls, *args, **kwargs):
"""Create new request handler factory.
Use factory method to create reusable request handlers that just
require a few configuration parameters to construct. Also useful
for injecting shared state between multiple request handler
instances without relying on global variables. For example, to
create a set of post handlers that will do simple text transformations
you can write:
class ChangeTextHandler(webapp.RequestHandler):
def __init__(self, transform):
self.transform = transform
def post(self):
response_text = self.transform(
self.request.request.body_file.getvalue())
self.response.out.write(response_text)
application = webapp.WSGIApplication(
[('/to_lower', ChangeTextHandler.new_factory(str.lower)),
('/to_upper', ChangeTextHandler.new_factory(str.upper)),
],
debug=True)
Text POSTed to /to_lower will be lower cased.
Text POSTed to /to_upper will be upper cased.
"""
def new_instance():
return cls(*args, **kwargs)
new_instance.__name__ = cls.__name__ + 'Factory'
return new_instance
@classmethod
def get_url(cls, *args, **kargs):
"""Returns the url for the given handler.
The default implementation uses the patterns passed to the active
WSGIApplication to create a url. However, it is different from Django's
urlresolvers.reverse() in the following ways:
- It does not try to resolve handlers via module loading
- It does not support named arguments
- It performs some post-prosessing on the url to remove some regex
operators.
- It will try to fill in the left-most missing arguments with the args
used in the active request.
Args:
args: Parameters for the url pattern's groups.
kwargs: Optionally contains 'implicit_args' that can either be a boolean
or a tuple. When it is True, it will use the arguments to the
active request as implicit arguments. When it is False (default),
it will not use any implicit arguments. When it is a tuple, it
will use the tuple as the implicit arguments.
the left-most args if some are missing from args.
Returns:
The url for this handler/args combination.
Raises:
NoUrlFoundError: No url pattern for this handler has the same
number of args that were passed in.
"""
app = WSGIApplication.active_instance
pattern_map = app._pattern_map
implicit_args = kargs.get('implicit_args', ())
if implicit_args == True:
implicit_args = app.current_request_args
min_params = len(args)
for pattern_tuple in pattern_map.get(cls, ()):
num_params_in_pattern = pattern_tuple[1]
if num_params_in_pattern < min_params:
continue
try:
num_implicit_args = max(0, num_params_in_pattern - len(args))
merged_args = implicit_args[:num_implicit_args] + args
url = _reverse_url_pattern(pattern_tuple[0], *merged_args)
url = url.replace('\\', '')
url = url.replace('?', '')
return url
except CannotReversePattern:
continue
logging.warning('get_url failed for Handler name: %r, Args: %r',
cls.__name__, args)
raise NoUrlFoundError
def _reverse_url_pattern(url_pattern, *args):
"""Turns a regex that matches a url back into a url by replacing
the url pattern's groups with the given args. Removes '^' and '$'
from the result.
Args:
url_pattern: A pattern used to match a URL.
args: list of values corresponding to groups in url_pattern.
Returns:
A string with url_pattern's groups filled in values from args.
Raises:
CannotReversePattern if either there aren't enough args to fill
url_pattern's groups, or if any arg isn't matched by the regular
expression fragment in the corresponding group.
"""
group_index = [0]
def expand_group(match):
group = match.group(1)
try:
value = str(args[group_index[0]])
group_index[0] += 1
except IndexError:
raise CannotReversePattern('Not enough arguments in url tag')
if not re.match(group + '$', value):
raise CannotReversePattern("Value %r doesn't match (%r)" % (value, group))
return value
result = re.sub(r'\(([^)]+)\)', expand_group, url_pattern.pattern)
result = result.replace('^', '')
result = result.replace('$', '')
return result
class RedirectHandler(RequestHandler):
"""Simple redirection handler.
Easily configure URLs to redirect to alternate targets. For example,
to configure a web application so that the root URL is always redirected
to the /home path, do:
application = webapp.WSGIApplication(
[('/', webapp.RedirectHandler.new_factory('/home', permanent=True)),
('/home', HomeHandler),
],
debug=True)
Handler also useful for setting up obsolete URLs to redirect to new paths.
"""
def __init__(self, path, permanent=False):
"""Constructor.
Do not use directly. Configure using new_factory method.
Args:
path: Path to redirect to.
permanent: if true, we use a 301 redirect instead of a 302 redirect.
"""
self.path = path
self.permanent = permanent
def get(self):
self.redirect(self.path, permanent=self.permanent)
class WSGIApplication(object):
"""Wraps a set of webapp RequestHandlers in a WSGI-compatible application.
To use this class, pass a list of (URI regular expression, RequestHandler)
pairs to the constructor, and pass the class instance to a WSGI handler.
See the example in the module comments for details.
The URL mapping is first-match based on the list ordering.
"""
REQUEST_CLASS = Request
RESPONSE_CLASS = Response
def __init__(self, url_mapping, debug=False):
"""Initializes this application with the given URL mapping.
Args:
url_mapping: list of (URI regular expression, RequestHandler) pairs
(e.g., [('/', ReqHan)])
debug: if true, we send Python stack traces to the browser on errors
"""
self._init_url_mappings(url_mapping)
self.__debug = debug
WSGIApplication.active_instance = self
self.current_request_args = ()
def __call__(self, environ, start_response):
"""Called by WSGI when a request comes in."""
request = self.REQUEST_CLASS(environ)
response = self.RESPONSE_CLASS()
WSGIApplication.active_instance = self
handler = None
groups = ()
for regexp, handler_class in self._url_mapping:
match = regexp.match(request.path)
if match:
try:
handler = handler_class()
handler.initialize(request, response)
except Exception, e:
if handler is None:
handler = RequestHandler()
handler.response = response
handler.handle_exception(e, self.__debug)
response.wsgi_write(start_response)
return ['']
groups = match.groups()
break
self.current_request_args = groups
if handler:
try:
method = environ['REQUEST_METHOD']
if method == 'GET':
handler.get(*groups)
elif method == 'POST':
handler.post(*groups)
elif method == 'HEAD':
handler.head(*groups)
elif method == 'OPTIONS':
handler.options(*groups)
elif method == 'PUT':
handler.put(*groups)
elif method == 'DELETE':
handler.delete(*groups)
elif method == 'TRACE':
handler.trace(*groups)
else:
handler.error(501)
except Exception, e:
handler.handle_exception(e, self.__debug)
else:
response.set_status(404)
response.wsgi_write(start_response)
return ['']
def _init_url_mappings(self, handler_tuples):
"""Initializes the maps needed for mapping urls to handlers and handlers
to urls.
Args:
handler_tuples: list of (URI, RequestHandler) pairs.
"""
handler_map = {}
pattern_map = {}
url_mapping = []
for regexp, handler in handler_tuples:
try:
handler_name = handler.__name__
except AttributeError:
pass
else:
handler_map[handler_name] = handler
if not regexp.startswith('^'):
regexp = '^' + regexp
if not regexp.endswith('$'):
regexp += '$'
if regexp == '^/form$':
logging.warning('The URL "/form" is reserved and will not be matched.')
compiled = re.compile(regexp)
url_mapping.append((compiled, handler))
num_groups = len(RE_FIND_GROUPS.findall(regexp))
handler_patterns = pattern_map.setdefault(handler, [])
handler_patterns.append((compiled, num_groups))
self._handler_map = handler_map
self._pattern_map = pattern_map
self._url_mapping = url_mapping
def get_registered_handler_by_name(self, handler_name):
"""Returns the handler given the handler's name.
This uses the application's url mapping.
Args:
handler_name: The __name__ of a handler to return.
Returns:
The handler with the given name.
Raises:
KeyError: If the handler name is not found in the parent application.
"""
try:
return self._handler_map[handler_name]
except:
logging.error('Handler does not map to any urls: %s', handler_name)
raise