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