[Catalyst-commits] r7586 - in Catalyst-Engine-Embeddable/trunk: . lib lib/Catalyst lib/Catalyst/Engine t t/lib t/lib/TestApp t/lib/TestApp/Controller

ruoso at dev.catalyst.perl.org ruoso at dev.catalyst.perl.org
Mon Apr 7 15:52:14 BST 2008


Author: ruoso
Date: 2008-04-07 15:52:13 +0100 (Mon, 07 Apr 2008)
New Revision: 7586

Added:
   Catalyst-Engine-Embeddable/trunk/Changes
   Catalyst-Engine-Embeddable/trunk/MANIFEST
   Catalyst-Engine-Embeddable/trunk/Makefile.PL
   Catalyst-Engine-Embeddable/trunk/README
   Catalyst-Engine-Embeddable/trunk/lib/
   Catalyst-Engine-Embeddable/trunk/lib/Catalyst/
   Catalyst-Engine-Embeddable/trunk/lib/Catalyst/Engine/
   Catalyst-Engine-Embeddable/trunk/lib/Catalyst/Engine/Embeddable.pm
   Catalyst-Engine-Embeddable/trunk/t/
   Catalyst-Engine-Embeddable/trunk/t/lib/
   Catalyst-Engine-Embeddable/trunk/t/lib/TestApp.pm
   Catalyst-Engine-Embeddable/trunk/t/lib/TestApp/
   Catalyst-Engine-Embeddable/trunk/t/lib/TestApp/Controller/
   Catalyst-Engine-Embeddable/trunk/t/lib/TestApp/Controller/Root.pm
   Catalyst-Engine-Embeddable/trunk/t/request.t
Log:
[C-E-Embeddable] Initial import

Added: Catalyst-Engine-Embeddable/trunk/Changes
===================================================================
--- Catalyst-Engine-Embeddable/trunk/Changes	                        (rev 0)
+++ Catalyst-Engine-Embeddable/trunk/Changes	2008-04-07 14:52:13 UTC (rev 7586)
@@ -0,0 +1,4 @@
+Author: Daniel Ruoso <daniel at ruoso.com>
+Date:   12 December 2007
+
+    initial revision

Added: Catalyst-Engine-Embeddable/trunk/MANIFEST
===================================================================
--- Catalyst-Engine-Embeddable/trunk/MANIFEST	                        (rev 0)
+++ Catalyst-Engine-Embeddable/trunk/MANIFEST	2008-04-07 14:52:13 UTC (rev 7586)
@@ -0,0 +1,8 @@
+t/lib/TestApp/Controller/Root.pm
+t/lib/TestApp.pm
+t/request.t
+Changes
+Makefile.PL
+README
+MANIFEST
+lib/Catalyst/Engine/Embeddable.pm

Added: Catalyst-Engine-Embeddable/trunk/Makefile.PL
===================================================================
--- Catalyst-Engine-Embeddable/trunk/Makefile.PL	                        (rev 0)
+++ Catalyst-Engine-Embeddable/trunk/Makefile.PL	2008-04-07 14:52:13 UTC (rev 7586)
@@ -0,0 +1,10 @@
+use 5.008008;
+use ExtUtils::MakeMaker;
+WriteMakefile
+  (
+   NAME              => 'Catalyst::Engine::Embeddable',
+   VERSION_FROM      => 'lib/Catalyst/Engine/Embeddable.pm',
+   PREREQ_PM         => { Catalyst::Runtime => 5.7011, URI => 0, HTTP::Response => 0, HTTP::Request => 0, HTTP::Body => 0 },
+   ABSTRACT_FROM  => 'lib/Catalyst/Engine/Embeddable.pm',
+   AUTHOR         => 'Daniel Ruoso <daniel.ruoso at verticalone.pt>',
+  );

