Permalink
Browse files

Commented out debugging code, applied some optimizations.

  • Loading branch information...
1 parent fe8708c commit 7e22f5420e698f86fdb207e309580aa9c1260415 Timothy Totten committed Oct 4, 2012
Showing with 62 additions and 47 deletions.
  1. +12 −9 lib/FastCGI.pm6
  2. +30 −30 lib/FastCGI/Connection.pm6
  3. +14 −2 lib/FastCGI/Protocol.pm6
  4. +6 −6 lib/FastCGI/Request.pm6
View
@@ -44,20 +44,23 @@ method accept ()
method handle (&closure)
{
my $log;
- if $.debug
+ if $.log
{
- $log = FastCGI::Logger.new(:name<FastCGI>, :string($.fancy-log));
+ if $.debug
+ {
+ $log = FastCGI::Logger.new(:name<FastCGI>, :string($.fancy-log));
+ }
+ else
+ {
+ $log = FastCGI::Logger.new(:string($.fancy-log), :!duration);
+ }
+ $log.say: "Loaded and waiting for connections.";
}
- elsif $.log
- {
- $log = FastCGI::Logger.new(:string($.fancy-log), :!duration);
- }
- $log.say: "Loaded and waiting for connections." if $.log || $.debug;
while (my $connection = self.accept)
{
- $log.say: "Received request." if $.log || $.debug;
+ $log.say: "Received request." if $.log;
$connection.handle-requests(&closure);
- $log.say: "Completed request." if $.log || $.debug;
+ $log.say: "Completed request." if $.log;
$connection.close;
}
}
View
@@ -8,7 +8,7 @@ use FastCGI::Errors;
use FastCGI::Constants;
use FastCGI::Protocol;
use FastCGI::Protocol::Constants :ALL;
-use FastCGI::Logger;
+#use FastCGI::Logger;
has $.socket;
has $.parent;
@@ -18,71 +18,71 @@ has $!closed = False;
method handle-requests (&closure)
{
- my $debug = $.parent.debug;
- my $log = FastCGI::Logger.new(:name<C::handle>);
+# my $debug = $.parent.debug;
+# my $log = FastCGI::Logger.new(:name<C::handle>);
loop
{
- $log.say: "Reading header." if $debug;
+# $log.say: "Reading header." if $debug;
my Buf $header = $.socket.read(FCGI_HEADER_LEN);
- $log.say: "Header read, parsing it." if $debug;
+# $log.say: "Header read, parsing it." if $debug;
my ($type, $id, $content-length) = parse_header($header);
- $log.say: "Header parsed. Now reading record." if $debug;
+# $log.say: "Header parsed. Now reading record." if $debug;
my Buf $record = $.socket.read($content-length);
- $log.say: "Record read, parsing it." if $debug;
+# $log.say: "Record read, parsing it." if $debug;
my %record = parse_record_body($type, $id, $record);
- $log.say: "Record parsed." if $debug;
+# $log.say: "Record parsed." if $debug;
given $type
{
when FCGI_BEGIN_REQUEST
{
- $log.say: "Creating Request object." if $debug;
+# $log.say: "Creating Request object." if $debug;
if %!requests.exists($id) { die "Request of id $id already exists"; }
%!requests{$id} = FastCGI::Request.new(:$id, :connection(self));
- $log.say: "Object created." if $debug;
+# $log.say: "Object created." if $debug;
}
when FCGI_PARAMS
{
- $log.say: "Parsing param." if $debug;
+# $log.say: "Parsing param." if $debug;
if ! %!requests.exists($id) { die "Invalid request id: $id"; }
my $req = %!requests{$id};
- if %record<content>
+ if %record<content>.defined
{
$req.param(%record<content>);
}
- $log.say: "Param parsed." if $debug;
+# $log.say: "Param parsed." if $debug;
}
when FCGI_STDIN
{
- $log.say: "Parsing STDIN" if $debug;
+# $log.say: "Parsing STDIN" if $debug;
if ! %!requests.exists($id) { die "Invalid request id: $id"; }
my $req = %!requests{$id};
- if %record<content>
+ if %record<content>.defined
{
$req.in(%record<content>);
- $log.say: "Added content to STDIN." if $debug;
+# $log.say: "Added content to STDIN." if $debug;
}
else
{
my %env = $req.env;
- $log.say: "Build environment, sending to closure." if $debug;
+# $log.say: "Build environment, sending to closure." if $debug;
my $return = &closure(%env);
- $log.say: "Got response from closure, sending it." if $debug;
+# $log.say: "Got response from closure, sending it." if $debug;
self.send-response($id, $return);
%!requests.delete($id);
- $log.say: "Sent response." if $debug;
+# $log.say: "Sent response." if $debug;
if ! $.parent.multiplex { return; }
}
}
when FCGI_GET_VALUES
{
- $log.say: "Handling GET_VALUES request." if $debug;
+# $log.say: "Handling GET_VALUES request." if $debug;
if $id != FCGI_NULL_REQUEST_ID
{
die "Invalid management request.";
}
self.send-values(%record<values>);
- $log.say: "Sent GET_VALUES_RESULT.";
+# $log.say: "Sent GET_VALUES_RESULT.";
if ! $.parent.multiplex { return; }
}
## TODO: FCGI_UNKNOWN_TYPE handling.
@@ -119,22 +119,22 @@ method send-values (%wanted)
method send-response ($request-id, $response-data)
{
- my $debug = $.parent.debug;
- my $log = FastCGI::Logger.new(:name<C::response>);
+# my $debug = $.parent.debug;
+# my $log = FastCGI::Logger.new(:name<C::response>);
my $http_message;
if $.parent.PSGI
{
- $log.say: "Building status code" if $debug;
+# $log.say: "Building status code" if $debug;
my $code = $response-data[0];
my $message = get_http_status_msg($code);
my $headers = "Status: $code $message"~CRLF;
- $log.say: "Status built, building headers." if $debug;
+# $log.say: "Status built, building headers." if $debug;
for @($response-data[1]) -> $header
{
$headers ~= $header.key ~ ": " ~ $header.value ~ CRLF;
}
$http_message = ($headers~CRLF).encode;
- $log.say: "Headers built, adding body." if $debug;
+# $log.say: "Headers built, adding body." if $debug;
for @($response-data[2]) -> $body
{
if $body ~~ Buf
@@ -162,7 +162,7 @@ method send-response ($request-id, $response-data)
my $res;
if $.err.messages.elems > 0
{
- $log.say: "Building response with error stream." if $debug;
+# $log.say: "Building response with error stream." if $debug;
my $errors = '';
for $.err.messages -> $emsg
{
@@ -172,13 +172,13 @@ method send-response ($request-id, $response-data)
}
else
{
- $log.say: "Building response." if $debug;
+# $log.say: "Building response." if $debug;
$res = build_end_request($request-id, $http_message);
}
- $log.say: "Response built, writing to socket." if $debug;
+# $log.say: "Response built, writing to socket." if $debug;
$.socket.write($res);
- $log.say: "Wrote response." if $debug;
+# $log.say: "Wrote response." if $debug;
}
method close
View
@@ -4,6 +4,7 @@ module FastCGI::Protocol;
use FastCGI::Constants;
use FastCGI::Protocol::Constants :ALL;
+#use FastCGI::Logger;
constant ERRMSG_OCTETS = 'Insufficient number of octets to parse %s';
constant ERRMSG_MALFORMED = 'Malformed record %s';
@@ -250,37 +251,48 @@ sub build_params (%params) is export
sub parse_params (Buf $octets) is export
{
+# my $log = FastCGI::Logger.new(:name<P::pp>);
my %params;
$octets.defined || return %params;
my $klen = 0;
my $vlen = 0;
my $olen = $octets.bytes;
my $offset = 0;
+# $log.say: "Okay, let's process the params.";
while $olen
{
for $klen, $vlen -> $len is rw
{
+# $log.say: "Determining length size.";
(1 <= $olen)
|| throw ERRMSG_OCTETS, 'FCGI_NameValuePair';
- $len = $octets.subbuf($offset++).unpack('C');
+ $len = $octets.subbuf($offset++, 1).unpack('C');
$olen--;
+# $log.say: "1 byte length: $len";
next if $len < 0x80;
+# $log.say: "Length uses 4 bytes";
(3 <= $olen)
|| throw ERRMSG_OCTETS, 'FCGI_NameValuePair';
$len = (pack('C', $len +& 0x7F) ~ $octets.subbuf($offset, 3)).unpack('N');
$offset+=3;
$olen-=3;
+# $log.say: "4 byte length: $len";
}
- ($klen + $vlen <= $octets.subbuf($offset).bytes)
+# $log.say: "Ensuring content is correct.";
+ ($klen + $vlen <= $olen)
|| throw ERRMSG_OCTETS, 'FCGI_NameValuePair';
+# $log.say: "Getting key";
my $key = $octets.subbuf($offset, $klen).decode;
$offset += $klen;
$olen -= $klen;
+# $log.say: "Getting value";
my $val = $octets.subbuf($offset, $vlen).decode;
$offset += $vlen;
$olen -= $vlen;
+# $log.say: "Setting param";
%params{$key} = $val;
}
+# $log.say: "Done processing parameters, returning.";
return %params;
}
View
@@ -4,7 +4,7 @@ class FastCGI::Request;
use FastCGI::Constants;
use FastCGI::Protocol;
-use FastCGI::Logger;
+#use FastCGI::Logger;
has $.connection;
has Buf $.input;
@@ -37,13 +37,13 @@ method in (Buf $stdin)
method env
{
- my $debug = $.connection.parent.debug;
- my $log = FastCGI::Logger.new(:name<R::env>);
+# my $debug = $.connection.parent.debug;
+# my $log = FastCGI::Logger.new(:name<R::env>);
## First, parse the environment.
- $log.say: "Going to parse params." if $debug;
+# $log.say: "Going to parse params." if $debug;
my %env = parse_params($!params);
- $log.say: "Parsed params, adding extra meta data." if $debug;
+# $log.say: "Parsed params, adding extra meta data." if $debug;
## Now add some meta data.
%env<fastcgi.request> = self;
@@ -60,7 +60,7 @@ method env
%env<psgi.streaming> = False; ## Allow eventually?
}
- $log.say: "Added meta data, returning env." if $debug;
+# $log.say: "Added meta data, returning env." if $debug;
return %env;
}

0 comments on commit 7e22f54

Please sign in to comment.