status.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package fasthttp
  2. import (
  3. "strconv"
  4. )
  5. const (
  6. statusMessageMin = 100
  7. statusMessageMax = 511
  8. )
  9. // HTTP status codes were stolen from net/http.
  10. const (
  11. StatusContinue = 100 // RFC 7231, 6.2.1
  12. StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
  13. StatusProcessing = 102 // RFC 2518, 10.1
  14. StatusEarlyHints = 103 // RFC 8297
  15. StatusOK = 200 // RFC 7231, 6.3.1
  16. StatusCreated = 201 // RFC 7231, 6.3.2
  17. StatusAccepted = 202 // RFC 7231, 6.3.3
  18. StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4
  19. StatusNoContent = 204 // RFC 7231, 6.3.5
  20. StatusResetContent = 205 // RFC 7231, 6.3.6
  21. StatusPartialContent = 206 // RFC 7233, 4.1
  22. StatusMultiStatus = 207 // RFC 4918, 11.1
  23. StatusAlreadyReported = 208 // RFC 5842, 7.1
  24. StatusIMUsed = 226 // RFC 3229, 10.4.1
  25. StatusMultipleChoices = 300 // RFC 7231, 6.4.1
  26. StatusMovedPermanently = 301 // RFC 7231, 6.4.2
  27. StatusFound = 302 // RFC 7231, 6.4.3
  28. StatusSeeOther = 303 // RFC 7231, 6.4.4
  29. StatusNotModified = 304 // RFC 7232, 4.1
  30. StatusUseProxy = 305 // RFC 7231, 6.4.5
  31. _ = 306 // RFC 7231, 6.4.6 (Unused)
  32. StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7
  33. StatusPermanentRedirect = 308 // RFC 7538, 3
  34. StatusBadRequest = 400 // RFC 7231, 6.5.1
  35. StatusUnauthorized = 401 // RFC 7235, 3.1
  36. StatusPaymentRequired = 402 // RFC 7231, 6.5.2
  37. StatusForbidden = 403 // RFC 7231, 6.5.3
  38. StatusNotFound = 404 // RFC 7231, 6.5.4
  39. StatusMethodNotAllowed = 405 // RFC 7231, 6.5.5
  40. StatusNotAcceptable = 406 // RFC 7231, 6.5.6
  41. StatusProxyAuthRequired = 407 // RFC 7235, 3.2
  42. StatusRequestTimeout = 408 // RFC 7231, 6.5.7
  43. StatusConflict = 409 // RFC 7231, 6.5.8
  44. StatusGone = 410 // RFC 7231, 6.5.9
  45. StatusLengthRequired = 411 // RFC 7231, 6.5.10
  46. StatusPreconditionFailed = 412 // RFC 7232, 4.2
  47. StatusRequestEntityTooLarge = 413 // RFC 7231, 6.5.11
  48. StatusRequestURITooLong = 414 // RFC 7231, 6.5.12
  49. StatusUnsupportedMediaType = 415 // RFC 7231, 6.5.13
  50. StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4
  51. StatusExpectationFailed = 417 // RFC 7231, 6.5.14
  52. StatusTeapot = 418 // RFC 7168, 2.3.3
  53. StatusMisdirectedRequest = 421 // RFC 7540, 9.1.2
  54. StatusUnprocessableEntity = 422 // RFC 4918, 11.2
  55. StatusLocked = 423 // RFC 4918, 11.3
  56. StatusFailedDependency = 424 // RFC 4918, 11.4
  57. StatusUpgradeRequired = 426 // RFC 7231, 6.5.15
  58. StatusPreconditionRequired = 428 // RFC 6585, 3
  59. StatusTooManyRequests = 429 // RFC 6585, 4
  60. StatusRequestHeaderFieldsTooLarge = 431 // RFC 6585, 5
  61. StatusUnavailableForLegalReasons = 451 // RFC 7725, 3
  62. StatusInternalServerError = 500 // RFC 7231, 6.6.1
  63. StatusNotImplemented = 501 // RFC 7231, 6.6.2
  64. StatusBadGateway = 502 // RFC 7231, 6.6.3
  65. StatusServiceUnavailable = 503 // RFC 7231, 6.6.4
  66. StatusGatewayTimeout = 504 // RFC 7231, 6.6.5
  67. StatusHTTPVersionNotSupported = 505 // RFC 7231, 6.6.6
  68. StatusVariantAlsoNegotiates = 506 // RFC 2295, 8.1
  69. StatusInsufficientStorage = 507 // RFC 4918, 11.5
  70. StatusLoopDetected = 508 // RFC 5842, 7.2
  71. StatusNotExtended = 510 // RFC 2774, 7
  72. StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6
  73. )
  74. var (
  75. unknownStatusCode = "Unknown Status Code"
  76. statusMessages = []string{
  77. StatusContinue: "Continue",
  78. StatusSwitchingProtocols: "Switching Protocols",
  79. StatusProcessing: "Processing",
  80. StatusEarlyHints: "Early Hints",
  81. StatusOK: "OK",
  82. StatusCreated: "Created",
  83. StatusAccepted: "Accepted",
  84. StatusNonAuthoritativeInfo: "Non-Authoritative Information",
  85. StatusNoContent: "No Content",
  86. StatusResetContent: "Reset Content",
  87. StatusPartialContent: "Partial Content",
  88. StatusMultiStatus: "Multi-Status",
  89. StatusAlreadyReported: "Already Reported",
  90. StatusIMUsed: "IM Used",
  91. StatusMultipleChoices: "Multiple Choices",
  92. StatusMovedPermanently: "Moved Permanently",
  93. StatusFound: "Found",
  94. StatusSeeOther: "See Other",
  95. StatusNotModified: "Not Modified",
  96. StatusUseProxy: "Use Proxy",
  97. StatusTemporaryRedirect: "Temporary Redirect",
  98. StatusPermanentRedirect: "Permanent Redirect",
  99. StatusBadRequest: "Bad Request",
  100. StatusUnauthorized: "Unauthorized",
  101. StatusPaymentRequired: "Payment Required",
  102. StatusForbidden: "Forbidden",
  103. StatusNotFound: "Not Found",
  104. StatusMethodNotAllowed: "Method Not Allowed",
  105. StatusNotAcceptable: "Not Acceptable",
  106. StatusProxyAuthRequired: "Proxy Authentication Required",
  107. StatusRequestTimeout: "Request Timeout",
  108. StatusConflict: "Conflict",
  109. StatusGone: "Gone",
  110. StatusLengthRequired: "Length Required",
  111. StatusPreconditionFailed: "Precondition Failed",
  112. StatusRequestEntityTooLarge: "Request Entity Too Large",
  113. StatusRequestURITooLong: "Request URI Too Long",
  114. StatusUnsupportedMediaType: "Unsupported Media Type",
  115. StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable",
  116. StatusExpectationFailed: "Expectation Failed",
  117. StatusTeapot: "I'm a teapot",
  118. StatusMisdirectedRequest: "Misdirected Request",
  119. StatusUnprocessableEntity: "Unprocessable Entity",
  120. StatusLocked: "Locked",
  121. StatusFailedDependency: "Failed Dependency",
  122. StatusUpgradeRequired: "Upgrade Required",
  123. StatusPreconditionRequired: "Precondition Required",
  124. StatusTooManyRequests: "Too Many Requests",
  125. StatusRequestHeaderFieldsTooLarge: "Request Header Fields Too Large",
  126. StatusUnavailableForLegalReasons: "Unavailable For Legal Reasons",
  127. StatusInternalServerError: "Internal Server Error",
  128. StatusNotImplemented: "Not Implemented",
  129. StatusBadGateway: "Bad Gateway",
  130. StatusServiceUnavailable: "Service Unavailable",
  131. StatusGatewayTimeout: "Gateway Timeout",
  132. StatusHTTPVersionNotSupported: "HTTP Version Not Supported",
  133. StatusVariantAlsoNegotiates: "Variant Also Negotiates",
  134. StatusInsufficientStorage: "Insufficient Storage",
  135. StatusLoopDetected: "Loop Detected",
  136. StatusNotExtended: "Not Extended",
  137. StatusNetworkAuthenticationRequired: "Network Authentication Required",
  138. }
  139. )
  140. // StatusMessage returns HTTP status message for the given status code.
  141. func StatusMessage(statusCode int) string {
  142. if statusCode < statusMessageMin || statusCode > statusMessageMax {
  143. return unknownStatusCode
  144. }
  145. if s := statusMessages[statusCode]; s != "" {
  146. return s
  147. }
  148. return unknownStatusCode
  149. }
  150. func formatStatusLine(dst []byte, protocol []byte, statusCode int, statusText []byte) []byte {
  151. dst = append(dst, protocol...)
  152. dst = append(dst, ' ')
  153. dst = strconv.AppendInt(dst, int64(statusCode), 10)
  154. dst = append(dst, ' ')
  155. if len(statusText) == 0 {
  156. dst = append(dst, s2b(StatusMessage(statusCode))...)
  157. } else {
  158. dst = append(dst, statusText...)
  159. }
  160. return append(dst, strCRLF...)
  161. }