github.com/gofiber/fiber/v2@v2.47.0/utils/http.go (about) 1 // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️ 2 // 🤖 Github Repository: https://github.com/gofiber/fiber 3 // 📌 API Documentation: https://docs.gofiber.io 4 5 package utils 6 7 import ( 8 "mime" 9 "strings" 10 ) 11 12 const MIMEOctetStream = "application/octet-stream" 13 14 // GetMIME returns the content-type of a file extension 15 func GetMIME(extension string) string { 16 if len(extension) == 0 { 17 return "" 18 } 19 var foundMime string 20 if extension[0] == '.' { 21 foundMime = mimeExtensions[extension[1:]] 22 } else { 23 foundMime = mimeExtensions[extension] 24 } 25 26 if len(foundMime) == 0 { 27 if extension[0] != '.' { 28 foundMime = mime.TypeByExtension("." + extension) 29 } else { 30 foundMime = mime.TypeByExtension(extension) 31 } 32 33 if foundMime == "" { 34 return MIMEOctetStream 35 } 36 } 37 return foundMime 38 } 39 40 // ParseVendorSpecificContentType check if content type is vendor specific and 41 // if it is parsable to any known types. If its not vendor specific then returns 42 // the original content type. 43 func ParseVendorSpecificContentType(cType string) string { 44 plusIndex := strings.Index(cType, "+") 45 46 if plusIndex == -1 { 47 return cType 48 } 49 50 var parsableType string 51 if semiColonIndex := strings.Index(cType, ";"); semiColonIndex == -1 { 52 parsableType = cType[plusIndex+1:] 53 } else if plusIndex < semiColonIndex { 54 parsableType = cType[plusIndex+1 : semiColonIndex] 55 } else { 56 return cType[:semiColonIndex] 57 } 58 59 slashIndex := strings.Index(cType, "/") 60 61 if slashIndex == -1 { 62 return cType 63 } 64 65 return cType[0:slashIndex+1] + parsableType 66 } 67 68 // limits for HTTP statuscodes 69 const ( 70 statusMessageMin = 100 71 statusMessageMax = 511 72 ) 73 74 // StatusMessage returns the correct message for the provided HTTP statuscode 75 func StatusMessage(status int) string { 76 if status < statusMessageMin || status > statusMessageMax { 77 return "" 78 } 79 return statusMessage[status] 80 } 81 82 // NOTE: Keep this in sync with the status code list 83 var statusMessage = []string{ 84 100: "Continue", // StatusContinue 85 101: "Switching Protocols", // StatusSwitchingProtocols 86 102: "Processing", // StatusProcessing 87 103: "Early Hints", // StatusEarlyHints 88 89 200: "OK", // StatusOK 90 201: "Created", // StatusCreated 91 202: "Accepted", // StatusAccepted 92 203: "Non-Authoritative Information", // StatusNonAuthoritativeInformation 93 204: "No Content", // StatusNoContent 94 205: "Reset Content", // StatusResetContent 95 206: "Partial Content", // StatusPartialContent 96 207: "Multi-Status", // StatusMultiStatus 97 208: "Already Reported", // StatusAlreadyReported 98 226: "IM Used", // StatusIMUsed 99 100 300: "Multiple Choices", // StatusMultipleChoices 101 301: "Moved Permanently", // StatusMovedPermanently 102 302: "Found", // StatusFound 103 303: "See Other", // StatusSeeOther 104 304: "Not Modified", // StatusNotModified 105 305: "Use Proxy", // StatusUseProxy 106 306: "Switch Proxy", // StatusSwitchProxy 107 307: "Temporary Redirect", // StatusTemporaryRedirect 108 308: "Permanent Redirect", // StatusPermanentRedirect 109 110 400: "Bad Request", // StatusBadRequest 111 401: "Unauthorized", // StatusUnauthorized 112 402: "Payment Required", // StatusPaymentRequired 113 403: "Forbidden", // StatusForbidden 114 404: "Not Found", // StatusNotFound 115 405: "Method Not Allowed", // StatusMethodNotAllowed 116 406: "Not Acceptable", // StatusNotAcceptable 117 407: "Proxy Authentication Required", // StatusProxyAuthRequired 118 408: "Request Timeout", // StatusRequestTimeout 119 409: "Conflict", // StatusConflict 120 410: "Gone", // StatusGone 121 411: "Length Required", // StatusLengthRequired 122 412: "Precondition Failed", // StatusPreconditionFailed 123 413: "Request Entity Too Large", // StatusRequestEntityTooLarge 124 414: "Request URI Too Long", // StatusRequestURITooLong 125 415: "Unsupported Media Type", // StatusUnsupportedMediaType 126 416: "Requested Range Not Satisfiable", // StatusRequestedRangeNotSatisfiable 127 417: "Expectation Failed", // StatusExpectationFailed 128 418: "I'm a teapot", // StatusTeapot 129 421: "Misdirected Request", // StatusMisdirectedRequest 130 422: "Unprocessable Entity", // StatusUnprocessableEntity 131 423: "Locked", // StatusLocked 132 424: "Failed Dependency", // StatusFailedDependency 133 425: "Too Early", // StatusTooEarly 134 426: "Upgrade Required", // StatusUpgradeRequired 135 428: "Precondition Required", // StatusPreconditionRequired 136 429: "Too Many Requests", // StatusTooManyRequests 137 431: "Request Header Fields Too Large", // StatusRequestHeaderFieldsTooLarge 138 451: "Unavailable For Legal Reasons", // StatusUnavailableForLegalReasons 139 140 500: "Internal Server Error", // StatusInternalServerError 141 501: "Not Implemented", // StatusNotImplemented 142 502: "Bad Gateway", // StatusBadGateway 143 503: "Service Unavailable", // StatusServiceUnavailable 144 504: "Gateway Timeout", // StatusGatewayTimeout 145 505: "HTTP Version Not Supported", // StatusHTTPVersionNotSupported 146 506: "Variant Also Negotiates", // StatusVariantAlsoNegotiates 147 507: "Insufficient Storage", // StatusInsufficientStorage 148 508: "Loop Detected", // StatusLoopDetected 149 510: "Not Extended", // StatusNotExtended 150 511: "Network Authentication Required", // StatusNetworkAuthenticationRequired 151 } 152 153 // MIME types were copied from https://github.com/nginx/nginx/blob/67d2a9541826ecd5db97d604f23460210fd3e517/conf/mime.types with the following updates: 154 // - Use "application/xml" instead of "text/xml" as recommended per https://datatracker.ietf.org/doc/html/rfc7303#section-4.1 155 // - Use "text/javascript" instead of "application/javascript" as recommended per https://www.rfc-editor.org/rfc/rfc9239#name-text-javascript 156 var mimeExtensions = map[string]string{ 157 "html": "text/html", 158 "htm": "text/html", 159 "shtml": "text/html", 160 "css": "text/css", 161 "xml": "application/xml", 162 "gif": "image/gif", 163 "jpeg": "image/jpeg", 164 "jpg": "image/jpeg", 165 "js": "text/javascript", 166 "atom": "application/atom+xml", 167 "rss": "application/rss+xml", 168 "mml": "text/mathml", 169 "txt": "text/plain", 170 "jad": "text/vnd.sun.j2me.app-descriptor", 171 "wml": "text/vnd.wap.wml", 172 "htc": "text/x-component", 173 "avif": "image/avif", 174 "png": "image/png", 175 "svg": "image/svg+xml", 176 "svgz": "image/svg+xml", 177 "tif": "image/tiff", 178 "tiff": "image/tiff", 179 "wbmp": "image/vnd.wap.wbmp", 180 "webp": "image/webp", 181 "ico": "image/x-icon", 182 "jng": "image/x-jng", 183 "bmp": "image/x-ms-bmp", 184 "woff": "font/woff", 185 "woff2": "font/woff2", 186 "jar": "application/java-archive", 187 "war": "application/java-archive", 188 "ear": "application/java-archive", 189 "json": "application/json", 190 "hqx": "application/mac-binhex40", 191 "doc": "application/msword", 192 "pdf": "application/pdf", 193 "ps": "application/postscript", 194 "eps": "application/postscript", 195 "ai": "application/postscript", 196 "rtf": "application/rtf", 197 "m3u8": "application/vnd.apple.mpegurl", 198 "kml": "application/vnd.google-earth.kml+xml", 199 "kmz": "application/vnd.google-earth.kmz", 200 "xls": "application/vnd.ms-excel", 201 "eot": "application/vnd.ms-fontobject", 202 "ppt": "application/vnd.ms-powerpoint", 203 "odg": "application/vnd.oasis.opendocument.graphics", 204 "odp": "application/vnd.oasis.opendocument.presentation", 205 "ods": "application/vnd.oasis.opendocument.spreadsheet", 206 "odt": "application/vnd.oasis.opendocument.text", 207 "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", 208 "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 209 "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 210 "wmlc": "application/vnd.wap.wmlc", 211 "wasm": "application/wasm", 212 "7z": "application/x-7z-compressed", 213 "cco": "application/x-cocoa", 214 "jardiff": "application/x-java-archive-diff", 215 "jnlp": "application/x-java-jnlp-file", 216 "run": "application/x-makeself", 217 "pl": "application/x-perl", 218 "pm": "application/x-perl", 219 "prc": "application/x-pilot", 220 "pdb": "application/x-pilot", 221 "rar": "application/x-rar-compressed", 222 "rpm": "application/x-redhat-package-manager", 223 "sea": "application/x-sea", 224 "swf": "application/x-shockwave-flash", 225 "sit": "application/x-stuffit", 226 "tcl": "application/x-tcl", 227 "tk": "application/x-tcl", 228 "der": "application/x-x509-ca-cert", 229 "pem": "application/x-x509-ca-cert", 230 "crt": "application/x-x509-ca-cert", 231 "xpi": "application/x-xpinstall", 232 "xhtml": "application/xhtml+xml", 233 "xspf": "application/xspf+xml", 234 "zip": "application/zip", 235 "bin": "application/octet-stream", 236 "exe": "application/octet-stream", 237 "dll": "application/octet-stream", 238 "deb": "application/octet-stream", 239 "dmg": "application/octet-stream", 240 "iso": "application/octet-stream", 241 "img": "application/octet-stream", 242 "msi": "application/octet-stream", 243 "msp": "application/octet-stream", 244 "msm": "application/octet-stream", 245 "mid": "audio/midi", 246 "midi": "audio/midi", 247 "kar": "audio/midi", 248 "mp3": "audio/mpeg", 249 "ogg": "audio/ogg", 250 "m4a": "audio/x-m4a", 251 "ra": "audio/x-realaudio", 252 "3gpp": "video/3gpp", 253 "3gp": "video/3gpp", 254 "ts": "video/mp2t", 255 "mp4": "video/mp4", 256 "mpeg": "video/mpeg", 257 "mpg": "video/mpeg", 258 "mov": "video/quicktime", 259 "webm": "video/webm", 260 "flv": "video/x-flv", 261 "m4v": "video/x-m4v", 262 "mng": "video/x-mng", 263 "asx": "video/x-ms-asf", 264 "asf": "video/x-ms-asf", 265 "wmv": "video/x-ms-wmv", 266 "avi": "video/x-msvideo", 267 }