Source file src/net/url/url.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package url parses URLs and implements query escaping.
     6  //
     7  // See RFC 3986. This package generally follows RFC 3986, except where
     8  // it deviates for compatibility reasons.
     9  // RFC 6874 followed for IPv6 zone literals.
    10  package url
    11  
    12  // When sending changes, first  search old issues for history on decisions.
    13  // Unit tests should also contain references to issue numbers with details.
    14  
    15  import (
    16  	"errors"
    17  	"fmt"
    18  	"net/netip"
    19  	"path"
    20  	"slices"
    21  	"strconv"
    22  	"strings"
    23  	_ "unsafe" // for linkname
    24  )
    25  
    26  // Error reports an error and the operation and URL that caused it.
    27  type Error struct {
    28  	Op  string
    29  	URL string
    30  	Err error
    31  }
    32  
    33  func (e *Error) Unwrap() error { return e.Err }
    34  func (e *Error) Error() string { return fmt.Sprintf("%s %q: %s", e.Op, e.URL, e.Err) }
    35  
    36  func (e *Error) Timeout() bool {
    37  	t, ok := e.Err.(interface {
    38  		Timeout() bool
    39  	})
    40  	return ok && t.Timeout()
    41  }
    42  
    43  func (e *Error) Temporary() bool {
    44  	t, ok := e.Err.(interface {
    45  		Temporary() bool
    46  	})
    47  	return ok && t.Temporary()
    48  }
    49  
    50  const upperhex = "0123456789ABCDEF"
    51  
    52  func ishex(c byte) bool {
    53  	switch {
    54  	case '0' <= c && c <= '9':
    55  		return true
    56  	case 'a' <= c && c <= 'f':
    57  		return true
    58  	case 'A' <= c && c <= 'F':
    59  		return true
    60  	}
    61  	return false
    62  }
    63  
    64  func unhex(c byte) byte {
    65  	switch {
    66  	case '0' <= c && c <= '9':
    67  		return c - '0'
    68  	case 'a' <= c && c <= 'f':
    69  		return c - 'a' + 10
    70  	case 'A' <= c && c <= 'F':
    71  		return c - 'A' + 10
    72  	default:
    73  		panic("invalid hex character")
    74  	}
    75  }
    76  
    77  type encoding int
    78  
    79  const (
    80  	encodePath encoding = 1 + iota
    81  	encodePathSegment
    82  	encodeHost
    83  	encodeZone
    84  	encodeUserPassword
    85  	encodeQueryComponent
    86  	encodeFragment
    87  )
    88  
    89  type EscapeError string
    90  
    91  func (e EscapeError) Error() string {
    92  	return "invalid URL escape " + strconv.Quote(string(e))
    93  }
    94  
    95  type InvalidHostError string
    96  
    97  func (e InvalidHostError) Error() string {
    98  	return "invalid character " + strconv.Quote(string(e)) + " in host name"
    99  }
   100  
   101  // Return true if the specified character should be escaped when
   102  // appearing in a URL string, according to RFC 3986.
   103  //
   104  // Please be informed that for now shouldEscape does not check all
   105  // reserved characters correctly. See golang.org/issue/5684.
   106  func shouldEscape(c byte, mode encoding) bool {
   107  	// §2.3 Unreserved characters (alphanum)
   108  	if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
   109  		return false
   110  	}
   111  
   112  	if mode == encodeHost || mode == encodeZone {
   113  		// §3.2.2 Host allows
   114  		//	sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
   115  		// as part of reg-name.
   116  		// We add : because we include :port as part of host.
   117  		// We add [ ] because we include [ipv6]:port as part of host.
   118  		// We add < > because they're the only characters left that
   119  		// we could possibly allow, and Parse will reject them if we
   120  		// escape them (because hosts can't use %-encoding for
   121  		// ASCII bytes).
   122  		switch c {
   123  		case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '[', ']', '<', '>', '"':
   124  			return false
   125  		}
   126  	}
   127  
   128  	switch c {
   129  	case '-', '_', '.', '~': // §2.3 Unreserved characters (mark)
   130  		return false
   131  
   132  	case '$', '&', '+', ',', '/', ':', ';', '=', '?', '@': // §2.2 Reserved characters (reserved)
   133  		// Different sections of the URL allow a few of
   134  		// the reserved characters to appear unescaped.
   135  		switch mode {
   136  		case encodePath: // §3.3
   137  			// The RFC allows : @ & = + $ but saves / ; , for assigning
   138  			// meaning to individual path segments. This package
   139  			// only manipulates the path as a whole, so we allow those
   140  			// last three as well. That leaves only ? to escape.
   141  			return c == '?'
   142  
   143  		case encodePathSegment: // §3.3
   144  			// The RFC allows : @ & = + $ but saves / ; , for assigning
   145  			// meaning to individual path segments.
   146  			return c == '/' || c == ';' || c == ',' || c == '?'
   147  
   148  		case encodeUserPassword: // §3.2.1
   149  			// The RFC allows ';', ':', '&', '=', '+', '$', and ',' in
   150  			// userinfo, so we must escape only '@', '/', and '?'.
   151  			// The parsing of userinfo treats ':' as special so we must escape
   152  			// that too.
   153  			return c == '@' || c == '/' || c == '?' || c == ':'
   154  
   155  		case encodeQueryComponent: // §3.4
   156  			// The RFC reserves (so we must escape) everything.
   157  			return true
   158  
   159  		case encodeFragment: // §4.1
   160  			// The RFC text is silent but the grammar allows
   161  			// everything, so escape nothing.
   162  			return false
   163  		}
   164  	}
   165  
   166  	if mode == encodeFragment {
   167  		// RFC 3986 §2.2 allows not escaping sub-delims. A subset of sub-delims are
   168  		// included in reserved from RFC 2396 §2.2. The remaining sub-delims do not
   169  		// need to be escaped. To minimize potential breakage, we apply two restrictions:
   170  		// (1) we always escape sub-delims outside of the fragment, and (2) we always
   171  		// escape single quote to avoid breaking callers that had previously assumed that
   172  		// single quotes would be escaped. See issue #19917.
   173  		switch c {
   174  		case '!', '(', ')', '*':
   175  			return false
   176  		}
   177  	}
   178  
   179  	// Everything else must be escaped.
   180  	return true
   181  }
   182  
   183  // QueryUnescape does the inverse transformation of [QueryEscape],
   184  // converting each 3-byte encoded substring of the form "%AB" into the
   185  // hex-decoded byte 0xAB.
   186  // It returns an error if any % is not followed by two hexadecimal
   187  // digits.
   188  func QueryUnescape(s string) (string, error) {
   189  	return unescape(s, encodeQueryComponent)
   190  }
   191  
   192  // PathUnescape does the inverse transformation of [PathEscape],
   193  // converting each 3-byte encoded substring of the form "%AB" into the
   194  // hex-decoded byte 0xAB. It returns an error if any % is not followed
   195  // by two hexadecimal digits.
   196  //
   197  // PathUnescape is identical to [QueryUnescape] except that it does not
   198  // unescape '+' to ' ' (space).
   199  func PathUnescape(s string) (string, error) {
   200  	return unescape(s, encodePathSegment)
   201  }
   202  
   203  // unescape unescapes a string; the mode specifies
   204  // which section of the URL string is being unescaped.
   205  func unescape(s string, mode encoding) (string, error) {
   206  	// Count %, check that they're well-formed.
   207  	n := 0
   208  	hasPlus := false
   209  	for i := 0; i < len(s); {
   210  		switch s[i] {
   211  		case '%':
   212  			n++
   213  			if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
   214  				s = s[i:]
   215  				if len(s) > 3 {
   216  					s = s[:3]
   217  				}
   218  				return "", EscapeError(s)
   219  			}
   220  			// Per https://tools.ietf.org/html/rfc3986#page-21
   221  			// in the host component %-encoding can only be used
   222  			// for non-ASCII bytes.
   223  			// But https://tools.ietf.org/html/rfc6874#section-2
   224  			// introduces %25 being allowed to escape a percent sign
   225  			// in IPv6 scoped-address literals. Yay.
   226  			if mode == encodeHost && unhex(s[i+1]) < 8 && s[i:i+3] != "%25" {
   227  				return "", EscapeError(s[i : i+3])
   228  			}
   229  			if mode == encodeZone {
   230  				// RFC 6874 says basically "anything goes" for zone identifiers
   231  				// and that even non-ASCII can be redundantly escaped,
   232  				// but it seems prudent to restrict %-escaped bytes here to those
   233  				// that are valid host name bytes in their unescaped form.
   234  				// That is, you can use escaping in the zone identifier but not
   235  				// to introduce bytes you couldn't just write directly.
   236  				// But Windows puts spaces here! Yay.
   237  				v := unhex(s[i+1])<<4 | unhex(s[i+2])
   238  				if s[i:i+3] != "%25" && v != ' ' && shouldEscape(v, encodeHost) {
   239  					return "", EscapeError(s[i : i+3])
   240  				}
   241  			}
   242  			i += 3
   243  		case '+':
   244  			hasPlus = mode == encodeQueryComponent
   245  			i++
   246  		default:
   247  			if (mode == encodeHost || mode == encodeZone) && s[i] < 0x80 && shouldEscape(s[i], mode) {
   248  				return "", InvalidHostError(s[i : i+1])
   249  			}
   250  			i++
   251  		}
   252  	}
   253  
   254  	if n == 0 && !hasPlus {
   255  		return s, nil
   256  	}
   257  
   258  	var t strings.Builder
   259  	t.Grow(len(s) - 2*n)
   260  	for i := 0; i < len(s); i++ {
   261  		switch s[i] {
   262  		case '%':
   263  			t.WriteByte(unhex(s[i+1])<<4 | unhex(s[i+2]))
   264  			i += 2
   265  		case '+':
   266  			if mode == encodeQueryComponent {
   267  				t.WriteByte(' ')
   268  			} else {
   269  				t.WriteByte('+')
   270  			}
   271  		default:
   272  			t.WriteByte(s[i])
   273  		}
   274  	}
   275  	return t.String(), nil
   276  }
   277  
   278  // QueryEscape escapes the string so it can be safely placed
   279  // inside a [URL] query.
   280  func QueryEscape(s string) string {
   281  	return escape(s, encodeQueryComponent)
   282  }
   283  
   284  // PathEscape escapes the string so it can be safely placed inside a [URL] path segment,
   285  // replacing special characters (including /) with %XX sequences as needed.
   286  func PathEscape(s string) string {
   287  	return escape(s, encodePathSegment)
   288  }
   289  
   290  func escape(s string, mode encoding) string {
   291  	spaceCount, hexCount := 0, 0
   292  	for i := 0; i < len(s); i++ {
   293  		c := s[i]
   294  		if shouldEscape(c, mode) {
   295  			if c == ' ' && mode == encodeQueryComponent {
   296  				spaceCount++
   297  			} else {
   298  				hexCount++
   299  			}
   300  		}
   301  	}
   302  
   303  	if spaceCount == 0 && hexCount == 0 {
   304  		return s
   305  	}
   306  
   307  	var buf [64]byte
   308  	var t []byte
   309  
   310  	required := len(s) + 2*hexCount
   311  	if required <= len(buf) {
   312  		t = buf[:required]
   313  	} else {
   314  		t = make([]byte, required)
   315  	}
   316  
   317  	if hexCount == 0 {
   318  		copy(t, s)
   319  		for i := 0; i < len(s); i++ {
   320  			if s[i] == ' ' {
   321  				t[i] = '+'
   322  			}
   323  		}
   324  		return string(t)
   325  	}
   326  
   327  	j := 0
   328  	for i := 0; i < len(s); i++ {
   329  		switch c := s[i]; {
   330  		case c == ' ' && mode == encodeQueryComponent:
   331  			t[j] = '+'
   332  			j++
   333  		case shouldEscape(c, mode):
   334  			t[j] = '%'
   335  			t[j+1] = upperhex[c>>4]
   336  			t[j+2] = upperhex[c&15]
   337  			j += 3
   338  		default:
   339  			t[j] = s[i]
   340  			j++
   341  		}
   342  	}
   343  	return string(t)
   344  }
   345  
   346  // A URL represents a parsed URL (technically, a URI reference).
   347  //
   348  // The general form represented is:
   349  //
   350  //	[scheme:][//[userinfo@]host][/]path[?query][#fragment]
   351  //
   352  // URLs that do not start with a slash after the scheme are interpreted as:
   353  //
   354  //	scheme:opaque[?query][#fragment]
   355  //
   356  // The Host field contains the host and port subcomponents of the URL.
   357  // When the port is present, it is separated from the host with a colon.
   358  // When the host is an IPv6 address, it must be enclosed in square brackets:
   359  // "[fe80::1]:80". The [net.JoinHostPort] function combines a host and port
   360  // into a string suitable for the Host field, adding square brackets to
   361  // the host when necessary.
   362  //
   363  // Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/.
   364  // A consequence is that it is impossible to tell which slashes in the Path were
   365  // slashes in the raw URL and which were %2f. This distinction is rarely important,
   366  // but when it is, the code should use the [URL.EscapedPath] method, which preserves
   367  // the original encoding of Path. The Fragment field is also stored in decoded form,
   368  // use [URL.EscapedFragment] to retrieve the original encoding.
   369  //
   370  // The [URL.String] method uses the [URL.EscapedPath] method to obtain the path.
   371  type URL struct {
   372  	Scheme   string
   373  	Opaque   string    // encoded opaque data
   374  	User     *Userinfo // username and password information
   375  	Host     string    // "host" or "host:port" (see Hostname and Port methods)
   376  	Path     string    // path (relative paths may omit leading slash)
   377  	Fragment string    // fragment for references (without '#')
   378  
   379  	// RawQuery contains the encoded query values, without the initial '?'.
   380  	// Use URL.Query to decode the query.
   381  	RawQuery string
   382  
   383  	// RawPath is an optional field containing an encoded path hint.
   384  	// See the EscapedPath method for more details.
   385  	//
   386  	// In general, code should call EscapedPath instead of reading RawPath.
   387  	RawPath string
   388  
   389  	// RawFragment is an optional field containing an encoded fragment hint.
   390  	// See the EscapedFragment method for more details.
   391  	//
   392  	// In general, code should call EscapedFragment instead of reading RawFragment.
   393  	RawFragment string
   394  
   395  	// ForceQuery indicates whether the original URL contained a query ('?') character.
   396  	// When set, the String method will include a trailing '?', even when RawQuery is empty.
   397  	ForceQuery bool
   398  
   399  	// OmitHost indicates the URL has an empty host (authority).
   400  	// When set, the String method will not include the host when it is empty.
   401  	OmitHost bool
   402  }
   403  
   404  // User returns a [Userinfo] containing the provided username
   405  // and no password set.
   406  func User(username string) *Userinfo {
   407  	return &Userinfo{username, "", false}
   408  }
   409  
   410  // UserPassword returns a [Userinfo] containing the provided username
   411  // and password.
   412  //
   413  // This functionality should only be used with legacy web sites.
   414  // RFC 2396 warns that interpreting Userinfo this way
   415  // “is NOT RECOMMENDED, because the passing of authentication
   416  // information in clear text (such as URI) has proven to be a
   417  // security risk in almost every case where it has been used.”
   418  func UserPassword(username, password string) *Userinfo {
   419  	return &Userinfo{username, password, true}
   420  }
   421  
   422  // The Userinfo type is an immutable encapsulation of username and
   423  // password details for a [URL]. An existing Userinfo value is guaranteed
   424  // to have a username set (potentially empty, as allowed by RFC 2396),
   425  // and optionally a password.
   426  type Userinfo struct {
   427  	username    string
   428  	password    string
   429  	passwordSet bool
   430  }
   431  
   432  // Username returns the username.
   433  func (u *Userinfo) Username() string {
   434  	if u == nil {
   435  		return ""
   436  	}
   437  	return u.username
   438  }
   439  
   440  // Password returns the password in case it is set, and whether it is set.
   441  func (u *Userinfo) Password() (string, bool) {
   442  	if u == nil {
   443  		return "", false
   444  	}
   445  	return u.password, u.passwordSet
   446  }
   447  
   448  // String returns the encoded userinfo information in the standard form
   449  // of "username[:password]".
   450  func (u *Userinfo) String() string {
   451  	if u == nil {
   452  		return ""
   453  	}
   454  	s := escape(u.username, encodeUserPassword)
   455  	if u.passwordSet {
   456  		s += ":" + escape(u.password, encodeUserPassword)
   457  	}
   458  	return s
   459  }
   460  
   461  // Maybe rawURL is of the form scheme:path.
   462  // (Scheme must be [a-zA-Z][a-zA-Z0-9+.-]*)
   463  // If so, return scheme, path; else return "", rawURL.
   464  func getScheme(rawURL string) (scheme, path string, err error) {
   465  	for i := 0; i < len(rawURL); i++ {
   466  		c := rawURL[i]
   467  		switch {
   468  		case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z':
   469  		// do nothing
   470  		case '0' <= c && c <= '9' || c == '+' || c == '-' || c == '.':
   471  			if i == 0 {
   472  				return "", rawURL, nil
   473  			}
   474  		case c == ':':
   475  			if i == 0 {
   476  				return "", "", errors.New("missing protocol scheme")
   477  			}
   478  			return rawURL[:i], rawURL[i+1:], nil
   479  		default:
   480  			// we have encountered an invalid character,
   481  			// so there is no valid scheme
   482  			return "", rawURL, nil
   483  		}
   484  	}
   485  	return "", rawURL, nil
   486  }
   487  
   488  // Parse parses a raw url into a [URL] structure.
   489  //
   490  // The url may be relative (a path, without a host) or absolute
   491  // (starting with a scheme). Trying to parse a hostname and path
   492  // without a scheme is invalid but may not necessarily return an
   493  // error, due to parsing ambiguities.
   494  func Parse(rawURL string) (*URL, error) {
   495  	// Cut off #frag
   496  	u, frag, _ := strings.Cut(rawURL, "#")
   497  	url, err := parse(u, false)
   498  	if err != nil {
   499  		return nil, &Error{"parse", u, err}
   500  	}
   501  	if frag == "" {
   502  		return url, nil
   503  	}
   504  	if err = url.setFragment(frag); err != nil {
   505  		return nil, &Error{"parse", rawURL, err}
   506  	}
   507  	return url, nil
   508  }
   509  
   510  // ParseRequestURI parses a raw url into a [URL] structure. It assumes that
   511  // url was received in an HTTP request, so the url is interpreted
   512  // only as an absolute URI or an absolute path.
   513  // The string url is assumed not to have a #fragment suffix.
   514  // (Web browsers strip #fragment before sending the URL to a web server.)
   515  func ParseRequestURI(rawURL string) (*URL, error) {
   516  	url, err := parse(rawURL, true)
   517  	if err != nil {
   518  		return nil, &Error{"parse", rawURL, err}
   519  	}
   520  	return url, nil
   521  }
   522  
   523  // parse parses a URL from a string in one of two contexts. If
   524  // viaRequest is true, the URL is assumed to have arrived via an HTTP request,
   525  // in which case only absolute URLs or path-absolute relative URLs are allowed.
   526  // If viaRequest is false, all forms of relative URLs are allowed.
   527  func parse(rawURL string, viaRequest bool) (*URL, error) {
   528  	var rest string
   529  	var err error
   530  
   531  	if stringContainsCTLByte(rawURL) {
   532  		return nil, errors.New("net/url: invalid control character in URL")
   533  	}
   534  
   535  	if rawURL == "" && viaRequest {
   536  		return nil, errors.New("empty url")
   537  	}
   538  	url := new(URL)
   539  
   540  	if rawURL == "*" {
   541  		url.Path = "*"
   542  		return url, nil
   543  	}
   544  
   545  	// Split off possible leading "http:", "mailto:", etc.
   546  	// Cannot contain escaped characters.
   547  	if url.Scheme, rest, err = getScheme(rawURL); err != nil {
   548  		return nil, err
   549  	}
   550  	url.Scheme = strings.ToLower(url.Scheme)
   551  
   552  	if strings.HasSuffix(rest, "?") && strings.Count(rest, "?") == 1 {
   553  		url.ForceQuery = true
   554  		rest = rest[:len(rest)-1]
   555  	} else {
   556  		rest, url.RawQuery, _ = strings.Cut(rest, "?")
   557  	}
   558  
   559  	if !strings.HasPrefix(rest, "/") {
   560  		if url.Scheme != "" {
   561  			// We consider rootless paths per RFC 3986 as opaque.
   562  			url.Opaque = rest
   563  			return url, nil
   564  		}
   565  		if viaRequest {
   566  			return nil, errors.New("invalid URI for request")
   567  		}
   568  
   569  		// Avoid confusion with malformed schemes, like cache_object:foo/bar.
   570  		// See golang.org/issue/16822.
   571  		//
   572  		// RFC 3986, §3.3:
   573  		// In addition, a URI reference (Section 4.1) may be a relative-path reference,
   574  		// in which case the first path segment cannot contain a colon (":") character.
   575  		if segment, _, _ := strings.Cut(rest, "/"); strings.Contains(segment, ":") {
   576  			// First path segment has colon. Not allowed in relative URL.
   577  			return nil, errors.New("first path segment in URL cannot contain colon")
   578  		}
   579  	}
   580  
   581  	if (url.Scheme != "" || !viaRequest && !strings.HasPrefix(rest, "///")) && strings.HasPrefix(rest, "//") {
   582  		var authority string
   583  		authority, rest = rest[2:], ""
   584  		if i := strings.Index(authority, "/"); i >= 0 {
   585  			authority, rest = authority[:i], authority[i:]
   586  		}
   587  		url.User, url.Host, err = parseAuthority(authority)
   588  		if err != nil {
   589  			return nil, err
   590  		}
   591  	} else if url.Scheme != "" && strings.HasPrefix(rest, "/") {
   592  		// OmitHost is set to true when rawURL has an empty host (authority).
   593  		// See golang.org/issue/46059.
   594  		url.OmitHost = true
   595  	}
   596  
   597  	// Set Path and, optionally, RawPath.
   598  	// RawPath is a hint of the encoding of Path. We don't want to set it if
   599  	// the default escaping of Path is equivalent, to help make sure that people
   600  	// don't rely on it in general.
   601  	if err := url.setPath(rest); err != nil {
   602  		return nil, err
   603  	}
   604  	return url, nil
   605  }
   606  
   607  func parseAuthority(authority string) (user *Userinfo, host string, err error) {
   608  	i := strings.LastIndex(authority, "@")
   609  	if i < 0 {
   610  		host, err = parseHost(authority)
   611  	} else {
   612  		host, err = parseHost(authority[i+1:])
   613  	}
   614  	if err != nil {
   615  		return nil, "", err
   616  	}
   617  	if i < 0 {
   618  		return nil, host, nil
   619  	}
   620  	userinfo := authority[:i]
   621  	if !validUserinfo(userinfo) {
   622  		return nil, "", errors.New("net/url: invalid userinfo")
   623  	}
   624  	if !strings.Contains(userinfo, ":") {
   625  		if userinfo, err = unescape(userinfo, encodeUserPassword); err != nil {
   626  			return nil, "", err
   627  		}
   628  		user = User(userinfo)
   629  	} else {
   630  		username, password, _ := strings.Cut(userinfo, ":")
   631  		if username, err = unescape(username, encodeUserPassword); err != nil {
   632  			return nil, "", err
   633  		}
   634  		if password, err = unescape(password, encodeUserPassword); err != nil {
   635  			return nil, "", err
   636  		}
   637  		user = UserPassword(username, password)
   638  	}
   639  	return user, host, nil
   640  }
   641  
   642  // parseHost parses host as an authority without user
   643  // information. That is, as host[:port].
   644  func parseHost(host string) (string, error) {
   645  	if openBracketIdx := strings.LastIndex(host, "["); openBracketIdx != -1 {
   646  		// Parse an IP-Literal in RFC 3986 and RFC 6874.
   647  		// E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
   648  		closeBracketIdx := strings.LastIndex(host, "]")
   649  		if closeBracketIdx < 0 {
   650  			return "", errors.New("missing ']' in host")
   651  		}
   652  
   653  		colonPort := host[closeBracketIdx+1:]
   654  		if !validOptionalPort(colonPort) {
   655  			return "", fmt.Errorf("invalid port %q after host", colonPort)
   656  		}
   657  		unescapedColonPort, err := unescape(colonPort, encodeHost)
   658  		if err != nil {
   659  			return "", err
   660  		}
   661  
   662  		hostname := host[openBracketIdx+1 : closeBracketIdx]
   663  		var unescapedHostname string
   664  		// RFC 6874 defines that %25 (%-encoded percent) introduces
   665  		// the zone identifier, and the zone identifier can use basically
   666  		// any %-encoding it likes. That's different from the host, which
   667  		// can only %-encode non-ASCII bytes.
   668  		// We do impose some restrictions on the zone, to avoid stupidity
   669  		// like newlines.
   670  		zoneIdx := strings.Index(hostname, "%25")
   671  		if zoneIdx >= 0 {
   672  			hostPart, err := unescape(hostname[:zoneIdx], encodeHost)
   673  			if err != nil {
   674  				return "", err
   675  			}
   676  			zonePart, err := unescape(hostname[zoneIdx:], encodeZone)
   677  			if err != nil {
   678  				return "", err
   679  			}
   680  			unescapedHostname = hostPart + zonePart
   681  		} else {
   682  			var err error
   683  			unescapedHostname, err = unescape(hostname, encodeHost)
   684  			if err != nil {
   685  				return "", err
   686  			}
   687  		}
   688  
   689  		// Per RFC 3986, only a host identified by a valid
   690  		// IPv6 address can be enclosed by square brackets.
   691  		// This excludes any IPv4, but notably not IPv4-mapped addresses.
   692  		addr, err := netip.ParseAddr(unescapedHostname)
   693  		if err != nil {
   694  			return "", fmt.Errorf("invalid host: %w", err)
   695  		}
   696  		if addr.Is4() {
   697  			return "", errors.New("invalid IP-literal")
   698  		}
   699  		return "[" + unescapedHostname + "]" + unescapedColonPort, nil
   700  	} else if i := strings.LastIndex(host, ":"); i != -1 {
   701  		colonPort := host[i:]
   702  		if !validOptionalPort(colonPort) {
   703  			return "", fmt.Errorf("invalid port %q after host", colonPort)
   704  		}
   705  	}
   706  
   707  	var err error
   708  	if host, err = unescape(host, encodeHost); err != nil {
   709  		return "", err
   710  	}
   711  	return host, nil
   712  }
   713  
   714  // setPath sets the Path and RawPath fields of the URL based on the provided
   715  // escaped path p. It maintains the invariant that RawPath is only specified
   716  // when it differs from the default encoding of the path.
   717  // For example:
   718  // - setPath("/foo/bar")   will set Path="/foo/bar" and RawPath=""
   719  // - setPath("/foo%2fbar") will set Path="/foo/bar" and RawPath="/foo%2fbar"
   720  // setPath will return an error only if the provided path contains an invalid
   721  // escaping.
   722  //
   723  // setPath should be an internal detail,
   724  // but widely used packages access it using linkname.
   725  // Notable members of the hall of shame include:
   726  //   - github.com/sagernet/sing
   727  //
   728  // Do not remove or change the type signature.
   729  // See go.dev/issue/67401.
   730  //
   731  //go:linkname badSetPath net/url.(*URL).setPath
   732  func (u *URL) setPath(p string) error {
   733  	path, err := unescape(p, encodePath)
   734  	if err != nil {
   735  		return err
   736  	}
   737  	u.Path = path
   738  	if escp := escape(path, encodePath); p == escp {
   739  		// Default encoding is fine.
   740  		u.RawPath = ""
   741  	} else {
   742  		u.RawPath = p
   743  	}
   744  	return nil
   745  }
   746  
   747  // for linkname because we cannot linkname methods directly
   748  func badSetPath(*URL, string) error
   749  
   750  // EscapedPath returns the escaped form of u.Path.
   751  // In general there are multiple possible escaped forms of any path.
   752  // EscapedPath returns u.RawPath when it is a valid escaping of u.Path.
   753  // Otherwise EscapedPath ignores u.RawPath and computes an escaped
   754  // form on its own.
   755  // The [URL.String] and [URL.RequestURI] methods use EscapedPath to construct
   756  // their results.
   757  // In general, code should call EscapedPath instead of
   758  // reading u.RawPath directly.
   759  func (u *URL) EscapedPath() string {
   760  	if u.RawPath != "" && validEncoded(u.RawPath, encodePath) {
   761  		p, err := unescape(u.RawPath, encodePath)
   762  		if err == nil && p == u.Path {
   763  			return u.RawPath
   764  		}
   765  	}
   766  	if u.Path == "*" {
   767  		return "*" // don't escape (Issue 11202)
   768  	}
   769  	return escape(u.Path, encodePath)
   770  }
   771  
   772  // validEncoded reports whether s is a valid encoded path or fragment,
   773  // according to mode.
   774  // It must not contain any bytes that require escaping during encoding.
   775  func validEncoded(s string, mode encoding) bool {
   776  	for i := 0; i < len(s); i++ {
   777  		// RFC 3986, Appendix A.
   778  		// pchar = unreserved / pct-encoded / sub-delims / ":" / "@".
   779  		// shouldEscape is not quite compliant with the RFC,
   780  		// so we check the sub-delims ourselves and let
   781  		// shouldEscape handle the others.
   782  		switch s[i] {
   783  		case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '@':
   784  			// ok
   785  		case '[', ']':
   786  			// ok - not specified in RFC 3986 but left alone by modern browsers
   787  		case '%':
   788  			// ok - percent encoded, will decode
   789  		default:
   790  			if shouldEscape(s[i], mode) {
   791  				return false
   792  			}
   793  		}
   794  	}
   795  	return true
   796  }
   797  
   798  // setFragment is like setPath but for Fragment/RawFragment.
   799  func (u *URL) setFragment(f string) error {
   800  	frag, err := unescape(f, encodeFragment)
   801  	if err != nil {
   802  		return err
   803  	}
   804  	u.Fragment = frag
   805  	if escf := escape(frag, encodeFragment); f == escf {
   806  		// Default encoding is fine.
   807  		u.RawFragment = ""
   808  	} else {
   809  		u.RawFragment = f
   810  	}
   811  	return nil
   812  }
   813  
   814  // EscapedFragment returns the escaped form of u.Fragment.
   815  // In general there are multiple possible escaped forms of any fragment.
   816  // EscapedFragment returns u.RawFragment when it is a valid escaping of u.Fragment.
   817  // Otherwise EscapedFragment ignores u.RawFragment and computes an escaped
   818  // form on its own.
   819  // The [URL.String] method uses EscapedFragment to construct its result.
   820  // In general, code should call EscapedFragment instead of
   821  // reading u.RawFragment directly.
   822  func (u *URL) EscapedFragment() string {
   823  	if u.RawFragment != "" && validEncoded(u.RawFragment, encodeFragment) {
   824  		f, err := unescape(u.RawFragment, encodeFragment)
   825  		if err == nil && f == u.Fragment {
   826  			return u.RawFragment
   827  		}
   828  	}
   829  	return escape(u.Fragment, encodeFragment)
   830  }
   831  
   832  // validOptionalPort reports whether port is either an empty string
   833  // or matches /^:\d*$/
   834  func validOptionalPort(port string) bool {
   835  	if port == "" {
   836  		return true
   837  	}
   838  	if port[0] != ':' {
   839  		return false
   840  	}
   841  	for _, b := range port[1:] {
   842  		if b < '0' || b > '9' {
   843  			return false
   844  		}
   845  	}
   846  	return true
   847  }
   848  
   849  // String reassembles the [URL] into a valid URL string.
   850  // The general form of the result is one of:
   851  //
   852  //	scheme:opaque?query#fragment
   853  //	scheme://userinfo@host/path?query#fragment
   854  //
   855  // If u.Opaque is non-empty, String uses the first form;
   856  // otherwise it uses the second form.
   857  // Any non-ASCII characters in host are escaped.
   858  // To obtain the path, String uses u.EscapedPath().
   859  //
   860  // In the second form, the following rules apply:
   861  //   - if u.Scheme is empty, scheme: is omitted.
   862  //   - if u.User is nil, userinfo@ is omitted.
   863  //   - if u.Host is empty, host/ is omitted.
   864  //   - if u.Scheme and u.Host are empty and u.User is nil,
   865  //     the entire scheme://userinfo@host/ is omitted.
   866  //   - if u.Host is non-empty and u.Path begins with a /,
   867  //     the form host/path does not add its own /.
   868  //   - if u.RawQuery is empty, ?query is omitted.
   869  //   - if u.Fragment is empty, #fragment is omitted.
   870  func (u *URL) String() string {
   871  	var buf strings.Builder
   872  
   873  	n := len(u.Scheme)
   874  	if u.Opaque != "" {
   875  		n += len(u.Opaque)
   876  	} else {
   877  		if !u.OmitHost && (u.Scheme != "" || u.Host != "" || u.User != nil) {
   878  			username := u.User.Username()
   879  			password, _ := u.User.Password()
   880  			n += len(username) + len(password) + len(u.Host)
   881  		}
   882  		n += len(u.Path)
   883  	}
   884  	n += len(u.RawQuery) + len(u.RawFragment)
   885  	n += len(":" + "//" + "//" + ":" + "@" + "/" + "./" + "?" + "#")
   886  	buf.Grow(n)
   887  
   888  	if u.Scheme != "" {
   889  		buf.WriteString(u.Scheme)
   890  		buf.WriteByte(':')
   891  	}
   892  	if u.Opaque != "" {
   893  		buf.WriteString(u.Opaque)
   894  	} else {
   895  		if u.Scheme != "" || u.Host != "" || u.User != nil {
   896  			if u.OmitHost && u.Host == "" && u.User == nil {
   897  				// omit empty host
   898  			} else {
   899  				if u.Host != "" || u.Path != "" || u.User != nil {
   900  					buf.WriteString("//")
   901  				}
   902  				if ui := u.User; ui != nil {
   903  					buf.WriteString(ui.String())
   904  					buf.WriteByte('@')
   905  				}
   906  				if h := u.Host; h != "" {
   907  					buf.WriteString(escape(h, encodeHost))
   908  				}
   909  			}
   910  		}
   911  		path := u.EscapedPath()
   912  		if path != "" && path[0] != '/' && u.Host != "" {
   913  			buf.WriteByte('/')
   914  		}
   915  		if buf.Len() == 0 {
   916  			// RFC 3986 §4.2
   917  			// A path segment that contains a colon character (e.g., "this:that")
   918  			// cannot be used as the first segment of a relative-path reference, as
   919  			// it would be mistaken for a scheme name. Such a segment must be
   920  			// preceded by a dot-segment (e.g., "./this:that") to make a relative-
   921  			// path reference.
   922  			if segment, _, _ := strings.Cut(path, "/"); strings.Contains(segment, ":") {
   923  				buf.WriteString("./")
   924  			}
   925  		}
   926  		buf.WriteString(path)
   927  	}
   928  	if u.ForceQuery || u.RawQuery != "" {
   929  		buf.WriteByte('?')
   930  		buf.WriteString(u.RawQuery)
   931  	}
   932  	if u.Fragment != "" {
   933  		buf.WriteByte('#')
   934  		buf.WriteString(u.EscapedFragment())
   935  	}
   936  	return buf.String()
   937  }
   938  
   939  // Redacted is like [URL.String] but replaces any password with "xxxxx".
   940  // Only the password in u.User is redacted.
   941  func (u *URL) Redacted() string {
   942  	if u == nil {
   943  		return ""
   944  	}
   945  
   946  	ru := *u
   947  	if _, has := ru.User.Password(); has {
   948  		ru.User = UserPassword(ru.User.Username(), "xxxxx")
   949  	}
   950  	return ru.String()
   951  }
   952  
   953  // Values maps a string key to a list of values.
   954  // It is typically used for query parameters and form values.
   955  // Unlike in the http.Header map, the keys in a Values map
   956  // are case-sensitive.
   957  type Values map[string][]string
   958  
   959  // Get gets the first value associated with the given key.
   960  // If there are no values associated with the key, Get returns
   961  // the empty string. To access multiple values, use the map
   962  // directly.
   963  func (v Values) Get(key string) string {
   964  	vs := v[key]
   965  	if len(vs) == 0 {
   966  		return ""
   967  	}
   968  	return vs[0]
   969  }
   970  
   971  // Set sets the key to value. It replaces any existing
   972  // values.
   973  func (v Values) Set(key, value string) {
   974  	v[key] = []string{value}
   975  }
   976  
   977  // Add adds the value to key. It appends to any existing
   978  // values associated with key.
   979  func (v Values) Add(key, value string) {
   980  	v[key] = append(v[key], value)
   981  }
   982  
   983  // Del deletes the values associated with key.
   984  func (v Values) Del(key string) {
   985  	delete(v, key)
   986  }
   987  
   988  // Has checks whether a given key is set.
   989  func (v Values) Has(key string) bool {
   990  	_, ok := v[key]
   991  	return ok
   992  }
   993  
   994  // ParseQuery parses the URL-encoded query string and returns
   995  // a map listing the values specified for each key.
   996  // ParseQuery always returns a non-nil map containing all the
   997  // valid query parameters found; err describes the first decoding error
   998  // encountered, if any.
   999  //
  1000  // Query is expected to be a list of key=value settings separated by ampersands.
  1001  // A setting without an equals sign is interpreted as a key set to an empty
  1002  // value.
  1003  // Settings containing a non-URL-encoded semicolon are considered invalid.
  1004  func ParseQuery(query string) (Values, error) {
  1005  	m := make(Values)
  1006  	err := parseQuery(m, query)
  1007  	return m, err
  1008  }
  1009  
  1010  func parseQuery(m Values, query string) (err error) {
  1011  	for query != "" {
  1012  		var key string
  1013  		key, query, _ = strings.Cut(query, "&")
  1014  		if strings.Contains(key, ";") {
  1015  			err = fmt.Errorf("invalid semicolon separator in query")
  1016  			continue
  1017  		}
  1018  		if key == "" {
  1019  			continue
  1020  		}
  1021  		key, value, _ := strings.Cut(key, "=")
  1022  		key, err1 := QueryUnescape(key)
  1023  		if err1 != nil {
  1024  			if err == nil {
  1025  				err = err1
  1026  			}
  1027  			continue
  1028  		}
  1029  		value, err1 = QueryUnescape(value)
  1030  		if err1 != nil {
  1031  			if err == nil {
  1032  				err = err1
  1033  			}
  1034  			continue
  1035  		}
  1036  		m[key] = append(m[key], value)
  1037  	}
  1038  	return err
  1039  }
  1040  
  1041  // Encode encodes the values into “URL encoded” form
  1042  // ("bar=baz&foo=quux") sorted by key.
  1043  func (v Values) Encode() string {
  1044  	if len(v) == 0 {
  1045  		return ""
  1046  	}
  1047  	var buf strings.Builder
  1048  	// To minimize allocations, we eschew iterators and pre-size the slice in
  1049  	// which we collect v's keys.
  1050  	keys := make([]string, len(v))
  1051  	var i int
  1052  	for k := range v {
  1053  		keys[i] = k
  1054  		i++
  1055  	}
  1056  	slices.Sort(keys)
  1057  	for _, k := range keys {
  1058  		vs := v[k]
  1059  		keyEscaped := QueryEscape(k)
  1060  		for _, v := range vs {
  1061  			if buf.Len() > 0 {
  1062  				buf.WriteByte('&')
  1063  			}
  1064  			buf.WriteString(keyEscaped)
  1065  			buf.WriteByte('=')
  1066  			buf.WriteString(QueryEscape(v))
  1067  		}
  1068  	}
  1069  	return buf.String()
  1070  }
  1071  
  1072  // resolvePath applies special path segments from refs and applies
  1073  // them to base, per RFC 3986.
  1074  func resolvePath(base, ref string) string {
  1075  	var full string
  1076  	if ref == "" {
  1077  		full = base
  1078  	} else if ref[0] != '/' {
  1079  		i := strings.LastIndex(base, "/")
  1080  		full = base[:i+1] + ref
  1081  	} else {
  1082  		full = ref
  1083  	}
  1084  	if full == "" {
  1085  		return ""
  1086  	}
  1087  
  1088  	var (
  1089  		elem string
  1090  		dst  strings.Builder
  1091  	)
  1092  	first := true
  1093  	remaining := full
  1094  	// We want to return a leading '/', so write it now.
  1095  	dst.WriteByte('/')
  1096  	found := true
  1097  	for found {
  1098  		elem, remaining, found = strings.Cut(remaining, "/")
  1099  		if elem == "." {
  1100  			first = false
  1101  			// drop
  1102  			continue
  1103  		}
  1104  
  1105  		if elem == ".." {
  1106  			// Ignore the leading '/' we already wrote.
  1107  			str := dst.String()[1:]
  1108  			index := strings.LastIndexByte(str, '/')
  1109  
  1110  			dst.Reset()
  1111  			dst.WriteByte('/')
  1112  			if index == -1 {
  1113  				first = true
  1114  			} else {
  1115  				dst.WriteString(str[:index])
  1116  			}
  1117  		} else {
  1118  			if !first {
  1119  				dst.WriteByte('/')
  1120  			}
  1121  			dst.WriteString(elem)
  1122  			first = false
  1123  		}
  1124  	}
  1125  
  1126  	if elem == "." || elem == ".." {
  1127  		dst.WriteByte('/')
  1128  	}
  1129  
  1130  	// We wrote an initial '/', but we don't want two.
  1131  	r := dst.String()
  1132  	if len(r) > 1 && r[1] == '/' {
  1133  		r = r[1:]
  1134  	}
  1135  	return r
  1136  }
  1137  
  1138  // IsAbs reports whether the [URL] is absolute.
  1139  // Absolute means that it has a non-empty scheme.
  1140  func (u *URL) IsAbs() bool {
  1141  	return u.Scheme != ""
  1142  }
  1143  
  1144  // Parse parses a [URL] in the context of the receiver. The provided URL
  1145  // may be relative or absolute. Parse returns nil, err on parse
  1146  // failure, otherwise its return value is the same as [URL.ResolveReference].
  1147  func (u *URL) Parse(ref string) (*URL, error) {
  1148  	refURL, err := Parse(ref)
  1149  	if err != nil {
  1150  		return nil, err
  1151  	}
  1152  	return u.ResolveReference(refURL), nil
  1153  }
  1154  
  1155  // ResolveReference resolves a URI reference to an absolute URI from
  1156  // an absolute base URI u, per RFC 3986 Section 5.2. The URI reference
  1157  // may be relative or absolute. ResolveReference always returns a new
  1158  // [URL] instance, even if the returned URL is identical to either the
  1159  // base or reference. If ref is an absolute URL, then ResolveReference
  1160  // ignores base and returns a copy of ref.
  1161  func (u *URL) ResolveReference(ref *URL) *URL {
  1162  	url := *ref
  1163  	if ref.Scheme == "" {
  1164  		url.Scheme = u.Scheme
  1165  	}
  1166  	if ref.Scheme != "" || ref.Host != "" || ref.User != nil {
  1167  		// The "absoluteURI" or "net_path" cases.
  1168  		// We can ignore the error from setPath since we know we provided a
  1169  		// validly-escaped path.
  1170  		url.setPath(resolvePath(ref.EscapedPath(), ""))
  1171  		return &url
  1172  	}
  1173  	if ref.Opaque != "" {
  1174  		url.User = nil
  1175  		url.Host = ""
  1176  		url.Path = ""
  1177  		return &url
  1178  	}
  1179  	if ref.Path == "" && !ref.ForceQuery && ref.RawQuery == "" {
  1180  		url.RawQuery = u.RawQuery
  1181  		if ref.Fragment == "" {
  1182  			url.Fragment = u.Fragment
  1183  			url.RawFragment = u.RawFragment
  1184  		}
  1185  	}
  1186  	if ref.Path == "" && u.Opaque != "" {
  1187  		url.Opaque = u.Opaque
  1188  		url.User = nil
  1189  		url.Host = ""
  1190  		url.Path = ""
  1191  		return &url
  1192  	}
  1193  	// The "abs_path" or "rel_path" cases.
  1194  	url.Host = u.Host
  1195  	url.User = u.User
  1196  	url.setPath(resolvePath(u.EscapedPath(), ref.EscapedPath()))
  1197  	return &url
  1198  }
  1199  
  1200  // Query parses RawQuery and returns the corresponding values.
  1201  // It silently discards malformed value pairs.
  1202  // To check errors use [ParseQuery].
  1203  func (u *URL) Query() Values {
  1204  	v, _ := ParseQuery(u.RawQuery)
  1205  	return v
  1206  }
  1207  
  1208  // RequestURI returns the encoded path?query or opaque?query
  1209  // string that would be used in an HTTP request for u.
  1210  func (u *URL) RequestURI() string {
  1211  	result := u.Opaque
  1212  	if result == "" {
  1213  		result = u.EscapedPath()
  1214  		if result == "" {
  1215  			result = "/"
  1216  		}
  1217  	} else {
  1218  		if strings.HasPrefix(result, "//") {
  1219  			result = u.Scheme + ":" + result
  1220  		}
  1221  	}
  1222  	if u.ForceQuery || u.RawQuery != "" {
  1223  		result += "?" + u.RawQuery
  1224  	}
  1225  	return result
  1226  }
  1227  
  1228  // Hostname returns u.Host, stripping any valid port number if present.
  1229  //
  1230  // If the result is enclosed in square brackets, as literal IPv6 addresses are,
  1231  // the square brackets are removed from the result.
  1232  func (u *URL) Hostname() string {
  1233  	host, _ := splitHostPort(u.Host)
  1234  	return host
  1235  }
  1236  
  1237  // Port returns the port part of u.Host, without the leading colon.
  1238  //
  1239  // If u.Host doesn't contain a valid numeric port, Port returns an empty string.
  1240  func (u *URL) Port() string {
  1241  	_, port := splitHostPort(u.Host)
  1242  	return port
  1243  }
  1244  
  1245  // splitHostPort separates host and port. If the port is not valid, it returns
  1246  // the entire input as host, and it doesn't check the validity of the host.
  1247  // Unlike net.SplitHostPort, but per RFC 3986, it requires ports to be numeric.
  1248  func splitHostPort(hostPort string) (host, port string) {
  1249  	host = hostPort
  1250  
  1251  	colon := strings.LastIndexByte(host, ':')
  1252  	if colon != -1 && validOptionalPort(host[colon:]) {
  1253  		host, port = host[:colon], host[colon+1:]
  1254  	}
  1255  
  1256  	if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
  1257  		host = host[1 : len(host)-1]
  1258  	}
  1259  
  1260  	return
  1261  }
  1262  
  1263  // Marshaling interface implementations.
  1264  // Would like to implement MarshalText/UnmarshalText but that will change the JSON representation of URLs.
  1265  
  1266  func (u *URL) MarshalBinary() (text []byte, err error) {
  1267  	return u.AppendBinary(nil)
  1268  }
  1269  
  1270  func (u *URL) AppendBinary(b []byte) ([]byte, error) {
  1271  	return append(b, u.String()...), nil
  1272  }
  1273  
  1274  func (u *URL) UnmarshalBinary(text []byte) error {
  1275  	u1, err := Parse(string(text))
  1276  	if err != nil {
  1277  		return err
  1278  	}
  1279  	*u = *u1
  1280  	return nil
  1281  }
  1282  
  1283  // JoinPath returns a new [URL] with the provided path elements joined to
  1284  // any existing path and the resulting path cleaned of any ./ or ../ elements.
  1285  // Any sequences of multiple / characters will be reduced to a single /.
  1286  func (u *URL) JoinPath(elem ...string) *URL {
  1287  	elem = append([]string{u.EscapedPath()}, elem...)
  1288  	var p string
  1289  	if !strings.HasPrefix(elem[0], "/") {
  1290  		// Return a relative path if u is relative,
  1291  		// but ensure that it contains no ../ elements.
  1292  		elem[0] = "/" + elem[0]
  1293  		p = path.Join(elem...)[1:]
  1294  	} else {
  1295  		p = path.Join(elem...)
  1296  	}
  1297  	// path.Join will remove any trailing slashes.
  1298  	// Preserve at least one.
  1299  	if strings.HasSuffix(elem[len(elem)-1], "/") && !strings.HasSuffix(p, "/") {
  1300  		p += "/"
  1301  	}
  1302  	url := *u
  1303  	url.setPath(p)
  1304  	return &url
  1305  }
  1306  
  1307  // validUserinfo reports whether s is a valid userinfo string per RFC 3986
  1308  // Section 3.2.1:
  1309  //
  1310  //	userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
  1311  //	unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
  1312  //	sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
  1313  //	              / "*" / "+" / "," / ";" / "="
  1314  //
  1315  // It doesn't validate pct-encoded. The caller does that via func unescape.
  1316  func validUserinfo(s string) bool {
  1317  	for _, r := range s {
  1318  		if 'A' <= r && r <= 'Z' {
  1319  			continue
  1320  		}
  1321  		if 'a' <= r && r <= 'z' {
  1322  			continue
  1323  		}
  1324  		if '0' <= r && r <= '9' {
  1325  			continue
  1326  		}
  1327  		switch r {
  1328  		case '-', '.', '_', ':', '~', '!', '$', '&', '\'',
  1329  			'(', ')', '*', '+', ',', ';', '=', '%':
  1330  			continue
  1331  		case '@':
  1332  			// `RFC 3986 section 3.2.1` does not allow '@' in userinfo.
  1333  			// It is a delimiter between userinfo and host.
  1334  			// However, URLs are diverse, and in some cases,
  1335  			// the userinfo may contain an '@' character,
  1336  			// for example, in "http://username:p@ssword@google.com",
  1337  			// the string "username:p@ssword" should be treated as valid userinfo.
  1338  			// Ref:
  1339  			//   https://go.dev/issue/3439
  1340  			//   https://go.dev/issue/22655
  1341  			continue
  1342  		default:
  1343  			return false
  1344  		}
  1345  	}
  1346  	return true
  1347  }
  1348  
  1349  // stringContainsCTLByte reports whether s contains any ASCII control character.
  1350  func stringContainsCTLByte(s string) bool {
  1351  	for i := 0; i < len(s); i++ {
  1352  		b := s[i]
  1353  		if b < ' ' || b == 0x7f {
  1354  			return true
  1355  		}
  1356  	}
  1357  	return false
  1358  }
  1359  
  1360  // JoinPath returns a [URL] string with the provided path elements joined to
  1361  // the existing path of base and the resulting path cleaned of any ./ or ../ elements.
  1362  func JoinPath(base string, elem ...string) (result string, err error) {
  1363  	url, err := Parse(base)
  1364  	if err != nil {
  1365  		return
  1366  	}
  1367  	result = url.JoinPath(elem...).String()
  1368  	return
  1369  }
  1370  

View as plain text