2014-08-29 21:49:50 +04:00
|
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2014-07-16 22:14:03 +04:00
|
|
|
|
package gin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/xml"
|
2015-05-19 23:18:58 +03:00
|
|
|
|
"net/http"
|
2015-08-16 17:19:51 +03:00
|
|
|
|
"os"
|
2015-04-07 13:22:38 +03:00
|
|
|
|
"path"
|
2014-08-19 05:40:52 +04:00
|
|
|
|
"reflect"
|
|
|
|
|
"runtime"
|
2020-04-18 11:46:57 +03:00
|
|
|
|
"strconv"
|
2014-08-31 00:22:57 +04:00
|
|
|
|
"strings"
|
2014-07-16 22:14:03 +04:00
|
|
|
|
)
|
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
|
// BindKey indicates a default bind key.
|
2015-06-13 00:42:54 +03:00
|
|
|
|
const BindKey = "_gin-gonic/gin/bindkey"
|
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
|
// Bind is a helper function for given interface object and returns a Gin middleware.
|
2015-06-13 00:42:54 +03:00
|
|
|
|
func Bind(val interface{}) HandlerFunc {
|
2015-06-13 01:01:02 +03:00
|
|
|
|
value := reflect.ValueOf(val)
|
|
|
|
|
if value.Kind() == reflect.Ptr {
|
|
|
|
|
panic(`Bind struct can not be a pointer. Example:
|
|
|
|
|
Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
|
|
|
|
|
`)
|
|
|
|
|
}
|
|
|
|
|
typ := value.Type()
|
|
|
|
|
|
2015-06-13 00:42:54 +03:00
|
|
|
|
return func(c *Context) {
|
|
|
|
|
obj := reflect.New(typ).Interface()
|
|
|
|
|
if c.Bind(obj) == nil {
|
|
|
|
|
c.Set(BindKey, obj)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
|
// WrapF is a helper function for wrapping http.HandlerFunc and returns a Gin middleware.
|
2015-05-20 01:39:52 +03:00
|
|
|
|
func WrapF(f http.HandlerFunc) HandlerFunc {
|
2015-05-19 23:18:58 +03:00
|
|
|
|
return func(c *Context) {
|
|
|
|
|
f(c.Writer, c.Request)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
|
// WrapH is a helper function for wrapping http.Handler and returns a Gin middleware.
|
2015-05-20 01:39:52 +03:00
|
|
|
|
func WrapH(h http.Handler) HandlerFunc {
|
|
|
|
|
return func(c *Context) {
|
|
|
|
|
h.ServeHTTP(c.Writer, c.Request)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 05:54:40 +03:00
|
|
|
|
// H is a shortcut for map[string]interface{}
|
2014-07-16 22:14:03 +04:00
|
|
|
|
type H map[string]interface{}
|
|
|
|
|
|
2017-08-16 06:55:50 +03:00
|
|
|
|
// MarshalXML allows type H to be used with xml.Marshal.
|
2014-07-16 22:14:03 +04:00
|
|
|
|
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
|
|
|
|
start.Name = xml.Name{
|
|
|
|
|
Space: "",
|
|
|
|
|
Local: "map",
|
|
|
|
|
}
|
|
|
|
|
if err := e.EncodeToken(start); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for key, value := range h {
|
|
|
|
|
elem := xml.StartElement{
|
|
|
|
|
Name: xml.Name{Space: "", Local: key},
|
|
|
|
|
Attr: []xml.Attr{},
|
|
|
|
|
}
|
|
|
|
|
if err := e.EncodeElement(value, elem); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-25 08:58:02 +03:00
|
|
|
|
|
|
|
|
|
return e.EncodeToken(xml.EndElement{Name: start.Name})
|
2014-07-16 22:14:03 +04:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 02:35:09 +03:00
|
|
|
|
func assert1(guard bool, text string) {
|
|
|
|
|
if !guard {
|
|
|
|
|
panic(text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 22:14:03 +04:00
|
|
|
|
func filterFlags(content string) string {
|
2014-10-09 03:40:42 +04:00
|
|
|
|
for i, char := range content {
|
|
|
|
|
if char == ' ' || char == ';' {
|
2014-07-16 22:14:03 +04:00
|
|
|
|
return content[:i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return content
|
|
|
|
|
}
|
2014-08-19 05:40:52 +04:00
|
|
|
|
|
2014-08-31 20:28:18 +04:00
|
|
|
|
func chooseData(custom, wildcard interface{}) interface{} {
|
|
|
|
|
if custom == nil {
|
|
|
|
|
if wildcard == nil {
|
2015-04-08 03:58:35 +03:00
|
|
|
|
panic("negotiation config is invalid")
|
2014-08-31 20:28:18 +04:00
|
|
|
|
}
|
|
|
|
|
return wildcard
|
2014-08-31 00:22:57 +04:00
|
|
|
|
}
|
2014-08-31 20:28:18 +04:00
|
|
|
|
return custom
|
2014-08-31 00:22:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
|
func parseAccept(acceptHeader string) []string {
|
|
|
|
|
parts := strings.Split(acceptHeader, ",")
|
|
|
|
|
out := make([]string, 0, len(parts))
|
|
|
|
|
for _, part := range parts {
|
utils: use strings.Split instead of strings.IndexByte (#1400)
And I test them benchmark:
code:
```go
# stringsbench.go
package stringsbench
import "strings"
func index(part string) string {
if index := strings.IndexByte(part, ';'); index >= 0 {
if part := strings.TrimSpace(strings.Split(part, ";")[0]); part != "" {
return part[0:index]
}
}
return ""
}
func split(part string) string {
return strings.Split(part, ";")[0]
}
```
```go
# stringsbench_test.go
package stringsbench
import (
"testing"
)
func BenchmarkIndex(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
index("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
}
})
}
func BenchmarkSplit(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
split("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
}
})
}
```
And the result:
```shell
➜ strings go test --bench=.
goos: darwin
goarch: amd64
BenchmarkIndex-8 30000000 46.1 ns/op
BenchmarkSplit-8 50000000 35.9 ns/op
PASS
ok _/Users/tianou/strings 3.271s
➜ strings go test --bench=.
goos: darwin
goarch: amd64
BenchmarkIndex-8 30000000 44.2 ns/op
BenchmarkSplit-8 50000000 34.7 ns/op
PASS
ok _/Users/tianou/strings 3.156s
➜ strings go test --bench=.
goos: darwin
goarch: amd64
BenchmarkIndex-8 30000000 45.6 ns/op
BenchmarkSplit-8 50000000 35.3 ns/op
PASS
ok _/Users/tianou/strings 3.230s
```
2018-06-21 04:31:43 +03:00
|
|
|
|
if part = strings.TrimSpace(strings.Split(part, ";")[0]); part != "" {
|
2015-04-08 03:58:35 +03:00
|
|
|
|
out = append(out, part)
|
|
|
|
|
}
|
2014-08-31 00:22:57 +04:00
|
|
|
|
}
|
2015-04-08 03:58:35 +03:00
|
|
|
|
return out
|
2014-08-31 00:22:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 23:37:26 +04:00
|
|
|
|
func lastChar(str string) uint8 {
|
2017-09-28 19:22:35 +03:00
|
|
|
|
if str == "" {
|
2015-04-08 03:58:35 +03:00
|
|
|
|
panic("The length of the string can't be 0")
|
2014-10-08 23:37:26 +04:00
|
|
|
|
}
|
2017-09-28 19:22:35 +03:00
|
|
|
|
return str[len(str)-1]
|
2014-10-08 23:37:26 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 03:40:42 +04:00
|
|
|
|
func nameOfFunction(f interface{}) string {
|
2014-08-19 05:40:52 +04:00
|
|
|
|
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
|
|
|
|
}
|
2015-03-31 22:39:06 +03:00
|
|
|
|
|
2015-04-07 13:22:38 +03:00
|
|
|
|
func joinPaths(absolutePath, relativePath string) string {
|
2017-09-28 19:22:35 +03:00
|
|
|
|
if relativePath == "" {
|
2015-04-07 13:22:38 +03:00
|
|
|
|
return absolutePath
|
|
|
|
|
}
|
2015-04-08 16:32:50 +03:00
|
|
|
|
|
|
|
|
|
finalPath := path.Join(absolutePath, relativePath)
|
|
|
|
|
appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
|
2015-04-07 13:22:38 +03:00
|
|
|
|
if appendSlash {
|
2015-04-08 16:32:50 +03:00
|
|
|
|
return finalPath + "/"
|
2015-03-31 22:39:06 +03:00
|
|
|
|
}
|
2015-04-08 16:32:50 +03:00
|
|
|
|
return finalPath
|
2015-03-31 22:39:06 +03:00
|
|
|
|
}
|
2015-08-16 17:19:51 +03:00
|
|
|
|
|
|
|
|
|
func resolveAddress(addr []string) string {
|
|
|
|
|
switch len(addr) {
|
|
|
|
|
case 0:
|
2017-09-28 19:22:35 +03:00
|
|
|
|
if port := os.Getenv("PORT"); port != "" {
|
2020-04-18 11:46:57 +03:00
|
|
|
|
if isValidPORTEnvVar(port) {
|
|
|
|
|
debugPrint("Environment variable PORT=\"%s\"", port)
|
|
|
|
|
return ":" + port
|
|
|
|
|
}
|
2015-08-16 17:19:51 +03:00
|
|
|
|
}
|
2020-04-18 11:46:57 +03:00
|
|
|
|
debugPrint("Environment variable PORT is undefined or invalid. Using port :8080 by default")
|
2020-03-23 12:48:25 +03:00
|
|
|
|
return ":8080"
|
2015-08-16 17:19:51 +03:00
|
|
|
|
case 1:
|
|
|
|
|
return addr[0]
|
|
|
|
|
default:
|
2019-07-04 02:57:52 +03:00
|
|
|
|
panic("too many parameters")
|
2015-08-16 17:19:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-18 11:46:57 +03:00
|
|
|
|
|
|
|
|
|
// Determine the PORT environment variable whether is valid。
|
|
|
|
|
// If the PORT can be parsed to uint(0-65535),return true。
|
|
|
|
|
func isValidPORTEnvVar(portString string) bool {
|
|
|
|
|
_, err := strconv.ParseUint(portString, 10, 16)
|
|
|
|
|
return err == nil
|
|
|
|
|
}
|