github.com/pubgo/xprocess@v0.1.11/xutil/http.go (about)

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