/
http.py
668 lines (462 loc) · 21 KB
/
http.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
# Copyright 2012 Gregory Holt
#
# 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.
"""
Contains utility classes for responding to WSGI requests. This isn't
WebOb http://www.webob.org/ but sometimes it's enough and you should
be able to upgrade easily to WebOb once you do need it.
Much like webob.exc there are classes for standard HTTP responses,
such as HTTPNotFound for 404. Continuing with webob.exc similarities,
these classes are all subclasses of HTTPException and therefore can
be raised and caught as well as used as WSGI apps. The 2xx classes
are all subclasses of HTTPOk, 3xx subclasses of HTTPRedirection, 4xx
subclasses of HTTPClientError, and 5xx subclasses of HTTPServerError.
HTTPClientError and HTTPServerError also both subclass HTTPError,
like webob.exc.
Instead of a Request object, this package just provides some simpler
classes and functions, like QueryParser and get_header_int, and some
replacement functions like quote (it's Unicode-safe by using UTF8).
"""
from urllib import quote as urllib_quote
from urlparse import parse_qs
from brim.conf import FALSE_VALUES, TRUE_VALUES
#: Translates an HTTP status code to an English status reason string,
#: such as CODE2NAME[201] = 'Created'.
CODE2NAME = {100: 'Continue',
101: 'Switching Protocols',
102: 'Processing',
200: 'OK',
201: 'Created',
202: 'Accepted',
203: 'Non-Authoritative Information',
204: 'No Content',
205: 'Reset Content',
206: 'Partial Content',
207: 'Multi-Status',
208: 'Already Reported',
226: 'IM Used',
300: 'Multiple Choices',
301: 'Moved Permanently',
302: 'Found',
303: 'See Other',
304: 'Not Modified',
305: 'Use Proxy',
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 Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Request Entity Too Large',
414: 'Request-URI Too Long',
415: 'Unsupported Media Type',
416: 'Requested Range Not Satisfiable',
417: 'Expectation Failed',
422: 'Unprocessable Entity',
423: 'Locked',
424: 'Failed Dependency',
426: 'Upgrade Required',
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTP Version Not Supported',
506: 'Variant Also Negotiates',
507: 'Insufficient Storage',
508: 'Loop Detected',
510: 'Not Extended'}
class HTTPException(Exception):
"""
Root class of all brim.http response classes.
:param body: The body of the response. If the body has a length
(as determined by len(body)) and the headers don't
have a content-length or transfer-encoding, the
content-length will be set the body's length.
:param headers: A dict of the headers to include in the response.
:param code: The HTTP response code to give; defaults to 500.
"""
def __init__(self, body=None, headers=None, code=500):
Exception.__init__(
self,
'%s %s %s' % (code, CODE2NAME.get(code, 'Status'), body or '-'))
self.code = code
self.body = body or ''
if not headers:
headers = {}
self.headers = {}
for k, v in headers.iteritems():
self.headers[k.lower()] = v
if 'content-length' not in self.headers and \
'transfer-encoding' not in self.headers and \
hasattr(self.body, '__len__'):
self.headers['content-length'] = len(self.body)
if 'content-type' not in self.headers:
self.headers['content-type'] = 'text/plain'
def __call__(self, env, start_response):
code = self.code
if self.code == 200:
try:
if int(self.headers['content-length']) == 0:
code = 204
except:
pass
start_response(
'%s %s' % (code, CODE2NAME.get(code, 'Status')),
[(k.title(), str(v)) for k, v in self.headers.iteritems()])
if env['REQUEST_METHOD'] == 'HEAD':
return []
return [self.body]
HTTPResponse = HTTPException
class HTTPInformational(HTTPException):
def __init__(self, body=None, headers=None, code=100):
HTTPException.__init__(self, body, headers, code)
class HTTPContinue(HTTPInformational):
def __init__(self, body=None, headers=None, code=100):
HTTPInformational.__init__(self, body, headers, code)
class HTTPSwitchingProtocols(HTTPInformational):
def __init__(self, body=None, headers=None, code=101):
HTTPInformational.__init__(self, body, headers, code)
class HTTPProcessing(HTTPInformational):
def __init__(self, body=None, headers=None, code=102):
HTTPInformational.__init__(self, body, headers, code)
class HTTPSuccess(HTTPException):
def __init__(self, body=None, headers=None, code=200):
HTTPException.__init__(self, body, headers, code)
HTTPOk = HTTPOK = HTTPSuccess
class HTTPCreated(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 201)
class HTTPAccepted(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 202)
class HTTPNonAuthoritativeInformation(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 203)
class HTTPNoContent(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 204)
class HTTPResetContent(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 205)
class HTTPPartialContent(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 206)
class HTTPMultiStatus(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 207)
class HTTPAlreadyReported(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 208)
class HTTPIMUsed(HTTPSuccess):
def __init__(self, body=None, headers=None):
HTTPSuccess.__init__(self, body, headers, 226)
class HTTPRedirection(HTTPException):
def __init__(self, body=None, headers=None, code=300):
HTTPException.__init__(self, body, headers, code)
HTTPMultipleChoices = HTTPRedirection
class HTTPMovedPermanently(HTTPRedirection):
def __init__(self, body=None, headers=None, code=301):
HTTPRedirection.__init__(self, body, headers, code)
class HTTPFound(HTTPRedirection):
def __init__(self, body=None, headers=None, code=302):
HTTPRedirection.__init__(self, body, headers, code)
class HTTPSeeOther(HTTPRedirection):
def __init__(self, body=None, headers=None, code=303):
HTTPRedirection.__init__(self, body, headers, code)
class HTTPNotModified(HTTPRedirection):
def __init__(self, body=None, headers=None, code=304):
HTTPRedirection.__init__(self, body, headers, code)
class HTTPUseProxy(HTTPRedirection):
def __init__(self, body=None, headers=None, code=305):
HTTPRedirection.__init__(self, body, headers, code)
class HTTPTemporaryRedirect(HTTPRedirection):
def __init__(self, body=None, headers=None, code=307):
HTTPRedirection.__init__(self, body, headers, code)
class HTTPError(HTTPException):
def __init__(self, body=None, headers=None, code=500):
HTTPException.__init__(self, body, headers, code)
class HTTPClientError(HTTPError):
def __init__(self, body=None, headers=None, code=400):
HTTPError.__init__(self, body, headers, code)
HTTPBadRequest = HTTPClientError
class HTTPUnauthorized(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 401)
class HTTPPaymentRequired(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 402)
class HTTPForbidden(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 403)
class HTTPNotFound(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 404)
class HTTPMethodNotAllowed(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 405)
class HTTPNotAcceptable(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 406)
class HTTPProxyAuthenticationRequired(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 407)
class HTTPRequestTimeout(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 408)
class HTTPConflict(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 409)
class HTTPGone(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 410)
class HTTPLengthRequired(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 411)
class HTTPPreconditionFailed(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 412)
class HTTPRequestEntityTooLarge(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 413)
class HTTPRequestURITooLong(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 414)
class HTTPUnsupportedMediaType(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 415)
class HTTPRequestedRangeNotSatisfiable(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 416)
class HTTPExpectationFailed(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 417)
class HTTPUnprocessableEntity(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 422)
class HTTPLocked(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 423)
class HTTPFailedDependency(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 424)
class HTTPUpgradeRequired(HTTPClientError):
def __init__(self, body=None, headers=None):
HTTPClientError.__init__(self, body, headers, 426)
class HTTPServerError(HTTPError):
def __init__(self, body=None, headers=None, code=500):
HTTPError.__init__(self, body, headers, code)
HTTPInternalServerError = HTTPServerError
class HTTPNotImplemented(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 501)
class HTTPBadGateway(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 502)
class HTTPServiceUnavailable(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 503)
class HTTPGatewayTimeout(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 504)
class HTTPHTTPVersionNotSupported(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 505)
class HTTPVariantAlsoNegotiates(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 506)
class HTTPInsufficientStorage(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 507)
class HTTPLoopDetected(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 508)
class HTTPNotExtended(HTTPServerError):
def __init__(self, body=None, headers=None):
HTTPServerError.__init__(self, body, headers, 510)
class QueryParser(object):
"""
Parses an HTTP query string and provides convenient retrieval
methods for parameter values.
:param query_string: The HTTP query string, such as in a WSGI
env['QUERY_STRING']. If using something
other than WSGI's QUERY_STRING, be sure you
do **not** include the question mark that
separates the path?query string or it will
be translated as the first character of the
first parameter.
"""
def __init__(self, query_string=None):
self.query = parse_qs(query_string or '', keep_blank_values=True)
def get(self, name, default=None, last_only=True):
"""
Returns the value of the query parameter, or the default
value if the parameter does not exist. If the default is None
and the parameter does not exist, an HTTPBadRequest with an
explanatory message is raised, indicating a required
parameter.
:param name: The name of the query parameter to retrieve.
:param default: The default value if the parameter does not
exist. If not specified, the parameter will
be considered required, and HTTPBadRequest
will be raised if the parameter is missing.
:param last_only: If True (the default), only the last value
of the parameter will be returned if the
parameter is specified multiple times. If
False, all values will be returned in a
list (even if there is only one value).
"""
v = self.query.get(name)
if v is None:
if default is not None:
return default
else:
raise HTTPBadRequest('Requires %s query parameter.\n' % name)
if last_only:
return v[-1]
return v
def get_bool(self, name, default=None):
"""
Returns the boolean value of the query parameter, or the
default value if the parameter does not exist. If the default
is None and the parameter does not exist, an HTTPBadRequest
with an explanatory message is raised, indicating a required
parameter.
If the parameter is included in the query string, but has no
value (such as ?param), then ``not default`` will be
returned.
Otherwise, the parameter value will be checked against known
TRUE_VALUES and FALSE_VALUES for translation, or
HTTPBadRequest will be raised if the value cannot be
translated.
:param name: The name of the query parameter to retrieve.
:param default: The default value if the parameter does not
exist. If not specified, the parameter will
be considered required, and HTTPBadRequest
will be raised if the parameter is missing.
"""
v = self.get(name, default)
if isinstance(v, bool):
return v
# Parameter included with no value, means invert the default.
if not v:
return not default
if v.lower() in FALSE_VALUES:
return False
if v.lower() in TRUE_VALUES:
return True
raise HTTPBadRequest(
'Query parameter %r value %r not boolean.\n' % (name, v))
def get_int(self, name, default=None):
"""
Returns the int value of the query parameter, or the default
value if the parameter does not exist. If the default is None
and the parameter does not exist, an HTTPBadRequest with an
explanatory message is raised, indicating a required
parameter.
:param name: The name of the query parameter to retrieve.
:param default: The default value if the parameter does not
exist or has no value. If not specified, the
parameter will be considered required, and
HTTPBadRequest will be raised if the
parameter is missing.
"""
v = self.get(name, default)
try:
return int(v)
except ValueError, err:
raise HTTPBadRequest(
'Query parameter %r value %r not int.\n' % (name, v))
def get_float(self, name, default=None):
"""
Returns the float value of the query parameter, or the
default value if the parameter does not exist. If the default
is None and the parameter does not exist, an HTTPBadRequest
with an explanatory message is raised, indicating a required
parameter.
:param name: The name of the query parameter to retrieve.
:param default: The default value if the parameter does not
exist or has no value. If not specified, the
parameter will be considered required, and
HTTPBadRequest will be raised if the
parameter is missing.
"""
v = self.get(name, default)
try:
return float(v)
except ValueError, err:
raise HTTPBadRequest(
'Query parameter %r value %r not float.\n' % (name, v))
def get_header(env, name, default=None):
"""
Returns the value of an HTTP header.
:param env: Standard WSGI env to read from.
:param name: The name of the header to retrieve.
:param default: If specified, the default value will be used if
the header cannot be found. If not specified, the
header value will be considered required, and
HTTPBadRequest will be raised if the header is
missing.
:returns: The value of the header, or raises HTTPBadRequest
if the header is missing and required.
"""
env_name = 'HTTP_' + name.upper().replace('-', '_')
if env_name not in env:
if default is not None:
return default
else:
raise HTTPBadRequest(
'Requires %s header.\n' % name.title())
return env[env_name]
def get_header_int(env, name, default=None):
"""
Returns the int value of an HTTP header.
:param env: Standard WSGI env to read from.
:param name: The name of the header to retrieve.
:param default: If specified, the default value will be used if
the header cannot be found. If not specified, the
header value will be considered required, and
HTTPBadRequest will be raised if the header is
missing.
:returns: The int value of the header, or raises HTTPBadRequest
if the header can't be translated to an integer or if
the header is missing and required.
"""
env_name = 'HTTP_' + name.upper().replace('-', '_')
if env_name not in env:
if default is not None:
return default
else:
raise HTTPBadRequest(
'Requires %s header.\n' % name.title())
try:
return int(env[env_name])
except ValueError:
raise HTTPBadRequest(
'Invalid %s header %r.\n' % (name.title(), env[env_name]))
def quote(value, safe='/'):
"""
Patched version of urllib.quote that encodes UTF8 strings before
quoting.
"""
if isinstance(value, unicode):
value = value.encode('utf8')
return urllib_quote(value, safe)