Apache-Test

 view release on metacpan or  search on metacpan

lib/Apache/TestClient.pm  view on Meta::CPAN

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package Apache::TestClient;

#this module provides some fallback for when libwww-perl is not installed
#it is by no means an LWP replacement, just enough for very simple requests

#this module does not and will never support certain features such as:
#file upload, http/1.1 (byteranges, keepalive, etc.), following redirects,
#authentication, GET body callbacks, SSL, etc.

use strict;
use warnings FATAL => 'all';

use Apache::TestRequest ();

my $CRLF = "\015\012";

sub request {
    my($method, $url, @headers) = @_;

    my @real_headers = ();
    my $content;

    for (my $i = 0; $i < scalar @headers; $i += 2) {
        if ($headers[$i] =~ /^content$/i) {
            $content = $headers[$i+1];
        }
        else {
            push @real_headers, ($headers[$i], $headers[$i+1]);
        }
    }

    ## XXX:
    ## This is not a FULL URL encode mapping
    ## space ' '; however is very common, so this
    ## is useful to convert
    $url =~ s/ /%20/g;

    my $config = Apache::Test::config();

    $method  ||= 'GET';
    $url     ||= '/';
    my %headers = ();

    my $hostport = Apache::TestRequest::hostport($config);
    $headers{Host} = (split ':', $hostport)[0];

    my $s = Apache::TestRequest::vhost_socket();

    unless ($s) {
        warn "cannot connect to $hostport: $!";
        return undef;
    }

    if ($content) {
        $headers{'Content-Length'} ||= length $content;
        $headers{'Content-Type'}   ||= 'application/x-www-form-urlencoded';
    }

    #for modules/setenvif
    $headers{'User-Agent'} ||= 'libwww-perl/0.00';

    my $request = join $CRLF,
      "$method $url HTTP/1.0",
      (map { "$_: $headers{$_}" } keys %headers);

    $request .= $CRLF;

    for (my $i = 0; $i < scalar @real_headers; $i += 2) {



( run in 1.128 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )