http.go 6.2 KB

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