...
Run Format

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

View as plain text