home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _f16cb5e35d968210181da36717acf7ac < prev    next >
Text File  |  2000-03-15  |  4KB  |  164 lines

  1. #
  2. # $Id: Request.pm,v 1.26 1999/03/20 07:37:35 gisle Exp $
  3.  
  4. package HTTP::Request;
  5.  
  6. =head1 NAME
  7.  
  8. HTTP::Request - Class encapsulating HTTP Requests
  9.  
  10. =head1 SYNOPSIS
  11.  
  12.  require HTTP::Request;
  13.  $request = HTTP::Request->new(GET => 'http://www.oslonett.no/');
  14.  
  15. =head1 DESCRIPTION
  16.  
  17. C<HTTP::Request> is a class encapsulating HTTP style requests,
  18. consisting of a request line, some headers, and some (potentially empty)
  19. content. Note that the LWP library also uses this HTTP style requests
  20. for non-HTTP protocols.
  21.  
  22. Instances of this class are usually passed to the C<request()> method
  23. of an C<LWP::UserAgent> object:
  24.  
  25.  $ua = LWP::UserAgent->new;
  26.  $request = HTTP::Request->new(GET => 'http://www.oslonett.no/');
  27.  $response = $ua->request($request);
  28.  
  29. C<HTTP::Request> is a subclass of C<HTTP::Message> and therefore
  30. inherits its methods.  The inherited methods most often used are header(),
  31. push_header(), remove_header(), headers_as_string() and content().
  32. See L<HTTP::Message> for details.
  33.  
  34. The following additional methods are available:
  35.  
  36. =over 4
  37.  
  38. =cut
  39.  
  40. require HTTP::Message;
  41. @ISA = qw(HTTP::Message);
  42. $VERSION = sprintf("%d.%02d", q$Revision: 1.26 $ =~ /(\d+)\.(\d+)/);
  43.  
  44. use strict;
  45.  
  46. =item $r = HTTP::Request->new($method, $uri, [$header, [$content]])
  47.  
  48. Constructs a new C<HTTP::Request> object describing a request on the
  49. object C<$uri> using method C<$method>.  The C<$uri> argument can be
  50. either a string, or a reference to a C<URI> object.  The $header
  51. argument should be a reference to an C<HTTP::Headers> object.
  52.  
  53. =cut
  54.  
  55. sub new
  56. {
  57.     my($class, $method, $uri, $header, $content) = @_;
  58.     my $self = $class->SUPER::new($header, $content);
  59.     $self->method($method);
  60.     $self->uri($uri);
  61.     $self;
  62. }
  63.  
  64.  
  65. sub clone
  66. {
  67.     my $self = shift;
  68.     my $clone = bless $self->SUPER::clone, ref($self);
  69.     $clone->method($self->method);
  70.     $clone->uri($self->uri);
  71.     $clone;
  72. }
  73.  
  74.  
  75. =item $r->method([$val])
  76.  
  77. =item $r->uri([$val])
  78.  
  79. These methods provide public access to the attributes containing
  80. respectively the method of the request and the URI object of the
  81. request.
  82.  
  83. If an argument is given the attribute is given that as its new
  84. value. If no argument is given the value is not touched. In either
  85. case the previous value is returned.
  86.  
  87. The uri() method accept both a reference to a URI object and a
  88. string as its argument.  If a string is given, then it should be
  89. parseable as an absolute URI.
  90.  
  91. =cut
  92.  
  93. sub method  { shift->_elem('_method', @_); }
  94.  
  95. sub uri
  96. {
  97.     my $self = shift;
  98.     my $old = $self->{'_uri'};
  99.     if (@_) {
  100.     my $uri = shift;
  101.     if (!defined $uri) {
  102.         # that's ok
  103.     } elsif (ref $uri) {
  104.         unless ($HTTP::URI_CLASS eq "URI") {
  105.         # Argh!! Hate this... old LWP legacy!
  106.         eval { $uri = $uri->abs; };
  107.         die $@ if $@ && $@ !~ /Missing base argument/;
  108.         }
  109.     } else {
  110.         $uri = $HTTP::URI_CLASS->new($uri);
  111.     }
  112.     $self->{'_uri'} = $uri;
  113.     }
  114.     $old;
  115. }
  116.  
  117. *url = \&uri;  # this is the same for now
  118.  
  119. =item $r->as_string()
  120.  
  121. Method returning a textual representation of the request.
  122. Mainly useful for debugging purposes. It takes no arguments.
  123.  
  124. =cut
  125.  
  126. sub as_string
  127. {
  128.     my $self = shift;
  129.     my @result;
  130.     #push(@result, "---- $self -----");
  131.     my $req_line = $self->method || "[NO METHOD]";
  132.     my $uri = $self->uri;
  133.     $uri = (defined $uri) ? $uri->as_string : "[NO URI]";
  134.     $req_line .= " $uri";
  135.     my $proto = $self->protocol;
  136.     $req_line .= " $proto" if $proto;
  137.  
  138.     push(@result, $req_line);
  139.     push(@result, $self->headers_as_string);
  140.     my $content = $self->content;
  141.     if (defined $content) {
  142.     push(@result, $content);
  143.     }
  144.     #push(@result, ("-" x 40));
  145.     join("\n", @result, "");
  146. }
  147.  
  148. 1;
  149.  
  150. =back
  151.  
  152. =head1 SEE ALSO
  153.  
  154. L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Request::Common>
  155.  
  156. =head1 COPYRIGHT
  157.  
  158. Copyright 1995-1998 Gisle Aas.
  159.  
  160. This library is free software; you can redistribute it and/or
  161. modify it under the same terms as Perl itself.
  162.  
  163. =cut
  164.