为什么 URI.escape 不转义单引号?

发布于 2024-12-09 17:29:52 字数 121 浏览 1 评论 0原文

为什么 URI.escape 不转义单引号?

URI.escape("foo'bar\" baz")
=> "foo'bar%22%20baz"

Why doesn't URI.escape escape single quotes?

URI.escape("foo'bar\" baz")
=> "foo'bar%22%20baz"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唐婉 2024-12-16 17:29:52

出于同样的原因,它不会转义 ?/: 等等。 URI.escape() 仅转义根本不能在 URL 中使用的字符,而不是具有特殊含义的字符。

您要查找的是 CGI.escape()

require "cgi"
CGI.escape("foo'bar\" baz")
=> "foo%27bar%22+baz"

For the same reason it doesn't escape ? or / or :, and so forth. URI.escape() only escapes characters that cannot be used in URLs at all, not characters that have a special meaning.

What you're looking for is CGI.escape():

require "cgi"
CGI.escape("foo'bar\" baz")
=> "foo%27bar%22+baz"
氛圍 2024-12-16 17:29:52

这是一个老问题了,但答案已经很长时间没有更新了。我想我会为其他遇到同样问题的人更新此内容。我找到的解决方案是发布在此处:如果您有的话,请使用ERB::Util.url_encode erb 模块可用。这解决了单引号和单引号的问题。 * 对我来说也是如此。

与加号相比,CGI::escape 无法正确转义空格 (%20)。

This is an old question, but the answer hasn't been updated in a long time. I thought I'd update this for others who are having the same problem. The solution I found was posted here: use ERB::Util.url_encode if you have the erb module available. This took care of single quotes & * for me as well.

CGI::escape doesn't escape spaces correctly (%20) versus plus signs.

陌路黄昏 2024-12-16 17:29:52

根据文档, URI.escape(str [, unsafe]) 使用正则表达式来匹配必须用代码替换的所有符号。默认情况下,该方法使用 REGEXP::UNSAFE。当这个参数是一个字符串时,它代表一个字符集。

在您的情况下,要修改 URI.escape 来转义单引号,您可以执行以下操作...

reserved_characters = /[^a-zA-Z0-9\-\.\_\~]/
URI.escape(YOUR_STRING, reserved_characters)

说明:有关规范的一些信息...

所有参数名称和值均使用 [rfc3986] 进行转义
百分比编码(%xx)机制。不在未保留的字符
字符集([rfc3986] 第 2.3 节)必须进行编码。中的字符
不得对非保留字符集进行编码。十六进制
编码中的字符必须是大写。文本名称和值必须
在根据 [rfc3629] 对它们进行百分比编码之前,将其编码为 utf-8 八位字节。

According to the docs, URI.escape(str [, unsafe]) uses a regexp that matches all symbols that must be replaced with codes. By default the method uses REGEXP::UNSAFE. When this argument is a String, it represents a character set.

In your case, to modify URI.escape to escape even the single quotes you can do something like this ...

reserved_characters = /[^a-zA-Z0-9\-\.\_\~]/
URI.escape(YOUR_STRING, reserved_characters)

Explanation: Some info on the spec ...

All parameter names and values are escaped using the [rfc3986]
percent- encoding (%xx) mechanism. Characters not in the unreserved
character set ([rfc3986] section 2.3) must be encoded. characters in
the unreserved character set must not be encoded. hexadecimal
characters in encodings must be upper case. text names and values must
be encoded as utf-8 octets before percent-encoding them per [rfc3629].

帅冕 2024-12-16 17:29:52

我知道这个问题已经得到解答,但我想要的东西略有不同,我想我不妨将其发布:我想保留网址中的“/”,但转义所有其他非标准字符。我是这样做的:

#public filename is a *nix filepath, 
#like `"/images/isn't/this a /horrible filepath/hello.png"`

public_filename.split("/").collect{|s| ERB::Util.url_encode(s)}.join("/")
=> "/images/isn%27t/this%20a%20/horrible%20filepath/hello.png"

当我为 AWS Cloudfront 编写缓存失效时,我需要转义单引号,AWS Cloudfront 不喜欢单引号并希望它们被转义。上面应该创建一个比标准 URI.escape 更安全的 uri,但它看起来仍然像一个 URI(CGI Escape 通过转义“/”来破坏 uri 格式)。

I know this has been answered, but what I wanted was something slightly different, and I thought I might as well post it up: I wanted to keep the "/" in the url, but escape all the other non-standard characters. I did it thus:

#public filename is a *nix filepath, 
#like `"/images/isn't/this a /horrible filepath/hello.png"`

public_filename.split("/").collect{|s| ERB::Util.url_encode(s)}.join("/")
=> "/images/isn%27t/this%20a%20/horrible%20filepath/hello.png"

I needed to escape the single quote as I was writing a cache invalidation for AWS Cloudfront, which didn't like the single quotes and expected them to be escaped. The above should make a uri which is more safe than the standard URI.escape but which still looks like a URI (CGI Escape breaks the uri format by escaping "/").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文