File: request.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 872 kB
  • sloc: makefile: 4
file content (23 lines) | stat: -rw-r--r-- 562 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package httptoo

import (
	"net"
	"net/http"

	"github.com/anacrolix/missinggo"
)

// Request is intended for localhost, either with a localhost name, or
// loopback IP.
func RequestIsForLocalhost(r *http.Request) bool {
	hostHost := missinggo.SplitHostMaybePort(r.Host).Host
	if ip := net.ParseIP(hostHost); ip != nil {
		return ip.IsLoopback()
	}
	return hostHost == "localhost"
}

// Request originated from a loopback IP.
func RequestIsFromLocalhost(r *http.Request) bool {
	return net.ParseIP(missinggo.SplitHostMaybePort(r.RemoteAddr).Host).IsLoopback()
}