parsing - 如何在 Web 服务器上的请求 URI 中解码保留的转义字符?

标签 parsing url escaping uri percent-encoding

很明显,Web 服务器必须解码任何转义的非保留字符(例如字母数字等)才能进行 URI 比较。例如,http://www.example.com/~user/index.htm应与 http://www.example.com/%7Euser/index.htm 相同.
我的问题是,我们将如何处理转义的保留字符?
一个例子是 %2F , 或 / .如果有 %2F在请求 URI 中,Web 服务器的解析器是否应该将其替换为 / ?在上面的例子中,这意味着 http://www.example.com/~user%2Findex.htm将与 http://www.example.com/~user/index.htm 相同?尽管我在 Apache 服务器(2.2.17 Unix)上尝试过它,但它似乎给出了“404 Not Found”错误。
那么这是否意味着 %2F和其他转义保留字符应单独保留(至少在 URI 比较之前)?
背景资料:
RFC 2616 (HTTP 1.1) 中有两个地方提到了转义解码问题:

The Request-URI is transmitted in the format specified in section 3.2.1. If the Request-URI is encoded using the “% HEX HEX” encoding [42], the origin server MUST decode the Request-URI in order to properly interpret the request. Servers SHOULD respond to invalid Request-URIs with an appropriate status code.



Characters other than those in the “reserved” and “unsafe” sets (see RFC 2396 [42]) are equivalent to their “"%" HEX HEX” encoding.


(根据 http://trac.tools.ietf.org/wg/httpbis/trac/ticket/2“不安全”是一个错误,应该从规范中删除。所以我们在这里只看“保留”。)
仅供引用,RFC 2396 中此类字符的定义:

reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","

unreserved = alphanum | mark

mark = "-" | "_" | "." | "!" | "˜" | "*" | "’" | "(" | ")"

最佳答案

tl;博士:
解码百分比编码的非保留字符,
保留百分比编码的保留字符。

URI 标准是 STD 66 ,目前是 RFC 3986 .
Section 6是关于归一化和比较,其中 section 6.2.2.2解释了如何处理百分比编码的八位字节:

These URIs should be normalized by decoding any percent-encoded octet that corresponds to an unreserved character […]


正如 section 2 中明确所述(粗体强调我的):
  • Unreserved characters :

    URIs that differ in the replacement of an unreserved character with its corresponding percent-encoded US-ASCII octet are equivalent


  • Reserved characters :

    URIs that differ in the replacement of a reserved character with its corresponding percent-encoded octet are not equivalent.


  • 关于parsing - 如何在 Web 服务器上的请求 URI 中解码保留的转义字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5885391/

    相关文章:

    javascript - 分隔 .CSV 文件中的数字

    java - Java中时间字符串的解析

    html - URL 片段中的多个参数

    cmd - Windows 10 runas 转义

    python - 从字符串中读取 Bunch()

    python - 将一本书解析成章节——Python

    php - 如何从 PHP 中的 URL 中删除 http ://, www 和斜杠?

    python - 剥离 URL - Python

    syntax - 从历史上看,为什么反斜杠 (\) 是转义字符?

    regex - 如何使用 sed/awk/perl 将匹配的模式替换为等效数量的破折号?