I wrote:
> Making Quixote use these internally rather than magic numbers is left as
> an exercise for the guilty :-).
This updated version provides a start.
--
http:// if le.o /
--- http_response.py.orig 2003-09-26 16:35:34.000000000 -0400
+++ http_response.py 2003-09-26 17:10:20.000000000 -0400
@@ -28,53 +28,105 @@
from rfc822 import formatdate
from types import StringType, IntType
+"""HTTP status code enumeration
+
+The names match the status descriptions in RFC-2616, with spaces and
+hyphens substituted with an underbar.
+"""
+CONTINUE = 100
+SWITCHING_PROTOCOLS = 101
+PROCESSING = 102 # non-standard
+OK = 200
+CREATED = 201
+ACCEPTED = 202
+NON_AUTHORITATIVE_INFORMATION = 203
+NO_CONTENT = 204
+RESET_CONTENT = 205
+PARTIAL_CONTENT = 206
+MULTI_STATUS = 207 # non-standard
+MULTIPLE_CHOICES = 300
+MOVED_PERMANENTLY = 301
+MOVED_TEMPORARILY = 302
+SEE_OTHER = 303
+NOT_MODIFIED = 304
+USE_PROXY = 305
+TEMPORARY_REDIRECT = 307
+BAD_REQUEST = 400
+UNAUTHORIZED = 401
+PAYMENT_REQUIRED = 402
+FORBIDDEN = 403
+NOT_FOUND = 404
+METHOD_NOT_ALLOWED = 405
+NOT_ACCEPTABLE = 406
+PROXY_AUTHENTICATION_REQUIRED = 407
+REQUEST_TIME_OUT = 408
+CONFLICT = 409
+GONE = 410
+LENGTH_REQUIRED = 411
+PRECONDITION_FAILED = 412
+REQUEST_ENTITY_TOO_LARGE = 413
+REQUEST_URI_TOO_LARGE = 414
+UNSUPPORTED_MEDIA_TYPE = 415
+REQUESTED_RANGE_NOT_SATISFIABLE = 416
+EXPECTATION_FAILED = 417
+UNPROCESSABLE_ENTITY = 422 # non-standard
+LOCKED = 423 # non-standard
+FAILED_DEPENDENCY = 424 # non-standard
+INTERNAL_SERVER_ERROR = 500
+NOT_IMPLEMENTED = 501
+BAD_GATEWAY = 502
+SERVICE_UNAVAILABLE = 503
+GATEWAY_TIME_OUT = 504
+HTTP_VERSION_NOT_SUPPORTED = 505
+INSUFFICIENT_STORAGE = 507 # non-standard
+
status_reasons = {
- 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',
- 300: 'Multiple Choices',
- 301: 'Moved Permanently',
- 302: 'Moved Temporarily',
- 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 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',
- 422: 'Unprocessable Entity',
- 423: 'Locked',
- 424: 'Failed Dependency',
- 500: 'Internal Server Error',
- 501: 'Not Implemented',
- 502: 'Bad Gateway',
- 503: 'Service Unavailable',
- 504: 'Gateway Time-out',
- 505: 'HTTP Version not supported',
- 507: 'Insufficient Storage',
+ CONTINUE : 'Continue',
+ SWITCHING_PROTOCOLS : 'Switching Protocols',
+ PROCESSING : 'Processing',
+ OK : 'OK',
+ CREATED : 'Created',
+ ACCEPTED : 'Accepted',
+ NON_AUTHORITATIVE_INFORMATION : 'Non-Authoritative Information',
+ NO_CONTENT : 'No Content',
+ RESET_CONTENT : 'Reset Content',
+ PARTIAL_CONTENT : 'Partial Content',
+ MULTI_STATUS : 'Multi-Status',
+ MULTIPLE_CHOICES : 'Multiple Choices',
+ MOVED_PERMANENTLY : 'Moved Permanently',
+ MOVED_TEMPORARILY : 'Moved Temporarily',
+ SEE_OTHER : 'See Other',
+ NOT_MODIFIED : 'Not Modified',
+ USE_PROXY : 'Use Proxy',
+ TEMPORARY_REDIRECT : 'Temporary Redirect',
+ BAD_REQUEST : 'Bad Request',
+ UNAUTHORIZED : 'Unauthorized',
+ PAYMENT_REQUIRED : 'Payment Required',
+ FORBIDDEN : 'Forbidden',
+ NOT_FOUND : 'Not Found',
+ METHOD_NOT_ALLOWED : 'Method Not Allowed',
+ NOT_ACCEPTABLE : 'Not Acceptable',
+ PROXY_AUTHENTICATION_REQUIRED : 'Proxy Authentication Required',
+ REQUEST_TIME_OUT : 'Request Time-out',
+ CONFLICT : 'Conflict',
+ GONE : 'Gone',
+ LENGTH_REQUIRED : 'Length Required',
+ PRECONDITION_FAILED : 'Precondition Failed',
+ REQUEST_ENTITY_TOO_LARGE : 'Request Entity Too Large',
+ REQUEST_URI_TOO_LARGE : 'Request-URI Too Large',
+ UNSUPPORTED_MEDIA_TYPE : 'Unsupported Media Type',
+ REQUESTED_RANGE_NOT_SATISFIABLE : 'Requested range not satisfiable',
+ EXPECTATION_FAILED : 'Expectation Failed',
+ UNPROCESSABLE_ENTITY : 'Unprocessable Entity',
+ LOCKED : 'Locked',
+ FAILED_DEPENDENCY : 'Failed Dependency',
+ INTERNAL_SERVER_ERROR : 'Internal Server Error',
+ NOT_IMPLEMENTED : 'Not Implemented',
+ BAD_GATEWAY : 'Bad Gateway',
+ SERVICE_UNAVAILABLE : 'Service Unavailable',
+ GATEWAY_TIME_OUT : 'Gateway Time-out',
+ HTTP_VERSION_NOT_SUPPORTED : 'HTTP Version not supported',
+ INSUFFICIENT_STORAGE : 'Insufficient Storage',
}
@@ -120,7 +172,7 @@
else's problem.
"""
- def __init__ (self, status=200, body=None):
+ def __init__ (self, status=OK, body=None):
"""
Creates a new HTTP response.
"""
@@ -268,9 +320,9 @@
if location.find('://') == -1:
raise ValueError, "URL must include the server name"
if permanent:
- status = 301
+ status = MOVED_PERMANENTLY
else:
- status = 302
+ status = MOVED_TEMPORARILY
self.set_status(status)
self.headers['location'] = location