Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Net:HTTP SSL bug or is it me?

I'm trying to download https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/174043_1701680_6450592_q.jpg using ruby, but getting a connection reset error. It downloads fine in a browser, but not in Ruby. Is this a bug in Ruby? A bug with Facebook?

This works fine for the majority of other facebook users. I'm on Ruby 1.8.7 and rails 2.3.11.

>> http = Net::HTTP.new("graph.facebook.com", Net::HTTP.https_default_port)
=> #<Net::HTTP graph.facebook.com:443 open=false>
>> http.use_ssl = true
=> true
>> http.start do |http|
?> response = Net::HTTP.get_response(URI.parse('https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/174043_1701680_6450592_q.jpg'))
>> end
warning: peer certificate won't be verified in this SSL session
Errno::ECONNRESET: An existing connection was forcibly closed by the remote host.
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:135:in `sysread'
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill'
        from c:/Ruby187/lib/ruby/1.8/timeout.rb:67:in `timeout'
        from c:/Ruby187/lib/ruby/1.8/timeout.rb:101:in `timeout'
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:126:in `readline'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:2028:in `read_status_line'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:2017:in `read_new'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:1051:in `request'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:948:in `request_get'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:380:in `get_response'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:543:in `start'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:379:in `get_response'
        from (irb):5
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:543:in `start'
        from (irb):4>>
like image 847
Brian Avatar asked Apr 19 '11 19:04

Brian


1 Answers

Here is a slightly revised version that works...

require 'uri'
require 'net/https'

url = URI.parse 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/174043_1701680_6450592_q.jpg'
http = Net::HTTP.new url.host, url.port
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true

http.start do |agent|
  p agent.get(url.path).read_body.length
end

and then...

    so ross$ ruby fbi
    1974
like image 129
DigitalRoss Avatar answered Sep 23 '22 05:09

DigitalRoss