Added: Catalyst-Engine-Embeddable/trunk/README
===================================================================
--- Catalyst-Engine-Embeddable/trunk/README	                        (rev 0)
+++ Catalyst-Engine-Embeddable/trunk/README	2008-04-07 14:52:13 UTC (rev 7586)
@@ -0,0 +1,88 @@
+NAME
+    Catalyst::Engine::Embeddable - Use a Catalyst application as an object
+
+SYNOPSIS
+      # after creating the application using this engine, you can just
+      my $http_response;
+      my $response_code $app->handle_request(
+               $http_request, \$http_response);
+
+ABSTRACT
+    Enables a Catalyst application to be used as a standard Perl object.
+
+SUMMARY
+    This module provides a way to embed a Catalyst application in any other
+    program using standard Perl Object Orientation to do a request and get
+    the response.
+
+    It works by using the arguments that are passed from the handle_request
+    method in the Catalyst module to the prepare_request method in the
+    engine, which will then handle the request as coming from the
+    HTTP::Request object passed, instead of trying to fetch the elements
+    from the ENV, like the CGI engine does.
+
+    As the handle_request method only returns the response code and not the
+    response object at all, in the case you want the complete response you
+    need to pass a second argument which is a scalar ref where the
+    HTTP::Response object will be stored in the "finalize" phase of the
+    processing.
+
+    This engine provides complete compatibility with any plugin the
+    application may use, in a way that different embedded applications may
+    use different plugins and still be used side-by-side.
+
+    There's one important consideration regarding the URI in the request.
+    For the means of the Catalyst processing, the base path for the script
+    is always constructed as the "/" path of the same URI as the request.
+
+METHODS
+    The following methods were overriden from Catalyst::Engine.
+
+    $engine->prepare_request($c, $http_request, $http_response_ret_ref)
+        This method is overrided in order to store the request and the
+        response in $c as to continue the processing later. The scalar ref
+        here will be used to set the response object, as there is no other
+        way to obtain the response.
+
+        This information will be stored as
+        $c->req->{_engine_embeddable}{req} and
+        $c->req->{_engine_embeddable}{res} for future usage.
+
+    $engine->prepare_headers($c)
+        This is where the headers are fetched from the HTTP::Request object
+        and set into $c->req.
+
+    $engine->prepare_path($c)
+        Get the path info from the HTTP::Request object.
+
+    $engine->prepare_query_parameters($c)
+        Set the query params from the HTTP::Request to the catalyst request.
+
+    $engine->prepare_body($c)
+        Gets the body of the HTTP::Request, creates an HTTP::Body object,
+        and set it in $c->req->{_body}, then being compatible with
+        Catalyst::Engine from now on.
+
+    $engine->finalize_headers($c)
+        Set the "Status" header in the response and store the headers from
+        the catalyst response object to the HTTP::Response object.
+
+    $engine->finalize_body($c)
+        Copies the body from the catalyst response to the HTTP::Response
+        object.
+
+SEE ALSO
+    Catalyst::Engine, Catalyst::Engine::CGI, HTTP::Request, HTTP::Reponse,
+    Catalyst
+
+AUTHORS
+    Daniel Ruoso "daniel.ruoso at verticalone.pt"
+
+BUG REPORTS
+    Please submit all bugs regarding "Catalyst::Engine::Embeddable" to
+    "bug-catalyst-engine-embeddable at rt.cpan.org"
+
+LICENSE
+    This library is free software, you can redistribute it and/or modify it
+    under the same terms as Perl itself.
+

