utils.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. "os"
  9. "path"
  10. "reflect"
  11. "runtime"
  12. "strings"
  13. "unicode"
  14. )
  15. // BindKey indicates a default bind key.
  16. const BindKey = "_gin-gonic/gin/bindkey"
  17. // Bind is a helper function for given interface object and returns a Gin middleware.
  18. func Bind(val any) HandlerFunc {
  19. value := reflect.ValueOf(val)
  20. if value.Kind() == reflect.Ptr {
  21. panic(`Bind struct can not be a pointer. Example:
  22. Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
  23. `)
  24. }
  25. typ := value.Type()
  26. return func(c *Context) {
  27. obj := reflect.New(typ).Interface()
  28. if c.Bind(obj) == nil {
  29. c.Set(BindKey, obj)
  30. }
  31. }
  32. }
  33. // WrapF is a helper function for wrapping http.HandlerFunc and returns a Gin middleware.
  34. func WrapF(f http.HandlerFunc) HandlerFunc {
  35. return func(c *Context) {
  36. f(c.Writer, c.Request)
  37. }
  38. }
  39. // WrapH is a helper function for wrapping http.Handler and returns a Gin middleware.
  40. func WrapH(h http.Handler) HandlerFunc {
  41. return func(c *Context) {
  42. h.ServeHTTP(c.Writer, c.Request)
  43. }
  44. }
  45. // H is a shortcut for map[string]interface{}
  46. type H map[string]any
  47. // MarshalXML allows type H to be used with xml.Marshal.
  48. func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  49. start.Name = xml.Name{
  50. Space: "",
  51. Local: "map",
  52. }
  53. if err := e.EncodeToken(start); err != nil {
  54. return err
  55. }
  56. for key, value := range h {
  57. elem := xml.StartElement{
  58. Name: xml.Name{Space: "", Local: key},
  59. Attr: []xml.Attr{},
  60. }
  61. if err := e.EncodeElement(value, elem); err != nil {
  62. return err
  63. }
  64. }
  65. return e.EncodeToken(xml.EndElement{Name: start.Name})
  66. }
  67. func assert1(guard bool, text string) {
  68. if !guard {
  69. panic(text)
  70. }
  71. }
  72. func filterFlags(content string) string {
  73. for i, char := range content {
  74. if char == ' ' || char == ';' {
  75. return content[:i]
  76. }
  77. }
  78. return content
  79. }
  80. func chooseData(custom, wildcard any) any {
  81. if custom != nil {
  82. return custom
  83. }
  84. if wildcard != nil {
  85. return wildcard
  86. }
  87. panic("negotiation config is invalid")
  88. }
  89. func parseAccept(acceptHeader string) []string {
  90. parts := strings.Split(acceptHeader, ",")
  91. out := make([]string, 0, len(parts))
  92. for _, part := range parts {
  93. if i := strings.IndexByte(part, ';'); i > 0 {
  94. part = part[:i]
  95. }
  96. if part = strings.TrimSpace(part); part != "" {
  97. out = append(out, part)
  98. }
  99. }
  100. return out
  101. }
  102. func lastChar(str string) uint8 {
  103. if str == "" {
  104. panic("The length of the string can't be 0")
  105. }
  106. return str[len(str)-1]
  107. }
  108. func nameOfFunction(f any) string {
  109. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  110. }
  111. func joinPaths(absolutePath, relativePath string) string {
  112. if relativePath == "" {
  113. return absolutePath
  114. }
  115. finalPath := path.Join(absolutePath, relativePath)
  116. if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
  117. return finalPath + "/"
  118. }
  119. return finalPath
  120. }
  121. func resolveAddress(addr []string) string {
  122. switch len(addr) {
  123. case 0:
  124. if port := os.Getenv("PORT"); port != "" {
  125. debugPrint("Environment variable PORT=\"%s\"", port)
  126. return ":" + port
  127. }
  128. debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
  129. return ":8080"
  130. case 1:
  131. return addr[0]
  132. default:
  133. panic("too many parameters")
  134. }
  135. }
  136. // https://stackoverflow.com/questions/53069040/checking-a-string-contains-only-ascii-characters
  137. func isASCII(s string) bool {
  138. for i := 0; i < len(s); i++ {
  139. if s[i] > unicode.MaxASCII {
  140. return false
  141. }
  142. }
  143. return true
  144. }