forked from ttacon/uri
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuri.go
More file actions
856 lines (727 loc) · 19.5 KB
/
uri.go
File metadata and controls
856 lines (727 loc) · 19.5 KB
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
// Package uri is meant to be an RFC 3986 compliant URI builder and parser.
//
// This is based on the work from ttacon/uri (credits: Trey Tacon).
//
// This fork concentrates on RFC 3986 strictness for URI parsing and validation.
//
// Reference: https://tools.ietf.org/html/rfc3986
//
// Tests have been augmented with test suites of URI validators in other languages:
// perl, python, scala, .Net.
//
// Extra features like MySQL URIs present in the original repo have been removed.
package uri
import (
"fmt"
"net/netip"
"net/url"
"strconv"
"strings"
)
// URI represents a general RFC3986 URI.
type URI interface {
// Scheme the URI conforms to.
Scheme() string
// Authority information for the URI, including the "//" prefix.
Authority() Authority
// Query returns a map of key/value pairs of all parameters
// in the query string of the URI.
Query() url.Values
// Fragment returns the fragment (component preceded by '#') in the
// URI if there is one.
Fragment() string
// Builder returns a Builder that can be used to modify the URI.
Builder() Builder
// String representation of the URI
String() string
// Validate the different components of the URI
Validate() error
// Is the current port the default for this scheme?
IsDefaultPort() bool
// Default port for this scheme
DefaultPort() int
Err() error
}
// Authority information that a URI contains
// as specified by RFC3986.
//
// Username and password (if present) are given by UserInfo().
type Authority interface {
UserInfo() string
Host() string
Port() string
Path() string
String() string
Validate(schemes ...string) error
IsIP() bool
IPAddr() netip.Addr
Err() error
}
type ipType struct {
isIPv4 bool
isIPv6 bool
isIPvFuture bool
}
const (
// char and string literals.
colonMark = ':'
questionMark = '?'
fragmentMark = '#'
percentMark = '%'
atHost = '@'
slashMark = '/'
openingBracketMark = '['
closingBracketMark = ']'
dotSeparator = '.'
authorityPrefix = "//"
)
const (
// DNS name constants.
maxSegmentLength = 63
maxDomainLength = 255
)
//nolint:gochecknoglobals // immutable private package-level constants for RFC 3986 character sets
var (
// predefined sets of accecpted runes beyond the "unreserved" character set.
pcharExtraRunes = []rune{colonMark, atHost} // pchar = unreserved | ':' | '@'
queryOrFragmentExtraRunes = append(pcharExtraRunes, slashMark, questionMark)
userInfoExtraRunes = append(pcharExtraRunes, colonMark)
)
// IsURI reports whether a URI is valid according to RFC3986/RFC3987.
func IsURI(raw string) bool {
_, err := Parse(raw)
return err == nil
}
// IsURIReference reports whether a URI reference is valid according to RFC3986/RFC3987.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-4.1 and
// https://www.rfc-editor.org/rfc/rfc3986#section-4.2
func IsURIReference(raw string) bool {
_, err := ParseReference(raw)
return err == nil
}
// Parse attempts to parse a URI.
// It returns an error if the URI is not RFC3986-compliant.
func Parse(raw string) (URI, error) {
return parse(raw, false)
}
// ParseReference attempts to parse a URI relative reference.
//
// It returns an error if the URI is not RFC3986-compliant.
func ParseReference(raw string) (URI, error) {
return parse(raw, true)
}
//nolint:gocognit,gocyclo,cyclop // essential complexity in RFC 3986 URI parsing, might be refactored in future
func parse(raw string, withURIReference bool) (URI, error) {
var (
scheme string
curr int
)
schemeEnd := strings.IndexByte(raw, colonMark) // position of a ":"
hierPartEnd := strings.IndexByte(raw, questionMark) // position of a "?"
queryEnd := strings.IndexByte(raw, fragmentMark) // position of a "#"
// exclude pathological input
if schemeEnd == 0 || hierPartEnd == 0 || queryEnd == 0 {
// ":", "?", "#"
err := errorsJoin(
ErrInvalidURI,
fmt.Errorf("URI cannot start by a ':', '?' or '#' mark: %w", ErrURI),
)
return nil, err
}
if schemeEnd == 1 {
return nil, errorsJoin(
ErrInvalidScheme,
fmt.Errorf("scheme has a minimum length of 2 characters: %w", ErrURI),
)
}
if hierPartEnd == 1 || queryEnd == 1 {
// ".:", ".?", ".#"
err := errorsJoin(
ErrInvalidURI,
fmt.Errorf("invalid combination of start markers, near: %q: %w", raw[:2], ErrURI),
)
return nil, err
}
if hierPartEnd > 0 && hierPartEnd < schemeEnd || queryEnd > 0 && queryEnd < schemeEnd {
// e.g. htt?p: ; h#ttp: ..
mini, maxi := miniMaxi(hierPartEnd, schemeEnd, queryEnd, schemeEnd)
err := errorsJoin(
ErrInvalidURI,
fmt.Errorf("URI part markers %q,%q,%q are in an incorrect order, near: %q: %w", colonMark, questionMark, fragmentMark, raw[mini:maxi], ErrURI),
)
return nil, err
}
if queryEnd > 0 && queryEnd < hierPartEnd {
// e.g. https://abc#a?b
hierPartEnd = queryEnd
}
isRelative := strings.HasPrefix(raw, authorityPrefix)
switch {
case schemeEnd > 0 && !isRelative:
scheme = raw[curr:schemeEnd]
if schemeEnd+1 == len(raw) {
// trailing ':' (e.g. http:)
u := &uri{
scheme: scheme,
}
return u, u.Validate()
}
case !withURIReference:
// scheme is required for URI
return nil, errorsJoin(
ErrNoSchemeFound,
fmt.Errorf("for URI (not URI reference), the scheme is required: %w", ErrURI),
)
case isRelative:
// scheme is optional for URI references.
//
// start with // and a ':' is following... e.g //example.com:8080/path
schemeEnd = -1
}
curr = schemeEnd + 1
if hierPartEnd == len(raw)-1 || (hierPartEnd < 0 && queryEnd < 0) {
// trailing ? or (no query & no fragment)
if hierPartEnd < 0 {
hierPartEnd = len(raw)
}
authority, err := parseAuthority(raw[curr:hierPartEnd])
if err != nil {
err = errorsJoin(ErrInvalidURI, err)
return nil, err
}
u := &uri{
scheme: scheme,
hierPart: raw[curr:hierPartEnd],
authority: authority,
}
return u, u.Validate()
}
var (
hierPart, query, fragment string
authority authorityInfo
err error
)
//nolint:nestif // essential complexity in URI component parsing, would require major refactoring
if hierPartEnd > 0 {
hierPart = raw[curr:hierPartEnd]
authority, err = parseAuthority(hierPart)
if err != nil {
return nil, errorsJoin(ErrInvalidURI, err)
}
if hierPartEnd+1 < len(raw) {
if queryEnd < 0 {
// query ?, no fragment
query = raw[hierPartEnd+1:]
} else if hierPartEnd < queryEnd-1 {
// query ?, fragment
query = raw[hierPartEnd+1 : queryEnd]
}
}
curr = hierPartEnd + 1
}
if queryEnd == len(raw)-1 && hierPartEnd < 0 {
// trailing #, no query "?"
hierPart = raw[curr:queryEnd]
authority, err = parseAuthority(hierPart)
if err != nil {
return nil, errorsJoin(ErrInvalidURI, err)
}
u := &uri{
scheme: scheme,
hierPart: hierPart,
authority: authority,
query: query,
}
if err = u.Validate(); err != nil {
return nil, err
}
return u, nil
}
if queryEnd > 0 {
// there is a fragment
if hierPartEnd < 0 {
// no query
hierPart = raw[curr:queryEnd]
authority, err = parseAuthority(hierPart)
if err != nil {
return nil, errorsJoin(ErrInvalidURI, err)
}
}
if queryEnd+1 < len(raw) {
fragment = raw[queryEnd+1:]
}
}
u := &uri{
scheme: scheme,
hierPart: hierPart,
query: query,
fragment: fragment,
authority: authority,
}
return u, u.Validate()
}
type uri struct {
// raw components
scheme string
hierPart string
query string
fragment string
// parsed components
authority authorityInfo
err error
}
func (u *uri) URI() URI {
return u
}
// Scheme for this URI.
func (u *uri) Scheme() string {
return u.scheme
}
// Authority information for the URI, including the "//" prefix.
func (u *uri) Authority() Authority {
u.ensureAuthorityExists()
return &u.authority
}
// Query returns a map of key/value pairs of all parameters
// in the query string of the URI, parsed like standard lib URL.Query().
func (u *uri) Query() url.Values {
v, _ := url.ParseQuery(u.query)
return v
}
func (u *uri) Fragment() string {
return u.fragment
}
// Validate checks that all parts of a URI abide by allowed characters.
func (u *uri) Validate() error {
if u.scheme != "" {
if err := u.validateScheme(u.scheme); err != nil {
u.err = err
return err
}
}
if u.query != "" {
if err := u.validateQuery(u.query); err != nil {
u.err = err
return err
}
}
if u.fragment != "" {
if err := u.validateFragment(u.fragment); err != nil {
u.err = err
u.err = err
return err
}
}
if u.hierPart != "" {
ip, err := u.authority.validate(u.scheme)
if err != nil {
u.err = err
u.authority.err = err
return err
}
u.authority.ipType = ip
}
// empty hierpart case
return nil
}
// String representation of a URI.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-6.2.2.1 and later.
func (u *uri) String() string {
buf := strings.Builder{}
buf.Grow(len(u.scheme) + 1 + len(u.query) + 1 + len(u.fragment) + 1 + u.authority.builderSize())
if len(u.scheme) > 0 {
buf.WriteString(u.scheme)
buf.WriteByte(colonMark)
}
u.authority.buildString(&buf)
if len(u.query) > 0 {
buf.WriteByte(questionMark)
buf.WriteString(u.query)
}
if len(u.fragment) > 0 {
buf.WriteByte(fragmentMark)
buf.WriteString(u.fragment)
}
return buf.String()
}
// validateScheme verifies the correctness of the scheme part.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-3.1
// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
//
// NOTE: the scheme is not supposed to contain any percent-encoded sequence.
func (u *uri) validateScheme(scheme string) error {
const minSchemeLength = 2
if len(scheme) < minSchemeLength {
return ErrInvalidScheme
}
c := scheme[0]
if !isASCIILetter(c) {
return errorsJoin(
ErrInvalidScheme,
fmt.Errorf("a URI scheme must start with an ASCII letter: %w", ErrURI),
)
}
for i := 1; i < len(scheme); i++ {
c := scheme[i]
switch {
case isDigit(c):
// ok
case isASCIILetter(c):
// ok
case c == '+' || c == '-' || c == '.':
// ok
default:
return errorsJoin(
ErrInvalidScheme,
fmt.Errorf("invalid character %q found in scheme: %w", c, ErrURI),
)
}
}
return nil
}
// validateQuery validates the query part.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-3.4
//
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
// query = *( pchar / "/" / "?" )
func (u *uri) validateQuery(query string) error {
if err := validateUnreservedWithExtra(query, queryOrFragmentExtraRunes); err != nil {
return errorsJoin(ErrInvalidQuery, err)
}
return nil
}
// validateFragment validates the fragment part.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-3.5
//
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
//
// fragment = *( pchar / "/" / "?" ).
func (u *uri) validateFragment(fragment string) error {
if err := validateUnreservedWithExtra(fragment, queryOrFragmentExtraRunes); err != nil {
return errorsJoin(ErrInvalidFragment, err)
}
return nil
}
type authorityInfo struct {
ipType
prefix string
userinfo string
host string
port string
path string
err error
}
func (a authorityInfo) UserInfo() string { return a.userinfo }
func (a authorityInfo) Host() string { return a.host }
func (a authorityInfo) Port() string { return a.port }
func (a authorityInfo) Path() string { return a.path }
func (a authorityInfo) String() string {
buf := strings.Builder{}
buf.Grow(a.builderSize())
a.buildString(&buf)
return buf.String()
}
// Validate validates the Authority part.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-3.2
func (a *authorityInfo) Validate(schemes ...string) error {
ip, err := a.validate(schemes...)
if err != nil {
a.err = err
return err
}
a.ipType = ip
return nil
}
func (a authorityInfo) builderSize() int {
return len(a.prefix) + len(a.userinfo) + 1 + len(a.host) + 2 + len(a.port) + 1 + len(a.path)
}
func (a authorityInfo) buildString(buf *strings.Builder) {
buf.WriteString(a.prefix)
buf.WriteString(a.userinfo)
if len(a.userinfo) > 0 {
buf.WriteByte(atHost)
}
if a.isIPv6 {
buf.WriteString("[" + a.host + "]")
} else {
buf.WriteString(a.host)
}
if len(a.port) > 0 {
buf.WriteByte(colonMark)
}
buf.WriteString(a.port)
buf.WriteString(a.path)
}
func (a authorityInfo) validate(schemes ...string) (ipType, error) {
var ip ipType
if a.path != "" {
if err := a.validatePath(a.path); err != nil {
return ip, err
}
}
if a.host != "" {
var err error
ip, err = a.validateHost(a.host, a.isIPv6, schemes...)
if err != nil {
return ip, err
}
}
if a.port != "" {
if err := a.validatePort(a.port, a.host); err != nil {
return ip, err
}
}
if a.userinfo != "" {
if err := a.validateUserInfo(a.userinfo); err != nil {
return ip, err
}
}
return ip, nil
}
// validatePath validates the path part.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-3.3
func (a authorityInfo) validatePath(path string) error {
if a.host == "" && a.port == "" && len(path) >= 2 && path[0] == slashMark && path[1] == slashMark {
return errorsJoin(
ErrInvalidPath,
fmt.Errorf(
`if a URI does not contain an authority component, then the path cannot begin with two slash characters ("//"): %q: %w`,
a.path, ErrURI,
))
}
var previousPos int
for pos, char := range path {
if char != slashMark {
continue
}
if pos > previousPos {
if err := validateUnreservedWithExtra(path[previousPos:pos], pcharExtraRunes); err != nil {
return errorsJoin(
ErrInvalidPath,
err,
)
}
}
previousPos = pos + 1
}
if previousPos < len(path) { // don't care if the last char was a separator
if err := validateUnreservedWithExtra(path[previousPos:], pcharExtraRunes); err != nil {
return errorsJoin(
ErrInvalidPath,
err,
)
}
}
return nil
}
// validateHost validates the host part.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-3.2.2
func (a authorityInfo) validateHost(host string, isIPv6 bool, schemes ...string) (ipType, error) {
// check for IP addresses
// * IPv6 are required to be enclosed within '[]' (isIPv6=true), if an IPv6 zone is present,
// there is a trailing escaped sequence, but the heading IPv6 literal must not be escaped.
// * IPv4 are not percent-escaped: strict addresses never contain parts starting a zero (e.g. 012 should be 12).
// * address the provision made in the RFC for a "IPvFuture"
if isIPv6 {
if host[0] == 'v' || host[0] == 'V' {
if err := validateIPvFuture(host[1:]); err != nil {
return ipType{}, errorsJoin(
ErrInvalidHostAddress,
err,
)
}
return ipType{isIPv6: true, isIPvFuture: true}, nil
}
return ipType{isIPv6: true}, validateIPv6(host)
}
if err := validateIPv4(host); err == nil {
return ipType{isIPv4: true}, nil
}
// This is not an IP: check for host DNS or registered name
if err := validateHostForScheme(host, schemes...); err != nil {
return ipType{}, errorsJoin(
ErrInvalidHost,
err,
)
}
return ipType{}, nil
}
// validateHostForScheme validates the host according to 2 different sets of rules:
// - if the scheme is a scheme well-known for using DNS host names, the DNS host validation applies (RFC)
// (applies to schemes at: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml)
// - otherwise, applies the "registered-name" validation stated by RFC 3986:
//
// dns-name see: https://www.rfc-editor.org/rfc/rfc1034, https://www.rfc-editor.org/info/rfc5890
// reg-name = *( unreserved / pct-encoded / sub-delims ).
func validateHostForScheme(host string, schemes ...string) error {
for _, scheme := range schemes {
if UsesDNSHostValidation(scheme) {
if err := validateDNSHostForScheme(host); err != nil {
return err
}
}
if err := validateRegisteredHostForScheme(host); err != nil {
return err
}
}
return nil
}
func validateRegisteredHostForScheme(host string) error {
// RFC 3986 registered name
if err := validateUnreservedWithExtra(host, nil); err != nil {
return errorsJoin(
ErrInvalidRegisteredName,
err,
)
}
return nil
}
// validatePort validates the port part.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-3.2.3
//
// port = *DIGIT.
func (a authorityInfo) validatePort(port, host string) error {
const maxPort uint64 = 65535
if !isNumerical(port) {
return ErrInvalidPort
}
if host == "" {
return errorsJoin(
ErrMissingHost,
fmt.Errorf("whenever a port is specified, a host part must be present: %w", ErrURI),
)
}
portNum, _ := strconv.ParseUint(port, 10, 64)
if portNum > maxPort {
return errorsJoin(
ErrInvalidPort,
fmt.Errorf("a valid port lies in the range (0-%d): %w", maxPort, ErrURI),
)
}
return nil
}
// validateUserInfo validates the userinfo part.
//
// Reference: https://www.rfc-editor.org/rfc/rfc3986#section-3.2.1
//
// userinfo = *( unreserved / pct-encoded / sub-delims / ":" ).
func (a authorityInfo) validateUserInfo(userinfo string) error {
if err := validateUnreservedWithExtra(userinfo, userInfoExtraRunes); err != nil {
return errorsJoin(
ErrInvalidUserInfo,
err,
)
}
return nil
}
func parseAuthority(hier string) (authorityInfo, error) {
// as per RFC 3986 Section 3.6
var (
prefix, userinfo, host, port, path string
isIPv6 bool
)
// authority sections MUST begin with a '//'
if strings.HasPrefix(hier, authorityPrefix) {
prefix = authorityPrefix
hier = strings.TrimPrefix(hier, authorityPrefix)
}
//nolint:nestif // essential complexity in path resolution logic, would require major refactoring
if prefix == "" {
path = hier
} else {
// authority = [ userinfo "@" ] host [ ":" port ]
slashEnd := strings.IndexByte(hier, slashMark)
if slashEnd > -1 {
if slashEnd < len(hier) {
path = hier[slashEnd:]
}
hier = hier[:slashEnd]
}
host = hier
if at := strings.IndexByte(host, atHost); at > 0 {
userinfo = host[:at]
if at+1 < len(host) {
host = host[at+1:]
}
}
if bracket := strings.IndexByte(host, openingBracketMark); bracket >= 0 {
// ipv6 addresses: "["xx:yy:zz"]":port
rawHost := host
closingbracket := strings.IndexByte(host, closingBracketMark)
switch {
case closingbracket > bracket+1:
host = host[bracket+1 : closingbracket]
rawHost = rawHost[closingbracket+1:]
isIPv6 = true
case closingbracket > bracket:
return authorityInfo{}, errorsJoin(
ErrInvalidHostAddress,
fmt.Errorf("empty IPv6 address: %w", ErrURI),
)
default:
return authorityInfo{}, errorsJoin(
ErrInvalidHostAddress,
fmt.Errorf("mismatched square brackets: %w", ErrURI),
)
}
if colon := strings.IndexByte(rawHost, colonMark); colon >= 0 {
if colon+1 < len(rawHost) {
port = rawHost[colon+1:]
}
}
} else {
if colon := strings.IndexByte(host, colonMark); colon >= 0 {
if colon+1 < len(host) {
port = host[colon+1:]
}
host = host[:colon]
}
}
}
return authorityInfo{
prefix: prefix,
userinfo: userinfo,
host: host,
port: port,
path: path,
ipType: ipType{isIPv6: isIPv6},
}, nil
}
func (u *uri) ensureAuthorityExists() {
if u.authority.userinfo != "" ||
u.authority.host != "" ||
u.authority.port != "" {
u.authority.prefix = authorityPrefix
}
}
func miniMaxi(vals ...int) (int, int) {
var mini, maxi int
if len(vals) == 0 {
return mini, maxi
}
mini, maxi = vals[0], vals[0]
for _, val := range vals[1:] {
if val < mini {
mini = val
}
if val > maxi {
maxi = val
}
}
if mini < 0 {
mini = 0
}
if maxi < 0 {
maxi = 0
}
return mini, maxi
}