禁止Ruby Open-URI重定向

k10s72fa  于 2022-09-21  发布在  Ruby
关注(0)|答案(4)|浏览(174)

我一直在开发这个简单的html解析器(用于学习目的)。

require 'open-uri'
puts "Enter URL to parse HTML: "
url = gets.chomp
puts "Enter tag to parse from: "
tag = gets.chomp
response = open(url).read
title1 = response.index(tag)
title2 = response.index(tag.insert(1,'/')) -1
result = response[(title1 + tag.length - 1)..title2]
print result

当我输入http://twitter.com时,我收到以下错误消息:

ERROR: `open_loop': redirection forbidden: http://twitter.com -> https://twitter.com/ (RuntimeError)
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:149:in `open_uri'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:704:in `open'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:34:in `open'
from /home/ubuntu/workspace/htmlparse.rb:6:in `<main>'

有什么建议或帮助吗?我是Ruby的新手,我知道其他的html解析模块,但我这样做是为了学习Ruby基础知识。谢谢。

qq24tv8q

qq24tv8q1#

看看open_uri_redirections宝石。

它为Ruby的OpenURI打了补丁,允许从HTTP重定向到HTTPS或从HTTPS重定向到HTTP。

uyhoqukh

uyhoqukh2#

您也可以捕获异常,然后使用‘https’url重试。

url = "http://classic.ona.io/api/v1/files/3538545?filename=gringgo/attachments/1485229166168.jpg"

uri = URI.parse(url)
tries = 3

begin
  uri.open(redirect: false)
rescue OpenURI::HTTPRedirect => redirect
  uri = redirect.uri # assigned from the "Location" response header
  retry if (tries -= 1) > 0
  raise
end

来源:https://twin.github.io/improving-open-uri/

vu8f3i0k

vu8f3i0k3#

Ruby 2.4修复了open-uri中的升级重定向(从http->HTTPS),因此现在:

RUBY_VERSION
=> "2.4.2"

require 'open-uri'
=> true

open('http://twitter.com')
=> #<Tempfile:/tmp/open-uri20170926-24254-1kflwxq>

来源:http://blog.bigbinary.com/2017/03/02/open-uri-in-ruby-2-4-allows-http-to-https-redirection.html

s6fujrry

s6fujrry4#

只需在源文件中覆盖方法reDirectable?fromopen-uri,该方法检查是否允许重定向,并返回Always True以允许所有情况下的重定向。

require 'open-uri'

def OpenURI.redirectable?(uri1, uri2)
  return true
end

url = "http://someurl.jpg"

URI.open(url)

相关问题