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