Permalink
Browse files

Skeleton class layout in place.

  • Loading branch information...
Timothy Totten
Timothy Totten committed Sep 29, 2012
0 parents commit 83d7253a107640ad6d5622998890d67137b85a8e
Showing with 189 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +7 −0 META.info
  3. +20 −0 README.md
  4. +44 −0 lib/FastCGI.pm6
  5. +22 −0 lib/FastCGI/Connection.pm6
  6. +7 −0 lib/FastCGI/Constants.pm6
  7. +21 −0 lib/FastCGI/Errors.pm6
  8. +35 −0 lib/FastCGI/Request.pm6
  9. +30 −0 lib/FastCGI/Response.pm6
@@ -0,0 +1,3 @@
+Makefile
+blib
+*.swp
@@ -0,0 +1,7 @@
+{
+ "name" : "FastCGI",
+ "version" : "*",
+ "description" : "A FastCGI library for Perl 6",
+ "depends" : [ "HTTP::Status" ],
+ "source-url" : "git://github.com/supernovus/FastCGI.git"
+}
@@ -0,0 +1,20 @@
+# FastCGI for Perl 6 #
+
+A library for building web applications using FastCGI in Perl 6.
+Uses a PSGI-compliant interface by default, so you can use it with
+any PSGI-compliant frameworks, such as WWW::App.
+
+## NOTE
+
+This is a work in progress, and currently is not implemented.
+When I'm done implementing this, I will likely go back and refactor
+my SCGI library to use a class structure closer to this.
+
+## Author
+
+This was build by Timothy Totten. You can find me on #perl6 with the nickname supernovus.
+
+## License
+
+Artistic License 2.0
+
@@ -0,0 +1,44 @@
+use v6;
+
+class FastCGI;
+
+use FastCGI::Connection;
+
+has Int $.port = 9090;
+has Str $.addr = 'localhost';
+has $.socket;
+
+has $.PSGI = True; ## Set to False to use raw HTTP responses.
+
+method connect (:$port=$.port, :$addr=$.addr)
+{
+ $.socket = IO::Socket::INET.new(
+ :localhost($addr),
+ :localport($port),
+ :listen(1)
+ );
+}
+
+method accept ()
+{
+ if (! $.socket)
+ {
+ self.connect();
+ }
+ my $connection = $.socket.accept() or return;
+ FastCGI::Connection(:socket($connection), :parent(self));
+}
+
+method handle (&closure)
+{
+ $*ERR.say: "[{time}] FastCGI is ready and waiting.";
+ while (my $connection = self.accept)
+ {
+ my $request = $connection.request;
+ my %env = $request.env;
+ my $return = closure(%env);
+ $connection.send($return);
+ $connection.close;
+ }
+}
+
@@ -0,0 +1,22 @@
+use v6;
+
+class FastCGI::Connection;
+
+use FastCGI::Request;
+use FastCGI::Response;
+use FastCGI::Errors;
+
+has $.socket;
+has $.parent;
+has $.err = FastCGI::Errors.new;
+
+method request ()
+{
+ FastCGI::Request(:connection(self)).parse;
+}
+
+method send ($output)
+{
+ FastCGI::Response(:connection(self)).send($output);
+}
+
@@ -0,0 +1,7 @@
+use v6;
+
+module FastCGI::Constants;
+
+constant CRLF is export = "\x0D\x0A";
+
+## TODO: Add the FastCGI constants below.
@@ -0,0 +1,21 @@
+use v6;
+
+class FastCGI::Errors;
+
+has @.messages;
+
+method print (*@msg)
+{
+ @.messages.push(@msg.join);
+}
+
+method say (*@msg)
+{
+ @.messages.push(@msg.join ~ "\n");
+}
+
+method printf ($format, *@msg)
+{
+ @.messages.push(sprintf($format, |@msg));
+}
+
@@ -0,0 +1,35 @@
+use v6;
+
+class FastCGI::Request;
+
+use FastCGI::Constants;
+
+has $.connection;
+has %.env;
+has $.input;
+
+method parse ()
+{
+ while my Buf $record = $.connection.socket.recv()
+ {
+ ## TODO: parse the record.
+ }
+
+ ## Now add some meta data.
+ %.env<fastcgi.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; ## Allow eventually?
+ }
+
+ return self;
+}
+
@@ -0,0 +1,30 @@
+use v6;
+
+class FastCGI::Response;
+
+use HTTP::Status;
+use FastCGI::Constants;
+
+has $.connection;
+
+method send ($response-data)
+{
+ my $http_message;
+ if ($.connection.parent.PSGI)
+ {
+ my $code = $response-data[0];
+ my $message = get_http_status_msg($code);
+ my $headers = "Status: $code $message"~CRLF;
+ for @($response-data[1]) -> $header
+ {
+ $headers ~= $header.key ~ ": " ~ $header.value ~ CRLF;
+ }
+ my $body = $response-data[2].join(CRLF);
+ $http_message = $headers~CRLF~$body;
+ }
+ else
+ {
+ $http_message = $response-data;
+ }
+ ## TODO: build and send the output records here.
+}

0 comments on commit 83d7253

Please sign in to comment.