1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#!perl -w
use strict;
use Test;
plan tests => 34;
#use Data::Dump ();
my $CRLF = "\015\012";
my $LF = "\012";
{
package HTTP;
use vars qw(@ISA);
require Net::HTTP::Methods;
@ISA=qw(Net::HTTP::Methods);
my %servers = (
a => { "/" => "HTTP/1.0 200 OK${CRLF}Content-Type: text/plain${CRLF}Content-Length: 6${CRLF}${CRLF}Hello\n",
"/bad1" => "HTTP/1.0 200 OK${LF}Server: foo${LF}HTTP/1.0 200 OK${LF}Content-type: text/foo${LF}${LF}abc\n",
"/09" => "Hello${CRLF}World!${CRLF}",
"/chunked" => "HTTP/1.1 200 OK${CRLF}Transfer-Encoding: chunked${CRLF}${CRLF}0002; foo=3; bar${CRLF}He${CRLF}1${CRLF}l${CRLF}2${CRLF}lo${CRLF}0000${CRLF}Content-MD5: xxx${CRLF}${CRLF}",
"/head" => "HTTP/1.1 200 OK${CRLF}Content-Length: 16${CRLF}Content-Type: text/plain${CRLF}${CRLF}",
"/colon-header" => "HTTP/1.1 200 OK${CRLF}Content-Type: text/plain${CRLF}Content-Length: 6${CRLF}Bad-Header: :foo${CRLF}${CRLF}Hello\n",
},
);
sub http_connect {
my($self, $cnf) = @_;
my $server = $servers{$cnf->{PeerAddr}} || return undef;
${*$self}{server} = $server;
${*$self}{read_chunk_size} = $cnf->{ReadChunkSize};
return $self;
}
sub print {
my $self = shift;
#Data::Dump::dump("PRINT", @_);
my $in = shift;
my($method, $uri) = split(' ', $in);
my $out;
if ($method eq "TRACE") {
my $len = length($in);
$out = "HTTP/1.0 200 OK${CRLF}Content-Length: $len${CRLF}" .
"Content-Type: message/http${CRLF}${CRLF}" .
$in;
}
else {
$out = ${*$self}{server}{$uri};
$out = "HTTP/1.0 404 Not found${CRLF}${CRLF}" unless defined $out;
}
${*$self}{out} .= $out;
return 1;
}
sub sysread {
my $self = shift;
#Data::Dump::dump("SYSREAD", @_);
my $length = $_[1];
my $offset = $_[2] || 0;
if (my $read_chunk_size = ${*$self}{read_chunk_size}) {
$length = $read_chunk_size if $read_chunk_size < $length;
}
my $data = substr(${*$self}{out}, 0, $length, "");
return 0 unless length($data);
$_[0] = "" unless defined $_[0];
substr($_[0], $offset) = $data;
return length($data);
}
# ----------------
sub request {
my($self, $method, $uri, $headers, $opt) = @_;
$headers ||= [];
$opt ||= {};
my($code, $message, @h);
my $buf = "";
eval {
$self->write_request($method, $uri, @$headers) || die "Can't write request";
($code, $message, @h) = $self->read_response_headers(%$opt);
my $tmp;
my $n;
while ($n = $self->read_entity_body($tmp, 32)) {
#Data::Dump::dump($tmp, $n);
$buf .= $tmp;
}
push(@h, $self->get_trailers);
};
my %res = ( code => $code,
message => $message,
headers => \@h,
content => $buf,
);
if ($@) {
$res{error} = $@;
}
return \%res;
}
}
# Start testing
my $h;
my $res;
$h = HTTP->new(Host => "a", KeepAlive => 1) || die;
$res = $h->request(GET => "/");
#Data::Dump::dump($res);
ok($res->{code}, 200);
ok($res->{content}, "Hello\n");
$res = $h->request(GET => "/404");
ok($res->{code}, 404);
$res = $h->request(TRACE => "/foo");
ok($res->{code}, 200);
ok($res->{content}, "TRACE /foo HTTP/1.1${CRLF}Keep-Alive: 300${CRLF}Connection: Keep-Alive${CRLF}Host: a${CRLF}${CRLF}");
# try to turn off keep alive
$h->keep_alive(0);
$res = $h->request(TRACE => "/foo");
ok($res->{code}, "200");
ok($res->{content}, "TRACE /foo HTTP/1.1${CRLF}Connection: close${CRLF}Host: a${CRLF}${CRLF}");
# try a bad one
$res = $h->request(GET => "/bad1", [], {laxed => 1});
ok($res->{code}, "200");
ok($res->{message}, "OK");
ok("@{$res->{headers}}", "Server foo Content-type text/foo");
ok($res->{content}, "abc\n");
$res = $h->request(GET => "/bad1");
ok($res->{error} =~ /Bad header/);
ok(!$res->{code});
$h = undef; # it is in a bad state now
$h = HTTP->new("a") || die; # reconnect
$res = $h->request(GET => "/09", [], {laxed => 1});
ok($res->{code}, "200");
ok($res->{message}, "Assumed OK");
ok($res->{content}, "Hello${CRLF}World!${CRLF}");
ok($h->peer_http_version, "0.9");
$res = $h->request(GET => "/09");
ok($res->{error} =~ /^Bad response status line: 'Hello'/);
$h = undef; # it's in a bad state again
$h = HTTP->new(Host => "a", KeepAlive => 1, ReadChunkSize => 1) || die; # reconnect
$res = $h->request(GET => "/chunked");
ok($res->{code}, 200);
ok($res->{content}, "Hello");
ok("@{$res->{headers}}", "Transfer-Encoding chunked Content-MD5 xxx");
# once more
$res = $h->request(GET => "/chunked");
ok($res->{code}, "200");
ok($res->{content}, "Hello");
ok("@{$res->{headers}}", "Transfer-Encoding chunked Content-MD5 xxx");
# test head
$res = $h->request(HEAD => "/head");
ok($res->{code}, "200");
ok($res->{content}, "");
ok("@{$res->{headers}}", "Content-Length 16 Content-Type text/plain");
$res = $h->request(GET => "/");
ok($res->{code}, "200");
ok($res->{content}, "Hello\n");
$h = HTTP->new(Host => undef, PeerAddr => "a", );
$h->http_version("1.0");
ok(!defined $h->host);
$res = $h->request(TRACE => "/");
ok($res->{code}, "200");
ok($res->{content}, "TRACE / HTTP/1.0\r\n\r\n");
# check that headers with colons at the start of values don't break
$res = $h->request(GET => '/colon-header');
ok("@{$res->{headers}}", "Content-Type text/plain Content-Length 6 Bad-Header :foo");
require Net::HTTP;
eval {
$h = Net::HTTP->new;
};
print "# $@";
ok($@);
|