home *** CD-ROM | disk | FTP | other *** search
/ PC Open 102 / PC Open 102 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / $_1_ / HTTP / Message.pm < prev    next >
Encoding:
Perl POD Document  |  2004-02-03  |  5.0 KB  |  219 lines

  1. package HTTP::Message;
  2.  
  3. # $Id: Message.pm,v 1.30 2003/10/24 10:25:16 gisle Exp $
  4.  
  5. use strict;
  6. use vars qw($VERSION $AUTOLOAD);
  7. $VERSION = sprintf("%d.%02d", q$Revision: 1.30 $ =~ /(\d+)\.(\d+)/);
  8.  
  9. require HTTP::Headers;
  10. require Carp;
  11.  
  12. $HTTP::URI_CLASS ||= $ENV{PERL_HTTP_URI_CLASS} || "URI";
  13. eval "require $HTTP::URI_CLASS"; die $@ if $@;
  14.  
  15.  
  16.  
  17. sub new
  18. {
  19.     my($class, $header, $content) = @_;
  20.     if (defined $header) {
  21.     Carp::croak("Bad header argument") unless ref $header;
  22.     $header = $header->clone;
  23.     }
  24.     else {
  25.     $header = HTTP::Headers->new;
  26.     }
  27.     $content = '' unless defined $content;
  28.     bless {
  29.     '_headers' => $header,
  30.     '_content' => $content,
  31.     }, $class;
  32. }
  33.  
  34.  
  35. sub clone
  36. {
  37.     my $self  = shift;
  38.     my $clone = HTTP::Message->new($self->{'_headers'}, $self->{'_content'});
  39.     $clone;
  40. }
  41.  
  42.  
  43. sub protocol { shift->_elem('_protocol',  @_); }
  44. sub content  { shift->_elem('_content',  @_); }
  45.  
  46.  
  47. sub add_content
  48. {
  49.     my $self = shift;
  50.     if (ref($_[0])) {
  51.     $self->{'_content'} .= ${$_[0]};  # for backwards compatability
  52.     }
  53.     else {
  54.     $self->{'_content'} .= $_[0];
  55.     }
  56. }
  57.  
  58.  
  59. sub content_ref
  60. {
  61.     my $self = shift;
  62.     \$self->{'_content'};
  63. }
  64.  
  65.  
  66. sub as_string
  67. {
  68.     "";  # To be overridden in subclasses
  69. }
  70.  
  71.  
  72. sub headers            { shift->{'_headers'};                }
  73. sub headers_as_string  { shift->{'_headers'}->as_string(@_); }
  74.  
  75.  
  76. # delegate all other method calls the the _headers object.
  77. sub AUTOLOAD
  78. {
  79.     my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
  80.     return if $method eq "DESTROY";
  81.  
  82.     # We create the function here so that it will not need to be
  83.     # autoloaded the next time.
  84.     no strict 'refs';
  85.     *$method = eval "sub { shift->{'_headers'}->$method(\@_) }";
  86.     goto &$method;
  87. }
  88.  
  89.  
  90. # Private method to access members in %$self
  91. sub _elem
  92. {
  93.     my $self = shift;
  94.     my $elem = shift;
  95.     my $old = $self->{$elem};
  96.     $self->{$elem} = $_[0] if @_;
  97.     return $old;
  98. }
  99.  
  100.  
  101. 1;
  102.  
  103.  
  104. __END__
  105.  
  106. =head1 NAME
  107.  
  108. HTTP::Message - HTTP style message base class
  109.  
  110. =head1 SYNOPSIS
  111.  
  112.  package HTTP::Request;  # or HTTP::Response
  113.  require HTTP::Message;
  114.  @ISA=qw(HTTP::Message);
  115.  
  116. =head1 DESCRIPTION
  117.  
  118. An C<HTTP::Message> object contains some headers and a content body.
  119. The class is abstract, i.e. it only used as a base class for
  120. C<HTTP::Request> and C<HTTP::Response> and should never instantiated
  121. as itself.  The following methods are available:
  122.  
  123. =over 4
  124.  
  125. =item $mess->content
  126.  
  127. =item $mess->content( $content )
  128.  
  129. The content() method sets the content if an argument is given.  If no
  130. argument is given the content is not touched.  In either case the
  131. previous content is returned.
  132.  
  133. Note that the content should be a string of bytes.  Strings in perl
  134. can contain characters outside the range of a byte.  The C<Encode>
  135. module can be used to turn such strings into a string of bytes.
  136.  
  137. =item $mess->add_content( $data )
  138.  
  139. The add_content() methods appends more data to the end of the current
  140. content buffer.
  141.  
  142. =item $mess->content_ref
  143.  
  144. The content_ref() method will return a reference to content buffer string.
  145. It can be more efficient to access the content this way if the content
  146. is huge, and it can even be used for direct manipulation of the content,
  147. for instance:
  148.  
  149.   ${$res->content_ref} =~ s/\bfoo\b/bar/g;
  150.  
  151. This example would modify the content buffer in-place.
  152.  
  153. =item $mess->headers
  154.  
  155. Returns the embedded HTTP::Headers object.
  156.  
  157. =item $mess->headers_as_string
  158.  
  159. =item $mess->headers_as_string( $endl )
  160.  
  161. Call the as_string() method for the headers in the
  162. message.  This will be the same as
  163.  
  164.     $mess->headers->as_string
  165.  
  166. but it will make your program a whole character shorter :-)
  167.  
  168. =item $mess->protocol
  169.  
  170. =item $mess->protocol( $proto )
  171.  
  172. Sets the HTTP protocol used for the message.  The protocol() is a string
  173. like C<HTTP/1.0> or C<HTTP/1.1>.
  174.  
  175. =item $mess->clone
  176.  
  177. Returns a copy of the message object.
  178.  
  179. =back
  180.  
  181. All methods unknown to C<HTTP::Message> itself are delegated to the
  182. C<HTTP::Headers> object that is part of every message.  This allows
  183. convenient access to these methods.  Refer to L<HTTP::Headers> for
  184. details of these methods:
  185.  
  186.     $mess->header( $field => $val )
  187.     $mess->push_header( $field => $val )
  188.     $mess->init_header( $field => $val )
  189.     $mess->remove_header( $field )
  190.     $mess->scan( \&doit )
  191.  
  192.     $mess->date
  193.     $mess->expires
  194.     $mess->if_modified_since
  195.     $mess->if_unmodified_since
  196.     $mess->last_modified
  197.     $mess->content_type
  198.     $mess->content_encoding
  199.     $mess->content_length
  200.     $mess->content_language
  201.     $mess->title
  202.     $mess->user_agent
  203.     $mess->server
  204.     $mess->from
  205.     $mess->referer
  206.     $mess->www_authenticate
  207.     $mess->authorization
  208.     $mess->proxy_authorization
  209.     $mess->authorization_basic
  210.     $mess->proxy_authorization_basic
  211.  
  212. =head1 COPYRIGHT
  213.  
  214. Copyright 1995-2001 Gisle Aas.
  215.  
  216. This library is free software; you can redistribute it and/or
  217. modify it under the same terms as Perl itself.
  218.  
  219.