utils.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "encoding/xml"
  7. "log"
  8. "reflect"
  9. "runtime"
  10. "strings"
  11. )
  12. const (
  13. methodGET = iota
  14. methodPOST = iota
  15. methodPUT = iota
  16. methodAHEAD = iota
  17. methodOPTIONS = iota
  18. methodDELETE = iota
  19. methodCONNECT = iota
  20. methodTRACE = iota
  21. methodUnknown = iota
  22. )
  23. type H map[string]interface{}
  24. // Allows type H to be used with xml.Marshal
  25. func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  26. start.Name = xml.Name{
  27. Space: "",
  28. Local: "map",
  29. }
  30. if err := e.EncodeToken(start); err != nil {
  31. return err
  32. }
  33. for key, value := range h {
  34. elem := xml.StartElement{
  35. Name: xml.Name{Space: "", Local: key},
  36. Attr: []xml.Attr{},
  37. }
  38. if err := e.EncodeElement(value, elem); err != nil {
  39. return err
  40. }
  41. }
  42. if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
  43. return err
  44. }
  45. return nil
  46. }
  47. func filterFlags(content string) string {
  48. for i, char := range content {
  49. if char == ' ' || char == ';' {
  50. return content[:i]
  51. }
  52. }
  53. return content
  54. }
  55. func chooseData(custom, wildcard interface{}) interface{} {
  56. if custom == nil {
  57. if wildcard == nil {
  58. log.Panic("negotiation config is invalid")
  59. }
  60. return wildcard
  61. }
  62. return custom
  63. }
  64. func parseAccept(acceptHeader string) (parts []string) {
  65. parts = strings.Split(acceptHeader, ",")
  66. for i, part := range parts {
  67. index := strings.IndexByte(part, ';')
  68. if index >= 0 {
  69. part = part[0:index]
  70. }
  71. parts[i] = strings.TrimSpace(part)
  72. }
  73. return
  74. }
  75. func lastChar(str string) uint8 {
  76. size := len(str)
  77. if size == 0 {
  78. log.Panic("The length of the string can't be 0")
  79. }
  80. return str[size-1]
  81. }
  82. func nameOfFunction(f interface{}) string {
  83. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  84. }
  85. func codeForHTTPMethod(method string) int {
  86. switch method {
  87. case "GET":
  88. return methodGET
  89. case "POST":
  90. return methodPOST
  91. case "PUT":
  92. return methodPUT
  93. case "AHEAD":
  94. return methodAHEAD
  95. case "OPTIONS":
  96. return methodOPTIONS
  97. case "DELETE":
  98. return methodDELETE
  99. case "TRACE":
  100. return methodTRACE
  101. case "CONNECT":
  102. return methodCONNECT
  103. default:
  104. return methodUnknown
  105. }
  106. }