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