| 1 |
# Thanks to merlyn for nudging me and giving me this snippet! |
| 2 |
use strict; |
| 3 |
use HTTP::Daemon; |
| 4 |
use CGI; |
| 5 |
|
| 6 |
$|++; |
| 7 |
|
| 8 |
my $d = HTTP::Daemon->new or die; |
| 9 |
print $d->url, "\n"; |
| 10 |
|
| 11 |
my ($filename,$logfile) = @ARGV[0,1]; |
| 12 |
if ($filename) { |
| 13 |
open DATA, "< $filename" |
| 14 |
or die "Couldn't read page '$filename' : $!\n"; |
| 15 |
}; |
| 16 |
#open LOG, ">", $logfile |
| 17 |
# or die "Couldn't create logfile '$logfile' : $!\n"; |
| 18 |
my $log; |
| 19 |
my $body = join "", <DATA>; |
| 20 |
|
| 21 |
sub debug($) { |
| 22 |
my $message = $_[0]; |
| 23 |
$message =~ s!\n!\n#SERVER:!g; |
| 24 |
warn "#SERVER: $message" |
| 25 |
if $ENV{TEST_HTTP_VERBOSE}; |
| 26 |
}; |
| 27 |
|
| 28 |
SERVERLOOP: { |
| 29 |
my $quitserver; |
| 30 |
while (my $c = $d->accept) { |
| 31 |
debug "New connection"; |
| 32 |
while (my $r = $c->get_request) { |
| 33 |
debug "Request:\n" . $r->as_string; |
| 34 |
my $location = ($r->uri->path || "/"); |
| 35 |
my ($link1,$link2) = ('',''); |
| 36 |
if ($location =~ m!^/link/([^/]+)/(.*)$!) { |
| 37 |
($link1,$link2) = ($1,$2); |
| 38 |
}; |
| 39 |
my $res; |
| 40 |
if ($location eq '/get_server_log') { |
| 41 |
$res = HTTP::Response->new(200, "OK", undef, $log); |
| 42 |
undef $log; |
| 43 |
} elsif ( $location eq '/quit_server') { |
| 44 |
debug "Quitting"; |
| 45 |
$res = HTTP::Response->new(200, "OK", undef, "quit"); |
| 46 |
$c->force_last_request; |
| 47 |
$quitserver = 1; |
| 48 |
#close LOG; |
| 49 |
} else { |
| 50 |
$log .= "Request:\n" . $r->as_string . "\n"; |
| 51 |
if ($location =~ m!^/redirect/(.*)$!) { |
| 52 |
$res = HTTP::Response->new(302); |
| 53 |
$res->header('location', $d->url . $1); |
| 54 |
} elsif ($location =~ m!^/notfound/(.*)$!) { |
| 55 |
$res = HTTP::Response->new(404); |
| 56 |
#$res->header('location', $d->url . $1); |
| 57 |
} else { |
| 58 |
my $q = CGI->new($r->uri->query); |
| 59 |
|
| 60 |
# Make sticky form fields |
| 61 |
my ($query,$session,%cat); |
| 62 |
$query = defined $q->param('query') ? $q->param('query') : "(empty)"; |
| 63 |
$session = defined $q->param('session') ? $q->param('session') : 1; |
| 64 |
%cat = map { $_ => 1 } (defined $q->param('cat') ? $q->param('cat') : qw( cat_foo cat_bar )); |
| 65 |
my @categories = map { $cat{$_} ? "checked" : "" } qw( cat_foo cat_bar cat_baz ); |
| 66 |
$res = HTTP::Response->new(200, "OK", undef, sprintf($body,$location,$session,$query,@categories)); |
| 67 |
$res->content_type('text/html'); |
| 68 |
debug "Request " . ($r->uri->path || "/"); |
| 69 |
}; |
| 70 |
}; |
| 71 |
debug "Response:\n" . $res->as_string; |
| 72 |
$c->send_response($res); |
| 73 |
last if $quitserver; |
| 74 |
} |
| 75 |
$c->close; |
| 76 |
undef($c); |
| 77 |
last SERVERLOOP |
| 78 |
if $quitserver; |
| 79 |
} |
| 80 |
}; |
| 81 |
END { debug "Server stopped" }; |
| 82 |
|
| 83 |
__DATA__ |
| 84 |
<html> |
| 85 |
<head> |
| 86 |
<title>WWW::Mechanize::Shell test page</title> |
| 87 |
</head> |
| 88 |
<body> |
| 89 |
<h1>Location: %s</h1> |
| 90 |
<p> |
| 91 |
<a href="/test">Link /test</a> |
| 92 |
<a href="/foo">Link /foo</a> |
| 93 |
<a href="/slash_end">Link /</a> |
| 94 |
<a href="/slash_front">/Link </a> |
| 95 |
<a href="/slash_both">/Link in slashes/</a> |
| 96 |
<a href="/foo1.save_log_server_test.tmp">Link foo1.save_log_server_test.tmp</a> |
| 97 |
<a href="/foo2.save_log_server_test.tmp">Link foo2.save_log_server_test.tmp</a> |
| 98 |
<a href="/foo3.save_log_server_test.tmp">Link foo3.save_log_server_test.tmp</a> |
| 99 |
<table> |
| 100 |
<tr><th>Col1</th><th>Col2</th><th>Col3</th></tr> |
| 101 |
<tr><td>A1</td><td>A2</td><td>A3</td></tr> |
| 102 |
<tr><td>B1</td><td>B2</td><td>B3</td></tr> |
| 103 |
<tr><td>C1</td><td>C2</td><td>C3</td></tr> |
| 104 |
</table> |
| 105 |
<form name='f' action="/formsubmit"> |
| 106 |
<input type="hidden" name="session" value="%s"/> |
| 107 |
<input type="text" name="query" value="%s"/> |
| 108 |
<input type="submit" name="submit" value="Go"/> |
| 109 |
<input type="checkbox" name="cat" value="cat_foo" %s /> |
| 110 |
<input type="checkbox" name="cat" value="cat_bar" %s /> |
| 111 |
<input type="checkbox" name="cat" value="cat_baz" %s /> |
| 112 |
</form> |
| 113 |
</p> |
| 114 |
</body> |
| 115 |
</html> |