This repository has been archived by the owner on Oct 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathurlutil.go
181 lines (154 loc) · 3.56 KB
/
urlutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package urlutil
import (
"net/url"
"strings"
"github.com/projectdiscovery/stringsutil"
)
const (
HTTP = "http"
HTTPS = "https"
schemeSeparator = "://"
DefaultHTTPPort = "80"
DefaultHTTPSPort = "443"
)
type URL struct {
Scheme string
Username string
Password string
Host string
Port string
RequestURI string
Fragment string
}
func (u URL) String() string {
var fullURL strings.Builder
fullURL.WriteString(u.Scheme)
fullURL.WriteString(schemeSeparator)
if u.Username != "" {
fullURL.WriteString(u.Username)
if u.Password != "" {
fullURL.WriteString(":" + u.Password)
}
fullURL.WriteString("@")
}
fullURL.WriteString(u.Host)
if u.Port != "" {
fullURL.WriteString(":" + u.Port)
}
fullURL.WriteString(u.RequestURI)
if u.Fragment != "" {
if u.RequestURI == "" {
fullURL.WriteString("/")
}
fullURL.WriteString("#" + u.Fragment)
}
return fullURL.String()
}
func Parse(u string) (*URL, error) {
return ParseWithScheme(u)
}
func ParseWithScheme(u string) (*URL, error) {
// prepend default scheme if absent to increase parsing capabilities
u = PreprendDefaultScheme(u)
var origReqURI string
U, err := url.Parse(u)
if err != nil {
// try to reparse without the request path
// attempt to find the forward slash at the beginning of the path
schemeDoubleForwardSlash := strings.Index(u, "//") + 2
forwardSlashPosition := schemeDoubleForwardSlash + strings.Index(u[schemeDoubleForwardSlash:], "/")
if forwardSlashPosition > 0 {
origReqURI = u[forwardSlashPosition:]
u = u[:forwardSlashPosition]
U, err = url.Parse(u)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
// attempts to infer port
port := U.Port()
if port == "" {
port = defaultPortForProtocol(U.Scheme)
}
// scheme
scheme := U.Scheme
// get host
host := U.Host
if port != "" {
host = TrimPort(host, port)
}
// get credentials if any
var username, password string
if U.User != nil {
username = U.User.Username()
password, _ = U.User.Password()
}
// get full raw path
var requri string
// for our specific case we set this to empty if it equals to "/"
if ruri := U.RequestURI(); ruri != "/" {
requri = ruri
}
if origReqURI != "" && origReqURI != "/" {
requri = origReqURI
}
// fragment
fragment := U.Fragment
return &URL{
Scheme: scheme,
Host: host,
Username: username,
Password: password,
Port: port,
RequestURI: requri,
Fragment: fragment,
}, nil
}
func PreprendDefaultScheme(u string) string {
if stringsutil.HasPrefixI(u, HTTP+schemeSeparator) || stringsutil.HasPrefixI(u, HTTPS+schemeSeparator) {
return u
}
return PreprendScheme(u, HTTPS)
}
func PreprendScheme(u, scheme string) string {
if stringsutil.HasPrefixI(u, scheme+schemeSeparator) {
return u
}
return scheme + schemeSeparator + u
}
func ChangePort(u, port string) (string, error) {
U, err := Parse(u)
if err != nil {
return u, err
}
U.Port = port
return U.String(), nil
}
func AppendRequestURI(u, requestURI string) (string, error) {
U, err := Parse(u)
if err != nil {
return u, err
}
U.RequestURI += requestURI
return U.String(), nil
}
func defaultPortForProtocol(protocol string) string {
switch protocol {
case HTTP:
return DefaultHTTPPort
case HTTPS:
return DefaultHTTPSPort
}
return DefaultHTTPPort
}
func TrimPort(host, port string) string {
return strings.TrimSuffix(host, ":"+port)
}
func TrimScheme(host string) string {
r := strings.TrimPrefix(host, HTTP+schemeSeparator)
r = strings.TrimPrefix(r, HTTPS+schemeSeparator)
return r
}