home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Perl_Libs / site_perl / HTTP / Message.pm < prev    next >
Text File  |  1997-12-03  |  4KB  |  208 lines

  1. #
  2. # $Id: Message.pm,v 1.21 1997/12/03 21:04:45 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. The following methods are available:
  24.  
  25. =over 4
  26.  
  27. =cut
  28.  
  29. #####################################################################
  30.  
  31. require HTTP::Headers;
  32. require Carp;
  33. use strict;
  34. use vars qw($AUTOLOAD);
  35.  
  36. =item $mess = new HTTP::Message;
  37.  
  38. This is the object constructor.  It should only be called internally
  39. by this library.  External code should construct C<HTTP::Request> or
  40. C<HTTP::Response> objects.
  41.  
  42. =cut
  43.  
  44. sub new
  45. {
  46.     my($class, $header, $content) = @_;
  47.     if (defined $header) {
  48.     Carp::croak("Bad header argument") unless ref $header;
  49.     $header = $header->clone;
  50.     } else {
  51.     $header = HTTP::Headers->new;
  52.     }
  53.     $content = '' unless defined $content;
  54.     bless {
  55.     '_headers' => $header,
  56.     '_content' => $content,
  57.     }, $class;
  58. }
  59.  
  60.  
  61. =item $mess->clone()
  62.  
  63. Returns a copy of the object.
  64.  
  65. =cut
  66.  
  67. sub clone
  68. {
  69.     my $self  = shift;
  70.     my $clone = HTTP::Message->new($self->{'_headers'}, $self->{'_content'});
  71.     $clone;
  72. }
  73.  
  74. =item $mess->protocol([$proto])
  75.  
  76. Sets the HTTP protocol used for the message.  The protocol() is a string
  77. like "HTTP/1.0" or "HTTP/1.1".
  78.  
  79. =cut
  80.  
  81. sub protocol { shift->_elem('_protocol',  @_); }
  82.  
  83. =item $mess->content([$content])
  84.  
  85. The content() method sets the content if an argument is given.  If no
  86. argument is given the content is not touched.  In either case the
  87. previous content is returned.
  88.  
  89. =item $mess->add_content($data)
  90.  
  91. The add_content() methods appends more data to the end of the previous
  92. content.
  93.  
  94. =cut
  95.  
  96. sub content   { shift->_elem('_content',  @_); }
  97.  
  98. sub add_content
  99. {
  100.     my $self = shift;
  101.     if (ref($_[0])) {
  102.     $self->{'_content'} .= ${$_[0]};  # for backwards compatability
  103.     } else {
  104.     $self->{'_content'} .= $_[0];
  105.     }
  106. }
  107.  
  108. =item $mess->content_ref
  109.  
  110. The content_ref() method will return a reference to content string.
  111. It can be more efficient to access the content this way if the content
  112. is huge, and it can be used for direct manipulation of the content,
  113. for instance:
  114.  
  115.   ${$res->content_ref} =~ s/\bfoo\b/bar/g;
  116.  
  117. =cut
  118.  
  119. sub content_ref
  120. {
  121.     my $self = shift;
  122.     \$self->{'_content'};
  123. }
  124.  
  125. sub as_string
  126. {
  127.     "";  # To be overridden in subclasses
  128. }
  129.  
  130. =item $mess->headers;
  131.  
  132. Return the embedded HTTP::Headers object.
  133.  
  134. =item $mess->headers_as_string([$endl])
  135.  
  136. Call the HTTP::Headers->as_string() method for the headers in the
  137. message.
  138.  
  139. =cut
  140.  
  141. sub headers            { shift->{'_headers'};                }
  142. sub headers_as_string  { shift->{'_headers'}->as_string(@_); }
  143.  
  144. =back
  145.  
  146. All unknown C<HTTP::Message> methods are delegated to the
  147. C<HTTP::Headers> object that is part of every message.  This allows
  148. convenient access to these methods.  Refer to L<HTTP::Headers> for
  149. details of these methods:
  150.  
  151.   $mess->header($field => $val);
  152.   $mess->scan(\&doit);
  153.   $mess->push_header($field => $val);
  154.   $mess->remove_header($field);
  155.  
  156.   $mess->date;
  157.   $mess->expires;
  158.   $mess->if_modified_since;
  159.   $mess->if_unmodified_since;
  160.   $mess->last_modified;
  161.   $mess->content_type;
  162.   $mess->content_encoding;
  163.   $mess->content_length;
  164.   $mess->content_language
  165.   $mess->title;
  166.   $mess->user_agent;
  167.   $mess->server;
  168.   $mess->from;
  169.   $mess->referer;
  170.   $mess->www_authenticate;
  171.   $mess->authorization;
  172.   $mess->proxy_authorization;
  173.   $mess->authorization_basic;
  174.   $mess->proxy_authorization_basic;
  175.  
  176. =cut
  177.  
  178.  
  179. # delegate all other method calls the the _headers object.
  180. sub AUTOLOAD
  181. {
  182.     my $self = shift;
  183.     my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
  184.     return if $method eq "DESTROY";
  185.     $self->{'_headers'}->$method(@_);
  186. }
  187.  
  188. # Private method to access members in %$self
  189. sub _elem
  190. {
  191.     my $self = shift;
  192.     my $elem = shift;
  193.     my $old = $self->{$elem};
  194.     $self->{$elem} = $_[0] if @_;
  195.     return $old;
  196. }
  197.  
  198. 1;
  199.  
  200. =head1 COPYRIGHT
  201.  
  202. Copyright 1995-1997 Gisle Aas.
  203.  
  204. This library is free software; you can redistribute it and/or
  205. modify it under the same terms as Perl itself.
  206.  
  207. =cut
  208.