- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
URI.escape
和 CGI.escape
有什么区别,我应该使用哪一个?
最佳答案
斧头和剑有什么区别,我应该使用哪一种?好吧,这取决于您需要做什么。
URI.escape
应该将字符串 (URL) 编码为所谓的“Percent-encoding ”。
CGI::escape
来自 CGI规范,它描述了数据应该如何在网络服务器和应用程序之间编码/解码。
现在,假设您需要在应用中转义 URI。这是一个更具体的用例。为此,Ruby 社区多年来一直使用 URI.escape
。 URI.escape
的问题在于它无法处理 RFC-3896 规范。
URI.escape 'http://google.com/foo?bar=at#anchor&title=My Blog & Your Blog'
# => "http://google.com/foo?bar=at%23anchor&title=My%20Blog%20&%20Your%20Blog"
URI.escape
被标记为过时:
Moreover current URI.encode is simple gsub. But I think it should split a URI to components, then escape each components, and finally join them.
So current URI.encode is considered harmful and deprecated. This will be removed or change behavior drastically.
What is the replacement at this time?
正如我上面所说,当前的 URI.encode 在规范级别上是错误的。所以我们 不会提供确切的替代品。更换将因它而异 用例。
不幸的是,文档中只字未提,了解它的唯一方法是检查源代码,或者运行带有详细级别警告的脚本 (-wW2
) (或者使用一些 google-fu)。
一些 proposed使用 CGI::Escape
作为查询参数,因为您无法转义整个 URI:
CGI::escape 'http://google.com/foo?bar=at#anchor&title=My Blog & Your Blog'
# => "http%3A%2F%2Fgoogle.com%2Ffoo%3Fbar%3Dat%23anchor%26title%3DMy+Blog+%26+Your+Blog"
CGI::escape
应该只用于查询参数,但结果将再次违反规范。实际上,最常见的用例是转义表单数据,例如在发送 application/x-www-form-urlencoded
POST 请求时。
还提到 WEBrick::HTTPUtils.escape
没有太大改进(同样它只是一个简单的 gsub
,在我看来,它甚至比 URI.escape
):
WEBrick::HTTPUtils.escape 'http://google.com/foo?bar=at#anchor&title=My Blog & Your Blog'
# => "http://google.com/foo?bar=at%23anchor&title=My%20Blog%20&%20Your%20Blog"
最接近规范的似乎是 Addressable gem :
require 'addressable/uri'
Addressable::URI.escape 'http://google.com/foo?bar=at#anchor&title=My Blog & Your Blog'
# => "http://google.com/foo?bar=at#anchor&title=My%20Blog%20&%20Your%20Blog"
请注意,与之前的所有选项不同,Addressable 不会转义 #
,这是预期的行为。您希望将 #
散列保留在 URI 路径中,但不保留在 URI 查询中。
剩下的唯一问题是我们没有正确地转义我们的查询参数,这使我们得出结论:我们不应该对整个 URI 使用单一方法,因为没有完美的解决方案(到目前为止)。如您所见,&
并未从“我的博客和您的博客”中转义。我们需要对查询参数使用不同形式的转义,用户可以在 URL 中放置具有特殊含义的不同字符。输入 URL 编码。 URL 编码应该用于每个“可疑”的查询值,类似于 ERB::Util.url_encode
所做的:
ERB::Util.url_encode "My Blod & Your Blog"
# => "My%20Blod%20%26%20Your%20Blog""
这很酷,但我们已经需要 Addressable:
uri = Addressable::URI.parse("http://www.go.com/foo")
# => #<Addressable::URI:0x186feb0 URI:http://www.go.com/foo>
uri.query_values = {title: "My Blog & Your Blog"}
uri.normalize.to_s
# => "http://www.go.com/foo?title=My%20Blog%20%26%20Your%20Blog"
结论:
URI.escape
或类似的CGI::escape
关于ruby - URI.escape 和 CGI.escape 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2824126/
一个简单的GNU m4问题,但是我找不到正确的答案。我想在代码部分的开始/结束处打印一个markdown header : ``` echo Hello ``` 如何创建包含3个反引号的GNU M4宏
我有一个 shell 变量(我们将调用 x),其中包含一个带有 shell 元字符的字符串。例如,我可能有 abc "def's" ghi 设置为 x='abc "def'\''s" ghi'
我有一个 shell 变量(我们将调用 x),其中包含一个带有 shell 元字符的字符串。例如,我可能有 abc "def's" ghi 设置为 x='abc "def'\''s" ghi'
执行以下行时: df = df[df['Directory'].str.contains("C:\Windows\System32\Tasks")] 我收到以下错误: File "/Users/pat
我正在使用 python-social-auth 从我的 Django 应用程序登录社交网络。在我的本地机器上一切正常,但是当我部署到服务器时出现以下错误: oauthlib.oauth1.rfc58
URI.escape 和 CGI.escape 有什么区别,我应该使用哪一个? 最佳答案 斧头和剑有什么区别,我应该使用哪一种?好吧,这取决于您需要做什么。 URI.escape 应该将字符串 (UR
我今天安装了 Zenwalk Linux。我发现 Escape 键没有绑定(bind)到任何东西(至少据我所知),而是 Escape 通常会做什么(关闭程序,打开 vim 命令行等)Shift-Esc
我遇到过一种情况,闭包可能在函数 f1 内部调用(闭包被传递到其中),或者可能会传递给其他函数 f2 >。 所以现在我想知道应该如何定义这个闭包的转义行为。我的意思是我应该放 @escaping 还是
在 js/jQuery 中有没有办法拥有这两种按键组合? ESCape 键 和 SHIFT + ESCape 键 当我实现它时使用: document.onkeydown = function(e){
我正在尝试将字符串中的任何换行符或转义换行符规范化为转义的 unix 换行符。我不明白为什么这不起作用: Pattern EOL = Pattern.compile("(\\\\r)?\\\\n|\r
swift 4.2,Xcode 10.1 在我正在处理的订单处理应用程序中,用户可以搜索已处理或提交的订单。发生这种情况时,它将检查是否有订单缓存,如果没有,它将使用异步 API 请求重新填充该缓存,
我的几个网页名称中包含以下字符&,例如“Shipping & Deliveries”等 我的架构标记注入(inject)了 GTM (JSON-LD),但在 SDTT 中我收到以下错误: Uncate
自版本 1.9.0 起,Twig为 escape 过滤器提供 html_attr 策略(参见 documentation )。 html 策略使用 htmlspecialchars PHP 函数(通过
我有一个像这样的 View Controller : class PublicationListViewController: UIViewController { var publicati
这是我的代码: class Main { init() { let x = Sub(s: foo) } func foo(completion: @escapi
我正在尝试将闭包用于一阶谓词演算,并且我打算定义以下函数: func ASSUM(p: @escaping Pred) -> (Pred) -> Pred { return { q in AN
我有关于 ezSQL_mysql 和 ezSQLcore 的问题,它可能是 PHP 版本不兼容,我分享下面的代码。我应该使用什么版本的 PHP 或者我应该做什么来定制 mysqli? (我的PHP版本
我有以下函数,其中有完成处理程序,但出现此错误: Closure use of non-escaping parameter may allow it to escape 这是我的代码: func m
我正在使用 Swift-VectorBoolean 库,它目前在 Swift 3.2 上,尚未针对 Swift 4.2 进行更新,但应该仍可在 Xcode 10 上运行。在 Xcode 9 上运行它,
我想用这个 ansible 命令在 nrpe.cfg 中插入一个 nrpe 命令 check_tomcat_threads.pl -H localhost -p 30011 -C '"http-bio
我是一名优秀的程序员,十分优秀!