Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
4.6 KiB

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