...
Run Format

Source file src/net/url/url.go

Documentation: net/url

     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  package url
     7  
     8  // See RFC 3986. This package generally follows RFC 3986, except where
     9  // it deviates for compatibility reasons. When sending changes, first
    10  // search old issues for history on decisions. Unit tests should also
    11  // contain references to issue numbers with details.
    12  
    13  import (
    14  	"errors"
    15  	"fmt"
    16  	"sort"
    17  	"strconv"
    18  	"strings"
    19  )
    20  
    21  // Error reports an error and the operation and URL that caused it.
    22  type Error struct {
    23  	Op  string
    24  	URL string
    25  	Err error
    26  }
    27  
    28  func (e *Error) Error() string { return e.Op + " " + e.URL + ": " + e.Err.Error() }
    29  
    30  type timeout interface {
    31  	Timeout() bool
    32  }
    33  
    34  func (e *Error) Timeout() bool {
    35  	t, ok := e.Err.(timeout)
    36  	return ok && t.Timeout()
    37  }
    38  
    39  type temporary interface {
    40  	Temporary() bool
    41  }
    42  
    43  func (e *Error) Temporary() bool {
    44  	t, ok := e.Err.(temporary)
    45  	return ok && t.Temporary()
    46  }
    47  
    48  func ishex(c byte) bool {
    49  	switch {
    50  	case '0' <= c && c <= '9':
    51  		return true
    52  	case 'a' <= c && c <= 'f':
    53  		return true
    54  	case 'A' <= c && c <= 'F':
    55  		return true
    56  	}
    57  	return false
    58  }
    59  
    60  func unhex(c byte) byte {
    61  	switch {
    62  	case '0' <= c && c <= '9':
    63  		return c - '0'
    64  	case 'a' <= c && c <= 'f':
    65  		return c - 'a' + 10
    66  	case 'A' <= c && c <= 'F':
    67  		return c - 'A' + 10
    68  	}
    69  	return 0
    70  }
    71  
    72  type encoding int
    73  
    74  const (
    75  	encodePath encoding = 1 + iota
    76  	encodePathSegment
    77  	encodeHost
    78  	encodeZone
    79  	encodeUserPassword
    80  	encodeQueryComponent
    81  	encodeFragment
    82  )
    83  
    84  type EscapeError string
    85  
    86  func (e EscapeError) Error() string {
    87  	return "invalid URL escape " + strconv.Quote(string(e))
    88  }
    89  
    90  type InvalidHostError string
    91  
    92  func (e InvalidHostError) Error() string {
    93  	return "invalid character " + strconv.Quote(string(e)) + " in host name"
    94  }
    95  
    96  // Return true if the specified character should be escaped when
    97  // appearing in a URL string, according to RFC 3986.
    98  //
    99  // Please be informed that for now shouldEscape does not check all
   100  // reserved characters correctly. See golang.org/issue/5684.
   101  func shouldEscape(c byte, mode encoding) bool {
   102  	// §2.3 Unreserved characters (alphanum)
   103  	if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' {
   104  		return false
   105  	}
   106  
   107  	if mode == encodeHost || mode == encodeZone {
   108  		// §3.2.2 Host allows
   109  		//	sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
   110  		// as part of reg-name.
   111  		// We add : because we include :port as part of host.
   112  		// We add [ ] because we include [ipv6]:port as part of host.
   113  		// We add < > because they're the only characters left that
   114  		// we could possibly allow, and Parse will reject them if we
   115  		// escape them (because hosts can't use %-encoding for
   116  		// ASCII bytes).
   117  		switch c {
   118  		case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '[', ']', '<', '>', '"':
   119  			return false
   120  		}
   121  	}
   122  
   123  	switch c {
   124  	case '-', '_', '.', '~': // §2.3 Unreserved characters (mark)
   125  		return false
   126  
   127  	case '$', '&', '+', ',', '/', ':', ';', '=', '?', '@': // §2.2 Reserved characters (reserved)
   128  		// Different sections of the URL allow a few of
   129  		// the reserved characters to appear unescaped.
   130  		switch mode {
   131  		case encodePath: // §3.3
   132  			// The RFC allows : @ & = + $ but saves / ; , for assigning
   133  			// meaning to individual path segments. This package
   134  			// only manipulates the path as a whole, so we allow those
   135  			// last three as well. That leaves only ? to escape.
   136  			return c == '?'
   137  
   138  		case encodePathSegment: // §3.3
   139  			// The RFC allows : @ & = + $ but saves / ; , for assigning
   140  			// meaning to individual path segments.
   141  			return c == '/' || c == ';' || c == ',' || c == '?'
   142  
   143  		case encodeUserPassword: // §3.2.1
   144  			// The RFC allows ';', ':', '&', '=', '+', '$', and ',' in
   145  			// userinfo, so we must escape only '@', '/', and '?'.
   146  			// The parsing of userinfo treats ':' as special so we must escape
   147  			// that too.
   148  			return c == '@' || c == '/' || c == '?' || c == ':'
   149  
   150  		case encodeQueryComponent: // §3.4
   151  			// The RFC reserves (so we must escape) everything.
   152  			return true
   153  
   154  		case encodeFragment: // §4.1
   155  			// The RFC text is silent but the grammar allows
   156  			// everything, so escape nothing.
   157  			return false
   158  		}
   159  	}
   160  
   161  	if mode == encodeFragment {
   162  		// RFC 3986 §2.2 allows not escaping sub-delims. A subset of sub-delims are
   163  		// included in reserved from RFC 2396 §2.2. The remaining sub-delims do not
   164  		// need to be escaped. To minimize potential breakage, we apply two restrictions:
   165  		// (1) we always escape sub-delims outside of the fragment, and (2) we always
   166  		// escape single quote to avoid breaking callers that had previously assumed that
   167  		// single quotes would be escaped. See issue #19917.
   168  		switch c {
   169  		case '!', '(', ')', '*':
   170  			return false
   171  		}
   172  	}
   173  
   174  	// Everything else must be escaped.
   175  	return true
   176  }
   177  
   178  // QueryUnescape does the inverse transformation of QueryEscape,
   179  // converting each 3-byte encoded substring of the form "%AB" into the
   180  // hex-decoded byte 0xAB.
   181  // It returns an error if any % is not followed by two hexadecimal
   182  // digits.
   183  func QueryUnescape(s string) (string, error) {
   184  	return unescape(s, encodeQueryComponent)
   185  }
   186  
   187  // PathUnescape does the inverse transformation of PathEscape,
   188  // converting each 3-byte encoded substring of the form "%AB" into the
   189  // hex-decoded byte 0xAB. It returns an error if any % is not followed
   190  // by two hexadecimal digits.
   191  //
   192  // PathUnescape is identical to QueryUnescape except that it does not
   193  // unescape '+' to ' ' (space).
   194  func PathUnescape(s string) (string, error) {
   195  	return unescape(s, encodePathSegment)
   196  }
   197  
   198  // unescape unescapes a string; the mode specifies
   199  // which section of the URL string is being unescaped.
   200  func unescape(s string, mode encoding) (string, error) {
   201  	// Count %, check that they're well-formed.
   202  	n := 0
   203  	hasPlus := false
   204  	for i := 0; i < len(s); {
   205  		switch s[i] {
   206  		case '%':
   207  			n++
   208  			if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
   209  				s = s[i:]
   210  				if len(s) > 3 {
   211  					s = s[:3]
   212  				}
   213  				return "", EscapeError(s)
   214  			}
   215  			// Per https://tools.ietf.org/html/rfc3986#page-21
   216  			// in the host component %-encoding can only be used
   217  			// for non-ASCII bytes.
   218  			// But https://tools.ietf.org/html/rfc6874#section-2
   219  			// introduces %25 being allowed to escape a percent sign
   220  			// in IPv6 scoped-address literals. Yay.
   221  			if mode == encodeHost && unhex(s[i+1]) < 8 && s[i:i+3] != "%25" {
   222  				return "", EscapeError(s[i : i+3])
   223  			}
   224  			if mode == encodeZone {
   225  				// RFC 6874 says basically "anything goes" for zone identifiers
   226  				// and that even non-ASCII can be redundantly escaped,
   227  				// but it seems prudent to restrict %-escaped bytes here to those
   228  				// that are valid host name bytes in their unescaped form.
   229  				// That is, you can use escaping in the zone identifier but not
   230  				// to introduce bytes you couldn't just write directly.
   231  				// But Windows puts spaces here! Yay.
   232  				v := unhex(s[i+1])<<4 | unhex(s[i+2])
   233  				if s[i:i+3] != "%25" && v != ' ' && shouldEscape(v, encodeHost) {
   234  					return "", EscapeError(s[i : i+3])
   235  				}
   236  			}
   237  			i += 3
   238  		case '+':
   239  			hasPlus = mode == encodeQueryComponent
   240  			i++
   241  		default:
   242  			if (mode == encodeHost || mode == encodeZone) && s[i] < 0x80 && shouldEscape(s[i], mode) {
   243  				return "", InvalidHostError(s[i : i+1])
   244  			}
   245  			i++
   246  		}
   247  	}
   248  
   249  	if n == 0 && !hasPlus {
   250  		return s, nil
   251  	}
   252  
   253  	t := make([]byte, len(s)-2*n)
   254  	j := 0
   255  	for i := 0; i < len(s); {
   256  		switch s[i] {
   257  		case '%':
   258  			t[j] = unhex(s[i+1])<<4 | unhex(s[i+2])
   259  			j++
   260  			i += 3
   261  		case '+':
   262  			if mode == encodeQueryComponent {
   263  				t[j] = ' '
   264  			} else {
   265  				t[j] = '+'
   266  			}
   267  			j++
   268  			i++
   269  		default:
   270  			t[j] = s[i]
   271  			j++
   272  			i++
   273  		}
   274  	}
   275  	return string(t), 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
   285  // inside a URL path segment.
   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] = "0123456789ABCDEF"[c>>4]
   336  			t[j+2] = "0123456789ABCDEF"[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  // Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/.
   357  // A consequence is that it is impossible to tell which slashes in the Path were
   358  // slashes in the raw URL and which were %2f. This distinction is rarely important,
   359  // but when it is, code must not use Path directly.
   360  // The Parse function sets both Path and RawPath in the URL it returns,
   361  // and URL's String method uses RawPath if it is a valid encoding of Path,
   362  // by calling the EscapedPath method.
   363  type URL struct {
   364  	Scheme     string
   365  	Opaque     string    // encoded opaque data
   366  	User       *Userinfo // username and password information
   367  	Host       string    // host or host:port
   368  	Path       string    // path (relative paths may omit leading slash)
   369  	RawPath    string    // encoded path hint (see EscapedPath method)
   370  	ForceQuery bool      // append a query ('?') even if RawQuery is empty
   371  	RawQuery   string    // encoded query values, without '?'
   372  	Fragment   string    // fragment for references, without '#'
   373  }
   374  
   375  // User returns a Userinfo containing the provided username
   376  // and no password set.
   377  func User(username string) *Userinfo {
   378  	return &Userinfo{username, "", false}
   379  }
   380  
   381  // UserPassword returns a Userinfo containing the provided username
   382  // and password.
   383  //
   384  // This functionality should only be used with legacy web sites.
   385  // RFC 2396 warns that interpreting Userinfo this way
   386  // ``is NOT RECOMMENDED, because the passing of authentication
   387  // information in clear text (such as URI) has proven to be a
   388  // security risk in almost every case where it has been used.''
   389  func UserPassword(username, password string) *Userinfo {
   390  	return &Userinfo{username, password, true}
   391  }
   392  
   393  // The Userinfo type is an immutable encapsulation of username and
   394  // password details for a URL. An existing Userinfo value is guaranteed
   395  // to have a username set (potentially empty, as allowed by RFC 2396),
   396  // and optionally a password.
   397  type Userinfo struct {
   398  	username    string
   399  	password    string
   400  	passwordSet bool
   401  }
   402  
   403  // Username returns the username.
   404  func (u *Userinfo) Username() string {
   405  	if u == nil {
   406  		return ""
   407  	}
   408  	return u.username
   409  }
   410  
   411  // Password returns the password in case it is set, and whether it is set.
   412  func (u *Userinfo) Password() (string, bool) {
   413  	if u == nil {
   414  		return "", false
   415  	}
   416  	return u.password, u.passwordSet
   417  }
   418  
   419  // String returns the encoded userinfo information in the standard form
   420  // of "username[:password]".
   421  func (u *Userinfo) String() string {
   422  	if u == nil {
   423  		return ""
   424  	}
   425  	s := escape(u.username, encodeUserPassword)
   426  	if u.passwordSet {
   427  		s += ":" + escape(u.password, encodeUserPassword)
   428  	}
   429  	return s
   430  }
   431  
   432  // Maybe rawurl is of the form scheme:path.
   433  // (Scheme must be [a-zA-Z][a-zA-Z0-9+-.]*)
   434  // If so, return scheme, path; else return "", rawurl.
   435  func getscheme(rawurl string) (scheme, path string, err error) {
   436  	for i := 0; i < len(rawurl); i++ {
   437  		c := rawurl[i]
   438  		switch {
   439  		case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z':
   440  		// do nothing
   441  		case '0' <= c && c <= '9' || c == '+' || c == '-' || c == '.':
   442  			if i == 0 {
   443  				return "", rawurl, nil
   444  			}
   445  		case c == ':':
   446  			if i == 0 {
   447  				return "", "", errors.New("missing protocol scheme")
   448  			}
   449  			return rawurl[:i], rawurl[i+1:], nil
   450  		default:
   451  			// we have encountered an invalid character,
   452  			// so there is no valid scheme
   453  			return "", rawurl, nil
   454  		}
   455  	}
   456  	return "", rawurl, nil
   457  }
   458  
   459  // Maybe s is of the form t c u.
   460  // If so, return t, c u (or t, u if cutc == true).
   461  // If not, return s, "".
   462  func split(s string, c string, cutc bool) (string, string) {
   463  	i := strings.Index(s, c)
   464  	if i < 0 {
   465  		return s, ""
   466  	}
   467  	if cutc {
   468  		return s[:i], s[i+len(c):]
   469  	}
   470  	return s[:i], s[i:]
   471  }
   472  
   473  // Parse parses rawurl into a URL structure.
   474  //
   475  // The rawurl may be relative (a path, without a host) or absolute
   476  // (starting with a scheme). Trying to parse a hostname and path
   477  // without a scheme is invalid but may not necessarily return an
   478  // error, due to parsing ambiguities.
   479  func Parse(rawurl string) (*URL, error) {
   480  	// Cut off #frag
   481  	u, frag := split(rawurl, "#", true)
   482  	url, err := parse(u, false)
   483  	if err != nil {
   484  		return nil, &Error{"parse", u, err}
   485  	}
   486  	if frag == "" {
   487  		return url, nil
   488  	}
   489  	if url.Fragment, err = unescape(frag, encodeFragment); err != nil {
   490  		return nil, &Error{"parse", rawurl, err}
   491  	}
   492  	return url, nil
   493  }
   494  
   495  // ParseRequestURI parses rawurl into a URL structure. It assumes that
   496  // rawurl was received in an HTTP request, so the rawurl is interpreted
   497  // only as an absolute URI or an absolute path.
   498  // The string rawurl is assumed not to have a #fragment suffix.
   499  // (Web browsers strip #fragment before sending the URL to a web server.)
   500  func ParseRequestURI(rawurl string) (*URL, error) {
   501  	url, err := parse(rawurl, true)
   502  	if err != nil {
   503  		return nil, &Error{"parse", rawurl, err}
   504  	}
   505  	return url, nil
   506  }
   507  
   508  // parse parses a URL from a string in one of two contexts. If
   509  // viaRequest is true, the URL is assumed to have arrived via an HTTP request,
   510  // in which case only absolute URLs or path-absolute relative URLs are allowed.
   511  // If viaRequest is false, all forms of relative URLs are allowed.
   512  func parse(rawurl string, viaRequest bool) (*URL, error) {
   513  	var rest string
   514  	var err error
   515  
   516  	if stringContainsCTLByte(rawurl) {
   517  		return nil, errors.New("net/url: invalid control character in URL")
   518  	}
   519  
   520  	if rawurl == "" && viaRequest {
   521  		return nil, errors.New("empty url")
   522  	}
   523  	url := new(URL)
   524  
   525  	if rawurl == "*" {
   526  		url.Path = "*"
   527  		return url, nil
   528  	}
   529  
   530  	// Split off possible leading "http:", "mailto:", etc.
   531  	// Cannot contain escaped characters.
   532  	if url.Scheme, rest, err = getscheme(rawurl); err != nil {
   533  		return nil, err
   534  	}
   535  	url.Scheme = strings.ToLower(url.Scheme)
   536  
   537  	if strings.HasSuffix(rest, "?") && strings.Count(rest, "?") == 1 {
   538  		url.ForceQuery = true
   539  		rest = rest[:len(rest)-1]
   540  	} else {
   541  		rest, url.RawQuery = split(rest, "?", true)
   542  	}
   543  
   544  	if !strings.HasPrefix(rest, "/") {
   545  		if url.Scheme != "" {
   546  			// We consider rootless paths per RFC 3986 as opaque.
   547  			url.Opaque = rest
   548  			return url, nil
   549  		}
   550  		if viaRequest {
   551  			return nil, errors.New("invalid URI for request")
   552  		}
   553  
   554  		// Avoid confusion with malformed schemes, like cache_object:foo/bar.
   555  		// See golang.org/issue/16822.
   556  		//
   557  		// RFC 3986, §3.3:
   558  		// In addition, a URI reference (Section 4.1) may be a relative-path reference,
   559  		// in which case the first path segment cannot contain a colon (":") character.
   560  		colon := strings.Index(rest, ":")
   561  		slash := strings.Index(rest, "/")
   562  		if colon >= 0 && (slash < 0 || colon < slash) {
   563  			// First path segment has colon. Not allowed in relative URL.
   564  			return nil, errors.New("first path segment in URL cannot contain colon")
   565  		}
   566  	}
   567  
   568  	if (url.Scheme != "" || !viaRequest && !strings.HasPrefix(rest, "///")) && strings.HasPrefix(rest, "//") {
   569  		var authority string
   570  		authority, rest = split(rest[2:], "/", false)
   571  		url.User, url.Host, err = parseAuthority(authority)
   572  		if err != nil {
   573  			return nil, err
   574  		}
   575  	}
   576  	// Set Path and, optionally, RawPath.
   577  	// RawPath is a hint of the encoding of Path. We don't want to set it if
   578  	// the default escaping of Path is equivalent, to help make sure that people
   579  	// don't rely on it in general.
   580  	if err := url.setPath(rest); err != nil {
   581  		return nil, err
   582  	}
   583  	return url, nil
   584  }
   585  
   586  func parseAuthority(authority string) (user *Userinfo, host string, err error) {
   587  	i := strings.LastIndex(authority, "@")
   588  	if i < 0 {
   589  		host, err = parseHost(authority)
   590  	} else {
   591  		host, err = parseHost(authority[i+1:])
   592  	}
   593  	if err != nil {
   594  		return nil, "", err
   595  	}
   596  	if i < 0 {
   597  		return nil, host, nil
   598  	}
   599  	userinfo := authority[:i]
   600  	if !validUserinfo(userinfo) {
   601  		return nil, "", errors.New("net/url: invalid userinfo")
   602  	}
   603  	if !strings.Contains(userinfo, ":") {
   604  		if userinfo, err = unescape(userinfo, encodeUserPassword); err != nil {
   605  			return nil, "", err
   606  		}
   607  		user = User(userinfo)
   608  	} else {
   609  		username, password := split(userinfo, ":", true)
   610  		if username, err = unescape(username, encodeUserPassword); err != nil {
   611  			return nil, "", err
   612  		}
   613  		if password, err = unescape(password, encodeUserPassword); err != nil {
   614  			return nil, "", err
   615  		}
   616  		user = UserPassword(username, password)
   617  	}
   618  	return user, host, nil
   619  }
   620  
   621  // parseHost parses host as an authority without user
   622  // information. That is, as host[:port].
   623  func parseHost(host string) (string, error) {
   624  	if strings.HasPrefix(host, "[") {
   625  		// Parse an IP-Literal in RFC 3986 and RFC 6874.
   626  		// E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
   627  		i := strings.LastIndex(host, "]")
   628  		if i < 0 {
   629  			return "", errors.New("missing ']' in host")
   630  		}
   631  		colonPort := host[i+1:]
   632  		if !validOptionalPort(colonPort) {
   633  			return "", fmt.Errorf("invalid port %q after host", colonPort)
   634  		}
   635  
   636  		// RFC 6874 defines that %25 (%-encoded percent) introduces
   637  		// the zone identifier, and the zone identifier can use basically
   638  		// any %-encoding it likes. That's different from the host, which
   639  		// can only %-encode non-ASCII bytes.
   640  		// We do impose some restrictions on the zone, to avoid stupidity
   641  		// like newlines.
   642  		zone := strings.Index(host[:i], "%25")
   643  		if zone >= 0 {
   644  			host1, err := unescape(host[:zone], encodeHost)
   645  			if err != nil {
   646  				return "", err
   647  			}
   648  			host2, err := unescape(host[zone:i], encodeZone)
   649  			if err != nil {
   650  				return "", err
   651  			}
   652  			host3, err := unescape(host[i:], encodeHost)
   653  			if err != nil {
   654  				return "", err
   655  			}
   656  			return host1 + host2 + host3, nil
   657  		}
   658  	}
   659  
   660  	var err error
   661  	if host, err = unescape(host, encodeHost); err != nil {
   662  		return "", err
   663  	}
   664  	return host, nil
   665  }
   666  
   667  // setPath sets the Path and RawPath fields of the URL based on the provided
   668  // escaped path p. It maintains the invariant that RawPath is only specified
   669  // when it differs from the default encoding of the path.
   670  // For example:
   671  // - setPath("/foo/bar")   will set Path="/foo/bar" and RawPath=""
   672  // - setPath("/foo%2fbar") will set Path="/foo/bar" and RawPath="/foo%2fbar"
   673  // setPath will return an error only if the provided path contains an invalid
   674  // escaping.
   675  func (u *URL) setPath(p string) error {
   676  	path, err := unescape(p, encodePath)
   677  	if err != nil {
   678  		return err
   679  	}
   680  	u.Path = path
   681  	if escp := escape(path, encodePath); p == escp {
   682  		// Default encoding is fine.
   683  		u.RawPath = ""
   684  	} else {
   685  		u.RawPath = p
   686  	}
   687  	return nil
   688  }
   689  
   690  // EscapedPath returns the escaped form of u.Path.
   691  // In general there are multiple possible escaped forms of any path.
   692  // EscapedPath returns u.RawPath when it is a valid escaping of u.Path.
   693  // Otherwise EscapedPath ignores u.RawPath and computes an escaped
   694  // form on its own.
   695  // The String and RequestURI methods use EscapedPath to construct
   696  // their results.
   697  // In general, code should call EscapedPath instead of
   698  // reading u.RawPath directly.
   699  func (u *URL) EscapedPath() string {
   700  	if u.RawPath != "" && validEncodedPath(u.RawPath) {
   701  		p, err := unescape(u.RawPath, encodePath)
   702  		if err == nil && p == u.Path {
   703  			return u.RawPath
   704  		}
   705  	}
   706  	if u.Path == "*" {
   707  		return "*" // don't escape (Issue 11202)
   708  	}
   709  	return escape(u.Path, encodePath)
   710  }
   711  
   712  // validEncodedPath reports whether s is a valid encoded path.
   713  // It must not contain any bytes that require escaping during path encoding.
   714  func validEncodedPath(s string) bool {
   715  	for i := 0; i < len(s); i++ {
   716  		// RFC 3986, Appendix A.
   717  		// pchar = unreserved / pct-encoded / sub-delims / ":" / "@".
   718  		// shouldEscape is not quite compliant with the RFC,
   719  		// so we check the sub-delims ourselves and let
   720  		// shouldEscape handle the others.
   721  		switch s[i] {
   722  		case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '@':
   723  			// ok
   724  		case '[', ']':
   725  			// ok - not specified in RFC 3986 but left alone by modern browsers
   726  		case '%':
   727  			// ok - percent encoded, will decode
   728  		default:
   729  			if shouldEscape(s[i], encodePath) {
   730  				return false
   731  			}
   732  		}
   733  	}
   734  	return true
   735  }
   736  
   737  // validOptionalPort reports whether port is either an empty string
   738  // or matches /^:\d*$/
   739  func validOptionalPort(port string) bool {
   740  	if port == "" {
   741  		return true
   742  	}
   743  	if port[0] != ':' {
   744  		return false
   745  	}
   746  	for _, b := range port[1:] {
   747  		if b < '0' || b > '9' {
   748  			return false
   749  		}
   750  	}
   751  	return true
   752  }
   753  
   754  // String reassembles the URL into a valid URL string.
   755  // The general form of the result is one of:
   756  //
   757  //	scheme:opaque?query#fragment
   758  //	scheme://userinfo@host/path?query#fragment
   759  //
   760  // If u.Opaque is non-empty, String uses the first form;
   761  // otherwise it uses the second form.
   762  // Any non-ASCII characters in host are escaped.
   763  // To obtain the path, String uses u.EscapedPath().
   764  //
   765  // In the second form, the following rules apply:
   766  //	- if u.Scheme is empty, scheme: is omitted.
   767  //	- if u.User is nil, userinfo@ is omitted.
   768  //	- if u.Host is empty, host/ is omitted.
   769  //	- if u.Scheme and u.Host are empty and u.User is nil,
   770  //	   the entire scheme://userinfo@host/ is omitted.
   771  //	- if u.Host is non-empty and u.Path begins with a /,
   772  //	   the form host/path does not add its own /.
   773  //	- if u.RawQuery is empty, ?query is omitted.
   774  //	- if u.Fragment is empty, #fragment is omitted.
   775  func (u *URL) String() string {
   776  	var buf strings.Builder
   777  	if u.Scheme != "" {
   778  		buf.WriteString(u.Scheme)
   779  		buf.WriteByte(':')
   780  	}
   781  	if u.Opaque != "" {
   782  		buf.WriteString(u.Opaque)
   783  	} else {
   784  		if u.Scheme != "" || u.Host != "" || u.User != nil {
   785  			if u.Host != "" || u.Path != "" || u.User != nil {
   786  				buf.WriteString("//")
   787  			}
   788  			if ui := u.User; ui != nil {
   789  				buf.WriteString(ui.String())
   790  				buf.WriteByte('@')
   791  			}
   792  			if h := u.Host; h != "" {
   793  				buf.WriteString(escape(h, encodeHost))
   794  			}
   795  		}
   796  		path := u.EscapedPath()
   797  		if path != "" && path[0] != '/' && u.Host != "" {
   798  			buf.WriteByte('/')
   799  		}
   800  		if buf.Len() == 0 {
   801  			// RFC 3986 §4.2
   802  			// A path segment that contains a colon character (e.g., "this:that")
   803  			// cannot be used as the first segment of a relative-path reference, as
   804  			// it would be mistaken for a scheme name. Such a segment must be
   805  			// preceded by a dot-segment (e.g., "./this:that") to make a relative-
   806  			// path reference.
   807  			if i := strings.IndexByte(path, ':'); i > -1 && strings.IndexByte(path[:i], '/') == -1 {
   808  				buf.WriteString("./")
   809  			}
   810  		}
   811  		buf.WriteString(path)
   812  	}
   813  	if u.ForceQuery || u.RawQuery != "" {
   814  		buf.WriteByte('?')
   815  		buf.WriteString(u.RawQuery)
   816  	}
   817  	if u.Fragment != "" {
   818  		buf.WriteByte('#')
   819  		buf.WriteString(escape(u.Fragment, encodeFragment))
   820  	}
   821  	return buf.String()
   822  }
   823  
   824  // Values maps a string key to a list of values.
   825  // It is typically used for query parameters and form values.
   826  // Unlike in the http.Header map, the keys in a Values map
   827  // are case-sensitive.
   828  type Values map[string][]string
   829  
   830  // Get gets the first value associated with the given key.
   831  // If there are no values associated with the key, Get returns
   832  // the empty string. To access multiple values, use the map
   833  // directly.
   834  func (v Values) Get(key string) string {
   835  	if v == nil {
   836  		return ""
   837  	}
   838  	vs := v[key]
   839  	if len(vs) == 0 {
   840  		return ""
   841  	}
   842  	return vs[0]
   843  }
   844  
   845  // Set sets the key to value. It replaces any existing
   846  // values.
   847  func (v Values) Set(key, value string) {
   848  	v[key] = []string{value}
   849  }
   850  
   851  // Add adds the value to key. It appends to any existing
   852  // values associated with key.
   853  func (v Values) Add(key, value string) {
   854  	v[key] = append(v[key], value)
   855  }
   856  
   857  // Del deletes the values associated with key.
   858  func (v Values) Del(key string) {
   859  	delete(v, key)
   860  }
   861  
   862  // ParseQuery parses the URL-encoded query string and returns
   863  // a map listing the values specified for each key.
   864  // ParseQuery always returns a non-nil map containing all the
   865  // valid query parameters found; err describes the first decoding error
   866  // encountered, if any.
   867  //
   868  // Query is expected to be a list of key=value settings separated by
   869  // ampersands or semicolons. A setting without an equals sign is
   870  // interpreted as a key set to an empty value.
   871  func ParseQuery(query string) (Values, error) {
   872  	m := make(Values)
   873  	err := parseQuery(m, query)
   874  	return m, err
   875  }
   876  
   877  func parseQuery(m Values, query string) (err error) {
   878  	for query != "" {
   879  		key := query
   880  		if i := strings.IndexAny(key, "&;"); i >= 0 {
   881  			key, query = key[:i], key[i+1:]
   882  		} else {
   883  			query = ""
   884  		}
   885  		if key == "" {
   886  			continue
   887  		}
   888  		value := ""
   889  		if i := strings.Index(key, "="); i >= 0 {
   890  			key, value = key[:i], key[i+1:]
   891  		}
   892  		key, err1 := QueryUnescape(key)
   893  		if err1 != nil {
   894  			if err == nil {
   895  				err = err1
   896  			}
   897  			continue
   898  		}
   899  		value, err1 = QueryUnescape(value)
   900  		if err1 != nil {
   901  			if err == nil {
   902  				err = err1
   903  			}
   904  			continue
   905  		}
   906  		m[key] = append(m[key], value)
   907  	}
   908  	return err
   909  }
   910  
   911  // Encode encodes the values into ``URL encoded'' form
   912  // ("bar=baz&foo=quux") sorted by key.
   913  func (v Values) Encode() string {
   914  	if v == nil {
   915  		return ""
   916  	}
   917  	var buf strings.Builder
   918  	keys := make([]string, 0, len(v))
   919  	for k := range v {
   920  		keys = append(keys, k)
   921  	}
   922  	sort.Strings(keys)
   923  	for _, k := range keys {
   924  		vs := v[k]
   925  		keyEscaped := QueryEscape(k)
   926  		for _, v := range vs {
   927  			if buf.Len() > 0 {
   928  				buf.WriteByte('&')
   929  			}
   930  			buf.WriteString(keyEscaped)
   931  			buf.WriteByte('=')
   932  			buf.WriteString(QueryEscape(v))
   933  		}
   934  	}
   935  	return buf.String()
   936  }
   937  
   938  // resolvePath applies special path segments from refs and applies
   939  // them to base, per RFC 3986.
   940  func resolvePath(base, ref string) string {
   941  	var full string
   942  	if ref == "" {
   943  		full = base
   944  	} else if ref[0] != '/' {
   945  		i := strings.LastIndex(base, "/")
   946  		full = base[:i+1] + ref
   947  	} else {
   948  		full = ref
   949  	}
   950  	if full == "" {
   951  		return ""
   952  	}
   953  	var dst []string
   954  	src := strings.Split(full, "/")
   955  	for _, elem := range src {
   956  		switch elem {
   957  		case ".":
   958  			// drop
   959  		case "..":
   960  			if len(dst) > 0 {
   961  				dst = dst[:len(dst)-1]
   962  			}
   963  		default:
   964  			dst = append(dst, elem)
   965  		}
   966  	}
   967  	if last := src[len(src)-1]; last == "." || last == ".." {
   968  		// Add final slash to the joined path.
   969  		dst = append(dst, "")
   970  	}
   971  	return "/" + strings.TrimPrefix(strings.Join(dst, "/"), "/")
   972  }
   973  
   974  // IsAbs reports whether the URL is absolute.
   975  // Absolute means that it has a non-empty scheme.
   976  func (u *URL) IsAbs() bool {
   977  	return u.Scheme != ""
   978  }
   979  
   980  // Parse parses a URL in the context of the receiver. The provided URL
   981  // may be relative or absolute. Parse returns nil, err on parse
   982  // failure, otherwise its return value is the same as ResolveReference.
   983  func (u *URL) Parse(ref string) (*URL, error) {
   984  	refurl, err := Parse(ref)
   985  	if err != nil {
   986  		return nil, err
   987  	}
   988  	return u.ResolveReference(refurl), nil
   989  }
   990  
   991  // ResolveReference resolves a URI reference to an absolute URI from
   992  // an absolute base URI u, per RFC 3986 Section 5.2. The URI reference
   993  // may be relative or absolute. ResolveReference always returns a new
   994  // URL instance, even if the returned URL is identical to either the
   995  // base or reference. If ref is an absolute URL, then ResolveReference
   996  // ignores base and returns a copy of ref.
   997  func (u *URL) ResolveReference(ref *URL) *URL {
   998  	url := *ref
   999  	if ref.Scheme == "" {
  1000  		url.Scheme = u.Scheme
  1001  	}
  1002  	if ref.Scheme != "" || ref.Host != "" || ref.User != nil {
  1003  		// The "absoluteURI" or "net_path" cases.
  1004  		// We can ignore the error from setPath since we know we provided a
  1005  		// validly-escaped path.
  1006  		url.setPath(resolvePath(ref.EscapedPath(), ""))
  1007  		return &url
  1008  	}
  1009  	if ref.Opaque != "" {
  1010  		url.User = nil
  1011  		url.Host = ""
  1012  		url.Path = ""
  1013  		return &url
  1014  	}
  1015  	if ref.Path == "" && ref.RawQuery == "" {
  1016  		url.RawQuery = u.RawQuery
  1017  		if ref.Fragment == "" {
  1018  			url.Fragment = u.Fragment
  1019  		}
  1020  	}
  1021  	// The "abs_path" or "rel_path" cases.
  1022  	url.Host = u.Host
  1023  	url.User = u.User
  1024  	url.setPath(resolvePath(u.EscapedPath(), ref.EscapedPath()))
  1025  	return &url
  1026  }
  1027  
  1028  // Query parses RawQuery and returns the corresponding values.
  1029  // It silently discards malformed value pairs.
  1030  // To check errors use ParseQuery.
  1031  func (u *URL) Query() Values {
  1032  	v, _ := ParseQuery(u.RawQuery)
  1033  	return v
  1034  }
  1035  
  1036  // RequestURI returns the encoded path?query or opaque?query
  1037  // string that would be used in an HTTP request for u.
  1038  func (u *URL) RequestURI() string {
  1039  	result := u.Opaque
  1040  	if result == "" {
  1041  		result = u.EscapedPath()
  1042  		if result == "" {
  1043  			result = "/"
  1044  		}
  1045  	} else {
  1046  		if strings.HasPrefix(result, "//") {
  1047  			result = u.Scheme + ":" + result
  1048  		}
  1049  	}
  1050  	if u.ForceQuery || u.RawQuery != "" {
  1051  		result += "?" + u.RawQuery
  1052  	}
  1053  	return result
  1054  }
  1055  
  1056  // Hostname returns u.Host, without any port number.
  1057  //
  1058  // If Host is an IPv6 literal with a port number, Hostname returns the
  1059  // IPv6 literal without the square brackets. IPv6 literals may include
  1060  // a zone identifier.
  1061  func (u *URL) Hostname() string {
  1062  	return stripPort(u.Host)
  1063  }
  1064  
  1065  // Port returns the port part of u.Host, without the leading colon.
  1066  // If u.Host doesn't contain a port, Port returns an empty string.
  1067  func (u *URL) Port() string {
  1068  	return portOnly(u.Host)
  1069  }
  1070  
  1071  func stripPort(hostport string) string {
  1072  	colon := strings.IndexByte(hostport, ':')
  1073  	if colon == -1 {
  1074  		return hostport
  1075  	}
  1076  	if i := strings.IndexByte(hostport, ']'); i != -1 {
  1077  		return strings.TrimPrefix(hostport[:i], "[")
  1078  	}
  1079  	return hostport[:colon]
  1080  }
  1081  
  1082  func portOnly(hostport string) string {
  1083  	colon := strings.IndexByte(hostport, ':')
  1084  	if colon == -1 {
  1085  		return ""
  1086  	}
  1087  	if i := strings.Index(hostport, "]:"); i != -1 {
  1088  		return hostport[i+len("]:"):]
  1089  	}
  1090  	if strings.Contains(hostport, "]") {
  1091  		return ""
  1092  	}
  1093  	return hostport[colon+len(":"):]
  1094  }
  1095  
  1096  // Marshaling interface implementations.
  1097  // Would like to implement MarshalText/UnmarshalText but that will change the JSON representation of URLs.
  1098  
  1099  func (u *URL) MarshalBinary() (text []byte, err error) {
  1100  	return []byte(u.String()), nil
  1101  }
  1102  
  1103  func (u *URL) UnmarshalBinary(text []byte) error {
  1104  	u1, err := Parse(string(text))
  1105  	if err != nil {
  1106  		return err
  1107  	}
  1108  	*u = *u1
  1109  	return nil
  1110  }
  1111  
  1112  // validUserinfo reports whether s is a valid userinfo string per RFC 3986
  1113  // Section 3.2.1:
  1114  //     userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
  1115  //     unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
  1116  //     sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
  1117  //                   / "*" / "+" / "," / ";" / "="
  1118  //
  1119  // It doesn't validate pct-encoded. The caller does that via func unescape.
  1120  func validUserinfo(s string) bool {
  1121  	for _, r := range s {
  1122  		if 'A' <= r && r <= 'Z' {
  1123  			continue
  1124  		}
  1125  		if 'a' <= r && r <= 'z' {
  1126  			continue
  1127  		}
  1128  		if '0' <= r && r <= '9' {
  1129  			continue
  1130  		}
  1131  		switch r {
  1132  		case '-', '.', '_', ':', '~', '!', '$', '&', '\'',
  1133  			'(', ')', '*', '+', ',', ';', '=', '%', '@':
  1134  			continue
  1135  		default:
  1136  			return false
  1137  		}
  1138  	}
  1139  	return true
  1140  }
  1141  
  1142  // stringContainsCTLByte reports whether s contains any ASCII control character.
  1143  func stringContainsCTLByte(s string) bool {
  1144  	for i := 0; i < len(s); i++ {
  1145  		b := s[i]
  1146  		if b < ' ' || b == 0x7f {
  1147  			return true
  1148  		}
  1149  	}
  1150  	return false
  1151  }
  1152  

View as plain text