github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/http/fast/utils.go (about)

     1  package fast
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/gorilla/schema"
     6  	jsoniter "github.com/json-iterator/go"
     7  	"net"
     8  	"regexp"
     9  	"strings"
    10  	"time"
    11  	"unsafe"
    12  )
    13  
    14  var isPrefork, isChild bool
    15  var jsonParser = jsoniter.ConfigCompatibleWithStandardLibrary
    16  var schemaDecoder = schema.NewDecoder()
    17  
    18  func groupPaths(prefix, path string) string {
    19  	if path == "/" {
    20  		path = ""
    21  	}
    22  	path = prefix + path
    23  	path = strings.Replace(path, "//", "/", -1)
    24  	return path
    25  }
    26  
    27  func getParams(path string) (params []string) {
    28  	if len(path) < 1 {
    29  		return
    30  	}
    31  	segments := strings.Split(path, "/")
    32  	replacer := strings.NewReplacer(":", "", "?", "")
    33  	for _, s := range segments {
    34  		if s == "" {
    35  			continue
    36  		} else if s[0] == ':' {
    37  			params = append(params, replacer.Replace(s))
    38  		}
    39  		if strings.Contains(s, "*") {
    40  			params = append(params, "*")
    41  		}
    42  	}
    43  	return
    44  }
    45  
    46  func getRegex(path string) (*regexp.Regexp, error) {
    47  	pattern := "^"
    48  	segments := strings.Split(path, "/")
    49  	for _, s := range segments {
    50  		if s == "" {
    51  			continue
    52  		}
    53  		if s[0] == ':' {
    54  			if strings.Contains(s, "?") {
    55  				pattern += "(?:/([^/]+?))?"
    56  			} else {
    57  				pattern += "/(?:([^/]+?))"
    58  			}
    59  		} else if s[0] == '*' {
    60  			pattern += "/(.*)"
    61  		} else {
    62  			pattern += "/" + s
    63  		}
    64  	}
    65  	pattern += "/?$"
    66  	regex, err := regexp.Compile(pattern)
    67  	return regex, err
    68  }
    69  
    70  func getMIME(extension string) (mime string) {
    71  	if extension == "" {
    72  		return mime
    73  	}
    74  	mime = extensionMIME[extension]
    75  	if mime == "" {
    76  		return "application/octet-stream"
    77  	}
    78  	return mime
    79  }
    80  
    81  var GetString = func(b []byte) string {
    82  	return *(*string)(unsafe.Pointer(&b))
    83  }
    84  var getStringImmutable = func(b []byte) string {
    85  	return string(b)
    86  }
    87  
    88  // https://golang.org/src/net/net.go#L113
    89  // Helper methods for application#test
    90  type testConn struct {
    91  	net.Conn
    92  	r bytes.Buffer
    93  	w bytes.Buffer
    94  }
    95  
    96  func (c *testConn) RemoteAddr() net.Addr               { return &net.TCPAddr{IP: net.IPv4(0, 0, 0, 0)} }
    97  func (c *testConn) LocalAddr() net.Addr                { return c.RemoteAddr() }
    98  func (c *testConn) Read(b []byte) (int, error)         { return c.r.Read(b) }
    99  func (c *testConn) Write(b []byte) (int, error)        { return c.w.Write(b) }
   100  func (c *testConn) Close() error                       { return nil }
   101  func (c *testConn) SetDeadline(t time.Time) error      { return nil }
   102  func (c *testConn) SetReadDeadline(t time.Time) error  { return nil }
   103  func (c *testConn) SetWriteDeadline(t time.Time) error { return nil }
   104  
   105  // HTTP status codes were copied from net/http.
   106  var statusMessages = map[int]string{
   107  	100: "Continue",
   108  	101: "Switching Protocols",
   109  	102: "Processing",
   110  	200: "OK",
   111  	201: "Created",
   112  	202: "Accepted",
   113  	203: "Non-Authoritative Information",
   114  	204: "No Content",
   115  	205: "Reset Content",
   116  	206: "Partial Content",
   117  	207: "Multi-Status",
   118  	208: "Already Reported",
   119  	226: "IM Used",
   120  	300: "Multiple Choices",
   121  	301: "Moved Permanently",
   122  	302: "Found",
   123  	303: "See Other",
   124  	304: "Not Modified",
   125  	305: "Use Proxy",
   126  	306: "Switch Proxy",
   127  	307: "Temporary Redirect",
   128  	308: "Permanent Redirect",
   129  	400: "Bad Request",
   130  	401: "Unauthorized",
   131  	402: "Payment Required",
   132  	403: "Forbidden",
   133  	404: "Not Found",
   134  	405: "Method Not Allowed",
   135  	406: "Not Acceptable",
   136  	407: "Proxy Authentication Required",
   137  	408: "Request Timeout",
   138  	409: "Conflict",
   139  	410: "Gone",
   140  	411: "Length Required",
   141  	412: "Precondition Failed",
   142  	413: "Request Entity Too Large",
   143  	414: "Request URI Too Long",
   144  	415: "Unsupported Media Type",
   145  	416: "Requested Range Not Satisfiable",
   146  	417: "Expectation Failed",
   147  	418: "I'm a teapot",
   148  	422: "Unprocessable Entity",
   149  	423: "Locked",
   150  	424: "Failed Dependency",
   151  	426: "Upgrade Required",
   152  	428: "Precondition Required",
   153  	429: "Too Many Requests",
   154  	431: "Request Header Fields Too Large",
   155  	451: "Unavailable For Legal Reasons",
   156  	500: "Internal Server Error",
   157  	501: "Not Implemented",
   158  	502: "Bad Gateway",
   159  	503: "Service Unavailable",
   160  	504: "Gateway Timeout",
   161  	505: "HTTP Version Not Supported",
   162  	506: "Variant Also Negotiates",
   163  	507: "Insufficient Storage",
   164  	508: "Loop Detected",
   165  	510: "Not Extended",
   166  	511: "Network Authentication Required",
   167  }
   168  
   169  // MIME types were copied from nginx/mime.types.
   170  var extensionMIME = map[string]string{
   171  	// without dot
   172  	"html":    "text/html",
   173  	"htm":     "text/html",
   174  	"shtml":   "text/html",
   175  	"css":     "text/css",
   176  	"gif":     "image/gif",
   177  	"jpeg":    "image/jpeg",
   178  	"jpg":     "image/jpeg",
   179  	"xml":     "application/xml",
   180  	"js":      "application/javascript",
   181  	"atom":    "application/atom+xml",
   182  	"rss":     "application/rss+xml",
   183  	"mml":     "text/mathml",
   184  	"txt":     "text/plain",
   185  	"jad":     "text/vnd.sun.j2me.app-descriptor",
   186  	"wml":     "text/vnd.wap.wml",
   187  	"htc":     "text/x-component",
   188  	"png":     "image/png",
   189  	"svg":     "image/svg+xml",
   190  	"svgz":    "image/svg+xml",
   191  	"tif":     "image/tiff",
   192  	"tiff":    "image/tiff",
   193  	"wbmp":    "image/vnd.wap.wbmp",
   194  	"webp":    "image/webp",
   195  	"ico":     "image/x-icon",
   196  	"jng":     "image/x-jng",
   197  	"bmp":     "image/x-ms-bmp",
   198  	"woff":    "font/woff",
   199  	"woff2":   "font/woff2",
   200  	"jar":     "application/java-archive",
   201  	"war":     "application/java-archive",
   202  	"ear":     "application/java-archive",
   203  	"json":    "application/json",
   204  	"hqx":     "application/mac-binhex40",
   205  	"doc":     "application/msword",
   206  	"pdf":     "application/pdf",
   207  	"ps":      "application/postscript",
   208  	"eps":     "application/postscript",
   209  	"ai":      "application/postscript",
   210  	"rtf":     "application/rtf",
   211  	"m3u8":    "application/vnd.apple.mpegurl",
   212  	"kml":     "application/vnd.google-earth.kml+xml",
   213  	"kmz":     "application/vnd.google-earth.kmz",
   214  	"xls":     "application/vnd.ms-excel",
   215  	"eot":     "application/vnd.ms-fontobject",
   216  	"ppt":     "application/vnd.ms-powerpoint",
   217  	"odg":     "application/vnd.oasis.opendocument.graphics",
   218  	"odp":     "application/vnd.oasis.opendocument.presentation",
   219  	"ods":     "application/vnd.oasis.opendocument.spreadsheet",
   220  	"odt":     "application/vnd.oasis.opendocument.text",
   221  	"wmlc":    "application/vnd.wap.wmlc",
   222  	"7z":      "application/x-7z-compressed",
   223  	"cco":     "application/x-cocoa",
   224  	"jardiff": "application/x-java-archive-diff",
   225  	"jnlp":    "application/x-java-jnlp-file",
   226  	"run":     "application/x-makeself",
   227  	"pl":      "application/x-perl",
   228  	"pm":      "application/x-perl",
   229  	"prc":     "application/x-pilot",
   230  	"pdb":     "application/x-pilot",
   231  	"rar":     "application/x-rar-compressed",
   232  	"rpm":     "application/x-redhat-package-manager",
   233  	"sea":     "application/x-sea",
   234  	"swf":     "application/x-shockwave-flash",
   235  	"sit":     "application/x-stuffit",
   236  	"tcl":     "application/x-tcl",
   237  	"tk":      "application/x-tcl",
   238  	"der":     "application/x-x509-ca-cert",
   239  	"pem":     "application/x-x509-ca-cert",
   240  	"crt":     "application/x-x509-ca-cert",
   241  	"xpi":     "application/x-xpinstall",
   242  	"xhtml":   "application/xhtml+xml",
   243  	"xspf":    "application/xspf+xml",
   244  	"zip":     "application/zip",
   245  	"bin":     "application/octet-stream",
   246  	"exe":     "application/octet-stream",
   247  	"dll":     "application/octet-stream",
   248  	"deb":     "application/octet-stream",
   249  	"dmg":     "application/octet-stream",
   250  	"iso":     "application/octet-stream",
   251  	"img":     "application/octet-stream",
   252  	"msi":     "application/octet-stream",
   253  	"msp":     "application/octet-stream",
   254  	"msm":     "application/octet-stream",
   255  	"mid":     "audio/midi",
   256  	"midi":    "audio/midi",
   257  	"kar":     "audio/midi",
   258  	"mp3":     "audio/mpeg",
   259  	"ogg":     "audio/ogg",
   260  	"m4a":     "audio/x-m4a",
   261  	"ra":      "audio/x-realaudio",
   262  	"3gpp":    "video/3gpp",
   263  	"3gp":     "video/3gpp",
   264  	"ts":      "video/mp2t",
   265  	"mp4":     "video/mp4",
   266  	"mpeg":    "video/mpeg",
   267  	"mpg":     "video/mpeg",
   268  	"mov":     "video/quicktime",
   269  	"webm":    "video/webm",
   270  	"flv":     "video/x-flv",
   271  	"m4v":     "video/x-m4v",
   272  	"mng":     "video/x-mng",
   273  	"asx":     "video/x-ms-asf",
   274  	"asf":     "video/x-ms-asf",
   275  	"wmv":     "video/x-ms-wmv",
   276  	"avi":     "video/x-msvideo",
   277  
   278  	// with dot
   279  	".html":    "text/html",
   280  	".htm":     "text/html",
   281  	".shtml":   "text/html",
   282  	".css":     "text/css",
   283  	".gif":     "image/gif",
   284  	".jpeg":    "image/jpeg",
   285  	".jpg":     "image/jpeg",
   286  	".xml":     "application/xml",
   287  	".js":      "application/javascript",
   288  	".atom":    "application/atom+xml",
   289  	".rss":     "application/rss+xml",
   290  	".mml":     "text/mathml",
   291  	".txt":     "text/plain",
   292  	".jad":     "text/vnd.sun.j2me.app-descriptor",
   293  	".wml":     "text/vnd.wap.wml",
   294  	".htc":     "text/x-component",
   295  	".png":     "image/png",
   296  	".svg":     "image/svg+xml",
   297  	".svgz":    "image/svg+xml",
   298  	".tif":     "image/tiff",
   299  	".tiff":    "image/tiff",
   300  	".wbmp":    "image/vnd.wap.wbmp",
   301  	".webp":    "image/webp",
   302  	".ico":     "image/x-icon",
   303  	".jng":     "image/x-jng",
   304  	".bmp":     "image/x-ms-bmp",
   305  	".woff":    "font/woff",
   306  	".woff2":   "font/woff2",
   307  	".jar":     "application/java-archive",
   308  	".war":     "application/java-archive",
   309  	".ear":     "application/java-archive",
   310  	".json":    "application/json",
   311  	".hqx":     "application/mac-binhex40",
   312  	".doc":     "application/msword",
   313  	".pdf":     "application/pdf",
   314  	".ps":      "application/postscript",
   315  	".eps":     "application/postscript",
   316  	".ai":      "application/postscript",
   317  	".rtf":     "application/rtf",
   318  	".m3u8":    "application/vnd.apple.mpegurl",
   319  	".kml":     "application/vnd.google-earth.kml+xml",
   320  	".kmz":     "application/vnd.google-earth.kmz",
   321  	".xls":     "application/vnd.ms-excel",
   322  	".eot":     "application/vnd.ms-fontobject",
   323  	".ppt":     "application/vnd.ms-powerpoint",
   324  	".odg":     "application/vnd.oasis.opendocument.graphics",
   325  	".odp":     "application/vnd.oasis.opendocument.presentation",
   326  	".ods":     "application/vnd.oasis.opendocument.spreadsheet",
   327  	".odt":     "application/vnd.oasis.opendocument.text",
   328  	".wmlc":    "application/vnd.wap.wmlc",
   329  	".7z":      "application/x-7z-compressed",
   330  	".cco":     "application/x-cocoa",
   331  	".jardiff": "application/x-java-archive-diff",
   332  	".jnlp":    "application/x-java-jnlp-file",
   333  	".run":     "application/x-makeself",
   334  	".pl":      "application/x-perl",
   335  	".pm":      "application/x-perl",
   336  	".prc":     "application/x-pilot",
   337  	".pdb":     "application/x-pilot",
   338  	".rar":     "application/x-rar-compressed",
   339  	".rpm":     "application/x-redhat-package-manager",
   340  	".sea":     "application/x-sea",
   341  	".swf":     "application/x-shockwave-flash",
   342  	".sit":     "application/x-stuffit",
   343  	".tcl":     "application/x-tcl",
   344  	".tk":      "application/x-tcl",
   345  	".der":     "application/x-x509-ca-cert",
   346  	".pem":     "application/x-x509-ca-cert",
   347  	".crt":     "application/x-x509-ca-cert",
   348  	".xpi":     "application/x-xpinstall",
   349  	".xhtml":   "application/xhtml+xml",
   350  	".xspf":    "application/xspf+xml",
   351  	".zip":     "application/zip",
   352  	".bin":     "application/octet-stream",
   353  	".exe":     "application/octet-stream",
   354  	".dll":     "application/octet-stream",
   355  	".deb":     "application/octet-stream",
   356  	".dmg":     "application/octet-stream",
   357  	".iso":     "application/octet-stream",
   358  	".img":     "application/octet-stream",
   359  	".msi":     "application/octet-stream",
   360  	".msp":     "application/octet-stream",
   361  	".msm":     "application/octet-stream",
   362  	".mid":     "audio/midi",
   363  	".midi":    "audio/midi",
   364  	".kar":     "audio/midi",
   365  	".mp3":     "audio/mpeg",
   366  	".ogg":     "audio/ogg",
   367  	".m4a":     "audio/x-m4a",
   368  	".ra":      "audio/x-realaudio",
   369  	".3gpp":    "video/3gpp",
   370  	".3gp":     "video/3gpp",
   371  	".ts":      "video/mp2t",
   372  	".mp4":     "video/mp4",
   373  	".mpeg":    "video/mpeg",
   374  	".mpg":     "video/mpeg",
   375  	".mov":     "video/quicktime",
   376  	".webm":    "video/webm",
   377  	".flv":     "video/x-flv",
   378  	".m4v":     "video/x-m4v",
   379  	".mng":     "video/x-mng",
   380  	".asx":     "video/x-ms-asf",
   381  	".asf":     "video/x-ms-asf",
   382  	".wmv":     "video/x-ms-wmv",
   383  	".avi":     "video/x-msvideo",
   384  }