home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / TestClient.pm < prev    next >
Encoding:
Perl POD Document  |  2003-09-17  |  3.9 KB  |  163 lines

  1. package Apache::TestClient;
  2.  
  3. #this module provides some fallback for when libwww-perl is not installed
  4. #it is by no means an LWP replacement, just enough for very simple requests
  5.  
  6. #this module does not and will never support certain features such as:
  7. #file upload, http/1.1 (byteranges, keepalive, etc.), following redirects,
  8. #authentication, GET body callbacks, SSL, etc.
  9.  
  10. use strict;
  11. use warnings FATAL => 'all';
  12.  
  13. use Apache::TestRequest ();
  14.  
  15. my $CRLF = "\015\012";
  16.  
  17. sub request {
  18.     my($method, $url, $headers) = @_;
  19.  
  20.     my $config = Apache::Test::config();
  21.  
  22.     $method  ||= 'GET';
  23.     $url     ||= '/';
  24.     $headers ||= {};
  25.  
  26.     my $hostport = Apache::TestRequest::hostport($config);
  27.     $headers->{Host} = (split ':', $hostport)[0];
  28.  
  29.     my $s = Apache::TestRequest::vhost_socket();
  30.  
  31.     unless ($s) {
  32.         warn "cannot connect to $hostport: $!";
  33.         return undef;
  34.     }
  35.  
  36.     my $content = delete $headers->{'content'};
  37.     if ($content) {
  38.         $headers->{'Content-Length'} ||= length $content;
  39.         $headers->{'Content-Type'}   ||= 'application/x-www-form-urlencoded';
  40.     }
  41.  
  42.     #for modules/setenvif
  43.     $headers->{'User-Agent'} ||= 'libwww-perl/0.00';
  44.  
  45.     my $request = join $CRLF,
  46.       "$method $url HTTP/1.0",
  47.       (map { "$_: $headers->{$_}" } keys %$headers), $CRLF;
  48.  
  49.     # using send() avoids the need to use SIGPIPE if the server aborts
  50.     # the connection
  51.     $s->send($request);
  52.     $s->send($content) if $content;
  53.  
  54.     $request =~ s/\015//g; #for as_string
  55.  
  56.     my $res = {
  57.         request => (bless {
  58.             headers_as_string => $request,
  59.             content => $content || '',
  60.         }, 'Apache::TestClientRequest'),
  61.         headers_as_string => '',
  62.         method => $method,
  63.     };
  64.  
  65.     my($response_line, $header_term);
  66.     my $eol = "\015?\012";
  67.  
  68.     local $_;
  69.  
  70.     while (<$s>) {
  71.         $res->{headers_as_string} .= $_;
  72.         if (m:^(HTTP/\d+\.\d+)[ \t]+(\d+)[ \t]*(.*?)$eol:io) {
  73.             $res->{protocol} = $1;
  74.             $res->{code} = $2;
  75.             $res->{message} = $3;
  76.             $response_line = 1;
  77.         }
  78.         elsif (/^([a-zA-Z0-9_\-]+)\s*:\s*(.*?)$eol/o) {
  79.             $res->{headers}->{lc $1} = $2;
  80.         }
  81.         elsif (/^$eol$/o) {
  82.             $header_term = 1;
  83.             last;
  84.         }
  85.     }
  86.  
  87.     unless ($response_line and $header_term) {
  88.         warn "malformed response";
  89.     }
  90.  
  91.     {
  92.         local $/;
  93.         $res->{content} = <$s>;
  94.     }
  95.     close $s;
  96.  
  97.     # an empty body is a valid response
  98.     $res->{content} = '' 
  99.         unless exists $res->{content} and defined $res->{content};
  100.  
  101.     $res->{headers_as_string} =~ s/\015//g; #for as_string
  102.  
  103.     bless $res, 'Apache::TestClientResponse';
  104. }
  105.  
  106. for my $method (qw(GET HEAD POST PUT)) {
  107.     no strict 'refs';
  108.     *$method = sub {
  109.         my $url = shift;
  110.         my $headers = { @_ };
  111.         request($method, $url, $headers);
  112.     };
  113. }
  114.  
  115. package Apache::TestClientResponse;
  116.  
  117. sub header {
  118.     my($self, $key) = @_;
  119.     $self->{headers}->{lc $key};
  120. }
  121.  
  122. my @headers = qw(Last-Modified Content-Type);
  123.  
  124. for my $header (@headers) {
  125.     no strict 'refs';
  126.     (my $method = lc $header) =~ s/-/_/g;
  127.     *$method = sub { shift->{headers}->{lc $header} };
  128. }
  129.  
  130. sub is_success {
  131.     my $code = shift->{code};
  132.     $code >= 200 && $code < 300;
  133. }
  134.  
  135. sub status_line {
  136.     my $self = shift;
  137.     "$self->{code} $self->{message}";
  138. }
  139.  
  140. sub as_string {
  141.     my $self = shift;
  142.     $self->{headers_as_string} . ($self->{content} || '');
  143. }
  144.  
  145. my @methods = qw(
  146. request protocol code message method
  147. headers_as_string headers content
  148. );
  149.  
  150. for my $method (@methods) {
  151.     no strict 'refs';
  152.     *$method = sub {
  153.         my($self, $val) = @_;
  154.         $self->{$method} = $val if $val;
  155.         $self->{$method};
  156.     };
  157. }
  158.  
  159. #inherit headers_as_string, as_string, protocol, content, etc. methods
  160. @Apache::TestClientRequest::ISA = qw(Apache::TestClientResponse);
  161.  
  162. 1;
  163.