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