2014-08-29 17:49:50 +00: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 18:14:03 +00:00
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2015-05-19 20:18:58 +00:00
|
|
|
"net/http"
|
2015-08-16 14:19:51 +00:00
|
|
|
"os"
|
2015-04-07 10:22:38 +00:00
|
|
|
"path"
|
2014-08-19 01:40:52 +00:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2014-08-30 20:22:57 +00:00
|
|
|
"strings"
|
2014-07-16 18:14:03 +00:00
|
|
|
)
|
|
|
|
|
2015-06-12 21:42:54 +00:00
|
|
|
const BindKey = "_gin-gonic/gin/bindkey"
|
|
|
|
|
|
|
|
func Bind(val interface{}) HandlerFunc {
|
2015-06-12 22:01:02 +00: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-12 21:42:54 +00:00
|
|
|
return func(c *Context) {
|
|
|
|
obj := reflect.New(typ).Interface()
|
|
|
|
if c.Bind(obj) == nil {
|
|
|
|
c.Set(BindKey, obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 22:39:52 +00:00
|
|
|
func WrapF(f http.HandlerFunc) HandlerFunc {
|
2015-05-19 20:18:58 +00:00
|
|
|
return func(c *Context) {
|
|
|
|
f(c.Writer, c.Request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 22:39:52 +00:00
|
|
|
func WrapH(h http.Handler) HandlerFunc {
|
|
|
|
return func(c *Context) {
|
|
|
|
h.ServeHTTP(c.Writer, c.Request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 18:14:03 +00:00
|
|
|
type H map[string]interface{}
|
|
|
|
|
2016-04-14 23:16:46 +00:00
|
|
|
// MarshalXML allows type H to be used with xml.Marshal
|
2014-07-16 18:14:03 +00: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:35:09 +00:00
|
|
|
func assert1(guard bool, text string) {
|
|
|
|
if !guard {
|
|
|
|
panic(text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 18:14:03 +00:00
|
|
|
func filterFlags(content string) string {
|
2014-10-08 23:40:42 +00:00
|
|
|
for i, char := range content {
|
|
|
|
if char == ' ' || char == ';' {
|
2014-07-16 18:14:03 +00:00
|
|
|
return content[:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content
|
|
|
|
}
|
2014-08-19 01:40:52 +00:00
|
|
|
|
2014-08-31 16:28:18 +00:00
|
|
|
func chooseData(custom, wildcard interface{}) interface{} {
|
|
|
|
if custom == nil {
|
|
|
|
if wildcard == nil {
|
2015-04-08 00:58:35 +00:00
|
|
|
panic("negotiation config is invalid")
|
2014-08-31 16:28:18 +00:00
|
|
|
}
|
|
|
|
return wildcard
|
2014-08-30 20:22:57 +00:00
|
|
|
}
|
2014-08-31 16:28:18 +00:00
|
|
|
return custom
|
2014-08-30 20:22:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func parseAccept(acceptHeader string) []string {
|
|
|
|
parts := strings.Split(acceptHeader, ",")
|
|
|
|
out := make([]string, 0, len(parts))
|
|
|
|
for _, part := range parts {
|
2017-07-07 00:43:47 +00:00
|
|
|
if index := strings.IndexByte(part, ';'); index >= 0 {
|
2014-08-30 20:22:57 +00:00
|
|
|
part = part[0:index]
|
|
|
|
}
|
2017-07-07 00:43:47 +00:00
|
|
|
if part = strings.TrimSpace(part); len(part) > 0 {
|
2015-04-08 00:58:35 +00:00
|
|
|
out = append(out, part)
|
|
|
|
}
|
2014-08-30 20:22:57 +00:00
|
|
|
}
|
2015-04-08 00:58:35 +00:00
|
|
|
return out
|
2014-08-30 20:22:57 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:37:26 +00:00
|
|
|
func lastChar(str string) uint8 {
|
|
|
|
size := len(str)
|
|
|
|
if size == 0 {
|
2015-04-08 00:58:35 +00:00
|
|
|
panic("The length of the string can't be 0")
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
return str[size-1]
|
|
|
|
}
|
|
|
|
|
2014-10-08 23:40:42 +00:00
|
|
|
func nameOfFunction(f interface{}) string {
|
2014-08-19 01:40:52 +00:00
|
|
|
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
|
|
|
}
|
2015-03-31 19:39:06 +00:00
|
|
|
|
2015-04-07 10:22:38 +00:00
|
|
|
func joinPaths(absolutePath, relativePath string) string {
|
|
|
|
if len(relativePath) == 0 {
|
|
|
|
return absolutePath
|
|
|
|
}
|
2015-04-08 13:32:50 +00:00
|
|
|
|
|
|
|
finalPath := path.Join(absolutePath, relativePath)
|
|
|
|
appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
|
2015-04-07 10:22:38 +00:00
|
|
|
if appendSlash {
|
2015-04-08 13:32:50 +00:00
|
|
|
return finalPath + "/"
|
2015-03-31 19:39:06 +00:00
|
|
|
}
|
2015-04-08 13:32:50 +00:00
|
|
|
return finalPath
|
2015-03-31 19:39:06 +00:00
|
|
|
}
|
2015-08-16 14:19:51 +00:00
|
|
|
|
|
|
|
func resolveAddress(addr []string) string {
|
|
|
|
switch len(addr) {
|
|
|
|
case 0:
|
|
|
|
if port := os.Getenv("PORT"); len(port) > 0 {
|
|
|
|
debugPrint("Environment variable PORT=\"%s\"", port)
|
|
|
|
return ":" + port
|
|
|
|
}
|
2016-04-14 23:16:46 +00:00
|
|
|
debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
|
|
|
|
return ":8080"
|
2015-08-16 14:19:51 +00:00
|
|
|
case 1:
|
|
|
|
return addr[0]
|
|
|
|
default:
|
|
|
|
panic("too much parameters")
|
|
|
|
}
|
|
|
|
}
|