| 1 |
# Thanks to merlyn for nudging me and giving me this snippet! |
| 2 |
use strict; |
| 3 |
use HTTP::Daemon (); |
| 4 |
use URI; |
| 5 |
use URI::QueryParam; |
| 6 |
use Getopt::Long; |
| 7 |
|
| 8 |
$|++; |
| 9 |
|
| 10 |
GetOptions( |
| 11 |
'e=s' => \my $expression, |
| 12 |
); |
| 13 |
|
| 14 |
my $d = HTTP::Daemon->new or die; |
| 15 |
|
| 16 |
print $d->url, "\n"; |
| 17 |
|
| 18 |
my ( $filename, $logfile ) = @ARGV[ 0, 1 ]; |
| 19 |
if ($filename) { |
| 20 |
open DATA, "< $filename" |
| 21 |
or die "Couldn't read page '$filename' : $!\n"; |
| 22 |
} |
| 23 |
|
| 24 |
#open LOG, ">", $logfile |
| 25 |
# or die "Couldn't create logfile '$logfile' : $!\n"; |
| 26 |
my $log; |
| 27 |
my $body = join "", <DATA>; |
| 28 |
$body =~ s/<!x([0-9a-fA-F]+)>/chr(hex($1))/eg; |
| 29 |
utf8::encode($body); |
| 30 |
utf8::upgrade($body); |
| 31 |
|
| 32 |
sub debug($) { |
| 33 |
my $message = $_[0]; |
| 34 |
$message =~ s!\n!\n#SERVER:!g; |
| 35 |
warn "#SERVER: $message" |
| 36 |
if $ENV{TEST_HTTP_VERBOSE}; |
| 37 |
} |
| 38 |
|
| 39 |
SERVERLOOP: { |
| 40 |
my $quitserver; |
| 41 |
while ( my $c = $d->accept ) { |
| 42 |
debug "New connection"; |
| 43 |
while ( my $r = $c->get_request ) { |
| 44 |
debug "Request:\n" . $r->as_string; |
| 45 |
my $location = ( $r->uri->path || "/" ); |
| 46 |
my ( $link1, $link2 ) = ( '', '' ); |
| 47 |
if ( $location =~ m!^/link/([^/]+)/(.*)$! ) { |
| 48 |
( $link1, $link2 ) = ( $1, $2 ); |
| 49 |
} |
| 50 |
my $res; |
| 51 |
if ( $location eq '/get_server_log' ) { |
| 52 |
$res = HTTP::Response->new( 200, "OK", undef, $log ); |
| 53 |
$log = ''; |
| 54 |
} |
| 55 |
elsif ( $location eq '/quit_server' ) { |
| 56 |
debug "Quitting"; |
| 57 |
$res = HTTP::Response->new( |
| 58 |
200, "OK", |
| 59 |
[ Connection => 'close' ], "quit" |
| 60 |
); |
| 61 |
$quitserver = 1; |
| 62 |
} |
| 63 |
else { |
| 64 |
eval $expression |
| 65 |
if $expression; |
| 66 |
warn "eval: $@" if $@; |
| 67 |
$log .= "Request:\n" . $r->as_string . "\n"; |
| 68 |
if ( $location =~ m!^/redirect/(.*)$! ) { |
| 69 |
$res = HTTP::Response->new(302); |
| 70 |
$res->header( 'location', $d->url . $1 ); |
| 71 |
} |
| 72 |
elsif ( $location =~ m!^/error/notfound/(.*)$! ) { |
| 73 |
$res = HTTP::Response->new( |
| 74 |
404, "Not found", |
| 75 |
[ Connection => 'close' ] |
| 76 |
); |
| 77 |
} |
| 78 |
elsif ( $location =~ m!^/error/timeout/(\d+)$! ) { |
| 79 |
sleep $1; |
| 80 |
$res = HTTP::Response->new( |
| 81 |
599, "Timeout reached", |
| 82 |
[ Connection => 'close' ] |
| 83 |
); |
| 84 |
} |
| 85 |
elsif ( $location =~ m!^/error/close/(\d+)$! ) { |
| 86 |
sleep $1; |
| 87 |
$res = undef; |
| 88 |
} |
| 89 |
elsif ( $location =~ m!^/chunks! ) { |
| 90 |
my $count = 5; |
| 91 |
$res = HTTP::Response->new( |
| 92 |
200, "OK", undef, |
| 93 |
sub { |
| 94 |
sleep 1; |
| 95 |
my $buf = 'x' x 16; |
| 96 |
return $buf if $count-- > 0; |
| 97 |
return undef; # done |
| 98 |
} |
| 99 |
); |
| 100 |
} |
| 101 |
elsif ( $location =~ m!^/error/after_headers$! ) { |
| 102 |
my $count = 2; |
| 103 |
$res = HTTP::Response->new( |
| 104 |
200, "OK", undef, |
| 105 |
sub { |
| 106 |
sleep 1; |
| 107 |
my $buf = 'x' x 16; |
| 108 |
return $buf if $count-- > 0; |
| 109 |
die "Planned error after headers"; |
| 110 |
} |
| 111 |
); |
| 112 |
} |
| 113 |
elsif ( $location =~ m!^/encoding/(.*)! ) { |
| 114 |
my $encoding = $1; |
| 115 |
$res = HTTP::Response->new( |
| 116 |
200, "OK", |
| 117 |
[ 'Content-Type' => "text/html; charset=$encoding" ], |
| 118 |
"encoding $encoding" |
| 119 |
); |
| 120 |
} |
| 121 |
else { |
| 122 |
my $uri = $r->uri; |
| 123 |
|
| 124 |
# Make sticky form fields |
| 125 |
my ( $query, $session, %cat ); |
| 126 |
$query |
| 127 |
= defined $uri->query_param('query') |
| 128 |
? $uri->query_param('query') |
| 129 |
: "(empty)"; |
| 130 |
$session |
| 131 |
= defined $uri->query_param('session') |
| 132 |
? $uri->query_param('session') |
| 133 |
: 1; |
| 134 |
%cat = map { $_ => 1 } ( |
| 135 |
defined $uri->query_param('cat') |
| 136 |
? $uri->query_param('cat') |
| 137 |
: qw( cat_foo cat_bar ) |
| 138 |
); |
| 139 |
my @categories = map { $cat{$_} ? "checked" : "" } |
| 140 |
qw( cat_foo cat_bar cat_baz ); |
| 141 |
( my $h = $r->headers->{host} ) =~ s/:\d+//; |
| 142 |
my $rbody = sprintf $body, $location, $session, $query, |
| 143 |
@categories; |
| 144 |
$res = HTTP::Response->new( |
| 145 |
200, "OK", |
| 146 |
[ |
| 147 |
"Set-Cookie" => 'log-server=shazam2; Path=/', |
| 148 |
'Cache-Control' => 'no-cache', |
| 149 |
'Pragma' => 'no-cache', |
| 150 |
'Max-Age' => 0, |
| 151 |
'Connection' => 'close', |
| 152 |
'Content-Length' => length($rbody), |
| 153 |
], |
| 154 |
$rbody |
| 155 |
); |
| 156 |
|
| 157 |
$res->content_type( |
| 158 |
$uri->query_param('xml') |
| 159 |
? 'application/xhtml+xml' |
| 160 |
: 'text/html' |
| 161 |
); |
| 162 |
|
| 163 |
debug "Request " . ( $r->uri->path || "/" ); |
| 164 |
} |
| 165 |
} |
| 166 |
debug "Response:\n" . $res->as_string |
| 167 |
if $res; |
| 168 |
eval { $c->send_response($res) if $res; }; |
| 169 |
if ( my $err = $@ ) { |
| 170 |
debug "Server raised error: $err"; |
| 171 |
if ( $err !~ /^Planned error\b/ ) { |
| 172 |
warn $err; |
| 173 |
} |
| 174 |
$c->close; |
| 175 |
} |
| 176 |
if ( !$res ) { |
| 177 |
$c->close; |
| 178 |
} |
| 179 |
last if $quitserver; |
| 180 |
} |
| 181 |
sleep 1; |
| 182 |
undef($c); |
| 183 |
last SERVERLOOP |
| 184 |
if $quitserver; |
| 185 |
} |
| 186 |
} |
| 187 |
END { debug "Server stopped" } |
| 188 |
|
| 189 |
__DATA__ |
| 190 |
<html> |
| 191 |
<head> |
| 192 |
<title>WWW::Mechanize test page</title> |
| 193 |
</head> |
| 194 |
<body> |
| 195 |
<h1>Location: %s</h1> |
| 196 |
<p> |
| 197 |
<a href="/test">Link /test</a> |
| 198 |
<a href="/foo">Link /foo</a> |
| 199 |
<a href="/slash_end">Link /</a> |
| 200 |
<a href="/slash_front">/Link </a> |
| 201 |
<a href="/slash_both">/Link in slashes/</a> |
| 202 |
<a href="/foo1.save_log_server_test.tmp">Link foo1.save_log_server_test.tmp</a> |
| 203 |
<a href="/foo2.save_log_server_test.tmp">Link foo2.save_log_server_test.tmp</a> |
| 204 |
<a href="/foo3.save_log_server_test.tmp">Link foo3.save_log_server_test.tmp</a> |
| 205 |
<a href="/o-umlaut">L<!xf6>schen -- testing for o-umlaut.</a> |
| 206 |
<a href="/o-umlaut-encoded">Stösberg -- testing for encoded o-umlaut.</a> |
| 207 |
|
| 208 |
<table> |
| 209 |
<tr><th>Col1</th><th>Col2</th><th>Col3</th></tr> |
| 210 |
<tr><td>A1</td><td>A2</td><td>A3</td></tr> |
| 211 |
<tr><td>B1</td><td>B2</td><td>B3</td></tr> |
| 212 |
<tr><td>C1</td><td>C2</td><td>C3</td></tr> |
| 213 |
</table> |
| 214 |
<form name="f" action="/formsubmit" class="test" foo=""> |
| 215 |
<input type="hidden" name="session" value="%s"/> |
| 216 |
<input type="text" name="query" value="%s"/> |
| 217 |
<input type="submit" name="submit" value="Go" id="0" /> |
| 218 |
<input type="checkbox" name="cat" value="cat_foo" %s /> |
| 219 |
<input type="checkbox" name="cat" value="cat_bar" %s /> |
| 220 |
<input type="checkbox" name="cat" value="cat_baz" %s /> |
| 221 |
<input type="file" name="upload" value="README" /> |
| 222 |
<button type="submit" name="button_tag" value="Walk" /> |
| 223 |
<input type="image" name="image_input" value="image" /> |
| 224 |
</form> |
| 225 |
<form id="pounder" action="/formsubmit" class="test" foo=""> |
| 226 |
<input type="text" name="query" value="%s"/> |
| 227 |
</form> |
| 228 |
<form id="searchbox" action="/google-cli"> |
| 229 |
<input type="text" name="query" value="%s"/> |
| 230 |
</form> |
| 231 |
<form name="mf" id="multiform" action="/formsubmit" class="test mf1" foo=""> |
| 232 |
<input type="text1" name="query" value="%s"/> |
| 233 |
</form> |
| 234 |
<form name="mf" id="multiform" action="/formsubmit" class="test mf2" foo=""> |
| 235 |
<input type="text2" name="query" value="%s"/> |
| 236 |
</form> |
| 237 |
</p> |
| 238 |
</body> |
| 239 |
</html> |