简体   繁体   English

如何编码“..”用于URL路径?

[英]How to encode “..” for use in a URL path?

I'm trying to construct a URL with something like: 我正在尝试构建一个类似于以下内容的URL:

var myUrl = '/path/to/api/' + encodeURIComponent(str);

But if str is .. then your browser automatically lops off a path segment so that the URL becomes /path/to which is not what I want. 但是如果str..那么你的浏览器会自动关闭一个路径段,以便URL成为/path/to不是我想要的。

I've tried encoding .. as %2E%2E but your browser still re-interprets it before the request is sent. 我已经尝试编码..作为%2E%2E但您的浏览器仍然在发送请求之前重新解释它。 Is there anything I can do to have path actually come through to my server as /path/to/api/.. ? 我有什么办法可以让路径实际作为/path/to/api/..进入我的服务器吗?

I believe this is not supported because the behaviour would violate RFC 3986 . 我认为这不受支持,因为该行为会违反RFC 3986

From Section 2.3. 2.3节。 Unreserved Characters (emphasis mine): 未保留的角色 (强调我的):

Characters that are allowed in a URI but do not have a reserved purpose are called unreserved. URI中允许但没有保留目的的字符称为unreserved。 These include uppercase and lowercase letters, decimal digits, hyphen, period, underscore, and tilde. 这些包括大写和小写字母,十进制数字,连字符,句点,下划线和波浪号。

  unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" 

URIs that differ in the replacement of an unreserved character with its corresponding percent-encoded US-ASCII octet are equivalent: they identify the same resource. 使用相应的百分比编码的US-ASCII八位字节替换未保留字符的URI是等效的:它们标识相同的资源。 However, URI comparison implementations do not always perform normalization prior to comparison (see Section 6). 但是,URI比较实现并不总是在比较之前执行规范化(参见第6节)。 For consistency, percent-encoded octets in the ranges of ALPHA (%41-%5A and %61-%7A), DIGIT (%30-%39), hyphen (%2D), period (%2E) , underscore (%5F), or tilde (%7E) should not be created by URI producers and, when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers . 为保持一致, ALPHA(%41-%5A和%61-%7A),DIGIT(%30-%39),连字符(%2D), 期间(%2E) ,下划线(% 范围内百分比编码八位字节 不应该由URI生成器创建 5F)或波浪号(%7E) ,并且当在URI中找到时,应该通过URI规范化器将其解码为其对应的未保留字符

From Section 6.2.2.3. 来自第6.2.2.3节。 Path Segment Normalization (emphasis mine): 路径段规范化 (强调我的):

The complete path segments "." 完整的路径段“。” and ".." are intended only for use within relative references (Section 4.1) and are removed as part of the reference resolution process (Section 5.2). 和“..”仅供相关参考文献(第4.1节)使用,并作为参考决议过程的一部分 (第5.2 节)删除 However, some deployed implementations incorrectly assume that reference resolution is not necessary when the reference is already a URI and thus fail to remove dot-segments when they occur in non-relative paths. 但是,某些部署的实现错误地认为当引用已经是URI时不需要引用解析,因此当它们出现在非相对路径中时无法删除点段。 URI normalizers should remove dot-segments by applying the remove_dot_segments algorithm to the path, as described in Section 5.2.4. URI规范化器应该通过将remove_dot_segments算法应用于路径来删除点段,如第5.2.4节中所述 ): ):

I've actually done similar by double encoding the text, then un-encoding it on the server back end. 我实际上通过对文本进行双重编码来完成相似的操作,然后在服务器后端对其进行解码。 However, mine were query parameters, not part of the path. 但是,我的是查询参数,而不是路径的一部分。

PS. PS。 This is written on my phone, I'll add an example later. 这是写在我的手机上,我稍后会添加一个例子。

Seeing as there's no solution , there's not much we can do but error: 看到没有解决方案 ,除了错误之外我们无能为力:

export function encodeUriComponent(str) {
    if(str === '.' || str === '..') {
        throw new Error(`Cannot URI-encode "${str}" per RFC 3986 §6.2.2.3`)
    }
    return encodeURIComponent(str);
}

I feel that this is a better option than arbitrarily modifying the URL path which is exactly what I was trying to avoid by using encodeURIComponent . 我觉得这是一个比任意修改URL路径更好的选择,这正是我试图通过使用encodeURIComponent避免的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM