request.go 562 B

1234567891011121314151617181920212223
  1. package httptoo
  2. import (
  3. "net"
  4. "net/http"
  5. "github.com/anacrolix/missinggo"
  6. )
  7. // Request is intended for localhost, either with a localhost name, or
  8. // loopback IP.
  9. func RequestIsForLocalhost(r *http.Request) bool {
  10. hostHost := missinggo.SplitHostMaybePort(r.Host).Host
  11. if ip := net.ParseIP(hostHost); ip != nil {
  12. return ip.IsLoopback()
  13. }
  14. return hostHost == "localhost"
  15. }
  16. // Request originated from a loopback IP.
  17. func RequestIsFromLocalhost(r *http.Request) bool {
  18. return net.ParseIP(missinggo.SplitHostMaybePort(r.RemoteAddr).Host).IsLoopback()
  19. }