...
1
2
3
4
5
6
7
8
9
10
11 package v4
12
13 import (
14 "net/http"
15 "strings"
16 )
17
18
19 func getHost(r *http.Request) string {
20 if r.Host != "" {
21 return r.Host
22 }
23
24 if r.URL == nil {
25 return ""
26 }
27
28 return r.URL.Host
29 }
30
31
32
33
34
35
36
37
38 func stripPort(hostport string) string {
39 colon := strings.IndexByte(hostport, ':')
40 if colon == -1 {
41 return hostport
42 }
43 if i := strings.IndexByte(hostport, ']'); i != -1 {
44 return strings.TrimPrefix(hostport[:i], "[")
45 }
46 return hostport[:colon]
47 }
48
49
50
51
52
53 func portOnly(hostport string) string {
54 colon := strings.IndexByte(hostport, ':')
55 if colon == -1 {
56 return ""
57 }
58 if i := strings.Index(hostport, "]:"); i != -1 {
59 return hostport[i+len("]:"):]
60 }
61 if strings.Contains(hostport, "]") {
62 return ""
63 }
64 return hostport[colon+len(":"):]
65 }
66
67
68
69 func isDefaultPort(scheme, port string) bool {
70 if port == "" {
71 return true
72 }
73
74 lowerCaseScheme := strings.ToLower(scheme)
75 if (lowerCaseScheme == "http" && port == "80") || (lowerCaseScheme == "https" && port == "443") {
76 return true
77 }
78
79 return false
80 }
81
View as plain text