1from enum import StrEnum, IntEnum, _simple_enum 2 3__all__ = ['HTTPStatus', 'HTTPMethod'] 4 5 6@_simple_enum(IntEnum) 7class HTTPStatus: 8 """HTTP status codes and reason phrases 9 10 Status codes from the following RFCs are all observed: 11 12 * RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616 13 * RFC 6585: Additional HTTP Status Codes 14 * RFC 3229: Delta encoding in HTTP 15 * RFC 4918: HTTP Extensions for WebDAV, obsoletes 2518 16 * RFC 5842: Binding Extensions to WebDAV 17 * RFC 7238: Permanent Redirect 18 * RFC 2295: Transparent Content Negotiation in HTTP 19 * RFC 2774: An HTTP Extension Framework 20 * RFC 7725: An HTTP Status Code to Report Legal Obstacles 21 * RFC 7540: Hypertext Transfer Protocol Version 2 (HTTP/2) 22 * RFC 2324: Hyper Text Coffee Pot Control Protocol (HTCPCP/1.0) 23 * RFC 8297: An HTTP Status Code for Indicating Hints 24 * RFC 8470: Using Early Data in HTTP 25 """ 26 def __new__(cls, value, phrase, description=''): 27 obj = int.__new__(cls, value) 28 obj._value_ = value 29 30 obj.phrase = phrase 31 obj.description = description 32 return obj 33 34 # informational 35 CONTINUE = 100, 'Continue', 'Request received, please continue' 36 SWITCHING_PROTOCOLS = (101, 'Switching Protocols', 37 'Switching to new protocol; obey Upgrade header') 38 PROCESSING = 102, 'Processing' 39 EARLY_HINTS = 103, 'Early Hints' 40 41 # success 42 OK = 200, 'OK', 'Request fulfilled, document follows' 43 CREATED = 201, 'Created', 'Document created, URL follows' 44 ACCEPTED = (202, 'Accepted', 45 'Request accepted, processing continues off-line') 46 NON_AUTHORITATIVE_INFORMATION = (203, 47 'Non-Authoritative Information', 'Request fulfilled from cache') 48 NO_CONTENT = 204, 'No Content', 'Request fulfilled, nothing follows' 49 RESET_CONTENT = 205, 'Reset Content', 'Clear input form for further input' 50 PARTIAL_CONTENT = 206, 'Partial Content', 'Partial content follows' 51 MULTI_STATUS = 207, 'Multi-Status' 52 ALREADY_REPORTED = 208, 'Already Reported' 53 IM_USED = 226, 'IM Used' 54 55 # redirection 56 MULTIPLE_CHOICES = (300, 'Multiple Choices', 57 'Object has several resources -- see URI list') 58 MOVED_PERMANENTLY = (301, 'Moved Permanently', 59 'Object moved permanently -- see URI list') 60 FOUND = 302, 'Found', 'Object moved temporarily -- see URI list' 61 SEE_OTHER = 303, 'See Other', 'Object moved -- see Method and URL list' 62 NOT_MODIFIED = (304, 'Not Modified', 63 'Document has not changed since given time') 64 USE_PROXY = (305, 'Use Proxy', 65 'You must use proxy specified in Location to access this resource') 66 TEMPORARY_REDIRECT = (307, 'Temporary Redirect', 67 'Object moved temporarily -- see URI list') 68 PERMANENT_REDIRECT = (308, 'Permanent Redirect', 69 'Object moved permanently -- see URI list') 70 71 # client error 72 BAD_REQUEST = (400, 'Bad Request', 73 'Bad request syntax or unsupported method') 74 UNAUTHORIZED = (401, 'Unauthorized', 75 'No permission -- see authorization schemes') 76 PAYMENT_REQUIRED = (402, 'Payment Required', 77 'No payment -- see charging schemes') 78 FORBIDDEN = (403, 'Forbidden', 79 'Request forbidden -- authorization will not help') 80 NOT_FOUND = (404, 'Not Found', 81 'Nothing matches the given URI') 82 METHOD_NOT_ALLOWED = (405, 'Method Not Allowed', 83 'Specified method is invalid for this resource') 84 NOT_ACCEPTABLE = (406, 'Not Acceptable', 85 'URI not available in preferred format') 86 PROXY_AUTHENTICATION_REQUIRED = (407, 87 'Proxy Authentication Required', 88 'You must authenticate with this proxy before proceeding') 89 REQUEST_TIMEOUT = (408, 'Request Timeout', 90 'Request timed out; try again later') 91 CONFLICT = 409, 'Conflict', 'Request conflict' 92 GONE = (410, 'Gone', 93 'URI no longer exists and has been permanently removed') 94 LENGTH_REQUIRED = (411, 'Length Required', 95 'Client must specify Content-Length') 96 PRECONDITION_FAILED = (412, 'Precondition Failed', 97 'Precondition in headers is false') 98 REQUEST_ENTITY_TOO_LARGE = (413, 'Request Entity Too Large', 99 'Entity is too large') 100 REQUEST_URI_TOO_LONG = (414, 'Request-URI Too Long', 101 'URI is too long') 102 UNSUPPORTED_MEDIA_TYPE = (415, 'Unsupported Media Type', 103 'Entity body in unsupported format') 104 REQUESTED_RANGE_NOT_SATISFIABLE = (416, 105 'Requested Range Not Satisfiable', 106 'Cannot satisfy request range') 107 EXPECTATION_FAILED = (417, 'Expectation Failed', 108 'Expect condition could not be satisfied') 109 IM_A_TEAPOT = (418, 'I\'m a Teapot', 110 'Server refuses to brew coffee because it is a teapot.') 111 MISDIRECTED_REQUEST = (421, 'Misdirected Request', 112 'Server is not able to produce a response') 113 UNPROCESSABLE_ENTITY = 422, 'Unprocessable Entity' 114 LOCKED = 423, 'Locked' 115 FAILED_DEPENDENCY = 424, 'Failed Dependency' 116 TOO_EARLY = 425, 'Too Early' 117 UPGRADE_REQUIRED = 426, 'Upgrade Required' 118 PRECONDITION_REQUIRED = (428, 'Precondition Required', 119 'The origin server requires the request to be conditional') 120 TOO_MANY_REQUESTS = (429, 'Too Many Requests', 121 'The user has sent too many requests in ' 122 'a given amount of time ("rate limiting")') 123 REQUEST_HEADER_FIELDS_TOO_LARGE = (431, 124 'Request Header Fields Too Large', 125 'The server is unwilling to process the request because its header ' 126 'fields are too large') 127 UNAVAILABLE_FOR_LEGAL_REASONS = (451, 128 'Unavailable For Legal Reasons', 129 'The server is denying access to the ' 130 'resource as a consequence of a legal demand') 131 132 # server errors 133 INTERNAL_SERVER_ERROR = (500, 'Internal Server Error', 134 'Server got itself in trouble') 135 NOT_IMPLEMENTED = (501, 'Not Implemented', 136 'Server does not support this operation') 137 BAD_GATEWAY = (502, 'Bad Gateway', 138 'Invalid responses from another server/proxy') 139 SERVICE_UNAVAILABLE = (503, 'Service Unavailable', 140 'The server cannot process the request due to a high load') 141 GATEWAY_TIMEOUT = (504, 'Gateway Timeout', 142 'The gateway server did not receive a timely response') 143 HTTP_VERSION_NOT_SUPPORTED = (505, 'HTTP Version Not Supported', 144 'Cannot fulfill request') 145 VARIANT_ALSO_NEGOTIATES = 506, 'Variant Also Negotiates' 146 INSUFFICIENT_STORAGE = 507, 'Insufficient Storage' 147 LOOP_DETECTED = 508, 'Loop Detected' 148 NOT_EXTENDED = 510, 'Not Extended' 149 NETWORK_AUTHENTICATION_REQUIRED = (511, 150 'Network Authentication Required', 151 'The client needs to authenticate to gain network access') 152 153 154@_simple_enum(StrEnum) 155class HTTPMethod: 156 """HTTP methods and descriptions 157 158 Methods from the following RFCs are all observed: 159 160 * RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616 161 * RFC 5789: PATCH Method for HTTP 162 """ 163 def __new__(cls, value, description): 164 obj = str.__new__(cls, value) 165 obj._value_ = value 166 obj.description = description 167 return obj 168 169 def __repr__(self): 170 return "<%s.%s>" % (self.__class__.__name__, self._name_) 171 172 CONNECT = 'CONNECT', 'Establish a connection to the server.' 173 DELETE = 'DELETE', 'Remove the target.' 174 GET = 'GET', 'Retrieve the target.' 175 HEAD = 'HEAD', 'Same as GET, but only retrieve the status line and header section.' 176 OPTIONS = 'OPTIONS', 'Describe the communication options for the target.' 177 PATCH = 'PATCH', 'Apply partial modifications to a target.' 178 POST = 'POST', 'Perform target-specific processing with the request payload.' 179 PUT = 'PUT', 'Replace the target with the request payload.' 180 TRACE = 'TRACE', 'Perform a message loop-back test along the path to the target.' 181