# Partial Read From URL

**URL:** <https://www.ruby-forum.com/t/partial-read-from-url/152742>\
**Category:** Ruby\
**Created:** [November 7, 2008, 12:26am UTC](https://www.ruby-forum.com/t/partial-read-from-url/152742 "2008-11-07T00:26:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [November 7, 2008, 12:26am UTC](https://www.ruby-forum.com/t/partial-read-from-url/152742/1 "2008-11-07T00:26:53Z")

</div>

Can anyone see a problem with this code to read only 20k from a URL? The  
http connection will get closed, right?

Is there a better way to do this? My goal is to download the start of  
mp3 files and then estimate their duration without downloading the  
entire file.

def partial\_uri\_read(uri)  
u = URI.parse(uri)  
s = “”  
Net::HTTP.start(u.host, u.port) do |http|  
http.request\_get(u.path) do |response|  
response.read\_body do |chunk|  
s \<\< chunk  
return s if s.size \> 20000  
end  
end  
end  
s  
end

Thanks  
Elliot T.

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [November 7, 2008, 12:34am UTC](https://www.ruby-forum.com/t/partial-read-from-url/152742/2 "2008-11-07T00:34:27Z")

</div>

On Nov 6, 2008, at 4:24 PM, Elliot T. wrote:

> s = “”
> 
> ## Thanks Elliot T.
> 
> Posted via [http://www.ruby-forum](http://www.ruby-forum).

maybe try

require ‘open-uri’

uri = …

open(uri){|fd| fd.read 20000}

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [November 7, 2008, 4:34am UTC](https://www.ruby-forum.com/t/partial-read-from-url/152742/3 "2008-11-07T04:34:11Z")

</div>

Elliot T. wrote:

> Net::HTTP.start(u.host, u.port) do |http|  
> Thanks  
> Elliot T.

You can try using the Range header in HTTP. Not all servers have it  
enabled I think (or that’s what 3 minutes playing with telnet tells me).  
Using range you can define which bytes of the file you want to  
download, so the whole file is not downloaded. You can use this to  
download the header and use the length of the file to estimate its  
length.

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [November 7, 2008, 12:36am UTC](https://www.ruby-forum.com/t/partial-read-from-url/152742/4 "2008-11-07T00:36:21Z")

</div>

Ara Howard wrote:

> On Nov 6, 2008, at 4:24 PM, Elliot T. wrote:
> 
> > s = “”
> > 
> > ## Thanks Elliot T.
> > 
> > Posted via [http://www.ruby-forum](http://www.ruby-forum).
> 
> maybe try
> 
> require ‘open-uri’
> 
> uri = …
> 
> open(uri){|fd| fd.read 20000}

I did try that, but it downloads the entire file in my tests. I tried it  
on a large file and it takes minutes to return, whereas my code returns  
in a couple seconds. Am I missing something?

> ruby -v  
> ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

Elliot