Added: Catalyst-Engine-Embeddable/trunk/lib/Catalyst/Engine/Embeddable.pm
===================================================================
--- Catalyst-Engine-Embeddable/trunk/lib/Catalyst/Engine/Embeddable.pm	                        (rev 0)
+++ Catalyst-Engine-Embeddable/trunk/lib/Catalyst/Engine/Embeddable.pm	2008-04-07 14:52:13 UTC (rev 7586)
@@ -0,0 +1,186 @@
+{   package Catalyst::Engine::Embeddable;
+
+    our $VERSION = '0.0.1';
+    use base qw(Catalyst::Engine);
+    use strict;
+    use warnings;
+    use URI;
+    use HTTP::Body;
+    use HTTP::Response;
+
+    sub prepare_request {
+        my ($self, $c, $req, $res_ref) = @_;
+        $c->req->{_engine_embeddable}{req} = $req;
+        $c->req->{_engine_embeddable}{res} = $res_ref;
+        $c->req->method($req->method);
+    }
+
+    sub prepare_headers {
+        my ($self, $c) = @_;
+        $c->req->{_engine_embeddable}{req}->scan
+          (sub {
+               my ($name, $value) = @_;
+               $c->req->header($name, $value);
+           });
+    }
+
+    sub prepare_path {
+        my ($self, $c) = @_;
+
+        my $uri = $c->req->{_engine_embeddable}{req}->uri();
+        my $base = $uri->clone; $base->path('/');
+
+        $c->req->uri($uri);
+        $c->req->base($base);
+    }
+
+    sub prepare_query_parameters {
+        my ($self, $c) = @_;
+        my %params = $c->req->{_engine_embeddable}{req}->uri->query_form;
+        $c->req->query_parameters(\%params);
+    }
+
+    sub prepare_body {
+        my ($self, $c) = @_;
+        my $req = $c->req->{_engine_embeddable}{req};
+        $req->content_length(0) unless $req->content_length;
+
+        $c->req->content_encoding($req->content_encoding);
+        $c->req->content_type($req->content_type);
+        $c->req->content_length($req->content_length);
+
+        my $http_body = HTTP::Body->new($c->req->content_type, $c->req->content_length);
+        $http_body->add($req->content());
+        $c->req->{_body} = $http_body;
+    }
+
+    sub finalize_headers {
+        my ($self, $c) = @_;
+
+        my $response = HTTP::Response->new($c->res->status,
+                                           'Catalyst-Engine-Embeddable',
+                                           $c->res->headers);
+
+        ${$c->req->{_engine_embeddable}{res}} = $response;
+    }
+
+    sub finalize_body {
+        my ($self, $c) = @_;
+        ${$c->req->{_engine_embeddable}{res}}->content($c->res->body());
+    }
+
+};
+1;
+
+__END__
+
+=head1 NAME
+
+Catalyst::Engine::Embeddable - Use a Catalyst application as an object
+
+=head1 SYNOPSIS
+
+  # after creating the application using this engine, you can just
+  my $http_response;
+  my $response_code $app->handle_request(
+           $http_request, \$http_response);
+
+=head1 ABSTRACT
+
+Enables a Catalyst application to be used as a standard Perl object.
+
+=head1 SUMMARY
+
+This module provides a way to embed a Catalyst application in any
+other program using standard Perl Object Orientation to do a request
+and get the response.
+
+It works by using the arguments that are passed from the
+handle_request method in the Catalyst module to the prepare_request
+method in the engine, which will then handle the request as coming
+from the HTTP::Request object passed, instead of trying to fetch the
+elements from the ENV, like the CGI engine does.
+
+As the handle_request method only returns the response code and not
+the response object at all, in the case you want the complete response
+you need to pass a second argument which is a scalar ref where the
+HTTP::Response object will be stored in the "finalize" phase of the
+processing.
+
+This engine provides complete compatibility with any plugin the
+application may use, in a way that different embedded applications may
+use different plugins and still be used side-by-side.
+
+There's one important consideration regarding the URI in the
+request. For the means of the Catalyst processing, the base path for
+the script is always constructed as the "/" path of the same URI as
+the request.
+
+=head1 METHODS
+
+The following methods were overriden from Catalyst::Engine.
+
+=over
+
+=item $engine->prepare_request($c, $http_request, $http_response_ret_ref)
+
+This method is overrided in order to store the request and the
+response in $c as to continue the processing later. The scalar ref
+here will be used to set the response object, as there is no other way
+to obtain the response.
+
+This information will be stored as $c->req->{_engine_embeddable}{req} and
+$c->req->{_engine_embeddable}{res} for future usage.
+
+=item $engine->prepare_headers($c)
+
+This is where the headers are fetched from the HTTP::Request object
+and set into $c->req.
+
+=item $engine->prepare_path($c)
+
+Get the path info from the HTTP::Request object.
+
+=item $engine->prepare_query_parameters($c)
+
+Set the query params from the HTTP::Request to the catalyst request.
+
+=item $engine->prepare_body($c)
+
+Gets the body of the HTTP::Request, creates an HTTP::Body object, and
+set it in $c->req->{_body}, then being compatible with
+Catalyst::Engine from now on.
+
+=item $engine->finalize_headers($c)
+
+Set the "Status" header in the response and store the headers from the
+catalyst response object to the HTTP::Response object.
+
+=item $engine->finalize_body($c)
+
+Copies the body from the catalyst response to the HTTP::Response
+object.
+
+=back
+
+=head1 SEE ALSO
+
+L<Catalyst::Engine>, L<Catalyst::Engine::CGI>, L<HTTP::Request>,
+L<HTTP::Reponse>, L<Catalyst>
+
+=head1 AUTHORS
+
+Daniel Ruoso C<daniel.ruoso at verticalone.pt>
+
+=head1 BUG REPORTS
+
+Please submit all bugs regarding C<Catalyst::Engine::Embeddable> to
+C<bug-catalyst-engine-embeddable at rt.cpan.org>
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+

