Permalink
Browse files

Now uses Netstring library, works on Rakudo again.

  • Loading branch information...
1 parent 6be09a9 commit 453a05d008aeb6d33d302a55af7d9ab38be89fc8 Timothy Totten committed Mar 6, 2013
Showing with 37 additions and 67 deletions.
  1. +1 −1 META.info
  2. +2 −2 README.md
  3. +34 −64 lib/SCGI/Request.pm6
View
@@ -2,6 +2,6 @@
"name" : "SCGI",
"version" : "*",
"description" : "A SCGI library for Perl 6",
- "depends" : [ "HTTP::Status" ],
+ "depends" : [ "HTTP::Status", "Netstring" ],
"source-url" : "git://github.com/supernovus/SCGI.git"
}
View
@@ -114,8 +114,8 @@ SCGI script configuration:
## Requirements
- * HTTP::Status
- http://github.com/supernovus/perl6-http-status
+ * [HTTP::Status](https://github.com/supernovus/perl6-http-status)
+ * [Netstring](https://github.com/supernovus/perl6-netstring)
## Author
View
@@ -1,5 +1,6 @@
class SCGI::Request;
+use Netstring;
use SCGI::Constants;
has $.connection;
@@ -11,84 +12,53 @@ has $.request;
method parse ()
{
my $debug = $.connection.parent.debug;
- my $received = False;
- ## The while loop with read() doesn't work, using a bytes test instead.
- #while my $chunk = $.connection.socket.read(256)
- while ! $received
- {
- my $chunk = $.connection.socket.read(256);
- my $bytes = $chunk.bytes;
- $*ERR.say: "Read $bytes of data." if $debug;
- if $bytes != 256 { $received = True; }
- $!request ~= $chunk.decode;
- }
+
+ my $netstring = read-netstring($.connection.socket);
+ $!request = $netstring.decode;
+
my $rlen = $.request.chars;
my $err = $.connection.err;
if $debug { $*ERR.say: "Receieved request: $.request"; }
- if $.request ~~ / ^ (\d+) \: /
+ my @env = $.request.split(SEP);
+ @env.pop;
+ %!env = @env;
+
+ if $.connection.parent.strict
{
- if $debug
- {
- $*ERR.say: "A proper request was received, parsing into an ENV";
- }
- my $length = +$0;
- my $offset = $0.Str.chars + 1;
- if ($rlen < $length + $offset)
+ unless defined %.env<CONTENT_LENGTH>
+ && %.env<CONTENT_LENGTH> ~~ / ^ \d+ $ /
{
- $err.say(SCGI_E_LENGTH ~ " [$rlen/$length+$offset]");
+ $err.say(SCGI_E_CONTENT);
return self;
}
- my $env_string = $.request.substr($offset, $length);
- my $comma = $.request.substr($offset+$length, 1);
- if $comma ne ','
+ unless %.env<SCGI> && %.env<SCGI> eq '1'
{
- $err.sayf(SCGI_E_COMMA, $comma);
+ $err.say(SCGI_E_SCGI);
return self;
}
- $!input = $.request.substr($offset+$length+1);
- my @env = $env_string.split(SEP);
- @env.pop;
- %!env = @env;
- if $.connection.parent.strict
- {
- unless defined %.env<CONTENT_LENGTH>
- && %.env<CONTENT_LENGTH> ~~ / ^ \d+ $ /
- {
- $err.say(SCGI_E_CONTENT);
- return self;
- }
- unless %.env<SCGI> && %.env<SCGI> eq '1'
- {
- $err.say(SCGI_E_SCGI);
- return self;
- }
- }
-
- %.env<scgi.request> = self;
- if $.connection.parent.PSGI
- {
- %.env<psgi.version> = [1,0];
- %.env<psgi.url_scheme> = 'http'; ## FIXME: detect this.
- %.env<psgi.multithread> = False;
- %.env<psgi.multiprocess> = False;
- %.env<psgi.input> = $.input;
- %.env<psgi.errors> = $.connection.err;
- %.env<psgi.run_once> = False;
- %.env<psgi.nonblocking> = False; ## Allow when NBIO.
- %.env<psgi.streaming> = False; ## Eventually?
- }
-
- $!success = True;
- return self;
}
- elsif $.request ~~ /:s ^ QUIT $ /
+
+ my $clen = +%.env<CONTENT_LENGTH>;
+ if $clen > 0
{
- $.connection.shutdown(SCGI_M_QUIT);
+ $!input = $.connection.socket.read($clen);
}
- else
+
+ %.env<scgi.request> = self;
+ if $.connection.parent.PSGI
{
- $err.sayf(SCGI_E_INVALID, $.request);
- return self;
+ %.env<psgi.version> = [1,0];
+ %.env<psgi.url_scheme> = 'http'; ## FIXME: detect this.
+ %.env<psgi.multithread> = False;
+ %.env<psgi.multiprocess> = False;
+ %.env<psgi.input> = $.input;
+ %.env<psgi.errors> = $.connection.err;
+ %.env<psgi.run_once> = False;
+ %.env<psgi.nonblocking> = False; ## Allow when NBIO.
+ %.env<psgi.streaming> = False; ## Eventually?
}
+
+ $!success = True;
+ return self;
}

0 comments on commit 453a05d

Please sign in to comment.