Ruby 2升级破坏了Nokogiri和/或open-uri编码吗? [英] Ruby 2 Upgrade Breaks Nokogiri and/or open-uri Encoding?
问题描述
将Rails3.2 Ruby 1.9应用程序升级到Rails3.2 Ruby 2.1.2时,我有一个谜需要解决. Nokogiri似乎中断了,因为它使用open-uri更改了行为.没有宝石的版本被改变,只有红宝石的版本(这在OSX Mavericks上全部使用brew,gcc4等).
I have a mystery to solve when upgrading our Rails3.2 Ruby 1.9 app to a Rails3.2 Ruby 2.1.2 one. Nokogiri seems to break, in that it changes its behavior using open-uri. No gem versions are changed, just the ruby version (this is all on OSX Mavericks, using brew, gcc4 etc).
复制步骤:
$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.1.0]
$ rails console
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.18)
> feed = Nokogiri::XML(open(URI.encode("http://anyblog.wordpress.org/feed/")))
=> #(Document:0x3fcb82f08448 {
name = "document",
children = [
..
> feed.xpath("//item").count
=> 10
一切都很好!接下来,在将rvm更改为Ruby 2.1.2并安装捆绑软件之后.
So all good! Next, after a rvm change to Ruby 2.1.2 and a bundle install..
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
$ rails console
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.18)
> feed = Nokogiri::XML(open(URI.encode("http://anyblog.wordpress.org/feed/")))
=>
> feed.inspect
=> "#<Nokogiri::XML::Document:0x86a1f21c name=\"document\">"
> feed.xpath("//item").count
=> 0
因此,似乎"open"编码已更改,原因是gzip http流没有正确输入到nokogiri?我检查了nokogiri -v,它使用的是打包的xml库,而不是系统的.这是Open-uri Ruby 2.1.2问题吗?
So it looks like the 'open' encoding has changed, in that a gzip http stream isn't being fed correctly to nokogiri? I checked with a nokogiri -v and it is using the packaged xml libs rather than system ones. Is this a open-uri Ruby 2.1.2 issue?
另一种理论是,其中一颗宝石的猴子用猴子修补了开放式uri,将其固定在1.9中,而这又打破了2.1?请帮助或想法!
Another theory is that one of the gems has monkey patched open-uri to fix something in 1.9 and that is breaking 2.1? Help or ideas please!
这是不使用Nokogiri的更多信息,即认为这更多是在Ruby 2.1.2上的open-uri问题:
Here's more info not using Nokogiri, i.e. thinking this is more a open-uri issue on Ruby 2.1.2:
> open(url) {|f|
* f.each_line {|line| p line}
* p f.content_type
* p f.charset
* p f.content_encoding
* }
"\u001F\x8B\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003\xED\x9D\xDBr\eW\xB2\xA6\xAF\xED\xA7\xA8\xCD\u001E\xB7/$\u0010..
(snip)
3\xF3\xA79\xA7\xFAɗ\xFF\u000F\xEAo\x9C\u0014k\xE8\u0000\u0000"
"text/xml"
"utf-8"
["gzip"]
=> ["gzip"]
.. 1.9版本是可读的,即gzip已被应用.
..the 1.9 version was readable, i.e. gzip was applied already.
如果我进入一个干净的红宝石irb,它可以正常工作,因此它一定是我的rails gem中的某种东西,它正在将open-uri open的行为更改为不放气/gzip.我有很多宝石被引用..:(
If I go into a clean ruby irb it works ok, so it must be something in my rails gems that is changing the behavior of open-uri open to not deflate/gzip. I have a lot of gems referenced.. :(
推荐答案
好,这是一个答案,也许就是答案. Ruby 2更改了它在HTTP请求和zipping/deflate中使用标头的方式,但在某些时候,他们改变了主意,将其改为1.9的工作方式.在此期间,一些Rails宝石维护者对HTTP:Net进行了修补,以使他们的宝石可以同时在1.9和2.0上运行.那些猴子补丁仍然在旧版宝石中徘徊,并引起类似我看到的从1.9升级到2.1的问题.
Ok, here's an answer, and maybe the answer. Ruby 2 changed how it uses headers in HTTP requests and zipping/deflating, but at some point they changed their minds back and put it to be how 1.9 worked. In the interim some Rails gem maintainers monkey patched HTTP:Net to make their gems work on both 1.9 and 2.0. Those monkey patches still linger in older versions of gems and cause issues like I saw upgrading from 1.9 to 2.1
此处的问题和解决方案摘要:
A summary of the issue and solution here:
我们使用宝石right_aws,关于红宝石版本的问题的详细信息如下:
We use the gem right_aws, and the details of that issue with ruby versions is here:
https://github.com/sferik/twitter/issues/473
解决方案是使用此作为我们Gemfile中的gem引用来撤消猴子补丁:
The solution was to undo the monkey patch using this as a gem reference in our Gemfile:
gem 'right_http_connection', git: 'git://github.com/rightscale/right_http_connection.git', ref: '3359524d81'
背景阅读和更多信息:
https://github.com/rightscale/right_aws/issues/167
这篇关于Ruby 2升级破坏了Nokogiri和/或open-uri编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!