| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- // 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.
- package gin
- import (
- "encoding/xml"
- "log"
- "reflect"
- "runtime"
- "strings"
- )
- const (
- methodGET = iota
- methodPOST = iota
- methodPUT = iota
- methodAHEAD = iota
- methodOPTIONS = iota
- methodDELETE = iota
- methodCONNECT = iota
- methodTRACE = iota
- methodUnknown = iota
- )
- 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 {
- for i, char := range content {
- if char == ' ' || char == ';' {
- return content[:i]
- }
- }
- return content
- }
- func chooseData(custom, wildcard interface{}) interface{} {
- if custom == nil {
- if wildcard == nil {
- log.Panic("negotiation config is invalid")
- }
- return wildcard
- }
- return custom
- }
- func parseAccept(acceptHeader string) (parts []string) {
- parts = strings.Split(acceptHeader, ",")
- for i, part := range parts {
- index := strings.IndexByte(part, ';')
- if index >= 0 {
- part = part[0:index]
- }
- parts[i] = strings.TrimSpace(part)
- }
- return
- }
- func lastChar(str string) uint8 {
- size := len(str)
- if size == 0 {
- log.Panic("The length of the string can't be 0")
- }
- return str[size-1]
- }
- func nameOfFunction(f interface{}) string {
- return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
- }
- func codeForHTTPMethod(method string) int {
- switch method {
- case "GET":
- return methodGET
- case "POST":
- return methodPOST
- case "PUT":
- return methodPUT
- case "AHEAD":
- return methodAHEAD
- case "OPTIONS":
- return methodOPTIONS
- case "DELETE":
- return methodDELETE
- case "TRACE":
- return methodTRACE
- case "CONNECT":
- return methodCONNECT
- default:
- return methodUnknown
- }
- }
|