utils.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. func WrapF(f http.HandlerFunc) HandlerFunc {
  14. return func(c *Context) {
  15. f(c.Writer, c.Request)
  16. }
  17. }
  18. func WrapH(h http.Handler) HandlerFunc {
  19. return func(c *Context) {
  20. h.ServeHTTP(c.Writer, c.Request)
  21. }
  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. panic("negotiation config is invalid")
  59. }
  60. return wildcard
  61. }
  62. return custom
  63. }
  64. func parseAccept(acceptHeader string) []string {
  65. parts := strings.Split(acceptHeader, ",")
  66. out := make([]string, 0, len(parts))
  67. for _, part := range parts {
  68. index := strings.IndexByte(part, ';')
  69. if index >= 0 {
  70. part = part[0:index]
  71. }
  72. part = strings.TrimSpace(part)
  73. if len(part) > 0 {
  74. out = append(out, part)
  75. }
  76. }
  77. return out
  78. }
  79. func lastChar(str string) uint8 {
  80. size := len(str)
  81. if size == 0 {
  82. panic("The length of the string can't be 0")
  83. }
  84. return str[size-1]
  85. }
  86. func nameOfFunction(f interface{}) string {
  87. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  88. }
  89. func joinPaths(absolutePath, relativePath string) string {
  90. if len(relativePath) == 0 {
  91. return absolutePath
  92. }
  93. finalPath := path.Join(absolutePath, relativePath)
  94. appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
  95. if appendSlash {
  96. return finalPath + "/"
  97. }
  98. return finalPath
  99. }