gpt4 book ai didi

url - 如何在 URL 中的片段标识符中编码空格

转载 作者:行者123 更新时间:2023-12-04 20:09:19 25 4
gpt4 key购买 nike

在 URL 的片段标识符中,空格是否应编码为 %20像在路径中或作为 +就像在查询字符串中一样?

最佳答案

对于 HTML 页面,它们应该是百分比编码的。

根据 RFC 2396 , RFC 3986 , 和 RFC 7320 ,片段标识符的格式取决于媒体类型。来自 RFC 2396 和 RFC 3986:

The semantics of a fragment identifier are defined by the set of representations that might result from a retrieval action on the primary resource. The fragment's format and resolution is therefore dependent on the media type [RFC2046] of a potentially retrieved representation, even though such a retrieval is only performed if the URI is dereferenced.



来自 RFC 7320:

Media type definitions (as per [RFC6838]) SHOULD specify the fragment identifier syntax(es) to be used with them; other specifications MUST NOT define structure within the fragment identifier, unless they are explicitly defining one for reuse by media type definitions.



HTML5 规范 only specifies percent encoding :

The indicated part of the document is the one that the fragment identifier, if any, identifies. The semantics of the fragment identifier in terms of mapping it to a specific DOM Node is defined by the specification that defines the MIME type used by the Document (for example, the processing of fragment identifiers for XML MIME types is the responsibility of RFC7303).

For HTML documents (and HTML MIME types), the following processing model must be followed to determine what the indicated part of the document is.

  1. Apply the URL parser algorithm to the URL, and let fragid be the fragment component of the resulting parsed URL.

  2. If fragid is the empty string, then the indicated part of the document is the top of the document; stop the algorithm here.

  3. Let fragid bytes be the result of percent-decoding fragid.

  4. Let decoded fragid be the result of applying the UTF-8 decoder algorithm to fragid bytes. If the UTF-8 decoder emits a decoder error, abort the decoder and instead jump to the step labeled no decoded fragid.

  5. [...]


(强调我的)

对于 XML 文档, RFC 7303指定 XPointer Framework 的语法这也 requires percent encoding用于保留的 URI 字符。

其他媒体类型可能有不同的规则。

关于url - 如何在 URL 中的片段标识符中编码空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20679500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com