14 require Net::HTTP::Methods;
15 @ISA=qw(Net::HTTP::Methods);
18 a => { "/" => "HTTP/1.0 200 OK${CRLF}Content-Type: text/plain${CRLF}Content-Length: 6${CRLF}${CRLF}Hello\n",
19 "/bad1" => "HTTP/1.0 200 OK${LF}Server: foo${LF}HTTP/1.0 200 OK${LF}Content-type: text/foo${LF}${LF}abc\n",
20 "/09" => "Hello${CRLF}World!${CRLF}",
21 "/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}",
22 "/head" => "HTTP/1.1 200 OK${CRLF}Content-Length: 16${CRLF}Content-Type: text/plain${CRLF}${CRLF}",
28 my $server = $servers{$cnf->{PeerAddr}} || return undef;
29 ${*$self}{server} = $server;
30 ${*$self}{read_chunk_size} = $cnf->{ReadChunkSize};
36 #Data::Dump::dump("PRINT", @_);
38 my($method, $uri) = split(' ', $in);
41 if ($method eq "TRACE") {
42 my $len = length($in);
43 $out = "HTTP/1.0 200 OK${CRLF}Content-Length: $len${CRLF}" .
44 "Content-Type: message/http${CRLF}${CRLF}" .
48 $out = ${*$self}{server}{$uri};
49 $out = "HTTP/1.0 404 Not found${CRLF}${CRLF}" unless defined $out;
52 ${*$self}{out} .= $out;
58 #Data::Dump::dump("SYSREAD", @_);
60 my $offset = $_[2] || 0;
62 if (my $read_chunk_size = ${*$self}{read_chunk_size}) {
63 $length = $read_chunk_size if $read_chunk_size < $length;
66 my $data = substr(${*$self}{out}, 0, $length, "");
67 return 0 unless length($data);
69 $_[0] = "" unless defined $_[0];
70 substr($_[0], $offset) = $data;
77 my($self, $method, $uri, $headers, $opt) = @_;
81 my($code, $message, @h);
84 $self->write_request($method, $uri, @$headers) || die "Can't write request";
85 ($code, $message, @h) = $self->read_response_headers(%$opt);
89 while ($n = $self->read_entity_body($tmp, 32)) {
90 #Data::Dump::dump($tmp, $n);
94 push(@h, $self->get_trailers);
98 my %res = ( code => $code,
116 $h = HTTP->new(Host => "a", KeepAlive => 1) || die;
117 $res = $h->request(GET => "/");
119 #Data::Dump::dump($res);
121 print "not " unless $res->{code} eq "200" && $res->{content} eq "Hello\n";
124 $res = $h->request(GET => "/404");
125 print "not " unless $res->{code} eq "404";
128 $res = $h->request(TRACE => "/foo");
129 print "not " unless $res->{code} eq "200" &&
130 $res->{content} eq "TRACE /foo HTTP/1.1${CRLF}Keep-Alive: 300${CRLF}Connection: Keep-Alive${CRLF}Host: a${CRLF}${CRLF}";
133 # try to turn off keep alive
135 $res = $h->request(TRACE => "/foo");
136 print "not " unless $res->{code} eq "200" &&
137 $res->{content} eq "TRACE /foo HTTP/1.1${CRLF}Connection: close${CRLF}Host: a${CRLF}${CRLF}";
141 $res = $h->request(GET => "/bad1", [], {laxed => 1});
142 print "not " unless $res->{code} eq "200" && $res->{message} eq "OK" &&
143 "@{$res->{headers}}" eq "Server foo Content-type text/foo" &&
144 $res->{content} eq "abc\n";
147 $res = $h->request(GET => "/bad1");
148 print "not " unless $res->{error} =~ /Bad header/ && !$res->{code};
150 $h = undef; # it is in a bad state now
152 $h = HTTP->new("a") || die; # reconnect
153 $res = $h->request(GET => "/09", [], {laxed => 1});
154 print "not " unless $res->{code} eq "200" && $res->{message} eq "Assumed OK" &&
155 $res->{content} eq "Hello${CRLF}World!${CRLF}" &&
156 $h->peer_http_version eq "0.9";
159 $res = $h->request(GET => "/09");
160 print "not " unless $res->{error} =~ /^Bad response status line: 'Hello'/;
162 $h = undef; # it's in a bad state again
164 $h = HTTP->new(Host => "a", KeepAlive => 1, ReadChunkSize => 1) || die; # reconnect
165 $res = $h->request(GET => "/chunked");
166 print "not " unless $res->{code} eq "200" && $res->{content} eq "Hello" &&
167 "@{$res->{headers}}" eq "Transfer-Encoding chunked Content-MD5 xxx";
171 $res = $h->request(GET => "/chunked");
172 print "not " unless $res->{code} eq "200" && $res->{content} eq "Hello" &&
173 "@{$res->{headers}}" eq "Transfer-Encoding chunked Content-MD5 xxx";
177 $res = $h->request(HEAD => "/head");
178 print "not " unless $res->{code} eq "200" && $res->{content} eq "" &&
179 "@{$res->{headers}}" eq "Content-Length 16 Content-Type text/plain";
182 $res = $h->request(GET => "/");
183 print "not " unless $res->{code} eq "200" && $res->{content} eq "Hello\n";
185 #use Data::Dump; Data::Dump::dump($res);
188 $h = HTTP->new(Host => undef, PeerAddr => "a", );
189 $h->http_version("1.0");
190 print "not " if defined $h->host;
192 $res = $h->request(TRACE => "/");
193 print "not " unless $res->{code} eq "200" && $res->{content} eq "TRACE / HTTP/1.0\r\n\r\n";
201 print "not " unless $@;