Created
April 7, 2009 07:09
-
-
Save masak/91124 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat test-daemon | |
use v6; | |
use HTTP::Daemon; | |
defined @*ARGS[0] && @*ARGS[0] eq '--request' ?? request() !! daemon(); | |
# handle a single browser request, executed in a child process of netcat | |
sub request { | |
my HTTP::Daemon $d .= new; | |
while my HTTP::Daemon::ClientConn $c = $d.accept { | |
while my HTTP::Request $r = $c.get_request { | |
my $method = $r.method; | |
if $r.method eq 'GET' { | |
given $r.url.path { | |
when '/' { root_dir( $c, $r ); } | |
when / ^ \/pub\/ $ / { pub_dir( $c, $r ); } | |
} | |
} | |
else { | |
$c.send_error('RC_FORBIDDEN'); | |
} | |
} | |
} | |
} | |
# start the main server and enter the endless loop in the inner daemon. | |
sub daemon { | |
my HTTP::Daemon $d .= new( host=>'127.0.0.1', port=>2080 ); | |
say "Browse this Perl 6 web server at {$d.url}"; | |
$d.daemon(); | |
} | |
# called from sub request for the '/' url | |
sub root_dir( HTTP::Daemon::ClientConn $c, HTTP::Request $r ) { | |
my $content = q[<html><head><title>Hello</title> | |
<body><h1>Rakudo web server</h1> | |
Hello, world! This is root. Go to <a href="/pub/">pub</a>. | |
</body></html>]; | |
$c.send_response( $content ); | |
} | |
# called from sub request for the '/pub/' url | |
sub pub_dir( HTTP::Daemon::ClientConn $c, HTTP::Request $r ) { | |
my $content = q[<html><head><title>Hello</title> | |
<body><h1>Rakudo web server</h1> | |
Hello again, this is pub. Go <a href="/">Home</a>. | |
</body></html>]; | |
$c.send_response( $content ); | |
} | |
$ perl6 test-daemon | |
error:imcc:syntax error, unexpected INTC, expecting '(' ('2') | |
in file '/Users/masak/gwork/perl6-examples/lib/HTTP/Daemon.pir' line 84 | |
error:imcc:syntax error, unexpected VAR, expecting '(' ('sock') | |
in file '/Users/masak/gwork/perl6-examples/lib/HTTP/Daemon.pir' line 85 | |
error:imcc:syntax error, unexpected VAR, expecting '(' ('sock') | |
in file '/Users/masak/gwork/perl6-examples/lib/HTTP/Daemon.pir' line 280 | |
error:imcc:syntax error, unexpected VAR, expecting '(' ('ret') | |
in file '/Users/masak/gwork/perl6-examples/lib/HTTP/Daemon.pir' line 342 | |
error:imcc:syntax error, unexpected VAR, expecting '(' ('work') | |
in file '/Users/masak/gwork/perl6-examples/lib/HTTP/Daemon.pir' line 406 | |
Browse this Perl 6 web server at http://127.0.0.1:2080/ | |
2009/04/07 09:08:25 socat[19621] E execvp("", "", "test-daemon", "--request", ...): No such file or directory | |
^C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment