diff -r a82d7e028458 -r c64f4c0d9c72 Lib/http/client.py --- a/Lib/http/client.py Mon Jun 16 19:26:56 2014 -0400 +++ b/Lib/http/client.py Mon Jun 16 21:49:24 2014 -0700 @@ -91,122 +91,84 @@ _CS_REQ_STARTED = 'Request-started' _CS_REQ_SENT = 'Request-sent' -# status codes -# informational -CONTINUE = 100 -SWITCHING_PROTOCOLS = 101 -PROCESSING = 102 -# successful -OK = 200 -CREATED = 201 -ACCEPTED = 202 -NON_AUTHORITATIVE_INFORMATION = 203 -NO_CONTENT = 204 -RESET_CONTENT = 205 -PARTIAL_CONTENT = 206 -MULTI_STATUS = 207 -IM_USED = 226 +responses = {} -# redirection -MULTIPLE_CHOICES = 300 -MOVED_PERMANENTLY = 301 -FOUND = 302 -SEE_OTHER = 303 -NOT_MODIFIED = 304 -USE_PROXY = 305 -TEMPORARY_REDIRECT = 307 -# client error -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_TIMEOUT = 408 -CONFLICT = 409 -GONE = 410 -LENGTH_REQUIRED = 411 -PRECONDITION_FAILED = 412 -REQUEST_ENTITY_TOO_LARGE = 413 -REQUEST_URI_TOO_LONG = 414 -UNSUPPORTED_MEDIA_TYPE = 415 -REQUESTED_RANGE_NOT_SATISFIABLE = 416 -EXPECTATION_FAILED = 417 -UNPROCESSABLE_ENTITY = 422 -LOCKED = 423 -FAILED_DEPENDENCY = 424 -UPGRADE_REQUIRED = 426 -PRECONDITION_REQUIRED = 428 -TOO_MANY_REQUESTS = 429 -REQUEST_HEADER_FIELDS_TOO_LARGE = 431 +def _define_status_codes(*codes): + for (name, code, message) in codes: + globals()[name] = code + responses[code] = message -# server error -INTERNAL_SERVER_ERROR = 500 -NOT_IMPLEMENTED = 501 -BAD_GATEWAY = 502 -SERVICE_UNAVAILABLE = 503 -GATEWAY_TIMEOUT = 504 -HTTP_VERSION_NOT_SUPPORTED = 505 -INSUFFICIENT_STORAGE = 507 -NOT_EXTENDED = 510 -NETWORK_AUTHENTICATION_REQUIRED = 511 -# Mapping status codes to official W3C names -responses = { - 100: 'Continue', - 101: 'Switching Protocols', +_define_status_codes( + # status codes + # informational + ('CONTINUE', 100, 'Continue'), + ('SWITCHING_PROTOCOLS', 101, 'Switching Protocols'), + ('PROCESSING', 102, 'Processing'), - 200: 'OK', - 201: 'Created', - 202: 'Accepted', - 203: 'Non-Authoritative Information', - 204: 'No Content', - 205: 'Reset Content', - 206: 'Partial Content', + # successful + ('OK', 200, 'OK'), + ('CREATED', 201, 'Created'), + ('ACCEPTED', 202, 'Accepted'), + ('NON_AUTHORITATIVE_INFORMATION', 203, 'Non-Authoritative Information'), + ('NO_CONTENT', 204, 'No Content'), + ('RESET_CONTENT', 205, 'Reset Content'), + ('PARTIAL_CONTENT', 206, 'Partial Content'), + ('MULTI_STATUS', 207, 'Multi Status'), + ('IM_USED', 226, 'IM Used'), - 300: 'Multiple Choices', - 301: 'Moved Permanently', - 302: 'Found', - 303: 'See Other', - 304: 'Not Modified', - 305: 'Use Proxy', - 306: '(Unused)', - 307: 'Temporary Redirect', + # redirection + ('MULTIPLE_CHOICES', 300, 'Multiple Choices'), + ('MOVED_PERMANENTLY', 301, 'Moved Permanently'), + ('FOUND', 302, 'Found'), + ('SEE_OTHER', 303, 'See Other'), + ('NOT_MODIFIED', 304, 'Not Modified'), + ('USE_PROXY', 305, 'Use Proxy'), + ('TEMPORARY_REDIRECT', 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', - 428: 'Precondition Required', - 429: 'Too Many Requests', - 431: 'Request Header Fields Too Large', + # client error + ('BAD_REQUEST', 400, 'Bad Request'), + ('UNAUTHORIZED', 401, 'Unauthorized'), + ('PAYMENT_REQUIRED', 402, 'Payment Required'), + ('FORBIDDEN', 403, 'Forbidden'), + ('NOT_FOUND', 404, 'Not Found'), + ('METHOD_NOT_ALLOWED', 405, 'Method Not Allowed'), + ('NOT_ACCEPTABLE', 406, 'Not Acceptable'), + ('PROXY_AUTHENTICATION_REQUIRED', 407, 'Proxy Authentication Required'), + ('REQUEST_TIMEOUT', 408, 'Request Timeout'), + ('CONFLICT', 409, 'Conflict'), + ('GONE', 410, 'Gone'), + ('LENGTH_REQUIRED', 411, 'Length Required'), + ('PRECONDITION_FAILED', 412, 'Precondition Failed'), + ('REQUEST_ENTITY_TOO_LARGE', 413, 'Request Entity Too Large'), + ('REQUEST_URI_TOO_LONG', 414, 'Request-URI Too Long'), + ('UNSUPPORTED_MEDIA_TYPE', 415, 'Unsupported Media Type'), + ('REQUESTED_RANGE_NOT_SATISFIABLE', 416, 'Request Range Not Satisfiable'), + ('EXPECTATION_FAILED', 417, 'Expectation Failed'), + ('UNPROCESSABLE_ENTITY', 422, 'Unprocessable Entity'), + ('LOCKED', 423, 'Locked'), + ('FAILED_DEPENDENCY', 424, 'Failed Dependency'), + ('UPGRADE_REQUIRED', 426, 'Upgrade Required'), + ('PRECONDITION_REQUIRED', 428, 'Precondition Required'), + ('TOO_MANY_REQUESTS', 429, 'Too Many Requests'), + ('REQUEST_HEADER_FIELDS_TOO_LARGE', 431, + 'Request Header Fields Too Large'), - 500: 'Internal Server Error', - 501: 'Not Implemented', - 502: 'Bad Gateway', - 503: 'Service Unavailable', - 504: 'Gateway Timeout', - 505: 'HTTP Version Not Supported', - 511: 'Network Authentication Required', -} + # server error + ('INTERNAL_SERVER_ERROR', 500, 'Internal Server Error'), + ('NOT_IMPLEMENTED', 501, 'Not Implemented'), + ('BAD_GATEWAY', 502, 'Bad Gateway'), + ('SERVICE_UNAVAILABLE', 503, 'Service Unavailable'), + ('GATEWAY_TIMEOUT', 504, 'Gateway Timeout'), + ('HTTP_VERSION_NOT_SUPPORTED', 505, 'HTTP Version Not Supported'), + ('INSUFFICIENT_STORAGE', 507, 'Insufficient Storage'), + ('NOT_EXTENDED', 510, 'Not Extended'), + ('NETWORK_AUTHENTICATION_REQUIRED', 511, + 'Network Authentication Required'), +) + # maximal amount of data to read at one time in _safe_read MAXAMOUNT = 1048576