home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / TestClient.pm < prev    next >
Encoding:
Perl POD Document  |  2004-03-04  |  4.4 KB  |  177 lines

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