Added: Catalyst-Engine-Embeddable/trunk/t/lib/TestApp/Controller/Root.pm
===================================================================
--- Catalyst-Engine-Embeddable/trunk/t/lib/TestApp/Controller/Root.pm	                        (rev 0)
+++ Catalyst-Engine-Embeddable/trunk/t/lib/TestApp/Controller/Root.pm	2008-04-07 14:52:13 UTC (rev 7586)
@@ -0,0 +1,23 @@
+package TestApp::Controller::Root;
+
+use strict;
+use warnings;
+use base 'Catalyst::Controller';
+__PACKAGE__->config->{namespace} = '';
+
+sub default : Private {
+    my ( $self, $c ) = @_;
+    $c->response->body( 'Hello' );
+}
+
+sub foo : Local {
+    my ( $self, $c ) = @_;
+    $c->res->body('Hello World!');
+}
+
+sub bar : Local {
+    my ( $self, $c ) = @_;
+    $c->res->body('Hello '.$c->req->param('who').'!');
+}
+
+1;

Added: Catalyst-Engine-Embeddable/trunk/t/lib/TestApp.pm
===================================================================
--- Catalyst-Engine-Embeddable/trunk/t/lib/TestApp.pm	                        (rev 0)
+++ Catalyst-Engine-Embeddable/trunk/t/lib/TestApp.pm	2008-04-07 14:52:13 UTC (rev 7586)
@@ -0,0 +1,14 @@
+package TestApp;
+
+use strict;
+use warnings;
+
+use Catalyst::Runtime '5.70';
+use Catalyst;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->config( name => 'TestApp' );
+__PACKAGE__->setup;
+
+1;

Added: Catalyst-Engine-Embeddable/trunk/t/request.t
===================================================================
--- Catalyst-Engine-Embeddable/trunk/t/request.t	                        (rev 0)
+++ Catalyst-Engine-Embeddable/trunk/t/request.t	2008-04-07 14:52:13 UTC (rev 7586)
@@ -0,0 +1,47 @@
+use strict;
+use warnings;
+
+use Test::More tests => 10;
+
+use HTTP::Request;
+
+BEGIN { $ENV{CATALYST_ENGINE} = 'Embeddable' };
+BEGIN { use_ok('Catalyst::Engine::Embeddable') };
+
+use lib 't/lib';
+require TestApp;
+
+my ($req, $res);
+
+$req = HTTP::Request->new(GET => '/foo');
+$res = undef;
+
+TestApp->handle_request($req, \$res);
+
+ok($res, 'Response object defined.');
+is($res->code, 200, 'Response code correct.');
+is($res->content, 'Hello World!', 'Resonse content correct.');
+
+$req = HTTP::Request->new(GET => '/bar?who=Embed');
+$res = undef;
+
+TestApp->handle_request($req, \$res);
+
+ok($res, 'Response object defined.');
+is($res->code, 200, 'Response code correct.');
+is($res->content, 'Hello Embed!', 'Resonse content correct.');
+
+$req = HTTP::Request->new(POST => '/bar');
+$res = undef;
+
+$req->content_type('application/x-www-form-urlencoded');
+my $post_body = 'who=Post';
+$req->content($post_body);
+$req->content_length(length($post_body));
+
+TestApp->handle_request($req, \$res);
+
+ok($res, 'Response object defined.');
+is($res->code, 200, 'Response code correct.');
+is($res->content, 'Hello Post!', 'Resonse content correct.');
+




More information about the Catalyst-commits mailing list