1package request
2
3import (
4	"net"
5	"net/http"
6)
7
8const (
9	// SchemeHTTP name for the HTTP scheme
10	SchemeHTTP = "http"
11	// SchemeHTTPS name for the HTTPS scheme
12	SchemeHTTPS = "https"
13)
14
15// IsHTTPS checks whether the request originated from HTTP or HTTPS.
16// It checks the value from r.URL.Scheme
17func IsHTTPS(r *http.Request) bool {
18	return r.URL.Scheme == SchemeHTTPS
19}
20
21// GetHostWithoutPort returns a host without the port. The host(:port) comes
22// from a Host: header if it is provided, otherwise it is a server name.
23func GetHostWithoutPort(r *http.Request) string {
24	host, _, err := net.SplitHostPort(r.Host)
25	if err != nil {
26		return r.Host
27	}
28
29	return host
30}
31
32// GetRemoteAddrWithoutPort strips the port from the r.RemoteAddr
33func GetRemoteAddrWithoutPort(r *http.Request) string {
34	remoteAddr, _, err := net.SplitHostPort(r.RemoteAddr)
35	if err != nil {
36		return r.RemoteAddr
37	}
38
39	return remoteAddr
40}
41