http.go 7.6 KB

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