url - 我们应该在 URL 的搜索部分对斜线进行编码吗?

标签 url rfc

rfc 1738 “搜索部分”中正斜杠的编码不准确:

If the character corresponding to an octet is reserved in a scheme, the octet must be encoded.

...

only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.

...

Within the 'path' and 'searchpart' components, "/", ";", "?" are reserved.

你知道url搜索部分的“/”的“保留用途”是什么吗?

是否有真正的理由遵循规范并对正斜杠进行编码,前提是 我的服务器处理未编码的斜杠?

当我需要不断解码仅包含斜杠的字母数字 url 参数时,这让我抓狂。

这是一个生活例子:

http://localhost/login?url=/a/path/to/protected/content

对比

http://localhost/login?url=%2Fa%2Fpath%2Fto%2Fprotected%2Fcontent "

最佳答案

请注意 RFC 3986更新 RFC 1738(虽然没有废弃它,我认为这表明它旨在澄清而不是矛盾)。

RFC 3986 在第 3.4 节中说,URI 的 query 部分的语法是:

query       = *( pchar / "/" / "?" )

URI 的 ABNF 很方便地收集在附录 A 中,这表明

pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
pct-encoded   = "%" HEXDIG HEXDIG
unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
              / "*" / "+" / "," / ";" / "="

这非常明确地表明斜线在查询部分是合法的,因此不需要编码。特别是,您的示例 http://localhost/login?url=/a/path/to/protected/content 就这样很好, http://localhost/login?abc123-.+~!$&'()*+,;=%00/?:@

第 2.4 节指出,仅当想要在 URI 的一部分中包含保留字符时才需要对字符进行编码(此处不适用)。

关于url - 我们应该在 URL 的搜索部分对斜线进行编码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7436704/

相关文章:

google-chrome - 使用chrome扩展程序获取所有窗口中所有标签的网址

java - 正则表达式匹配带有 www 且无连续点的 URL

http-headers - 如果加权 HTTP header 具有重复值,且权重不同,应如何处理?

javascript - 这是一个有效的电子邮件地址吗?

oracle - Oracle 的 SYS_GUID() UUID RFC 4122 是否兼容?

Git 子模块 url 已更改

javascript - 重定向到 JavaScript 中的相对 URL

swift - 本地 Bundle.main 的 URL 总是返回 nil

email - 在线 SpamAssassin 评估/RFC 一致性检查