forked from mberends/http-server-simple
/
02-simple-small.pl6
36 lines (34 loc) · 1.12 KB
/
02-simple-small.pl6
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
# http-server-simple/examples/02-simple-small.pl6
# An implementation of the Perl 5 version's second synopsis example
# (using a sub-class).
use v6;
use HTTP::Server::Simple;
class Example::Simple::Small is HTTP::Server::Simple {
has %!header;
has $!path;
has $!query_string;
method setup ( :$path, :$query_string, *%rest ) {
$!path = $path;
$!query_string = $query_string;
}
method header ( $key, $value ) {
%!header{$key} = $value;
}
# override the request handler of the base class
method handle_request () {
print "HTTP/1.0 200 OK\x0D\x0A\x0D\x0A";
say "<html>\n<body>";
say self.WHAT, " at {$.host}:{$.port}";
say "<table border=\"1\">";
for %!header.keys -> $key {
my $value = %!header{$key};
say "<tr><td>{$key}</td><td>{$value}</td></tr>";
}
say "</table>";
say "Path: {$!path}<br/>";
say "Query string: {$!query_string}<br/>";
say "</body>\n</html>";
}
}
my Example::Simple::Small $server .= new;
$server.run; # now browse http://localhost:8080/whatever?name=value