__init__.py 8.1 KB

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