deprecated.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "log"
  7. "net"
  8. "net/http"
  9. "strings"
  10. "github.com/gin-gonic/gin/binding"
  11. )
  12. const (
  13. MIMEJSON = binding.MIMEJSON
  14. MIMEHTML = binding.MIMEHTML
  15. MIMEXML = binding.MIMEXML
  16. MIMEXML2 = binding.MIMEXML2
  17. MIMEPlain = binding.MIMEPlain
  18. MIMEPOSTForm = binding.MIMEPOSTForm
  19. MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm
  20. )
  21. // DEPRECATED, use Bind() instead.
  22. // Like ParseBody() but this method also writes a 400 error if the json is not valid.
  23. func (c *Context) EnsureBody(item interface{}) bool {
  24. return c.Bind(item)
  25. }
  26. // DEPRECATED use bindings directly
  27. // Parses the body content as a JSON input. It decodes the json payload into the struct specified as a pointer.
  28. func (c *Context) ParseBody(item interface{}) error {
  29. return binding.JSON.Bind(c.Request, item)
  30. }
  31. // DEPRECATED use gin.Static() instead
  32. // ServeFiles serves files from the given file system root.
  33. // The path must end with "/*filepath", files are then served from the local
  34. // path /defined/root/dir/*filepath.
  35. // For example if root is "/etc" and *filepath is "passwd", the local file
  36. // "/etc/passwd" would be served.
  37. // Internally a http.FileServer is used, therefore http.NotFound is used instead
  38. // of the Router's NotFound handler.
  39. // To use the operating system's file system implementation,
  40. // use http.Dir:
  41. // router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
  42. func (engine *Engine) ServeFiles(path string, root http.FileSystem) {
  43. engine.router.ServeFiles(path, root)
  44. }
  45. // DEPRECATED use gin.LoadHTMLGlob() or gin.LoadHTMLFiles() instead
  46. func (engine *Engine) LoadHTMLTemplates(pattern string) {
  47. engine.LoadHTMLGlob(pattern)
  48. }
  49. // DEPRECATED. Use NoRoute() instead
  50. func (engine *Engine) NotFound404(handlers ...HandlerFunc) {
  51. engine.NoRoute(handlers...)
  52. }
  53. // the ForwardedFor middleware unwraps the X-Forwarded-For headers, be careful to only use this
  54. // middleware if you've got servers in front of this server. The list with (known) proxies and
  55. // local ips are being filtered out of the forwarded for list, giving the last not local ip being
  56. // the real client ip.
  57. func ForwardedFor(proxies ...interface{}) HandlerFunc {
  58. if len(proxies) == 0 {
  59. // default to local ips
  60. var reservedLocalIps = []string{"10.0.0.0/8", "127.0.0.1/32", "172.16.0.0/12", "192.168.0.0/16"}
  61. proxies = make([]interface{}, len(reservedLocalIps))
  62. for i, v := range reservedLocalIps {
  63. proxies[i] = v
  64. }
  65. }
  66. return func(c *Context) {
  67. // the X-Forwarded-For header contains an array with left most the client ip, then
  68. // comma separated, all proxies the request passed. The last proxy appears
  69. // as the remote address of the request. Returning the client
  70. // ip to comply with default RemoteAddr response.
  71. // check if remoteaddr is local ip or in list of defined proxies
  72. remoteIp := net.ParseIP(strings.Split(c.Request.RemoteAddr, ":")[0])
  73. if !ipInMasks(remoteIp, proxies) {
  74. return
  75. }
  76. if forwardedFor := c.Request.Header.Get("X-Forwarded-For"); forwardedFor != "" {
  77. parts := strings.Split(forwardedFor, ",")
  78. for i := len(parts) - 1; i >= 0; i-- {
  79. part := parts[i]
  80. ip := net.ParseIP(strings.TrimSpace(part))
  81. if ipInMasks(ip, proxies) {
  82. continue
  83. }
  84. // returning remote addr conform the original remote addr format
  85. c.Request.RemoteAddr = ip.String() + ":0"
  86. // remove forwarded for address
  87. c.Request.Header.Set("X-Forwarded-For", "")
  88. return
  89. }
  90. }
  91. }
  92. }
  93. func ipInMasks(ip net.IP, masks []interface{}) bool {
  94. for _, proxy := range masks {
  95. var mask *net.IPNet
  96. var err error
  97. switch t := proxy.(type) {
  98. case string:
  99. if _, mask, err = net.ParseCIDR(t); err != nil {
  100. log.Panic(err)
  101. }
  102. case net.IP:
  103. mask = &net.IPNet{IP: t, Mask: net.CIDRMask(len(t)*8, len(t)*8)}
  104. case net.IPNet:
  105. mask = &t
  106. }
  107. if mask.Contains(ip) {
  108. return true
  109. }
  110. }
  111. return false
  112. }