home *** CD-ROM | disk | FTP | other *** search
/ ftp.f-secure.com / 2014.06.ftp.f-secure.com.tar / ftp.f-secure.com / support / hotfix / fsis / IS-SpamControl.fsfix / iufssc / lib / HTTP / Request.pm < prev    next >
Text File  |  2006-11-29  |  5KB  |  213 lines

  1. package HTTP::Request;
  2.  
  3. # $Id: Request.pm 2397 2005-12-23 13:06:15Z kankri $
  4.  
  5. require HTTP::Message;
  6. @ISA = qw(HTTP::Message);
  7. $VERSION = sprintf("%d.%02d", q$Revision: 2397 $ =~ /(\d+)\.(\d+)/);
  8.  
  9. use strict;
  10.  
  11.  
  12.  
  13. sub new
  14. {
  15.     my($class, $method, $uri, $header, $content) = @_;
  16.     my $self = $class->SUPER::new($header, $content);
  17.     $self->method($method);
  18.     $self->uri($uri);
  19.     $self;
  20. }
  21.  
  22.  
  23. sub parse
  24. {
  25.     my($class, $str) = @_;
  26.     my $request_line;
  27.     if ($str =~ s/^(.*)\n//) {
  28.     $request_line = $1;
  29.     }
  30.     else {
  31.     $request_line = $str;
  32.     $str = "";
  33.     }
  34.  
  35.     my $self = $class->SUPER::parse($str);
  36.     my($method, $uri, $protocol) = split(' ', $request_line);
  37.     $self->method($method) if defined($method);
  38.     $self->uri($uri) if defined($uri);
  39.     $self->protocol($protocol) if $protocol;
  40.     $self;
  41. }
  42.  
  43.  
  44. sub clone
  45. {
  46.     my $self = shift;
  47.     my $clone = bless $self->SUPER::clone, ref($self);
  48.     $clone->method($self->method);
  49.     $clone->uri($self->uri);
  50.     $clone;
  51. }
  52.  
  53.  
  54. sub method
  55. {
  56.     shift->_elem('_method', @_);
  57. }
  58.  
  59.  
  60. sub uri
  61. {
  62.     my $self = shift;
  63.     my $old = $self->{'_uri'};
  64.     if (@_) {
  65.     my $uri = shift;
  66.     if (!defined $uri) {
  67.         # that's ok
  68.     }
  69.     elsif (ref $uri) {
  70.         Carp::croak("A URI can't be a " . ref($uri) . " reference")
  71.         if ref($uri) eq 'HASH' or ref($uri) eq 'ARRAY';
  72.         Carp::croak("Can't use a " . ref($uri) . " object as a URI")
  73.         unless $uri->can('scheme');
  74.         $uri = $uri->clone;
  75.         unless ($HTTP::URI_CLASS eq "URI") {
  76.         # Argh!! Hate this... old LWP legacy!
  77.         eval { local $SIG{__DIE__}; $uri = $uri->abs; };
  78.         die $@ if $@ && $@ !~ /Missing base argument/;
  79.         }
  80.     }
  81.     else {
  82.         $uri = $HTTP::URI_CLASS->new($uri);
  83.     }
  84.     $self->{'_uri'} = $uri;
  85.     }
  86.     $old;
  87. }
  88.  
  89. *url = \&uri;  # legacy
  90.  
  91.  
  92. sub as_string
  93. {
  94.     my $self = shift;
  95.     my($eol) = @_;
  96.     $eol = "\n" unless defined $eol;
  97.  
  98.     my $req_line = $self->method || "-";
  99.     my $uri = $self->uri;
  100.     $uri = (defined $uri) ? $uri->as_string : "-";
  101.     $req_line .= " $uri";
  102.     my $proto = $self->protocol;
  103.     $req_line .= " $proto" if $proto;
  104.  
  105.     return join($eol, $req_line, $self->SUPER::as_string(@_));
  106. }
  107.  
  108.  
  109. 1;
  110.  
  111. __END__
  112.  
  113. =head1 NAME
  114.  
  115. HTTP::Request - HTTP style request message
  116.  
  117. =head1 SYNOPSIS
  118.  
  119.  require HTTP::Request;
  120.  $request = HTTP::Request->new(GET => 'http://www.example.com/');
  121.  
  122. and usually used like this:
  123.  
  124.  $ua = LWP::UserAgent->new;
  125.  $response = $ua->request($request);
  126.  
  127. =head1 DESCRIPTION
  128.  
  129. C<HTTP::Request> is a class encapsulating HTTP style requests,
  130. consisting of a request line, some headers, and a content body. Note
  131. that the LWP library uses HTTP style requests even for non-HTTP
  132. protocols.  Instances of this class are usually passed to the
  133. request() method of an C<LWP::UserAgent> object.
  134.  
  135. C<HTTP::Request> is a subclass of C<HTTP::Message> and therefore
  136. inherits its methods.  The following additional methods are available:
  137.  
  138. =over 4
  139.  
  140. =item $r = HTTP::Request->new( $method, $uri )
  141.  
  142. =item $r = HTTP::Request->new( $method, $uri, $header )
  143.  
  144. =item $r = HTTP::Request->new( $method, $uri, $header, $content )
  145.  
  146. Constructs a new C<HTTP::Request> object describing a request on the
  147. object $uri using method $method.  The $method argument must be a
  148. string.  The $uri argument can be either a string, or a reference to a
  149. C<URI> object.  The optional $header argument should be a reference to
  150. an C<HTTP::Headers> object or a plain array reference of key/value
  151. pairs.  The optional $content argument should be a string of bytes.
  152.  
  153. =item $r = HTTP::Request->parse( $str )
  154.  
  155. This constructs a new request object by parsing the given string.
  156.  
  157. =item $r->method
  158.  
  159. =item $r->method( $val )
  160.  
  161. This is used to get/set the method attribute.  The method should be a
  162. short string like "GET", "HEAD", "PUT" or "POST".
  163.  
  164. =item $r->uri
  165.  
  166. =item $r->uri( $val )
  167.  
  168. This is used to get/set the uri attribute.  The $val can be a
  169. reference to a URI object or a plain string.  If a string is given,
  170. then it should be parseable as an absolute URI.
  171.  
  172. =item $r->header( $field )
  173.  
  174. =item $r->header( $field => $value )
  175.  
  176. This is used to get/set header values and it is inherited from
  177. C<HTTP::Headers> via C<HTTP::Message>.  See L<HTTP::Headers> for
  178. details and other similar methods that can be used to access the
  179. headers.
  180.  
  181. =item $r->content
  182.  
  183. =item $r->content( $content )
  184.  
  185. This is used to get/set the content and it is inherited from the
  186. C<HTTP::Message> base class.  See L<HTTP::Message> for details and
  187. other methods that can be used to access the content.
  188.  
  189. Note that the content should be a string of bytes.  Strings in perl
  190. can contain characters outside the range of a byte.  The C<Encode>
  191. module can be used to turn such strings into a string of bytes.
  192.  
  193. =item $r->as_string
  194.  
  195. =item $r->as_string( $eol )
  196.  
  197. Method returning a textual representation of the request.
  198.  
  199. =back
  200.  
  201. =head1 SEE ALSO
  202.  
  203. L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Request::Common>,
  204. L<HTTP::Response>
  205.  
  206. =head1 COPYRIGHT
  207.  
  208. Copyright 1995-2004 Gisle Aas.
  209.  
  210. This library is free software; you can redistribute it and/or
  211. modify it under the same terms as Perl itself.
  212.  
  213.