Skip to content
Browse files

Fixed up some issues, now works, but nom needs buffer flushing.

  • Loading branch information...
1 parent dcb9498 commit 4ac7097e7ada0fb9e2c156f3f7bb4390e7160594 Timothy Totten committed Sep 15, 2011
Showing with 25 additions and 25 deletions.
  1. +12 −12 lib/HTTP/Easy.pm
  2. +13 −13 lib/HTTP/Easy/PSGI.pm
View
24 lib/HTTP/Easy.pm
@@ -7,14 +7,14 @@ class HTTP::Easy {
has Int $.port = 8080;
has Str $.host = 'localhost';
has $!listener = IO::Socket::INET.new(:localhost($.host), :localport($.port), :listen(1));
- has $!connection is rw; ## To be populated by accept().
- has %!env; ## The environment, generated by run().
- has $!body is rw; ## Any request body, populated by run().
+ has $.connection is rw; ## To be populated by accept().
+ has %.env; ## The environment, generated by run().
+ has $.body is rw; ## Any request body, populated by run().
method run {
$*ERR.say: "{time} Started HTTP server.";
self.pre-connection;
- while $!connection = $!listener.accept {
+ while $.connection = $!listener.accept {
self.on-connection;
my $received = $!connection.recv();
my @request = split("\x0D\x0A", $received);
@@ -24,13 +24,13 @@ class HTTP::Easy {
my ($path, $query) = $uri.split('?', 2);
$query //= '';
## First, let's add our "known" headers.
- %!env<SERVER_PROTOCOL> = $protocol;
- %!env<REQUEST_METHOD> = $method;
- %!env<QUERY_STRING> = $query;
- %!env<PATH_INFO> = $path;
- %!env<REQUEST_URI> = $uri;
- %!env<SERVER_NAME> = $.host;
- %!env<SERVER_PORT> = $.port;
+ %.env<SERVER_PROTOCOL> = $protocol;
+ %.env<REQUEST_METHOD> = $method;
+ %.env<QUERY_STRING> = $query;
+ %.env<PATH_INFO> = $path;
+ %.env<REQUEST_URI> = $uri;
+ %.env<SERVER_NAME> = $.host;
+ %.env<SERVER_PORT> = $.port;
## Next, let's add HTTP request headers.
while my $header = shift @request { ## Will end at the first empty line.
my ($key, $value) = $header.split(': ');
@@ -47,7 +47,7 @@ class HTTP::Easy {
}
}
## Anything left in @request is now the body.
- $!body = @request.join("\x0D\x0A"); ## Put it back together with CRLF.
+ $.body = @request.join("\x0D\x0A"); ## Put it back together with CRLF.
my $res = self.handler; ## Call our handler. It can return an HTTP response, or handle it itself, in which case it should return Nil.
if defined $res {
$!connection.send($res);
View
26 lib/HTTP/Easy/PSGI.pm
@@ -14,30 +14,30 @@ method app ($app) {
method handler {
## First, let's add any necessary PSGI variables.
- %!env<psgi.version> = [1,0];
- %!env<psgi.url_scheme> = 'http'; ## TODO: detect this.
- %!env<psgi.multithread> = False;
- %!env<psgi.multiprocess> = False;
- %!env<psgi.input> = $!body;
- %!env<psgi.errors> = $*ERR; ## TODO: allow override on this.
- %!env<psgi.run_once> = False;
- %!env<psgi.nonblocking> = False; ## Allow when NBIO.
- %!env<psgi.streaming> = False; ## Eventually?
+ %.env<psgi.version> = [1,0];
+ %.env<psgi.url_scheme> = 'http'; ## TODO: detect this.
+ %.env<psgi.multithread> = False;
+ %.env<psgi.multiprocess> = False;
+ %.env<psgi.input> = $.body;
+ %.env<psgi.errors> = $*ERR; ## TODO: allow override on this.
+ %.env<psgi.run_once> = False;
+ %.env<psgi.nonblocking> = False; ## Allow when NBIO.
+ %.env<psgi.streaming> = False; ## Eventually?
my $result;
if $!app ~~ Callable {
- $result = $!app(%!env);
+ $result = $!app(%.env);
}
elsif $!app.can('handle') {
- $result = $!app.handle(%!env);
+ $result = $!app.handle(%.env);
}
else {
die "Invalid {self.WHAT} application.";
}
- my $output = %!env<SERVER_PROTOCOL>~' '~$result[0]~$!crlf;
+ my $output = %.env<SERVER_PROTOCOL>~' '~$result[0]~$!crlf;
for @($result[1]) -> $header {
$output ~= $header.key ~ ': ' ~ $header.value ~ $!crlf;
}
- my $body = $return[2].join($!crlf);
+ my $body = $result[2].join($!crlf);
$output ~= $!crlf ~ $body;
return $output;
}

0 comments on commit 4ac7097

Please sign in to comment.
Something went wrong with that request. Please try again.