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/