utils.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "net/http"
  8. "path"
  9. "reflect"
  10. "runtime"
  11. "strings"
  12. )
  13. const BindKey = "_gin-gonic/gin/bindkey"
  14. func Bind(val interface{}) HandlerFunc {
  15. value := reflect.ValueOf(val)
  16. if value.Kind() == reflect.Ptr {
  17. panic(`Bind struct can not be a pointer. Example:
  18. Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
  19. `)
  20. }
  21. typ := value.Type()
  22. return func(c *Context) {
  23. obj := reflect.New(typ).Interface()
  24. if c.Bind(obj) == nil {
  25. c.Set(BindKey, obj)
  26. }
  27. }
  28. }
  29. func WrapF(f http.HandlerFunc) HandlerFunc {
  30. return func(c *Context) {
  31. f(c.Writer, c.Request)
  32. }
  33. }
  34. func WrapH(h http.Handler) HandlerFunc {
  35. return func(c *Context) {
  36. h.ServeHTTP(c.Writer, c.Request)
  37. }
  38. }
  39. type H map[string]interface{}
  40. // Allows type H to be used with xml.Marshal
  41. func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  42. start.Name = xml.Name{
  43. Space: "",
  44. Local: "map",
  45. }
  46. if err := e.EncodeToken(start); err != nil {
  47. return err
  48. }
  49. for key, value := range h {
  50. elem := xml.StartElement{
  51. Name: xml.Name{Space: "", Local: key},
  52. Attr: []xml.Attr{},
  53. }
  54. if err := e.EncodeElement(value, elem); err != nil {
  55. return err
  56. }
  57. }
  58. if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func filterFlags(content string) string {
  64. for i, char := range content {
  65. if char == ' ' || char == ';' {
  66. return content[:i]
  67. }
  68. }
  69. return content
  70. }
  71. func chooseData(custom, wildcard interface{}) interface{} {
  72. if custom == nil {
  73. if wildcard == nil {
  74. panic("negotiation config is invalid")
  75. }
  76. return wildcard
  77. }
  78. return custom
  79. }
  80. func parseAccept(acceptHeader string) []string {
  81. parts := strings.Split(acceptHeader, ",")
  82. out := make([]string, 0, len(parts))
  83. for _, part := range parts {
  84. index := strings.IndexByte(part, ';')
  85. if index >= 0 {
  86. part = part[0:index]
  87. }
  88. part = strings.TrimSpace(part)
  89. if len(part) > 0 {
  90. out = append(out, part)
  91. }
  92. }
  93. return out
  94. }
  95. func lastChar(str string) uint8 {
  96. size := len(str)
  97. if size == 0 {
  98. panic("The length of the string can't be 0")
  99. }
  100. return str[size-1]
  101. }
  102. func nameOfFunction(f interface{}) string {
  103. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  104. }
  105. func joinPaths(absolutePath, relativePath string) string {
  106. if len(relativePath) == 0 {
  107. return absolutePath
  108. }
  109. finalPath := path.Join(absolutePath, relativePath)
  110. appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
  111. if appendSlash {
  112. return finalPath + "/"
  113. }
  114. return finalPath
  115. }