get file size before downloading via http

In the previous question I was shown how to specify the byte range in an http request (using HTTP::Request and LWP::UserAgent). This allows one to download a specific chunk of a while (e.g., bytes 10000 to 20000).

What I forgot to include in that question is: Can I get the size of the file before specifying the bytes range. Since I will always be retrieving the last x bytes in the file, I would like to get the total size so that I can calculate the range so that the request range is, in effect, total_size minus x to total_size.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Check the Content-Length response header from code like the following:
use LWP;
my $agent = LWP::UserAgent->new(); 
my $request = HTTP::Request->new(HEAD => ""); 
my $response = $agent->request($request); 
Response portion pertinent:
Content-Length: 3470512

Open in new window

drunnelsAuthor Commented:
I don't think this will work for my purposes. There is a very large file and I need to see the size of it before I request it. I believe that by the time you see the content length in the response, you've already gotten the file.
Negative.  Look carefully at the HTTP::Request->new() call.  We're specifying HEAD, not GET or POST.  That gets -only- the headers, nothing else.
Here's the one-liner version, so forgive the spacing, but I'll illustrate the entire response:

 perl -e 'use LWP;my $agent = LWP::UserAgent->new(); my $r
equest = HTTP::Request->new(HEAD => "
rabber_setup.exe"); my $response = $agent->request($request); print($response->a
HTTP/1.1 200 OK
Connection: close
Date: Wed, 15 Jul 2009 18:41:20 GMT
Accept-Ranges: bytes
ETag: "f7a5db-34f4b0-4a2e3ff4"
Server: Apache/1.3.41 (Unix)
Content-Length: 3470512
Content-Type: application/octet-stream
Last-Modified: Tue, 09 Jun 2009 10:56:52 GMT
Client-Date: Wed, 15 Jul 2009 18:41:20 GMT
Client-Response-Num: 1
#####  That's -all- it obtains.  Just headers, since we specify HEAD.

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
drunnelsAuthor Commented:
Sorry - missed the HEAD obviously.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.