tags:

views:

1885

answers:

3

I only need to download the first few kilobytes of a file via HTTP.

I tried

require 'open-uri'
url = 'http://example.com/big-file.dat'
file = open(url)
content = file.read(limit)

But it actually downloads the full file.

A: 

Check out this link. You might be able to abuse the methods in there to interrupt downloading/throw away the rest of the result after a preset limit.

Joel Meador
Thanks for interesting point
taro
+3  A: 

This seems to work when using sockets:

require 'socket'                  
host = "download.thinkbroadband.com"                 
path = "/1GB.zip" # get 1gb sample file
request = "GET #{path} HTTP/1.0\r\n\r\n"
socket = TCPSocket.open(host,80) 
socket.print(request)        

# find beginning of response body
buffer = ""                    
while !buffer.match("\r\n\r\n") do
  buffer += socket.read(1)  
end           

response = socket.read(100) #read first 100 bytes of body
puts response

I'm curious if there is a "ruby way".

Michel de Graaf
Hi Michel, for some reason whenever I try a file such as `http://www.forcefieldpr.com/asdyoucantbealone.mp3`, which works in the browser, I keep getting a 404 html page. Would this be to do with the request?
Aaron Moodie
A: 

Odd, the following code worked for me:

require 'open-uri'

uri = URI.parse("http://www.google.com")
limit = 20

open(uri) do |file| 
  puts file.read(limit)
end

It did outputed only the first 20 chars from the response.

pschneider
I think this still tries to get the entire file before outputting any text.
Joel Meador