Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

$response->current_age() behaving unexpectedly

by mooseboy (Pilgrim)
on Dec 21, 2002 at 12:52 UTC ( #221620=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

Was just playing around with some of the code in Sean M Burke's Perl and LWP (nice book, BTW) and encountered some unexpected behaviour with one of the examples from chapter three which uses $response->current_age(). Here's the program:

#!/usr/bin/perl use warnings; use strict; use LWP; my $browser = LWP::UserAgent->new(); my $response = $browser->get(' +blk.gif', ':content_cb' => \&hex_dump, ':read_size_hint' => 123, ); print "Waiting a bit...\n"; sleep 10; my $age = $response->current_age(); print "$age\n"; # debug my $days = int($age/86400); $age -= $days * 86400; my $hours = int($age/3600); $age -= $hours * 3600; my $mins = int($age/60); $age -= $mins * 60; my $secs = $age; print "The document is $days days, $hours hours, $mins minutes, and $s +ecs seconds old.\n"; sub hex_dump { my ($data, $response) = @_; print length($data), " bytes:\n"; print ' ', unpack('H*', substr($data,0,16,'')), "\n" while length $data; return; }

On my machine (SuSE Linux 8.0, Perl 5.6.1, LWP 5.66), this program outputs "The document is 0 days, 8 hours, 57 minutes, and 28 seconds old."

But according to the HTTP::Response man page, $response->current_age

calculates the "current age" of the response as specified by draft-ietf-http-v11-spec-07 section 13.2.3. The age of a response is the time since it was sent by the origin server. The returned value is a number representing the age in seconds.

I'm puzzled as to why this program does not say the document is 10 seconds old. The only thing I can think of is that the "current age" of the document (about 9 hrs) is roughly the same as the time difference between my location (Austria) and O'Reilly in CA. Could that be the reason, or is it something else entirely?

Thanks in advance, mooseboy

Replies are listed 'Best First'.
Re: $response->current_age() behaving unexpectedly
by pg (Canon) on Dec 21, 2002 at 17:21 UTC
    1. This has nothing to do with the time difference. If you do a Dumper against your $response, you will see the Date field includes time-zone info:
      use Data::Dumper; ... print Dumper($response);
      When you said sleep(10), you didn't set the current_age to 10s, but just added ABOUT 10s on top of it.
    2. Someone between you and the origin server caches this page, not neccessary to be your direct ISP. The interesting result you got, does prove that current_age() serves its purpose very well. The reason HTTP header including this Date field is to record the time the page was generated on the origin server, and it should not be modified by any one in middle, thus can be used to measure a page's freshness.

      Thanks pg, I followed your Data::Dumper suggestion and also read up on the HTTP RFC 2068. Here's part of the header I get from a request to Perl Monks:

      'date' => 'Sun, 22 Dec 2002 10:14:58 GMT',
      'server' => 'Apache/1.3.26',
      'client-date' => 'Sun, 22 Dec 2002 10:12:17 GMT'

      I *think* I get it now, but the RFC is kinda dull ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://221620]
Approved by larsen
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-06-18 02:42 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (87 votes). Check out past polls.