Documentation
¶
Overview ¶
Package fasturl parses URLs and implements query escaping.
Index ¶
- Constants
- Variables
- func PathUnescape(s string) string
- type Error
- type EscapeError
- type InvalidHostError
- type Scheme
- type URL
- func (u *URL) IsAbs() bool
- func (u *URL) MarshalBinary() (text []byte, err error)
- func (u *URL) Parse(rawurl string) error
- func (u *URL) ParseRel(out *URL, ref string) error
- func (u *URL) ParseRequestURI(rawurl string) error
- func (u *URL) ResolveReference(url *URL, ref *URL)
- func (u *URL) String() string
- func (u *URL) UnmarshalBinary(text []byte) error
Constants ¶
const ( SchemeInvalid = iota SchemeHTTP SchemeHTTPS SchemeCount )
Variables ¶
var ErrUnknownScheme = errors.New("unknown protocol scheme")
var Schemes = [SchemeCount]string{
"",
"http",
"https",
}
Functions ¶
func PathUnescape ¶
Types ¶
type EscapeError ¶
type EscapeError string
func (EscapeError) Error ¶
func (e EscapeError) Error() string
type InvalidHostError ¶
type InvalidHostError string
func (InvalidHostError) Error ¶
func (e InvalidHostError) Error() string
type URL ¶
type URL struct {
Scheme Scheme
Host string // host or host:port
Path string // path (relative paths may omit leading slash)
}
A URL represents a parsed URL (technically, a URI reference).
The general form represented is:
[scheme:][//[userinfo@]host][/]path[?query][#fragment]
URLs that do not start with a slash after the scheme are interpreted as:
scheme:opaque[?query][#fragment]
Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/. A consequence is that it is impossible to tell which slashes in the Path were slashes in the raw URL and which were %2f. This distinction is rarely important, but when it is, code must not use Path directly. The Parse function sets both Path and RawPath in the URL it returns, and URL's String method uses RawPath if it is a valid encoding of Path, by calling the EscapedPath method.
func (*URL) IsAbs ¶
IsAbs reports whether the URL is absolute. Absolute means that it has a non-empty scheme.
func (*URL) MarshalBinary ¶
func (*URL) Parse ¶
Parse parses rawurl into a URL structure.
The rawurl may be relative (a path, without a host) or absolute (starting with a scheme). Trying to parse a hostname and path without a scheme is invalid but may not necessarily return an error, due to parsing ambiguities.
func (*URL) ParseRel ¶
ParseRel parses a URL in the context of the receiver. The provided URL may be relative or absolute. Parse returns nil, err on parse failure, otherwise its return value is the same as ResolveReference.
func (*URL) ParseRequestURI ¶
ParseRequestURI parses rawurl into a URL structure. It assumes that rawurl was received in an HTTP request, so the rawurl is interpreted only as an absolute URI or an absolute path. The string rawurl is assumed not to have a #fragment suffix. (Web browsers strip #fragment before sending the URL to a web server.)
func (*URL) ResolveReference ¶
ResolveReference resolves a URI reference to an absolute URI from an absolute base URI u, per RFC 3986 Section 5.2. The URI reference may be relative or absolute. ResolveReference always returns a new URL instance, even if the returned URL is identical to either the base or reference. If ref is an absolute URL, then ResolveReference ignores base and returns a copy of ref.
func (*URL) String ¶
String reassembles the URL into a valid URL string. The general form of the result is one of:
scheme:opaque?query#fragment scheme://userinfo@host/path?query#fragment
If u.Opaque is non-empty, String uses the first form; otherwise it uses the second form. To obtain the path, String uses u.EscapedPath().
In the second form, the following rules apply:
- if u.Scheme is empty, scheme: is omitted.
- if u.User is nil, userinfo@ is omitted.
- if u.Host is empty, host/ is omitted.
- if u.Scheme and u.Host are empty and u.User is nil, the entire scheme://userinfo@host/ is omitted.
- if u.Host is non-empty and u.Path begins with a /, the form host/path does not add its own /.
- if u.RawQuery is empty, ?query is omitted.
- if u.Fragment is empty, #fragment is omitted.