home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _d21206610d4ed1f33a82b9f45610d01d < prev    next >
Text File  |  2004-06-01  |  12KB  |  436 lines

  1. package HTTP::Response;
  2.  
  3. # $Id: Response.pm,v 1.49 2004/04/09 20:30:41 gisle Exp $
  4.  
  5. require HTTP::Message;
  6. @ISA = qw(HTTP::Message);
  7. $VERSION = sprintf("%d.%02d", q$Revision: 1.49 $ =~ /(\d+)\.(\d+)/);
  8.  
  9. use strict;
  10. use HTTP::Status ();
  11.  
  12.  
  13.  
  14. sub new
  15. {
  16.     my($class, $rc, $msg, $header, $content) = @_;
  17.     my $self = $class->SUPER::new($header, $content);
  18.     $self->code($rc);
  19.     $self->message($msg);
  20.     $self;
  21. }
  22.  
  23.  
  24. sub parse
  25. {
  26.     my($class, $str) = @_;
  27.     my $status_line;
  28.     if ($str =~ s/^(.*)\n//) {
  29.     $status_line = $1;
  30.     }
  31.     else {
  32.     $status_line = $str;
  33.     $str = "";
  34.     }
  35.  
  36.     my $self = $class->SUPER::parse($str);
  37.     my($protocol, $code, $message) = split(' ', $status_line, 3);
  38.     $self->protocol($protocol) if $protocol;
  39.     $self->code($code) if defined($code);
  40.     $self->message($message) if defined($message);
  41.     $self;
  42. }
  43.  
  44.  
  45. sub clone
  46. {
  47.     my $self = shift;
  48.     my $clone = bless $self->SUPER::clone, ref($self);
  49.     $clone->code($self->code);
  50.     $clone->message($self->message);
  51.     $clone->request($self->request->clone) if $self->request;
  52.     # we don't clone previous
  53.     $clone;
  54. }
  55.  
  56.  
  57. sub code      { shift->_elem('_rc',      @_); }
  58. sub message   { shift->_elem('_msg',     @_); }
  59. sub previous  { shift->_elem('_previous',@_); }
  60. sub request   { shift->_elem('_request', @_); }
  61.  
  62.  
  63. sub status_line
  64. {
  65.     my $self = shift;
  66.     my $code = $self->{'_rc'}  || "000";
  67.     my $mess = $self->{'_msg'} || HTTP::Status::status_message($code) || "?";
  68.     return "$code $mess";
  69. }
  70.  
  71.  
  72. sub base
  73. {
  74.     my $self = shift;
  75.     my $base = $self->header('Content-Base')     ||  # used to be HTTP/1.1
  76.                $self->header('Content-Location') ||  # HTTP/1.1
  77.                $self->header('Base');                # HTTP/1.0
  78.     return $HTTP::URI_CLASS->new_abs($base, $self->request->uri);
  79.     # So yes, if $base is undef, the return value is effectively
  80.     # just a copy of $self->request->uri.
  81. }
  82.  
  83.  
  84. sub as_string
  85. {
  86.     require HTTP::Status;
  87.     my $self = shift;
  88.     my($eol) = @_;
  89.     $eol = "\n" unless defined $eol;
  90.  
  91.     my $code = $self->code;
  92.     my $status_message = HTTP::Status::status_message($code) || "Unknown code";
  93.     my $message = $self->message || "";
  94.  
  95.     my $status_line = "$code";
  96.     my $proto = $self->protocol;
  97.     $status_line = "$proto $status_line" if $proto;
  98.     $status_line .= " ($status_message)" if $status_message ne $message;
  99.     $status_line .= " $message";
  100.  
  101.     return join($eol, $status_line, $self->SUPER::as_string(@_));
  102. }
  103.  
  104.  
  105. sub is_info     { HTTP::Status::is_info     (shift->{'_rc'}); }
  106. sub is_success  { HTTP::Status::is_success  (shift->{'_rc'}); }
  107. sub is_redirect { HTTP::Status::is_redirect (shift->{'_rc'}); }
  108. sub is_error    { HTTP::Status::is_error    (shift->{'_rc'}); }
  109.  
  110.  
  111. sub error_as_HTML
  112. {
  113.     my $self = shift;
  114.     my $title = 'An Error Occurred';
  115.     my $body  = $self->status_line;
  116.     return <<EOM;
  117. <HTML>
  118. <HEAD><TITLE>$title</TITLE></HEAD>
  119. <BODY>
  120. <H1>$title</H1>
  121. $body
  122. </BODY>
  123. </HTML>
  124. EOM
  125. }
  126.  
  127.  
  128. sub current_age
  129. {
  130.     my $self = shift;
  131.     # Implementation of RFC 2616 section 13.2.3
  132.     # (age calculations)
  133.     my $response_time = $self->client_date;
  134.     my $date = $self->date;
  135.  
  136.     my $age = 0;
  137.     if ($response_time && $date) {
  138.     $age = $response_time - $date;  # apparent_age
  139.     $age = 0 if $age < 0;
  140.     }
  141.  
  142.     my $age_v = $self->header('Age');
  143.     if ($age_v && $age_v > $age) {
  144.     $age = $age_v;   # corrected_received_age
  145.     }
  146.  
  147.     my $request = $self->request;
  148.     if ($request) {
  149.     my $request_time = $request->date;
  150.     if ($request_time) {
  151.         # Add response_delay to age to get 'corrected_initial_age'
  152.         $age += $response_time - $request_time;
  153.     }
  154.     }
  155.     if ($response_time) {
  156.     $age += time - $response_time;
  157.     }
  158.     return $age;
  159. }
  160.  
  161.  
  162. sub freshness_lifetime
  163. {
  164.     my $self = shift;
  165.  
  166.     # First look for the Cache-Control: max-age=n header
  167.     my @cc = $self->header('Cache-Control');
  168.     if (@cc) {
  169.     my $cc;
  170.     for $cc (@cc) {
  171.         my $cc_dir;
  172.         for $cc_dir (split(/\s*,\s*/, $cc)) {
  173.         if ($cc_dir =~ /max-age\s*=\s*(\d+)/i) {
  174.             return $1;
  175.         }
  176.         }
  177.     }
  178.     }
  179.  
  180.     # Next possibility is to look at the "Expires" header
  181.     my $date = $self->date || $self->client_date || time;      
  182.     my $expires = $self->expires;
  183.     unless ($expires) {
  184.     # Must apply heuristic expiration
  185.     my $last_modified = $self->last_modified;
  186.     if ($last_modified) {
  187.         my $h_exp = ($date - $last_modified) * 0.10;  # 10% since last-mod
  188.         if ($h_exp < 60) {
  189.         return 60;  # minimum
  190.         }
  191.         elsif ($h_exp > 24 * 3600) {
  192.         # Should give a warning if more than 24 hours according to
  193.         # RFC 2616 section 13.2.4, but I don't know how to do it
  194.         # from this function interface, so I just make this the
  195.         # maximum value.
  196.         return 24 * 3600;
  197.         }
  198.         return $h_exp;
  199.     }
  200.     else {
  201.         return 3600;  # 1 hour is fallback when all else fails
  202.     }
  203.     }
  204.     return $expires - $date;
  205. }
  206.  
  207.  
  208. sub is_fresh
  209. {
  210.     my $self = shift;
  211.     $self->freshness_lifetime > $self->current_age;
  212. }
  213.  
  214.  
  215. sub fresh_until
  216. {
  217.     my $self = shift;
  218.     return $self->freshness_lifetime - $self->current_age + time;
  219. }
  220.  
  221. 1;
  222.  
  223.  
  224. __END__
  225.  
  226. =head1 NAME
  227.  
  228. HTTP::Response - HTTP style response message
  229.  
  230. =head1 SYNOPSIS
  231.  
  232. Response objects are returned by the request() method of the C<LWP::UserAgent>:
  233.  
  234.     # ...
  235.     $response = $ua->request($request)
  236.     if ($response->is_success) {
  237.         print $response->content;
  238.     }
  239.     else {
  240.         print STDERR $response->status_line, "\n";
  241.     }
  242.  
  243. =head1 DESCRIPTION
  244.  
  245. The C<HTTP::Response> class encapsulates HTTP style responses.  A
  246. response consists of a response line, some headers, and a content
  247. body. Note that the LWP library uses HTTP style responses even for
  248. non-HTTP protocol schemes.  Instances of this class are usually
  249. created and returned by the request() method of an C<LWP::UserAgent>
  250. object.
  251.  
  252. C<HTTP::Response> is a subclass of C<HTTP::Message> and therefore
  253. inherits its methods.  The following additional methods are available:
  254.  
  255. =over 4
  256.  
  257. =item $r = HTTP::Response->new( $code )
  258.  
  259. =item $r = HTTP::Response->new( $code, $msg )
  260.  
  261. =item $r = HTTP::Response->new( $code, $msg, $header )
  262.  
  263. =item $r = HTTP::Response->new( $code, $msg, $header, $content )
  264.  
  265. Constructs a new C<HTTP::Response> object describing a response with
  266. response code $code and optional message $msg.  The optional $header
  267. argument should be a reference to an C<HTTP::Headers> object or a
  268. plain array reference of key/value pairs.  The optional $content
  269. argument should be a string of bytes.  The meaning these arguments are
  270. described below.
  271.  
  272. =item $r = HTTP::Response->parse( $str )
  273.  
  274. This constructs a new response object by parsing the given string.
  275.  
  276. =item $r->code
  277.  
  278. =item $r->code( $code )
  279.  
  280. This is used to get/set the code attribute.  The code is a 3 digit
  281. number that encode the overall outcome of a HTTP response.  The
  282. C<HTTP::Status> module provide constants that provide mnemonic names
  283. for the code attribute.
  284.  
  285. =item $r->message
  286.  
  287. =item $r->message( $message )
  288.  
  289. This is used to get/set the message attribute.  The message is a short
  290. human readable single line string that explains the response code.
  291.  
  292. =item $r->header( $field )
  293.  
  294. =item $r->header( $field => $value )
  295.  
  296. This is used to get/set header values and it is inherited from
  297. C<HTTP::Headers> via C<HTTP::Message>.  See L<HTTP::Headers> for
  298. details and other similar methods that can be used to access the
  299. headers.
  300.  
  301. =item $r->content
  302.  
  303. =item $r->content( $content )
  304.  
  305. This is used to get/set the content and it is inherited from the
  306. C<HTTP::Message> base class.  See L<HTTP::Message> for details and
  307. other methods that can be used to access the content.
  308.  
  309. =item $r->request
  310.  
  311. =item $r->request( $request )
  312.  
  313. This is used to get/set the request attribute.  The request attribute
  314. is a reference to the the request that caused this response.  It does
  315. not have to be the same request passed to the $ua->request() method,
  316. because there might have been redirects and authorization retries in
  317. between.
  318.  
  319. =item $r->previous
  320.  
  321. =item $r->previous( $response )
  322.  
  323. This is used to get/set the previous attribute.  The previous
  324. attribute is used to link together chains of responses.  You get
  325. chains of responses if the first response is redirect or unauthorized.
  326. The value is C<undef> if this is the first response in a chain.
  327.  
  328. =item $r->status_line
  329.  
  330. Returns the string "E<lt>code> E<lt>message>".  If the message attribute
  331. is not set then the official name of E<lt>code> (see L<HTTP::Status>)
  332. is substituted.
  333.  
  334. =item $r->base
  335.  
  336. Returns the base URI for this response.  The return value will be a
  337. reference to a URI object.
  338.  
  339. The base URI is obtained from one the following sources (in priority
  340. order):
  341.  
  342. =over 4
  343.  
  344. =item 1.
  345.  
  346. Embedded in the document content, for instance <BASE HREF="...">
  347. in HTML documents.
  348.  
  349. =item 2.
  350.  
  351. A "Content-Base:" or a "Content-Location:" header in the response.
  352.  
  353. For backwards compatibility with older HTTP implementations we will
  354. also look for the "Base:" header.
  355.  
  356. =item 3.
  357.  
  358. The URI used to request this response. This might not be the original
  359. URI that was passed to $ua->request() method, because we might have
  360. received some redirect responses first.
  361.  
  362. =back
  363.  
  364. When the LWP protocol modules produce the HTTP::Response object, then
  365. any base URI embedded in the document (step 1) will already have
  366. initialized the "Content-Base:" header. This means that this method
  367. only performs the last 2 steps (the content is not always available
  368. either).
  369.  
  370. =item $r->as_string
  371.  
  372. =item $r->as_string( $eol )
  373.  
  374. Returns a textual representation of the response.
  375.  
  376. =item $r->is_info
  377.  
  378. =item $r->is_success
  379.  
  380. =item $r->is_redirect
  381.  
  382. =item $r->is_error
  383.  
  384. These methods indicate if the response was informational, successful, a
  385. redirection, or an error.  See L<HTTP::Status> for the meaning of these.
  386.  
  387. =item $r->error_as_HTML
  388.  
  389. Returns a string containing a complete HTML document indicating what
  390. error occurred.  This method should only be called when $r->is_error
  391. is TRUE.
  392.  
  393. =item $r->current_age
  394.  
  395. Calculates the "current age" of the response as specified by RFC 2616
  396. section 13.2.3.  The age of a response is the time since it was sent
  397. by the origin server.  The returned value is a number representing the
  398. age in seconds.
  399.  
  400. =item $r->freshness_lifetime
  401.  
  402. Calculates the "freshness lifetime" of the response as specified by
  403. RFC 2616 section 13.2.4.  The "freshness lifetime" is the length of
  404. time between the generation of a response and its expiration time.
  405. The returned value is a number representing the freshness lifetime in
  406. seconds.
  407.  
  408. If the response does not contain an "Expires" or a "Cache-Control"
  409. header, then this function will apply some simple heuristic based on
  410. 'Last-Modified' to determine a suitable lifetime.
  411.  
  412. =item $r->is_fresh
  413.  
  414. Returns TRUE if the response is fresh, based on the values of
  415. freshness_lifetime() and current_age().  If the response is no longer
  416. fresh, then it has to be refetched or revalidated by the origin
  417. server.
  418.  
  419. =item $r->fresh_until
  420.  
  421. Returns the time when this entity is no longer fresh.
  422.  
  423. =back
  424.  
  425. =head1 SEE ALSO
  426.  
  427. L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Status>, L<HTTP::Request>
  428.  
  429. =head1 COPYRIGHT
  430.  
  431. Copyright 1995-2004 Gisle Aas.
  432.  
  433. This library is free software; you can redistribute it and/or
  434. modify it under the same terms as Perl itself.
  435.  
  436.