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