github.com/cloudwego/hertz@v0.9.3/pkg/protocol/consts/status.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * The MIT License (MIT) 17 * 18 * Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors 19 * 20 * Permission is hereby granted, free of charge, to any person obtaining a copy 21 * of this software and associated documentation files (the "Software"), to deal 22 * in the Software without restriction, including without limitation the rights 23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 24 * copies of the Software, and to permit persons to whom the Software is 25 * furnished to do so, subject to the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be included in 28 * all copies or substantial portions of the Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 36 * THE SOFTWARE. 37 * 38 * This file may have been modified by CloudWeGo authors. All CloudWeGo 39 * Modifications are Copyright 2022 CloudWeGo Authors. 40 */ 41 42 package consts 43 44 import ( 45 "fmt" 46 "sync/atomic" 47 ) 48 49 const ( 50 statusMessageMin = 100 51 statusMessageMax = 511 52 ) 53 54 // HTTP status codes were stolen from net/http. 55 const ( 56 StatusContinue = 100 // RFC 7231, 6.2.1 57 StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2 58 StatusProcessing = 102 // RFC 2518, 10.1 59 60 StatusOK = 200 // RFC 7231, 6.3.1 61 StatusCreated = 201 // RFC 7231, 6.3.2 62 StatusAccepted = 202 // RFC 7231, 6.3.3 63 StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4 64 StatusNoContent = 204 // RFC 7231, 6.3.5 65 StatusResetContent = 205 // RFC 7231, 6.3.6 66 StatusPartialContent = 206 // RFC 7233, 4.1 67 StatusMultiStatus = 207 // RFC 4918, 11.1 68 StatusAlreadyReported = 208 // RFC 5842, 7.1 69 StatusIMUsed = 226 // RFC 3229, 10.4.1 70 71 StatusMultipleChoices = 300 // RFC 7231, 6.4.1 72 StatusMovedPermanently = 301 // RFC 7231, 6.4.2 73 StatusFound = 302 // RFC 7231, 6.4.3 74 StatusSeeOther = 303 // RFC 7231, 6.4.4 75 StatusNotModified = 304 // RFC 7232, 4.1 76 StatusUseProxy = 305 // RFC 7231, 6.4.5 77 _ = 306 // RFC 7231, 6.4.6 (Unused) 78 StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7 79 StatusPermanentRedirect = 308 // RFC 7538, 3 80 81 StatusBadRequest = 400 // RFC 7231, 6.5.1 82 StatusUnauthorized = 401 // RFC 7235, 3.1 83 StatusPaymentRequired = 402 // RFC 7231, 6.5.2 84 StatusForbidden = 403 // RFC 7231, 6.5.3 85 StatusNotFound = 404 // RFC 7231, 6.5.4 86 StatusMethodNotAllowed = 405 // RFC 7231, 6.5.5 87 StatusNotAcceptable = 406 // RFC 7231, 6.5.6 88 StatusProxyAuthRequired = 407 // RFC 7235, 3.2 89 StatusRequestTimeout = 408 // RFC 7231, 6.5.7 90 StatusConflict = 409 // RFC 7231, 6.5.8 91 StatusGone = 410 // RFC 7231, 6.5.9 92 StatusLengthRequired = 411 // RFC 7231, 6.5.10 93 StatusPreconditionFailed = 412 // RFC 7232, 4.2 94 StatusRequestEntityTooLarge = 413 // RFC 7231, 6.5.11 95 StatusRequestURITooLong = 414 // RFC 7231, 6.5.12 96 StatusUnsupportedMediaType = 415 // RFC 7231, 6.5.13 97 StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4 98 StatusExpectationFailed = 417 // RFC 7231, 6.5.14 99 StatusTeapot = 418 // RFC 7168, 2.3.3 100 StatusUnprocessableEntity = 422 // RFC 4918, 11.2 101 StatusLocked = 423 // RFC 4918, 11.3 102 StatusFailedDependency = 424 // RFC 4918, 11.4 103 StatusUpgradeRequired = 426 // RFC 7231, 6.5.15 104 StatusPreconditionRequired = 428 // RFC 6585, 3 105 StatusTooManyRequests = 429 // RFC 6585, 4 106 StatusRequestHeaderFieldsTooLarge = 431 // RFC 6585, 5 107 StatusUnavailableForLegalReasons = 451 // RFC 7725, 3 108 109 StatusInternalServerError = 500 // RFC 7231, 6.6.1 110 StatusNotImplemented = 501 // RFC 7231, 6.6.2 111 StatusBadGateway = 502 // RFC 7231, 6.6.3 112 StatusServiceUnavailable = 503 // RFC 7231, 6.6.4 113 StatusGatewayTimeout = 504 // RFC 7231, 6.6.5 114 StatusHTTPVersionNotSupported = 505 // RFC 7231, 6.6.6 115 StatusVariantAlsoNegotiates = 506 // RFC 2295, 8.1 116 StatusInsufficientStorage = 507 // RFC 4918, 11.5 117 StatusLoopDetected = 508 // RFC 5842, 7.2 118 StatusNotExtended = 510 // RFC 2774, 7 119 StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6 120 ) 121 122 var ( 123 statusLines atomic.Value 124 125 statusMessages = map[int]string{ 126 StatusContinue: "Continue", 127 StatusSwitchingProtocols: "Switching Protocols", 128 StatusProcessing: "Processing", 129 130 StatusOK: "OK", 131 StatusCreated: "Created", 132 StatusAccepted: "Accepted", 133 StatusNonAuthoritativeInfo: "Non-Authoritative Information", 134 StatusNoContent: "No Content", 135 StatusResetContent: "Reset Content", 136 StatusPartialContent: "Partial Content", 137 StatusMultiStatus: "Multi-Status", 138 StatusAlreadyReported: "Already Reported", 139 StatusIMUsed: "IM Used", 140 141 StatusMultipleChoices: "Multiple Choices", 142 StatusMovedPermanently: "Moved Permanently", 143 StatusFound: "Found", 144 StatusSeeOther: "See Other", 145 StatusNotModified: "Not Modified", 146 StatusUseProxy: "Use Proxy", 147 StatusTemporaryRedirect: "Temporary Redirect", 148 StatusPermanentRedirect: "Permanent Redirect", 149 150 StatusBadRequest: "Bad Request", 151 StatusUnauthorized: "Unauthorized", 152 StatusPaymentRequired: "Payment Required", 153 StatusForbidden: "Forbidden", 154 StatusNotFound: "404 Page not found", 155 StatusMethodNotAllowed: "Method Not Allowed", 156 StatusNotAcceptable: "Not Acceptable", 157 StatusProxyAuthRequired: "Proxy Authentication Required", 158 StatusRequestTimeout: "Request Timeout", 159 StatusConflict: "Conflict", 160 StatusGone: "Gone", 161 StatusLengthRequired: "Length Required", 162 StatusPreconditionFailed: "Precondition Failed", 163 StatusRequestEntityTooLarge: "Request Entity Too Large", 164 StatusRequestURITooLong: "Request URI Too Long", 165 StatusUnsupportedMediaType: "Unsupported Media Type", 166 StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable", 167 StatusExpectationFailed: "Expectation Failed", 168 StatusTeapot: "I'm a teapot", 169 StatusUnprocessableEntity: "Unprocessable Entity", 170 StatusLocked: "Locked", 171 StatusFailedDependency: "Failed Dependency", 172 StatusUpgradeRequired: "Upgrade Required", 173 StatusPreconditionRequired: "Precondition Required", 174 StatusTooManyRequests: "Too Many Requests", 175 StatusRequestHeaderFieldsTooLarge: "Request Header Fields Too Large", 176 StatusUnavailableForLegalReasons: "Unavailable For Legal Reasons", 177 178 StatusInternalServerError: "Internal Server Error", 179 StatusNotImplemented: "Not Implemented", 180 StatusBadGateway: "Bad Gateway", 181 StatusServiceUnavailable: "Service Unavailable", 182 StatusGatewayTimeout: "Gateway Timeout", 183 StatusHTTPVersionNotSupported: "HTTP Version Not Supported", 184 StatusVariantAlsoNegotiates: "Variant Also Negotiates", 185 StatusInsufficientStorage: "Insufficient Storage", 186 StatusLoopDetected: "Loop Detected", 187 StatusNotExtended: "Not Extended", 188 StatusNetworkAuthenticationRequired: "Network Authentication Required", 189 } 190 ) 191 192 // StatusMessage returns HTTP status message for the given status code. 193 func StatusMessage(statusCode int) string { 194 if statusCode < statusMessageMin || statusCode > statusMessageMax { 195 return "Unknown Status Code" 196 } 197 198 s := statusMessages[statusCode] 199 if s == "" { 200 s = "Unknown Status Code" 201 } 202 return s 203 } 204 205 func init() { 206 statusLines.Store(make(map[int][]byte)) 207 } 208 209 func StatusLine(statusCode int) []byte { 210 m := statusLines.Load().(map[int][]byte) 211 h := m[statusCode] 212 if h != nil { 213 return h 214 } 215 216 statusText := StatusMessage(statusCode) 217 218 h = []byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", statusCode, statusText)) 219 newM := make(map[int][]byte, len(m)+1) 220 for k, v := range m { 221 newM[k] = v 222 } 223 newM[statusCode] = h 224 statusLines.Store(newM) 225 return h 226 }