http.go 10 KB

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