home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / HTTP / Message.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  3.7 KB  |  180 lines

  1.  
  2. package HTTP::Message;
  3.  
  4. =head1 NAME
  5.  
  6. HTTP::Message - Class encapsulating HTTP messages
  7.  
  8. =head1 SYNOPSIS
  9.  
  10.  package HTTP::Request;  # or HTTP::Response
  11.  require HTTP::Message;
  12.  @ISA=qw(HTTP::Message);
  13.  
  14. =head1 DESCRIPTION
  15.  
  16. A C<HTTP::Message> object contains some headers and a content (body).
  17. The class is abstract, i.e. it only used as a base class for
  18. C<HTTP::Request> and C<HTTP::Response> and should never instantiated
  19. as itself.
  20.  
  21. =head1 METHODS
  22.  
  23. =cut
  24.  
  25.  
  26. require HTTP::Headers;
  27. require Carp;
  28. use strict;
  29. use vars qw($AUTOLOAD);
  30.  
  31. =head2 $mess = new HTTP::Message;
  32.  
  33. This is the object constructor.  It should only be called internally
  34. by this library.  External code should construct C<HTTP::Request> or
  35. C<HTTP::Response> objects.
  36.  
  37. =cut
  38.  
  39. sub new
  40. {
  41.     my($class, $header, $content) = @_;
  42.     if (defined $header) {
  43.     Carp::croak("Bad header argument") unless ref $header;
  44.     $header = $header->clone;
  45.     } else {
  46.     $header = new HTTP::Headers;
  47.     }
  48.     $content = '' unless defined $content;
  49.     bless {
  50.     '_headers' => $header,
  51.     '_content' => $content,
  52.     }, $class;
  53. }
  54.  
  55.  
  56. =head2 $mess->clone()
  57.  
  58. Returns a copy of the object.
  59.  
  60. =cut
  61.  
  62. sub clone
  63. {
  64.     my $self  = shift;
  65.     my $clone = new HTTP::Message $self->{'_headers'}, $self->{'_content'};
  66.     $clone;
  67. }
  68.  
  69. =head2 $mess->protocol([$proto])
  70.  
  71. Sets the HTTP protocol used for the message.  The protocol() is a string
  72. like "HTTP/1.0" or "HTTP/1.1".
  73.  
  74. =cut
  75.  
  76. sub protocol { shift->_elem('_protocol',  @_); }
  77.  
  78. =head2 $mess->content([$content])
  79.  
  80. The content() method sets the content if an argument is given.  If no
  81. argument is given the content is not touched.  In either case the
  82. previous content is returned.
  83.  
  84. =head2 $mess->add_content($data)
  85.  
  86. The add_content() methods appends more data to the end of the previous
  87. content.
  88.  
  89. =cut
  90.  
  91. sub content   { shift->_elem('_content',  @_); }
  92.  
  93. sub add_content
  94. {
  95.     my $self = shift;
  96.     if (ref($_[0])) {
  97.     $self->{'_content'} .= ${$_[0]};  # for backwards compatability
  98.     } else {
  99.     $self->{'_content'} .= $_[0];
  100.     }
  101. }
  102.  
  103. =head2 $mess->content_ref
  104.  
  105. The content_ref() method will return a reference to content string.
  106. It can be more efficient to access the content this way if the content
  107. is huge, and it can be used for direct manipulation of the content,
  108. for instance:
  109.  
  110.   ${$res->content_ref} =~ s/\bfoo\b/bar/g;
  111.  
  112. =cut
  113.  
  114. sub content_ref
  115. {
  116.     my $self = shift;
  117.     \$self->{'_content'};
  118. }
  119.  
  120. sub as_string
  121. {
  122.     "";  # To be overridden in subclasses
  123. }
  124.  
  125. =head1 HEADER METHODS
  126.  
  127. All unknown C<HTTP::Message> methods are delegated to the
  128. C<HTTP::Headers> object that is part of every message.  This allows
  129. convenient access to these methods.  Refer to L<HTTP::Headers> for
  130. details of these methods:
  131.  
  132.   $mess->header($field => $val);
  133.   $mess->scan(&doit);
  134.   $mess->push_header($field => $val);
  135.   $mess->remove_header($field);
  136.  
  137.   $mess->date;
  138.   $mess->expires;
  139.   $mess->if_modified_since;
  140.   $mess->last_modified;
  141.   $mess->content_type;
  142.   $mess->content_encoding;
  143.   $mess->content_length;
  144.   $mess->title;
  145.   $mess->user_agent;
  146.   $mess->server;
  147.   $mess->from;
  148.   $mess->referer;
  149.   $mess->www_authenticate;
  150.   $mess->authorization;
  151.   $mess->authorization_basic;
  152.  
  153.  
  154. =head2 $mess->headers_as_string([$endl])
  155.  
  156. Call the HTTP::Headers->as_string() method for the headers in the
  157. message.
  158.  
  159. =cut
  160.  
  161. sub headers_as_string  { shift->{'_headers'}->as_string(@_);     }
  162.  
  163. sub AUTOLOAD
  164. {
  165.     my $self = shift;
  166.     my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
  167.     return if $method eq "DESTROY";
  168.     $self->{'_headers'}->$method(@_);
  169. }
  170.  
  171. sub _elem
  172. {
  173.     my($self, $elem, $val) = @_;
  174.     my $old = $self->{$elem};
  175.     $self->{$elem} = $val if defined $val;
  176.     return $old;
  177. }
  178.  
  179. 1;
  180.