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

  1. #
  2. # $Id: Request.pm,v 1.21 1997/12/03 21:07:08 aas 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 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.  
  43. use URI::URL ();
  44. use strict;
  45.  
  46. =item $r = HTTP::Request->new($method, $url, [$header, [$content]])
  47.  
  48. Constructs a new C<HTTP::Request> object describing a request on the
  49. object C<$url> using method C<$method>.  The C<$url> argument can be
  50. either a string, or a reference to a C<URI::URL> 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, $url, $header, $content) = @_;
  58.     my $self = $class->SUPER::new($header, $content);
  59.     $self->method($method);
  60.     $self->url($url);
  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->url($self->url);
  71.     $clone;
  72. }
  73.  
  74.  
  75. =item $r->method([$val])
  76.  
  77. =item $r->url([$val])
  78.  
  79. These methods provide public access to the member variables containing
  80. respectively the method of the request and the URL object of the
  81. request.
  82.  
  83. If an argument is given the member variable 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 url() method accept both a reference to a URI::URL object and a
  88. string as its argument.  If a string is given, then it should be
  89. parseable as an absolute URL.
  90.  
  91. =cut
  92.  
  93. sub method  { shift->_elem('_method', @_); }
  94.  
  95. sub url
  96. {
  97.     my $self = shift;
  98.     my $old = $self->{'_url'};
  99.     if (@_) {
  100.     my $url = shift;
  101.     if (!defined $url) {
  102.         # that's ok
  103.     } elsif (ref $url) {
  104.         $url = $url->abs;
  105.     } else {
  106.         $url = eval { URI::URL->new($url) };
  107.     }
  108.     $self->{'_url'} = $url;
  109.     }
  110.     $old;
  111. }
  112.  
  113. *uri = \&url;  # this is the same for now
  114.  
  115. =item $r->as_string()
  116.  
  117. Method returning a textual representation of the request.
  118. Mainly useful for debugging purposes. It takes no arguments.
  119.  
  120. =cut
  121.  
  122. sub as_string
  123. {
  124.     my $self = shift;
  125.     my @result;
  126.     #push(@result, "---- $self -----");
  127.     my $req_line = $self->method || "[NO METHOD]";
  128.     my $url = $self->url;
  129.     $url = (defined $url) ? $url->as_string : "[NO URL]";
  130.     $req_line .= " $url";
  131.     my $proto = $self->protocol;
  132.     $req_line .= " $proto" if $proto;
  133.  
  134.     push(@result, $req_line);
  135.     push(@result, $self->headers_as_string);
  136.     my $content = $self->content;
  137.     if (defined $content) {
  138.     push(@result, $content);
  139.     }
  140.     #push(@result, ("-" x 40));
  141.     join("\n", @result, "");
  142. }
  143.  
  144. 1;
  145.  
  146. =back
  147.  
  148. =head1 COPYRIGHT
  149.  
  150. Copyright 1995-1997 Gisle Aas.
  151.  
  152. This library is free software; you can redistribute it and/or
  153. modify it under the same terms as Perl itself.
  154.  
  155. =cut
  156.