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-04-07 13:22:38 +03:00
|
|
|
"path"
|
2014-08-19 05:40:52 +04:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2014-08-31 00:22:57 +04:00
|
|
|
"strings"
|
2014-07-16 22:14:03 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type H map[string]interface{}
|
|
|
|
|
|
|
|
// Allows type H to be used with xml.Marshal
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2014-08-31 00:22:57 +04:00
|
|
|
index := strings.IndexByte(part, ';')
|
|
|
|
if index >= 0 {
|
|
|
|
part = part[0:index]
|
|
|
|
}
|
2015-04-08 03:58:35 +03:00
|
|
|
part = strings.TrimSpace(part)
|
|
|
|
if len(part) > 0 {
|
|
|
|
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 {
|
|
|
|
size := len(str)
|
|
|
|
if size == 0 {
|
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
|
|
|
}
|
|
|
|
return str[size-1]
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if len(relativePath) == 0 {
|
|
|
|
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
|
|
|
}
|