Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks, I have a favor to ask. How can I find the content length for a specific page of a remote address.

This is the code I'm using but its not returning a value for content length.

#/perl/bin/perl.exe -w use strict; my %env_info=( REMOTE_ADDR => "http://www.google.com", CONTENT_LENGTH => " " ); my $name; foreach $name (keys %ENV) { $env_info{$name} = "info provided by the server" unless exists $env_info{$name}; } foreach $name (sort keys %env_info) { my $info = $env_info{$name}; my $value = $ENV{$name} || print "Not defined\n"; print "$name INFO: $info VALUE: $value\n"; }

Replies are listed 'Best First'.
Re: Content Length
by gjb (Vicar) on Nov 11, 2002 at 14:28 UTC

    If this is all of the code you're using, it shouldn't come as a suprise that no useful information is returned. You never request any information, you just try to read some environment variables of the local machine the script happens to be running on.

    Maybe it would help if you'd clearly state what you'd like to obtain since I have the impression you're mixing up CGI with user agent functionality.

    Are you trying to obtain the number of characters of a web page that is specified by an URL?

    Regards, -gjb-

      I'm trying to obtain the content length in bytes or lines for google.com, or any other site specified
        Take a look at LWP, along with the associated HTTP::Request, HTTP::Response, and HTTP::Headers object
        use LWP; use HTTP::Request; my $ua = LWP::UserAgent->new(); my $request = HTTP::Request->new( GET => 'http://www.google.com' ); my $response = $ua->request( $request ); my $headers = $response->headers(); print $headers->header( 'Content-Length');
Re: Content Length
by fglock (Vicar) on Nov 11, 2002 at 14:48 UTC

    I suggest you study the HEAD script that comes with LWP.

    prompt> HEAD http://www.google.com 200 OK Cache-Control: private Connection: close Date: Mon, 11 Nov 2002 14:45:43 GMT Server: GWS/2.0 Content-Length: 2949 Content-Type: text/html ... more
Re: Content Length
by LTjake (Prior) on Nov 11, 2002 at 14:48 UTC
    You'll want to investigate the LWP modules. example:
    use strict; use LWP::UserAgent; my $request = new HTTP::Request('GET'=>"http://www.google.com" ); my $ua = new LWP::UserAgent; my $response = $ua->request($request) || return "ERROR\t$!"; print $response->content_length;
    in return i get:
    2927


    --
    Rock is dead. Long live paper and scissors!
Re: Content Length
by Jaap (Curate) on Nov 11, 2002 at 14:52 UTC
    You'd have to use something like LWP::UserAgent, which is a part of libwww.
    And then you'd need some code like this:
    require LWP::UserAgent; my $ua = LWP::UserAgent->new(timeout => 30); my $headRequest = HTTP::Request->new(HEAD => 'http://google.com/'); my $headResult = $ua->request($headRequest); print $headResult->header('Content-Length')
Re: Content Length
by ajt (Prior) on Nov 11, 2002 at 14:47 UTC

    Anon,

    You may find the following node useful "size of page", which as some examples and a few ideas.


    --
    ajt
Re: Content Length
by pg (Canon) on Nov 11, 2002 at 16:37 UTC
    According to HTTP/1.1 standard, this field is not mandatory. A solution could be first try to determine the size base on Content-Length, but calculate by yourself, if the field is not present.