headers.go 1012 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package http
  2. import (
  3. "net/http"
  4. "strings"
  5. "v2ray.com/core/common/net"
  6. )
  7. func ParseXForwardedFor(header http.Header) []net.Address {
  8. xff := header.Get("X-Forwarded-For")
  9. if len(xff) == 0 {
  10. return nil
  11. }
  12. list := strings.Split(xff, ",")
  13. addrs := make([]net.Address, 0, len(list))
  14. for _, proxy := range list {
  15. addrs = append(addrs, net.ParseAddress(proxy))
  16. }
  17. return addrs
  18. }
  19. func RemoveHopByHopHeaders(header http.Header) {
  20. // Strip hop-by-hop header based on RFC:
  21. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
  22. // https://www.mnot.net/blog/2011/07/11/what_proxies_must_do
  23. header.Del("Proxy-Connection")
  24. header.Del("Proxy-Authenticate")
  25. header.Del("Proxy-Authorization")
  26. header.Del("TE")
  27. header.Del("Trailers")
  28. header.Del("Transfer-Encoding")
  29. header.Del("Upgrade")
  30. connections := header.Get("Connection")
  31. header.Del("Connection")
  32. if len(connections) == 0 {
  33. return
  34. }
  35. for _, h := range strings.Split(connections, ",") {
  36. header.Del(strings.TrimSpace(h))
  37. }
  38. }