-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
476 lines (415 loc) · 10.5 KB
/
helpers.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package rawurlparser
import (
"bytes"
"encoding/hex"
"strconv"
"strings"
"unicode/utf8"
)
// Helper methods //
// URLComponent represents different parts of a URL that can be updated
type URLComponent int
const (
Scheme URLComponent = iota
Username
Password
Host
Port
Path
Query
Fragment
RawURI
)
// URLBuilder represents a mutable URL structure for manipulation
type RawURLBuilder struct {
*RawURL // Embed the original RawURL
workingURI string // Working copy of RequestURI
}
// NewURLBuilder creates a new builder from RawURL
func NewRawURLBuilder(u *RawURL) *RawURLBuilder {
return &RawURLBuilder{
RawURL: u,
workingURI: u.RawRequestURI,
}
}
// FullString reconstructs the URL from its components
// deprecated
func (u *RawURL) FullString() string {
var buf strings.Builder
if u.Scheme != "" {
buf.WriteString(u.Scheme)
buf.WriteString("://")
}
if u.User != nil {
buf.WriteString(u.User.username)
if u.User.passwordSet {
buf.WriteRune(':')
buf.WriteString(u.User.password)
}
buf.WriteRune('@')
}
buf.WriteString(u.Host)
buf.WriteString(u.Path)
if u.Query != "" {
buf.WriteRune('?')
buf.WriteString(u.Query)
}
if u.Fragment != "" {
buf.WriteRune('#')
buf.WriteString(u.Fragment)
}
return buf.String()
}
// GetRawScheme reconstructs the scheme from its components
func GetRawScheme(u *RawURL) string {
if u.Scheme == "" {
return ""
}
var buf strings.Builder
buf.WriteString(u.Scheme)
buf.WriteString("://")
return buf.String()
}
// GetRawUserInfo reconstructs the userinfo from its components
func GetRawUserInfo(u *RawURL) string {
if u.User == nil {
return ""
}
var buf strings.Builder
buf.WriteString(u.User.username)
if u.User.passwordSet {
buf.WriteRune(':')
buf.WriteString(u.User.password)
}
buf.WriteRune('@')
return buf.String()
}
// GetRawAuthority reconstructs the authority from its components
func GetRawAuthority(u *RawURL) string {
var buf strings.Builder
if u.User != nil {
buf.WriteString(GetRawUserInfo(u))
}
buf.WriteString(u.Host)
return buf.String()
}
// GetRawHost reconstructs the host of the URL (with port)
func GetRawHost(u *RawURL) string {
var buf strings.Builder
buf.WriteString(u.Host)
return buf.String()
}
// GetHostname returns the hostname without port.
// For IPv6 addresses, the square brackets are preserved.
func (u *RawURL) GetHostname() string {
host := u.Host
// Handle IPv6 addresses
if strings.HasPrefix(host, "[") {
if closeBracket := strings.LastIndex(host, "]"); closeBracket != -1 {
// Return the IPv6 address with brackets
if len(host) > closeBracket+1 && host[closeBracket+1] == ':' {
return host[:closeBracket+1]
}
return host
}
return host // Malformed IPv6, return as-is
}
// Handle IPv4 and regular hostnames
if i := strings.LastIndex(host, ":"); i != -1 {
return host[:i]
}
return host
}
// GetPort returns the port part of the host.
// Returns empty string if no port is present.
func (u *RawURL) GetPort() string {
host := u.Host
// Handle IPv6 addresses
if strings.HasPrefix(host, "[") {
if closeBracket := strings.LastIndex(host, "]"); closeBracket != -1 {
if len(host) > closeBracket+1 && host[closeBracket+1] == ':' {
return host[closeBracket+2:] // Return everything after ]:
}
return ""
}
return ""
}
// Handle IPv4 and regular hostnames
if i := strings.LastIndex(host, ":"); i != -1 {
port := host[i+1:]
// Validate port is numeric
for _, b := range port {
if b < '0' || b > '9' {
return ""
}
}
return port
}
return ""
}
// GetRawPath reconstructs the path from its components
func GetRawPath(u *RawURL) string {
var buf strings.Builder
if u.Path == "" {
buf.WriteString("/")
return buf.String()
}
// Check first byte directly for '/'
if len(u.Path) > 0 && u.Path[0] != '/' {
buf.WriteString("/")
}
buf.WriteString(u.Path)
return buf.String()
}
// GetRawPathUnsafe reconstructs the path from its components
// Similar to GetRawPath but will omit first / in path
// Might be needed when fuzzing full paths
func GetRawPathUnsafe(u *RawURL) string {
if u.Path == "" {
return ""
}
var buf strings.Builder
// Skip first char if it's a '/'
if len(u.Path) > 0 {
if u.Path[0] == '/' {
buf.WriteString(u.Path[1:])
} else {
buf.WriteString(u.Path)
}
}
return buf.String()
}
// GetRawQuery reconstructs the query from its components
func GetRawQuery(u *RawURL) string {
if u.Query == "" {
return ""
}
var buf strings.Builder
buf.WriteRune('?')
buf.WriteString(u.Query)
return buf.String()
}
// GetRawFragment reconstructs the fragment from its components
func GetRawFragment(u *RawURL) string {
if u.Fragment == "" {
return ""
}
var buf strings.Builder
buf.WriteRune('#')
buf.WriteString(u.Fragment)
return buf.String()
}
// QueryValues returns a map of query parameters
func (u *RawURL) GetQueryValues() map[string][]string {
values := make(map[string][]string)
for _, pair := range strings.Split(u.Query, "&") {
if pair == "" {
continue
}
kv := strings.SplitN(pair, "=", 2)
key := kv[0]
value := ""
if len(kv) == 2 {
value = kv[1]
}
values[key] = append(values[key], value)
}
return values
}
/*
GetRawFullURL reconstructs the full URL from its components
---> scheme://host/path?query#fragment
userinfo host port path query fragment
|------| |-------------| |--||---------------| |-------------------------| |-----------|
https://[email protected]:8092/forum/questions/?tag=networking&order=newest#fragmentation
|----| |---------------------------|
scheme authority
*/
func (u *RawURL) GetRawFullURL() string {
var buf strings.Builder
// Scheme
if u.Scheme != "" {
buf.WriteString(u.Scheme)
buf.WriteString("://")
}
// Authority (userinfo + host)
buf.WriteString(GetRawAuthority(u))
// Path
buf.WriteString(GetRawPath(u))
// Query
if u.Query != "" {
buf.WriteRune('?')
buf.WriteString(u.Query)
}
// Fragment
if u.Fragment != "" {
buf.WriteRune('#')
buf.WriteString(u.Fragment)
}
return buf.String()
}
// GetRawRequestURI returns the exact URI as it would appear in an HTTP request line
// It could be "//a/b/c../;/?x=test", "\\a\\bb\\..\\test//..//..//users.json", "@collaboratorhost", etc
func (u *RawURL) GetRawRequestURI() string {
if u.RawRequestURI != "" {
return u.RawRequestURI
}
var buf strings.Builder
// If no custom RawRequestURI is set, construct from Path
if u.Path != "" {
buf.WriteString(u.Path)
}
// Add query if present
if u.Query != "" {
buf.WriteRune('?')
buf.WriteString(u.Query)
}
// Add fragment if present
if u.Fragment != "" {
buf.WriteRune('#')
buf.WriteString(u.Fragment)
}
return buf.String()
}
// GetAbsoluteURI returns the full URI including scheme and host
// Example: "http://example.com/path?query#fragment"
func (u *RawURL) GetRawAbsoluteURI() string {
var buf strings.Builder
if u.Scheme != "" {
buf.WriteString(u.Scheme)
buf.WriteString("://")
}
buf.WriteString(GetRawAuthority(u))
buf.WriteString(u.GetRawRequestURI())
return buf.String()
}
// UpdateRawURL updates a specific component of the URL with a new value
func (u *RawURL) UpdateRawURL(component URLComponent, newValue string) {
switch component {
case Scheme:
u.Scheme = newValue
case Username:
if u.User == nil {
u.User = &Userinfo{}
}
u.User.username = newValue
case Password:
if u.User == nil {
u.User = &Userinfo{}
}
u.User.password = newValue
u.User.passwordSet = true
case Host:
// Update host without affecting port
if port := u.GetPort(); port != "" {
u.Host = newValue + ":" + port
} else {
u.Host = newValue
}
case Port:
// Update port without affecting host
hostname := u.GetHostname()
if newValue != "" {
u.Host = hostname + ":" + newValue
} else {
u.Host = hostname
}
case Path:
u.Path = newValue
u.RawRequestURI = "" // Clear any custom raw URI when updating path
case Query:
u.Query = newValue
u.RawRequestURI = "" // Clear any custom raw URI when updating query
case Fragment:
u.Fragment = newValue
u.RawRequestURI = "" // Clear any custom raw URI when updating fragment
case RawURI:
u.RawRequestURI = newValue // Same as SetRawRequestURI
}
}
// SetRawRequestURI allows setting a custom request URI
// This is useful for fuzzing/testing with non-standard URIs
// It's equivalent to UpdateRawURL(RawURI, uri)
func (u *RawURL) SetRawRequestURI(uri string) {
u.UpdateRawURL(RawURI, uri)
}
// splitHostPort() separates host and port. If the port is not valid, it returns
// the entire input as host, and it doesn't check the validity of the host.
// Unlike net.SplitHostPort, but per RFC 3986, it requires ports to be numeric.
func splitHostPort(hostPort string) (host, port string) {
host = hostPort
colon := strings.LastIndexByte(host, ':')
if colon != -1 && validOptionalPort(host[colon:]) {
host, port = host[:colon], host[colon+1:]
}
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
host = host[1 : len(host)-1]
}
return
}
// validOptionalPort reports whether port is either an empty string
//
// or matches /^:\d*$/
func validOptionalPort(port string) bool {
if port == "" {
return true
}
if port[0] != ':' {
return false
}
for _, b := range port[1:] {
if b < '0' || b > '9' {
return false
}
}
return true
}
// GetAsciiHex returns hex value of ascii char
func GetAsciiHex(r rune) string {
val := strconv.FormatInt(int64(r), 16)
if len(val) == 1 {
// append 0 formatInt skips it by default
val = "0" + val
}
return strings.ToUpper(val)
}
// GetUTF8Hex returns hex value of utf-8 non-ascii char
func GetUTF8Hex(r rune) string {
// Percent Encoding is only done in hexadecimal values and in ASCII Range only
// other UTF-8 chars (chinese etc) can be used by utf-8 encoding and byte conversion
// let golang do utf-8 encoding of rune
var buff bytes.Buffer
utfchar := string(r)
hexencstr := hex.EncodeToString([]byte(utfchar))
for k, v := range hexencstr {
if k != 0 && k%2 == 0 {
buff.WriteRune('%')
}
buff.WriteRune(v)
}
return buff.String()
}
// lastIndexRune returns the last index} of a rune in a string
func lastIndexRune(s string, r rune) int {
// Fast path for ASCII
if r < utf8.RuneSelf {
return strings.LastIndex(s, string(r))
}
// For non-ASCII runes, we need to scan backwards
for i := len(s); i > 0; {
r1, size := utf8.DecodeLastRuneInString(s[:i])
if r1 == r {
return i - size
}
i -= size
}
return -1
}
// GetRuneMap returns a map of runes
func GetRuneMap(runes []rune) map[rune]struct{} {
x := map[rune]struct{}{}
for _, v := range runes {
x[v] = struct{}{}
}
return x
}