home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / HTTP / Message.pm < prev    next >
Text File  |  1997-04-05  |  4KB  |  185 lines

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