home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / HTTP / Request.pm < prev    next >
Text File  |  1997-11-18  |  3KB  |  138 lines

  1. #
  2. # $Id: Request.pm,v 1.19 1997/11/18 17:27:55 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 = new HTTP::Request 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, a MIME header, and optional
  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 = new LWP::UserAgent;
  26.  $request = new HTTP::Request GET => 'http://www.oslonett.no/';
  27.  $response = $ua->request($request);
  28.  
  29. =head1 METHODS
  30.  
  31. C<HTTP::Request> is a subclass of C<HTTP::Message> and therefore
  32. inherits its methods.  The inherited methods are header(),
  33. push_header(), remove_header(), headers_as_string() and content().
  34. See L<HTTP::Message> for details.
  35.  
  36. =cut
  37.  
  38. require HTTP::Message;
  39. @ISA = qw(HTTP::Message);
  40.  
  41. use URI::URL ();
  42. use strict;
  43.  
  44. =head2 $r = new HTTP::Request $method, $url, [$header, [$content]]
  45.  
  46. Constructs a new C<HTTP::Request> object describing a request on the
  47. object C<$url> using method C<$method>.  The C<$url> argument can be
  48. either a string, or a reference to a C<URI::URL> object.  The $header
  49. argument should be a reference to a HTTP::Headers object.
  50.  
  51.  $request = new HTTP::Request GET => 'http://www.oslonett.no/';
  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->HTTP::Message::clone;
  69.     $clone->method($self->method);
  70.     $clone->url($self->url);
  71.     $clone;
  72. }
  73.  
  74.  
  75. =head2 $r->method([$val])
  76.  
  77. =head2 $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($url) = @_;
  99.     if (@_) {
  100.     if (!defined $url) {
  101.         # that's ok
  102.     } elsif (ref $url) {
  103.         $url = $url->abs;
  104.     } else {
  105.         eval {  $url = URI::URL->new($url); };
  106.         $url = undef if $@;
  107.     }
  108.     }
  109.     $self->_elem('_url', $url);
  110. }
  111.  
  112. *uri = \&url;  # this is the same for now
  113.  
  114. =head2 $r->as_string()
  115.  
  116. Method returning a textual representation of the request.
  117. Mainly useful for debugging purposes. It takes no arguments.
  118.  
  119. =cut
  120.  
  121. sub as_string
  122. {
  123.     my $self = shift;
  124.     my @result = ("--- $self ---");
  125.     my $url = $self->url;
  126.     $url = (defined $url) ? $url->as_string : "[NO URL]";
  127.     push(@result, $self->method . " $url");
  128.     push(@result, $self->headers_as_string);
  129.     my $content = $self->content;
  130.     if ($content) {
  131.     push(@result, $self->content);
  132.     }
  133.     push(@result, ("-" x 35));
  134.     join("\n", @result, "");
  135. }
  136.  
  137. 1;
  138.