url
12 分钟阅读
Package url parses URLs and implements query escaping.
url 包解析 URL 并实现查询转义。
常量
This section is empty.
变量
This section is empty.
函数
func JoinPath <- go1.19
|
|
JoinPath returns a URL string with the provided path elements joined to the existing path of base and the resulting path cleaned of any ./ or ../ elements.
JoinPath 返回一个 URL 字符串,其中提供的路径元素与 base 的现有路径连接,并且结果路径已清除所有 ./ 或 ../ 元素。
func PathEscape <- go1.8
|
|
PathEscape escapes the string so it can be safely placed inside a URL path segment, replacing special characters (including /) with %XX sequences as needed.
PathEscape 转义字符串,以便可以安全地将其置于 URL 路径段中,根据需要将特殊字符(包括 /)替换为 %XX 序列。
PathEscape Example
|
|
func PathUnescape <- go1.8
|
|
PathUnescape does the inverse transformation of PathEscape, converting each 3-byte encoded substring of the form “%AB” into the hex-decoded byte 0xAB. It returns an error if any % is not followed by two hexadecimal digits.
PathUnescape 执行 PathEscape 的逆转换,将每个 3 字节编码的“%AB”形式的子字符串转换为十六进制解码的字节 0xAB。如果任何 % 后面没有两个十六进制数字,它将返回一个错误。
PathUnescape is identical to QueryUnescape except that it does not unescape ‘+’ to ’ ’ (space).
PathUnescape 与 QueryUnescape 相同,只是它不会将“+”转义为“’”(空格)。
PathUnescape Example
|
|
func QueryEscape
|
|
QueryEscape escapes the string so it can be safely placed inside a URL query.
QueryEscape 转义字符串,以便可以安全地将其置于 URL 查询中。
QueryEscape Example
|
|
func QueryUnescape
|
|
QueryUnescape does the inverse transformation of QueryEscape, converting each 3-byte encoded substring of the form “%AB” into the hex-decoded byte 0xAB. It returns an error if any % is not followed by two hexadecimal digits.
QueryUnescape 执行 QueryEscape 的逆转换,将每个形式为“%AB”的 3 字节编码子字符串转换为十六进制解码的字节 0xAB。如果任何 % 后面没有两个十六进制数字,它将返回一个错误。
QueryUnescape Example
|
|
类型
type Error
|
|
Error reports an error and the operation and URL that caused it.
Error 报告错误以及导致错误的操作和 URL。
(*Error) Error
|
|
(*Error) Temporary <- go1.6
|
|
(*Error) Timeout <- go1.6
|
|
(*Error) Unwrap <- go1.13
|
|
type EscapeError
|
|
(EscapeError) Error
|
|
type InvalidHostError <- go1.6
|
|
(InvalidHostError) Error <- go1.6
|
|
type URL
|
|
A URL represents a parsed URL (technically, a URI reference).
URL 表示已解析的 URL(从技术上讲,是 URI 引用)。
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:
方案后不以斜杠开头的 URL 被解释为:
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, the code should use the EscapedPath method, which preserves the original encoding of Path.
请注意,路径字段以解码形式存储:/%47%6f%2f 变为 /Go/。因此,无法分辨路径中的哪些斜杠是原始 URL 中的斜杠,哪些是 %2f。这种区别很少重要,但当它很重要时,代码应使用 EscapedPath 方法,该方法保留路径的原始编码。
The RawPath field is an optional field which is only set when the default encoding of Path is different from the escaped path. See the EscapedPath method for more details.
RawPath 字段是一个可选字段,仅在路径的默认编码与转义路径不同时设置。有关更多详细信息,请参阅 EscapedPath 方法。
URL’s String method uses the EscapedPath method to obtain the path.
URL 的 String 方法使用 EscapedPath 方法获取路径。
Example
|
|
Example(Roundtrip)
|
|
func Parse
|
|
Parse parses a raw url into a URL structure.
Parse 将原始 URL 解析为 URL 结构。
The url 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.
URL 可以是相对的(路径,没有主机)或绝对的(以方案开头)。尝试解析没有方案的主机名和路径是无效的,但由于解析歧义,可能不一定返回错误。
func ParseRequestURI
|
|
ParseRequestURI parses a raw url into a URL structure. It assumes that url was received in an HTTP request, so the url is interpreted only as an absolute URI or an absolute path. The string url is assumed not to have a #fragment suffix. (Web browsers strip #fragment before sending the URL to a web server.)
ParseRequestURI 将原始 URL 解析为 URL 结构。它假定在 HTTP 请求中收到了 URL,因此 URL 仅解释为绝对 URI 或绝对路径。假定字符串 URL 没有 #fragment 后缀。(Web 浏览器在将 URL 发送到 Web 服务器之前会删除 #fragment。)
(*URL) EscapedFragment <- go1.15
|
|
EscapedFragment returns the escaped form of u.Fragment. In general there are multiple possible escaped forms of any fragment. EscapedFragment returns u.RawFragment when it is a valid escaping of u.Fragment. Otherwise EscapedFragment ignores u.RawFragment and computes an escaped form on its own. The String method uses EscapedFragment to construct its result. In general, code should call EscapedFragment instead of reading u.RawFragment directly.
EscapedFragment 返回 u.Fragment 的转义形式。通常,任何片段都有多种可能的转义形式。当 u.RawFragment 是 u.Fragment 的有效转义时,EscapedFragment 返回 u.RawFragment。否则,EscapedFragment 会忽略 u.RawFragment 并自行计算转义形式。String 方法使用 EscapedFragment 来构造其结果。通常,代码应调用 EscapedFragment,而不是直接读取 u.RawFragment。
EscapedFragment Example
|
|
(*URL) EscapedPath <- go1.5
|
|
EscapedPath returns the escaped form of u.Path. In general there are multiple possible escaped forms of any path. EscapedPath returns u.RawPath when it is a valid escaping of u.Path. Otherwise EscapedPath ignores u.RawPath and computes an escaped form on its own. The String and RequestURI methods use EscapedPath to construct their results. In general, code should call EscapedPath instead of reading u.RawPath directly.
EscapedPath 返回 u.Path 的转义形式。通常,任何路径都有多种可能的转义形式。当 u.RawPath 是 u.Path 的有效转义时,EscapedPath 返回 u.RawPath。否则,EscapedPath 会忽略 u.RawPath 并自行计算转义形式。String 和 RequestURI 方法使用 EscapedPath 来构造其结果。通常,代码应调用 EscapedPath,而不是直接读取 u.RawPath。
EscapedPath Example
|
|
(*URL) Hostname <- go1.8
|
|
Hostname returns u.Host, stripping any valid port number if present.
Hostname 返回 u.Host,如果存在,则剥离任何有效的端口号。
If the result is enclosed in square brackets, as literal IPv6 addresses are, the square brackets are removed from the result.
如果结果用方括号括起来,就像字面 IPv6 地址一样,则从结果中删除方括号。
Hostname Example
|
|
(*URL) IsAbs
|
|
IsAbs reports whether the URL is absolute. Absolute means that it has a non-empty scheme.
IsAbs 报告 URL 是否是绝对的。绝对意味着它具有非空方案。
IsAbs Example
|
|
(*URL) JoinPath <- go1.19
|
|
JoinPath returns a new URL with the provided path elements joined to any existing path and the resulting path cleaned of any ./ or ../ elements. Any sequences of multiple / characters will be reduced to a single /.
JoinPath 返回一个新的 URL,其中提供的路径元素连接到任何现有路径,并且结果路径已清除任何 ./ 或 ../ 元素。任何多个 / 字符的序列都将减少为单个 /。
(*URL) MarshalBinary <- go1.8
|
|
MarshalBinary Example
|
|
(*URL) Parse
|
|
Parse 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.
Parse 在接收者的上下文中解析 URL。提供的 URL 可能为相对或绝对 URL。Parse 在解析失败时返回 nil 和 err,否则其返回值与 ResolveReference 相同。
Parse Example
|
|
(*URL) Port <- go1.8
|
|
Port returns the port part of u.Host, without the leading colon.
Port 返回 u.Host 的端口部分,不带前导冒号。
If u.Host doesn’t contain a valid numeric port, Port returns an empty string.
如果 u.Host 不包含有效的数字端口,Port 返回空字符串。
Port Example
|
|
(*URL) Query
|
|
Query parses RawQuery and returns the corresponding values. It silently discards malformed value pairs. To check errors use ParseQuery.
Query 解析 RawQuery 并返回相应的值。它会静默地丢弃格式错误的值对。要检查错误,请使用 ParseQuery。
Query Example
|
|
(*URL) Redacted <- go1.15
|
|
Redacted is like String but replaces any password with “xxxxx”. Only the password in u.URL is redacted.
Redacted 类似于 String,但会将任何密码替换为“xxxxx”。仅对 u.URL 中的密码进行编辑。
Redacted Example
|
|
(*URL) RequestURI
|
|
RequestURI returns the encoded path?query or opaque?query string that would be used in an HTTP request for u.
RequestURI 返回编码的 path?query 或 opaque?query 字符串,该字符串将用于 u 的 HTTP 请求中。
RequestURI Example
|
|
(*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.
ResolveReference 根据 RFC 3986 第 5.2 节,将 URI 引用解析为绝对基本 URI u 的绝对 URI。URI 引用可以是相对的或绝对的。ResolveReference 始终返回一个新的 URL 实例,即使返回的 URL 与基本 URL 或引用 URL 相同。如果 ref 是绝对 URL,则 ResolveReference 会忽略基本 URL 并返回 ref 的副本。
ResolveReference Example
|
|
(*URL) String
|
|
String reassembles the URL into a valid URL string. The general form of the result is one of:
String 将 URL 重新组合成有效的 URL 字符串。结果的一般形式之一为:
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. Any non-ASCII characters in host are escaped. To obtain the path, String uses u.EscapedPath().
如果 u.Opaque 为非空,String 使用第一种形式;否则,它使用第二种形式。host 中的任何非 ASCII 字符都将被转义。要获取路径,String 使用 u.EscapedPath()。
In the second form, the following rules apply:
在第二种形式中,应用以下规则:
- if u.Scheme is empty, scheme: is omitted. 如果 u.Scheme 为空,则省略 scheme:。
- if u.User is nil, userinfo@ is omitted. 如果 u.User 为 nil,则省略 userinfo@。
- if u.Host is empty, host/ is omitted. 如果 u.Host 为空,则省略 host/。
- if u.Scheme and u.Host are empty and u.User is nil, the entire scheme://userinfo@host/ is omitted. 如果 u.Scheme 和 u.Host 为空,并且 u.User 为 nil,则省略整个 scheme://userinfo@host/。
- if u.Host is non-empty and u.Path begins with a /, the form host/path does not add its own /. 如果 u.Host 为非空,并且 u.Path 以 / 开头,则形式 host/path 不会添加其自己的 /。
- if u.RawQuery is empty, ?query is omitted. 如果 u.RawQuery 为空,则省略 ?query。
- if u.Fragment is empty, #fragment is omitted. 如果 u.Fragment 为空,则省略 #fragment。
String Example
|
|
(*URL) UnmarshalBinary <- go1.8
|
|
UnmarshalBinary Example
|
|
type Userinfo
|
|
The Userinfo type is an immutable encapsulation of username and password details for a URL. An existing Userinfo value is guaranteed to have a username set (potentially empty, as allowed by RFC 2396), and optionally a password.
Userinfo 类型是对 URL 的用户名和密码详细信息的不可变封装。现有 Userinfo 值保证设置了用户名(可能为空,RFC 2396 允许),并且可以选择密码。
func User
|
|
User returns a Userinfo containing the provided username and no password set.
User 返回包含提供的用户名且未设置密码的 Userinfo。
func UserPassword
|
|
UserPassword returns a Userinfo containing the provided username and password.
UserPassword 返回包含提供的用户名和密码的 Userinfo。
This functionality should only be used with legacy web sites. RFC 2396 warns that interpreting Userinfo this way “is NOT RECOMMENDED, because the passing of authentication information in clear text (such as URI) has proven to be a security risk in almost every case where it has been used.”
此功能仅应与旧版网站配合使用。RFC 2396 警告以这种方式解释 Userinfo “不推荐,因为以明文(例如 URI)传递身份验证信息已被证明在几乎所有使用它的情况下都是安全风险。”
(*Userinfo) Password
|
|
Password returns the password in case it is set, and whether it is set.
Password 返回设置的密码(如果已设置)以及是否已设置。
(*Userinfo) String
|
|
String returns the encoded userinfo information in the standard form of “username[:password]”.
String 返回标准形式“username[:password]”中编码的用户信息。
(*Userinfo) Username
|
|
Username returns the username.
Username 返回用户名。
type Values
|
|
Values maps a string key to a list of values. It is typically used for query parameters and form values. Unlike in the http.Header map, the keys in a Values map are case-sensitive.
Values 将字符串键映射到值列表。它通常用于查询参数和表单值。与 http.Header 映射不同,Values 映射中的键区分大小写。
Example
|
|
func ParseQuery
|
|
ParseQuery parses the URL-encoded query string and returns a map listing the values specified for each key. ParseQuery always returns a non-nil map containing all the valid query parameters found; err describes the first decoding error encountered, if any.
ParseQuery 解析 URL 编码的查询字符串,并返回一个映射,其中列出了为每个键指定的值。ParseQuery 始终返回一个非 nil 映射,其中包含找到的所有有效查询参数;err 描述遇到的第一个解码错误(如果有)。
Query is expected to be a list of key=value settings separated by ampersands. A setting without an equals sign is interpreted as a key set to an empty value. Settings containing a non-URL-encoded semicolon are considered invalid.
Query 预计是通过与号分隔的一系列 key=value 设置。没有等号的设置将被解释为一个键,该键设置为一个空值。包含未经 URL 编码的分号的设置被认为无效。
ParseQuery Example
|
|
(Values) Add
|
|
Add adds the value to key. It appends to any existing values associated with key.
Add 将值添加到键。它追加到与键关联的任何现有值。
Add Example
|
|
(Values) Del
|
|
Del deletes the values associated with key.
Del 删除与键关联的值。
Del Example
|
|
(Values) Encode
|
|
Encode encodes the values into “URL encoded” form (“bar=baz&foo=quux”) sorted by key.
Encode 将值编码为“URL 编码”形式(“bar=baz&foo=quux”),按键排序。
Encode Example
|
|
(Values) Get
|
|
Get gets the first value associated with the given key. If there are no values associated with the key, Get returns the empty string. To access multiple values, use the map directly.
Get 获取与给定键关联的第一个值。如果与键关联的值不存在,则 Get 返回空字符串。要访问多个值,请直接使用映射。
Get Example
|
|
(Values) Has <- go1.17
|
|
Has checks whether a given key is set.
Has 检查给定的键是否已设置。
Has Example
|
|
(Values) Set
|
|
Set sets the key to value. It replaces any existing values.
Set 将键设置为值。它将替换任何现有值。
Set Example
|
|