Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

EOFError: end of file reached #1

Open
edgedalmacio opened this Issue Jul 6, 2009 · 5 comments

Comments

Projects
None yet
5 participants

Hi, I'm new to ruby and I'm trying to use your gem to upload an image to ImageShack. I'm getting this error when I run the code below:

Execution:
MultipartPostTest.new

Source:
require 'rubygems'
require 'net/http/post/multipart'

class MultipartPostTest
def initialize
url = URI.parse('http://www.imageshack.us/upload_api.php')
File.open('c:/x.jpg') do |jpg|
req = Net::HTTP::Post::Multipart.new url.path,
"fileupload" => UploadIO.new(jpg, "image/jpeg", 'c:/x.jpg')
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end
end
end

Trace:
EOFError: end of file reached
from ruby/lib/ruby/1.8/net/protocol.rb:133:in sysread' from ruby/lib/ruby/1.8/net/protocol.rb:133:inrbuf_fill'
from ruby/lib/ruby/1.8/timeout.rb:56:in timeout' from ruby/lib/ruby/1.8/timeout.rb:76:intimeout'
from ruby/lib/ruby/1.8/net/protocol.rb:132:in rbuf_fill' from ruby/lib/ruby/1.8/net/protocol.rb:116:inreaduntil'
from ruby/lib/ruby/1.8/net/protocol.rb:126:in readline' from ruby/lib/ruby/1.8/net/http.rb:2029:inread_status_line'
from ruby/lib/ruby/1.8/net/http.rb:2018:in read_new' from ruby/lib/ruby/1.8/net/http.rb:1059:inrequest'
from test_project/lib/multipart_post_test.rb:11:in initialize' from ruby/lib/ruby/1.8/net/http.rb:547:instart'
from ruby/lib/ruby/1.8/net/http.rb:440:in start' from test_project/lib/multipart_post_test.rb:10:ininitialize'
from test_project/lib/multipart_post_test.rb:7:in open' from test_project/lib/multipart_post_test.rb:7:ininitialize'
from (irb):1:in `new'
from (irb):1>>

I had the same error so I broke down the code and stuck in a trace, similar to this:

url = URI.parse('http://www.imageshack.us/upload_api.php')
file = 'c:/x.jpg'
io = UploadIO.new(jpg, "image/jpeg", 'c:/x.jpg')
req = Net::HTTP::Post::Multipart.new url.path, io
http = Net::HTTP.new(url.host, url.port)
http.set_debug_output $stderr if true  # dump out stuff
res = http.start { |http| http.request(req) }
puts res

My problem occurred because I was posting via https - http worked fine - which I resolved by adding this line to the above code, before the 'http.start' line:

require 'net/https'
http.use_ssl = url.port == 443

Hope this helps.

dje29 commented Sep 12, 2011

This helped ;)
If someone has a better solution I'm open for proposal, but here is my final implementation.
It handles redirection and save the file into a temp file that is returned (unclosed)

   require 'uri'
   require 'net/http'
   require 'net/https'
   def http_fetch( uri_str, temp_file_name, limit = 10 )
     # You should choose better exception.
     raise ArgumentError, 'HTTP redirect too deep' if limit == 0

     url = URI.parse( uri_str )

     req = Net::HTTP::Get.new url.path

     http = Net::HTTP.new(url.host, url.port)
     if url.scheme == "https"
       http.use_ssl = url.port == 443
     end

     #http.set_debug_output $stderr if true  # dump out stuff
     begin
       response = http.start { |http| http.request(req) }
       case response
       when Net::HTTPRedirection 
       then
         http_fetch(response['location'], temp_file_name, limit - 1 )

       when Net::HTTPSuccess
       then
         file = ::Tempfile.new( temp_file_name )
         file.binmode
         file.unlink
         file.write( response.body )
         file.rewind
         file
       else
         response.error!
       end
     end
   rescue EOFError
     # Reached the end of the response
   end
Collaborator

ioquatix commented May 3, 2017

Can you please confirm if this is still an issue?

I tried all the above methods to no avail ... does anyone know what the real problem is ... looks like in this context does not have anything to do with net/http(s) because I see this error on following line:

file = 'iron_man.jpg'
io = UploadIO.new(file, 'image/jpeg')

do we know why is that happening?

Collaborator

ioquatix commented Aug 25, 2017

What error are you seeing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment