Source file
src/net/url/url.go
Documentation: net/url
1
2
3
4
5
6 package url
7
8
9
10
11
12
13 import (
14 "errors"
15 "fmt"
16 "sort"
17 "strconv"
18 "strings"
19 )
20
21
22 type Error struct {
23 Op string
24 URL string
25 Err error
26 }
27
28 func (e *Error) Unwrap() error { return e.Err }
29 func (e *Error) Error() string { return fmt.Sprintf("%s %q: %s", e.Op, e.URL, e.Err) }
30
31 func (e *Error) Timeout() bool {
32 t, ok := e.Err.(interface {
33 Timeout() bool
34 })
35 return ok && t.Timeout()
36 }
37
38 func (e *Error) Temporary() bool {
39 t, ok := e.Err.(interface {
40 Temporary() bool
41 })
42 return ok && t.Temporary()
43 }
44
45 const upperhex = "0123456789ABCDEF"
46
47 func ishex(c byte) bool {
48 switch {
49 case '0' <= c && c <= '9':
50 return true
51 case 'a' <= c && c <= 'f':
52 return true
53 case 'A' <= c && c <= 'F':
54 return true
55 }
56 return false
57 }
58
59 func unhex(c byte) byte {
60 switch {
61 case '0' <= c && c <= '9':
62 return c - '0'
63 case 'a' <= c && c <= 'f':
64 return c - 'a' + 10
65 case 'A' <= c && c <= 'F':
66 return c - 'A' + 10
67 }
68 return 0
69 }
70
71 type encoding int
72
73 const (
74 encodePath encoding = 1 + iota
75 encodePathSegment
76 encodeHost
77 encodeZone
78 encodeUserPassword
79 encodeQueryComponent
80 encodeFragment
81 )
82
83 type EscapeError string
84
85 func (e EscapeError) Error() string {
86 return "invalid URL escape " + strconv.Quote(string(e))
87 }
88
89 type InvalidHostError string
90
91 func (e InvalidHostError) Error() string {
92 return "invalid character " + strconv.Quote(string(e)) + " in host name"
93 }
94
95
96
97
98
99
100 func shouldEscape(c byte, mode encoding) bool {
101
102 if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
103 return false
104 }
105
106 if mode == encodeHost || mode == encodeZone {
107
108
109
110
111
112
113
114
115
116 switch c {
117 case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '[', ']', '<', '>', '"':
118 return false
119 }
120 }
121
122 switch c {
123 case '-', '_', '.', '~':
124 return false
125
126 case '$', '&', '+', ',', '/', ':', ';', '=', '?', '@':
127
128
129 switch mode {
130 case encodePath:
131
132
133
134
135 return c == '?'
136
137 case encodePathSegment:
138
139
140 return c == '/' || c == ';' || c == ',' || c == '?'
141
142 case encodeUserPassword:
143
144
145
146
147 return c == '@' || c == '/' || c == '?' || c == ':'
148
149 case encodeQueryComponent:
150
151 return true
152
153 case encodeFragment:
154
155
156 return false
157 }
158 }
159
160 if mode == encodeFragment {
161
162
163
164
165
166
167 switch c {
168 case '!', '(', ')', '*':
169 return false
170 }
171 }
172
173
174 return true
175 }
176
177
178
179
180
181
182 func QueryUnescape(s string) (string, error) {
183 return unescape(s, encodeQueryComponent)
184 }
185
186
187
188
189
190
191
192
193 func PathUnescape(s string) (string, error) {
194 return unescape(s, encodePathSegment)
195 }
196
197
198
199 func unescape(s string, mode encoding) (string, error) {
200
201 n := 0
202 hasPlus := false
203 for i := 0; i < len(s); {
204 switch s[i] {
205 case '%':
206 n++
207 if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
208 s = s[i:]
209 if len(s) > 3 {
210 s = s[:3]
211 }
212 return "", EscapeError(s)
213 }
214
215
216
217
218
219
220 if mode == encodeHost && unhex(s[i+1]) < 8 && s[i:i+3] != "%25" {
221 return "", EscapeError(s[i : i+3])
222 }
223 if mode == encodeZone {
224
225
226
227
228
229
230
231 v := unhex(s[i+1])<<4 | unhex(s[i+2])
232 if s[i:i+3] != "%25" && v != ' ' && shouldEscape(v, encodeHost) {
233 return "", EscapeError(s[i : i+3])
234 }
235 }
236 i += 3
237 case '+':
238 hasPlus = mode == encodeQueryComponent
239 i++
240 default:
241 if (mode == encodeHost || mode == encodeZone) && s[i] < 0x80 && shouldEscape(s[i], mode) {
242 return "", InvalidHostError(s[i : i+1])
243 }
244 i++
245 }
246 }
247
248 if n == 0 && !hasPlus {
249 return s, nil
250 }
251
252 var t strings.Builder
253 t.Grow(len(s) - 2*n)
254 for i := 0; i < len(s); i++ {
255 switch s[i] {
256 case '%':
257 t.WriteByte(unhex(s[i+1])<<4 | unhex(s[i+2]))
258 i += 2
259 case '+':
260 if mode == encodeQueryComponent {
261 t.WriteByte(' ')
262 } else {
263 t.WriteByte('+')
264 }
265 default:
266 t.WriteByte(s[i])
267 }
268 }
269 return t.String(), nil
270 }
271
272
273
274 func QueryEscape(s string) string {
275 return escape(s, encodeQueryComponent)
276 }
277
278
279
280 func PathEscape(s string) string {
281 return escape(s, encodePathSegment)
282 }
283
284 func escape(s string, mode encoding) string {
285 spaceCount, hexCount := 0, 0
286 for i := 0; i < len(s); i++ {
287 c := s[i]
288 if shouldEscape(c, mode) {
289 if c == ' ' && mode == encodeQueryComponent {
290 spaceCount++
291 } else {
292 hexCount++
293 }
294 }
295 }
296
297 if spaceCount == 0 && hexCount == 0 {
298 return s
299 }
300
301 var buf [64]byte
302 var t []byte
303
304 required := len(s) + 2*hexCount
305 if required <= len(buf) {
306 t = buf[:required]
307 } else {
308 t = make([]byte, required)
309 }
310
311 if hexCount == 0 {
312 copy(t, s)
313 for i := 0; i < len(s); i++ {
314 if s[i] == ' ' {
315 t[i] = '+'
316 }
317 }
318 return string(t)
319 }
320
321 j := 0
322 for i := 0; i < len(s); i++ {
323 switch c := s[i]; {
324 case c == ' ' && mode == encodeQueryComponent:
325 t[j] = '+'
326 j++
327 case shouldEscape(c, mode):
328 t[j] = '%'
329 t[j+1] = upperhex[c>>4]
330 t[j+2] = upperhex[c&15]
331 j += 3
332 default:
333 t[j] = s[i]
334 j++
335 }
336 }
337 return string(t)
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358 type URL struct {
359 Scheme string
360 Opaque string
361 User *Userinfo
362 Host string
363 Path string
364 RawPath string
365 ForceQuery bool
366 RawQuery string
367 Fragment string
368 RawFragment string
369 }
370
371
372
373 func User(username string) *Userinfo {
374 return &Userinfo{username, "", false}
375 }
376
377
378
379
380
381
382
383
384
385 func UserPassword(username, password string) *Userinfo {
386 return &Userinfo{username, password, true}
387 }
388
389
390
391
392
393 type Userinfo struct {
394 username string
395 password string
396 passwordSet bool
397 }
398
399
400 func (u *Userinfo) Username() string {
401 if u == nil {
402 return ""
403 }
404 return u.username
405 }
406
407
408 func (u *Userinfo) Password() (string, bool) {
409 if u == nil {
410 return "", false
411 }
412 return u.password, u.passwordSet
413 }
414
415
416
417 func (u *Userinfo) String() string {
418 if u == nil {
419 return ""
420 }
421 s := escape(u.username, encodeUserPassword)
422 if u.passwordSet {
423 s += ":" + escape(u.password, encodeUserPassword)
424 }
425 return s
426 }
427
428
429
430
431 func getScheme(rawURL string) (scheme, path string, err error) {
432 for i := 0; i < len(rawURL); i++ {
433 c := rawURL[i]
434 switch {
435 case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z':
436
437 case '0' <= c && c <= '9' || c == '+' || c == '-' || c == '.':
438 if i == 0 {
439 return "", rawURL, nil
440 }
441 case c == ':':
442 if i == 0 {
443 return "", "", errors.New("missing protocol scheme")
444 }
445 return rawURL[:i], rawURL[i+1:], nil
446 default:
447
448
449 return "", rawURL, nil
450 }
451 }
452 return "", rawURL, nil
453 }
454
455
456
457
458 func split(s string, sep byte, cutc bool) (string, string) {
459 i := strings.IndexByte(s, sep)
460 if i < 0 {
461 return s, ""
462 }
463 if cutc {
464 return s[:i], s[i+1:]
465 }
466 return s[:i], s[i:]
467 }
468
469
470
471
472
473
474
475 func Parse(rawURL string) (*URL, error) {
476
477 u, frag := split(rawURL, '#', true)
478 url, err := parse(u, false)
479 if err != nil {
480 return nil, &Error{"parse", u, err}
481 }
482 if frag == "" {
483 return url, nil
484 }
485 if err = url.setFragment(frag); err != nil {
486 return nil, &Error{"parse", rawURL, err}
487 }
488 return url, nil
489 }
490
491
492
493
494
495
496 func ParseRequestURI(rawURL string) (*URL, error) {
497 url, err := parse(rawURL, true)
498 if err != nil {
499 return nil, &Error{"parse", rawURL, err}
500 }
501 return url, nil
502 }
503
504
505
506
507
508 func parse(rawURL string, viaRequest bool) (*URL, error) {
509 var rest string
510 var err error
511
512 if stringContainsCTLByte(rawURL) {
513 return nil, errors.New("net/url: invalid control character in URL")
514 }
515
516 if rawURL == "" && viaRequest {
517 return nil, errors.New("empty url")
518 }
519 url := new(URL)
520
521 if rawURL == "*" {
522 url.Path = "*"
523 return url, nil
524 }
525
526
527
528 if url.Scheme, rest, err = getScheme(rawURL); err != nil {
529 return nil, err
530 }
531 url.Scheme = strings.ToLower(url.Scheme)
532
533 if strings.HasSuffix(rest, "?") && strings.Count(rest, "?") == 1 {
534 url.ForceQuery = true
535 rest = rest[:len(rest)-1]
536 } else {
537 rest, url.RawQuery = split(rest, '?', true)
538 }
539
540 if !strings.HasPrefix(rest, "/") {
541 if url.Scheme != "" {
542
543 url.Opaque = rest
544 return url, nil
545 }
546 if viaRequest {
547 return nil, errors.New("invalid URI for request")
548 }
549
550
551
552
553
554
555
556 colon := strings.Index(rest, ":")
557 slash := strings.Index(rest, "/")
558 if colon >= 0 && (slash < 0 || colon < slash) {
559
560 return nil, errors.New("first path segment in URL cannot contain colon")
561 }
562 }
563
564 if (url.Scheme != "" || !viaRequest && !strings.HasPrefix(rest, "///")) && strings.HasPrefix(rest, "//") {
565 var authority string
566 authority, rest = split(rest[2:], '/', false)
567 url.User, url.Host, err = parseAuthority(authority)
568 if err != nil {
569 return nil, err
570 }
571 }
572
573
574
575
576 if err := url.setPath(rest); err != nil {
577 return nil, err
578 }
579 return url, nil
580 }
581
582 func parseAuthority(authority string) (user *Userinfo, host string, err error) {
583 i := strings.LastIndex(authority, "@")
584 if i < 0 {
585 host, err = parseHost(authority)
586 } else {
587 host, err = parseHost(authority[i+1:])
588 }
589 if err != nil {
590 return nil, "", err
591 }
592 if i < 0 {
593 return nil, host, nil
594 }
595 userinfo := authority[:i]
596 if !validUserinfo(userinfo) {
597 return nil, "", errors.New("net/url: invalid userinfo")
598 }
599 if !strings.Contains(userinfo, ":") {
600 if userinfo, err = unescape(userinfo, encodeUserPassword); err != nil {
601 return nil, "", err
602 }
603 user = User(userinfo)
604 } else {
605 username, password := split(userinfo, ':', true)
606 if username, err = unescape(username, encodeUserPassword); err != nil {
607 return nil, "", err
608 }
609 if password, err = unescape(password, encodeUserPassword); err != nil {
610 return nil, "", err
611 }
612 user = UserPassword(username, password)
613 }
614 return user, host, nil
615 }
616
617
618
619 func parseHost(host string) (string, error) {
620 if strings.HasPrefix(host, "[") {
621
622
623 i := strings.LastIndex(host, "]")
624 if i < 0 {
625 return "", errors.New("missing ']' in host")
626 }
627 colonPort := host[i+1:]
628 if !validOptionalPort(colonPort) {
629 return "", fmt.Errorf("invalid port %q after host", colonPort)
630 }
631
632
633
634
635
636
637
638 zone := strings.Index(host[:i], "%25")
639 if zone >= 0 {
640 host1, err := unescape(host[:zone], encodeHost)
641 if err != nil {
642 return "", err
643 }
644 host2, err := unescape(host[zone:i], encodeZone)
645 if err != nil {
646 return "", err
647 }
648 host3, err := unescape(host[i:], encodeHost)
649 if err != nil {
650 return "", err
651 }
652 return host1 + host2 + host3, nil
653 }
654 } else if i := strings.LastIndex(host, ":"); i != -1 {
655 colonPort := host[i:]
656 if !validOptionalPort(colonPort) {
657 return "", fmt.Errorf("invalid port %q after host", colonPort)
658 }
659 }
660
661 var err error
662 if host, err = unescape(host, encodeHost); err != nil {
663 return "", err
664 }
665 return host, nil
666 }
667
668
669
670
671
672
673
674
675
676 func (u *URL) setPath(p string) error {
677 path, err := unescape(p, encodePath)
678 if err != nil {
679 return err
680 }
681 u.Path = path
682 if escp := escape(path, encodePath); p == escp {
683
684 u.RawPath = ""
685 } else {
686 u.RawPath = p
687 }
688 return nil
689 }
690
691
692
693
694
695
696
697
698
699
700 func (u *URL) EscapedPath() string {
701 if u.RawPath != "" && validEncoded(u.RawPath, encodePath) {
702 p, err := unescape(u.RawPath, encodePath)
703 if err == nil && p == u.Path {
704 return u.RawPath
705 }
706 }
707 if u.Path == "*" {
708 return "*"
709 }
710 return escape(u.Path, encodePath)
711 }
712
713
714
715
716 func validEncoded(s string, mode encoding) bool {
717 for i := 0; i < len(s); i++ {
718
719
720
721
722
723 switch s[i] {
724 case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '@':
725
726 case '[', ']':
727
728 case '%':
729
730 default:
731 if shouldEscape(s[i], mode) {
732 return false
733 }
734 }
735 }
736 return true
737 }
738
739
740 func (u *URL) setFragment(f string) error {
741 frag, err := unescape(f, encodeFragment)
742 if err != nil {
743 return err
744 }
745 u.Fragment = frag
746 if escf := escape(frag, encodeFragment); f == escf {
747
748 u.RawFragment = ""
749 } else {
750 u.RawFragment = f
751 }
752 return nil
753 }
754
755
756
757
758
759
760
761
762
763 func (u *URL) EscapedFragment() string {
764 if u.RawFragment != "" && validEncoded(u.RawFragment, encodeFragment) {
765 f, err := unescape(u.RawFragment, encodeFragment)
766 if err == nil && f == u.Fragment {
767 return u.RawFragment
768 }
769 }
770 return escape(u.Fragment, encodeFragment)
771 }
772
773
774
775 func validOptionalPort(port string) bool {
776 if port == "" {
777 return true
778 }
779 if port[0] != ':' {
780 return false
781 }
782 for _, b := range port[1:] {
783 if b < '0' || b > '9' {
784 return false
785 }
786 }
787 return true
788 }
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811 func (u *URL) String() string {
812 var buf strings.Builder
813 if u.Scheme != "" {
814 buf.WriteString(u.Scheme)
815 buf.WriteByte(':')
816 }
817 if u.Opaque != "" {
818 buf.WriteString(u.Opaque)
819 } else {
820 if u.Scheme != "" || u.Host != "" || u.User != nil {
821 if u.Host != "" || u.Path != "" || u.User != nil {
822 buf.WriteString("//")
823 }
824 if ui := u.User; ui != nil {
825 buf.WriteString(ui.String())
826 buf.WriteByte('@')
827 }
828 if h := u.Host; h != "" {
829 buf.WriteString(escape(h, encodeHost))
830 }
831 }
832 path := u.EscapedPath()
833 if path != "" && path[0] != '/' && u.Host != "" {
834 buf.WriteByte('/')
835 }
836 if buf.Len() == 0 {
837
838
839
840
841
842
843 if i := strings.IndexByte(path, ':'); i > -1 && strings.IndexByte(path[:i], '/') == -1 {
844 buf.WriteString("./")
845 }
846 }
847 buf.WriteString(path)
848 }
849 if u.ForceQuery || u.RawQuery != "" {
850 buf.WriteByte('?')
851 buf.WriteString(u.RawQuery)
852 }
853 if u.Fragment != "" {
854 buf.WriteByte('#')
855 buf.WriteString(u.EscapedFragment())
856 }
857 return buf.String()
858 }
859
860
861
862 func (u *URL) Redacted() string {
863 if u == nil {
864 return ""
865 }
866
867 ru := *u
868 if _, has := ru.User.Password(); has {
869 ru.User = UserPassword(ru.User.Username(), "xxxxx")
870 }
871 return ru.String()
872 }
873
874
875
876
877
878 type Values map[string][]string
879
880
881
882
883
884 func (v Values) Get(key string) string {
885 if v == nil {
886 return ""
887 }
888 vs := v[key]
889 if len(vs) == 0 {
890 return ""
891 }
892 return vs[0]
893 }
894
895
896
897 func (v Values) Set(key, value string) {
898 v[key] = []string{value}
899 }
900
901
902
903 func (v Values) Add(key, value string) {
904 v[key] = append(v[key], value)
905 }
906
907
908 func (v Values) Del(key string) {
909 delete(v, key)
910 }
911
912
913 func (v Values) Has(key string) bool {
914 _, ok := v[key]
915 return ok
916 }
917
918
919
920
921
922
923
924
925
926
927
928 func ParseQuery(query string) (Values, error) {
929 m := make(Values)
930 err := parseQuery(m, query)
931 return m, err
932 }
933
934 func parseQuery(m Values, query string) (err error) {
935 for query != "" {
936 key := query
937 if i := strings.IndexAny(key, "&"); i >= 0 {
938 key, query = key[:i], key[i+1:]
939 } else {
940 query = ""
941 }
942 if strings.Contains(key, ";") {
943 err = fmt.Errorf("invalid semicolon separator in query")
944 continue
945 }
946 if key == "" {
947 continue
948 }
949 value := ""
950 if i := strings.Index(key, "="); i >= 0 {
951 key, value = key[:i], key[i+1:]
952 }
953 key, err1 := QueryUnescape(key)
954 if err1 != nil {
955 if err == nil {
956 err = err1
957 }
958 continue
959 }
960 value, err1 = QueryUnescape(value)
961 if err1 != nil {
962 if err == nil {
963 err = err1
964 }
965 continue
966 }
967 m[key] = append(m[key], value)
968 }
969 return err
970 }
971
972
973
974 func (v Values) Encode() string {
975 if v == nil {
976 return ""
977 }
978 var buf strings.Builder
979 keys := make([]string, 0, len(v))
980 for k := range v {
981 keys = append(keys, k)
982 }
983 sort.Strings(keys)
984 for _, k := range keys {
985 vs := v[k]
986 keyEscaped := QueryEscape(k)
987 for _, v := range vs {
988 if buf.Len() > 0 {
989 buf.WriteByte('&')
990 }
991 buf.WriteString(keyEscaped)
992 buf.WriteByte('=')
993 buf.WriteString(QueryEscape(v))
994 }
995 }
996 return buf.String()
997 }
998
999
1000
1001 func resolvePath(base, ref string) string {
1002 var full string
1003 if ref == "" {
1004 full = base
1005 } else if ref[0] != '/' {
1006 i := strings.LastIndex(base, "/")
1007 full = base[:i+1] + ref
1008 } else {
1009 full = ref
1010 }
1011 if full == "" {
1012 return ""
1013 }
1014
1015 var (
1016 last string
1017 elem string
1018 i int
1019 dst strings.Builder
1020 )
1021 first := true
1022 remaining := full
1023
1024 dst.WriteByte('/')
1025 for i >= 0 {
1026 i = strings.IndexByte(remaining, '/')
1027 if i < 0 {
1028 last, elem, remaining = remaining, remaining, ""
1029 } else {
1030 elem, remaining = remaining[:i], remaining[i+1:]
1031 }
1032 if elem == "." {
1033 first = false
1034
1035 continue
1036 }
1037
1038 if elem == ".." {
1039
1040 str := dst.String()[1:]
1041 index := strings.LastIndexByte(str, '/')
1042
1043 dst.Reset()
1044 dst.WriteByte('/')
1045 if index == -1 {
1046 first = true
1047 } else {
1048 dst.WriteString(str[:index])
1049 }
1050 } else {
1051 if !first {
1052 dst.WriteByte('/')
1053 }
1054 dst.WriteString(elem)
1055 first = false
1056 }
1057 }
1058
1059 if last == "." || last == ".." {
1060 dst.WriteByte('/')
1061 }
1062
1063
1064 r := dst.String()
1065 if len(r) > 1 && r[1] == '/' {
1066 r = r[1:]
1067 }
1068 return r
1069 }
1070
1071
1072
1073 func (u *URL) IsAbs() bool {
1074 return u.Scheme != ""
1075 }
1076
1077
1078
1079
1080 func (u *URL) Parse(ref string) (*URL, error) {
1081 refURL, err := Parse(ref)
1082 if err != nil {
1083 return nil, err
1084 }
1085 return u.ResolveReference(refURL), nil
1086 }
1087
1088
1089
1090
1091
1092
1093
1094 func (u *URL) ResolveReference(ref *URL) *URL {
1095 url := *ref
1096 if ref.Scheme == "" {
1097 url.Scheme = u.Scheme
1098 }
1099 if ref.Scheme != "" || ref.Host != "" || ref.User != nil {
1100
1101
1102
1103 url.setPath(resolvePath(ref.EscapedPath(), ""))
1104 return &url
1105 }
1106 if ref.Opaque != "" {
1107 url.User = nil
1108 url.Host = ""
1109 url.Path = ""
1110 return &url
1111 }
1112 if ref.Path == "" && ref.RawQuery == "" {
1113 url.RawQuery = u.RawQuery
1114 if ref.Fragment == "" {
1115 url.Fragment = u.Fragment
1116 url.RawFragment = u.RawFragment
1117 }
1118 }
1119
1120 url.Host = u.Host
1121 url.User = u.User
1122 url.setPath(resolvePath(u.EscapedPath(), ref.EscapedPath()))
1123 return &url
1124 }
1125
1126
1127
1128
1129 func (u *URL) Query() Values {
1130 v, _ := ParseQuery(u.RawQuery)
1131 return v
1132 }
1133
1134
1135
1136 func (u *URL) RequestURI() string {
1137 result := u.Opaque
1138 if result == "" {
1139 result = u.EscapedPath()
1140 if result == "" {
1141 result = "/"
1142 }
1143 } else {
1144 if strings.HasPrefix(result, "//") {
1145 result = u.Scheme + ":" + result
1146 }
1147 }
1148 if u.ForceQuery || u.RawQuery != "" {
1149 result += "?" + u.RawQuery
1150 }
1151 return result
1152 }
1153
1154
1155
1156
1157
1158 func (u *URL) Hostname() string {
1159 host, _ := splitHostPort(u.Host)
1160 return host
1161 }
1162
1163
1164
1165
1166 func (u *URL) Port() string {
1167 _, port := splitHostPort(u.Host)
1168 return port
1169 }
1170
1171
1172
1173
1174 func splitHostPort(hostPort string) (host, port string) {
1175 host = hostPort
1176
1177 colon := strings.LastIndexByte(host, ':')
1178 if colon != -1 && validOptionalPort(host[colon:]) {
1179 host, port = host[:colon], host[colon+1:]
1180 }
1181
1182 if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
1183 host = host[1 : len(host)-1]
1184 }
1185
1186 return
1187 }
1188
1189
1190
1191
1192 func (u *URL) MarshalBinary() (text []byte, err error) {
1193 return []byte(u.String()), nil
1194 }
1195
1196 func (u *URL) UnmarshalBinary(text []byte) error {
1197 u1, err := Parse(string(text))
1198 if err != nil {
1199 return err
1200 }
1201 *u = *u1
1202 return nil
1203 }
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213 func validUserinfo(s string) bool {
1214 for _, r := range s {
1215 if 'A' <= r && r <= 'Z' {
1216 continue
1217 }
1218 if 'a' <= r && r <= 'z' {
1219 continue
1220 }
1221 if '0' <= r && r <= '9' {
1222 continue
1223 }
1224 switch r {
1225 case '-', '.', '_', ':', '~', '!', '$', '&', '\'',
1226 '(', ')', '*', '+', ',', ';', '=', '%', '@':
1227 continue
1228 default:
1229 return false
1230 }
1231 }
1232 return true
1233 }
1234
1235
1236 func stringContainsCTLByte(s string) bool {
1237 for i := 0; i < len(s); i++ {
1238 b := s[i]
1239 if b < ' ' || b == 0x7f {
1240 return true
1241 }
1242 }
1243 return false
1244 }
1245
View as plain text