_http.py 6.2 KB

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