REST-Application-0.992/0000755000175000017500000000000010656657711014475 5ustar matthewmatthewREST-Application-0.992/t/0000755000175000017500000000000010656657711014740 5ustar matthewmatthewREST-Application-0.992/t/02-routes.t0000644000175000017500000004513310656657260016672 0ustar matthewmatthew#!/usr/bin/perl use strict; use warnings; use Test::More tests => 67; use lib 't/'; # Some special helpers to restove the environment my %ORIG_ENV = %ENV; sub restoreENV { %ENV = %ORIG_ENV } # TEST: use BEGIN { use_ok('REST::Application::Routes'); $SIG{'__WARN__'} = sub { &Carp::croak }; } # TEST: new() { my $rest = REST::Application::Routes->new(); isa_ok($rest, 'REST::Application::Routes', "Object instantiation."); isa_ok($rest, 'REST::Application', "Object instantiation."); } # TEST: query() { my $rest = REST::Application::Routes->new(); my $query = $rest->defaultQueryObject(); is(ref($query), 'CGI', "Retrieving default query object."); } # TEST: query() { my $rest = REST::Application::Routes->new(); my $query = $rest->query(); is(ref($query), 'CGI', "Retrieving query object."); } # TEST: query($value) { my $rest = REST::Application::Routes->new(); my $query = $rest->query("x/a/b"); is($query, 'x/a/b', "Setting and retrieving a query object."); } # TEST: query(undef) { my $rest = REST::Application::Routes->new(); my $query = $rest->query(undef); is($query, undef, "Setting and retrieving a query object w/ undef."); } # TEST: defaultQueryObject($value) { my $rest = REST::Application::Routes->new(); my $query = $rest->defaultQueryObject("xxx"); is($query, 'xxx', "Setting and retrieving default query object."); } # TEST: defaultQueryObject(undef) { my $rest = REST::Application::Routes->new(); my $query = $rest->defaultQueryObject(undef); is($query, undef, "Setting and retrieving default query object w/ undef value."); } # TEST: resourceHooks() { my $rest = REST::Application::Routes->new(); my $resources = $rest->resourceHooks(); is_deeply($resources, {}, "Getting resource hooks when none are set."); } # TEST: resourceHooks() { my $rest = REST::Application::Routes->new(); my $sub = sub {}; my $template = '/var/foo/bar/baz'; my $resources = $rest->resourceHooks($template => $sub); isa_ok($resources, 'HASH', "Resource hook using a code ref (data type check)"); is($resources->{$template}, $sub, "Resource hook using a code ref (value check)"); } # TEST: resourceHooks() { my $rest = REST::Application::Routes->new(); my %uniq; my @keys = map { int(rand(100000)); } (1 .. 10000); @uniq{@keys} = 1; @keys = keys(%uniq); # Make sure we have no duplicate keys my @k2 = @keys; my $resources = $rest->resourceHooks(map { $_ => "x" } @keys); my $keys = [ keys %$resources ]; is_deeply(\@k2, \@keys, "Resource hook regexes have their order preserved."); } # TEST: resourceHooks() { my $rest = REST::Application::Routes->new(); my $resources = $rest->resourceHooks({ foo => 1 }); is_deeply($resources, {foo => 1}, "Resource hook set from a hash ref"); } # TEST: resourceHooks() { my $rest = REST::Application::Routes->new(); $rest->resourceHooks(foo => 1); my $resources = $rest->resourceHooks(); is_deeply($resources, {foo => 1}, "Resource hook set and retrieved in 2 steps."); } # TEST: getPathInfo() { require_ok('CGI'); restoreENV(); CGI->initialize_globals(); $ENV{PATH_INFO} = "/blah/bar"; my $rest = REST::Application::Routes->new(); is_deeply($rest->getPathInfo(), "/blah/bar", "Retrieving path info."); } # TEST: getRequestMethod() { restoreENV(); CGI->initialize_globals(); $ENV{REQUEST_METHOD} = "PuT"; my $rest = REST::Application::Routes->new(); is_deeply($rest->getRequestMethod(), "PUT", "Retrieving request method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{PATH_INFO} = "/parts/12345/foo"; $rest->resourceHooks('/parts/:nums/:var' => sub { ref($_[0]) . $_[1]->{nums}.$_[1]->{var} }); is(${$rest->loadResource()}, "REST::Application::Routes12345foo", "Loading resource - code reference"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{PATH_INFO} = "/parts/12345/foo"; $rest->resourceHooks('/parts/:nums/:var' => undef); is(${$rest->loadResource()}, undef, "Loading resource - default hook via undef"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{PATH_INFO} = "/NOEXIST/12345.xml"; $rest->resourceHooks('/parts/:nums/:var' => sub {1}); is(${$rest->loadResource()}, undef, "Loading resource - default hook via non-match"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); use_ok('RoutesTestClass'); my $rest = RoutesTestClass->new(); $rest->resourceHooks(parts => "barMethod"); my $resource = $rest->loadResource("parts", "blah", "bar", "baz"); is($$resource, 'blah:bar:baz', "Loading resource - \"methodName\" hook"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{PATH_INFO} = "/parts/12345/cows"; $rest->resourceHooks('/parts/:var/:foo' => [$rest, "getPathInfo"]); my $resource = $rest->loadResource(); is($$resource, '/parts/12345/cows', "Loading resource - [\$object w/ \"methodName\"] hook"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345/cows"; $rest->resourceHooks('/parts/:var/:foo' => { get => sub { "x" }, puT => [$rest, "getPathInfo"], POST => "getPathInfo", 'deLEte' => undef, }); my $resource = $rest->loadResource(); is($$resource, 'x', "Loading resource for GET HTTP method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{REQUEST_METHOD} = "PuT"; $ENV{PATH_INFO} = "/parts/12345/cows"; $rest->resourceHooks('/parts/:var/:foo' => { get => sub { "x" }, puT => [$rest, "getPathInfo"], POST => "getPathInfo", 'deLEte' => undef, }); my $resource = $rest->loadResource(); is($$resource, '/parts/12345/cows', "Loading resource for PUT HTTP method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{REQUEST_METHOD} = "PoSt"; $ENV{PATH_INFO} = "/parts/12345/cows"; $rest->resourceHooks('/parts/:var/:foo' => { get => sub { "x" }, puT => [$rest, "getPathInfo"], POST => "getPathInfo", 'deLEte' => undef, }); my $resource = $rest->loadResource(); is($$resource, '/parts/12345/cows', "Loading resource for POST HTTP method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{REQUEST_METHOD} = "delete"; $ENV{PATH_INFO} = "/parts/12345/cows"; $rest->resourceHooks('/parts/:var/:foo' => { get => sub { "x" }, puT => [$rest, "getPathInfo"], POST => "getPathInfo", 'deLEte' => undef, }); my $resource = $rest->loadResource(); is($$resource, undef, "Loading resource for DELETE HTTP method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{REQUEST_METHOD} = "delete"; $ENV{PATH_INFO} = "/parts/12345/cows"; my $obj = RoutesTestClass->new(); $rest->resourceHooks('/parts/:var/:nam' => $obj); my $resource = $rest->loadResource(); is($$resource, "xAbC", "Loading resource - with \$object->getResource() hook"); } # TEST: headerType { my $rest = REST::Application::Routes->new(); is($rest->headerType(), 'header', "Retrieving default header type"); $rest->headerType("redIRect"); is($rest->headerType(), 'redirect', "Setting header type to \"redirect\"."); $rest->headerType("nOne"); is($rest->headerType(), 'none', "Setting header type to \"none\"."); eval { $rest->headerType("blahblahlbha") }; ok($@, "Checking error for invalid header type"); } # TEST: header { my $rest = REST::Application::Routes->new(); my %hash = $rest->header(); is_deeply(\%hash, {}, "Retrieving default header values."); $rest->header(-type => 'text/html', -foobar => 5); %hash = $rest->header(); is_deeply(\%hash, {-type => 'text/html', -foobar => 5}, "Retrieving custom header values."); } # TEST: resetHeader() { my $rest = REST::Application::Routes->new(); my %hash1 = $rest->header(-type => 'text/html', -foobar => 5); my %hash2 = $rest->resetHeader(); my %hash3 = $rest->header(); is_deeply(\%hash1, \%hash2, "Resetting header, verifying return value."); is_deeply(\%hash3, {}, "Resetting header, verifying reset."); } # TEST: run() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/honda/hubcaps/12345"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { my ($app, $hash) = @_; $app->header(-type => 'text/plain'); return "hubcap - $hash->{id} - Honda"; }; $rest->resourceHooks('/honda/hubcaps/:id' => $resourceHook); my $output = $rest->run(); my $answer = "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nhubcap - 12345 - Honda"; is($output, $answer, "Running a REST Application"); } # TEST: run() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/honda/hubcaps/12345"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { "this is a test" }; $rest->resourceHooks('/honda/hubcaps/:id' => $resourceHook); my $answer = "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nthis is a test"; is($rest->run(), $answer, "Running a REST Application which has a resource being its own repr."); } # TEST: addRepresentation { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); my $x = "hi"; $rest->addRepresentation(" world", \$x); is($x, "hi world", "Adding representation w/ a string."); my $xx = "hi"; my $y = " world"; $rest->addRepresentation(\$y, \$xx); is ($xx, "hi world", "Adding representation w/ a scalar references."); } # TEST: getHeaders { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $rest->header(-type => "text/xml", -foop => "helloWorld"); like($rest->getHeaders(), qr{[Ff]oop: helloWorld\r\nContent-Type: text/xml; charset=ISO-8859-1\r\n\r\n}, "Sending representation."); } # TEST: BUG: run() produced warnings when sendRepresentation() returned an # undefined value. This test should exploit that and fail if it happens. { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345/cows"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { return }; $rest->resourceHooks('/parts/:num/:var' => $resourceHook); my $output = $rest->run(); my $answer = "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"; is($output, $answer, "Running a REST Application"); } # TEST: preRun() and postRun() { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345/cows"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { return "my resource" }; $rest->resourceHooks('/parts/:num/:var' => $resourceHook); $rest->run(); is($rest->{preRun}, 1, "preRun() method."); is($rest->{postRun}, "my resource", "postRun() method."); } # TEST: setRedirect() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application::Routes->new(); $rest->setRedirect("http://www.google.com"); like($rest->getHeaders(), qr{^Status: 302 (Moved|Found)\r\n[lL]ocation: http://www\.google\.com\r\n\r\n$}, "Redirect header"); } # TEST: getMatchText() { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345/cows"; $ENV{REST_APP_RETURN_ONLY} = 1; $rest->{TEST_TEXT} = "radio/is/friendly"; # See TestClass my $resourceHook = sub { my ($app, $h) = @_; return "$h->{bar} $h->{foo}"; }; $rest->resourceHooks('radio/:foo/:bar' => $resourceHook); my $output = $rest->run(); is($output, "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nfriendly is", "Using alternate matching text instead of PATH_INFO."); } # TEST: checkMatch() { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345.xml"; $ENV{REST_APP_RETURN_ONLY} = 1; $rest->{TEST_TEXT} = "Quis/hic/locus?"; # See TestClass $rest->resourceHooks( q(Quis/hic/locus?) => sub { return "I match" }, q(Quis) => sub { undef } ); my $output = $rest->run(); is($output, "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nI match", "Using custom matching logic, checkMatch()."); } # TEST: extraHandlerArgs() { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { shift; shift; join(" ", @_) }; $rest->{TEST_TEXT} = "foo"; $rest->resourceHooks(q(foo) => $resourceHook); $rest->extraHandlerArgs(qw(hello jello world foo bar)); my $output = $rest->run(); is($output, "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nhello jello world foo bar", "Setting arguments for the handler."); } # TEST: preHandler() { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { shift; shift; join(" ", @_) }; $rest->{TEST_PRE} = 1; $rest->resourceHooks(q(foo) => $resourceHook); $rest->extraHandlerArgs(qw(hello jello world foo bar)); my $output = $rest->run(); is($rest->{preHandler}, "hello:jello:world:foo:bar"); } # TEST: postHandler() { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{PATH_INFO} = "foo"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { shift; shift; return join(" ", @_) ; }; $rest->{TEST_POST} = 1; $rest->resourceHooks(foo => $resourceHook); $rest->extraHandlerArgs(qw(hello jello world foo bar)); my $output = $rest->run(); is($rest->{postHandler}, "hello jello world foo barhello:jello:world:foo:bar"); } # TEST: callHandler() { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{PATH_INFO} = "foo"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { }; $rest->{TEST_CALL} = 1; my $output = $rest->callHandler($resourceHook, {}, "a", "b", "c"); is($output, "CODEa:b:c", "The handle caller w/o error."); } # TEST: callHandler() { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{PATH_INFO} = "foo"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { die "TEST ERROR" }; $rest->{TEST_CALL} = 1; $rest->{TEST_CALL_ERROR} = 1; eval { $rest->callHandler($resourceHook); }; like($@, qr/TEST ERROR/, "The handle caller with error."); } # TEST: Test that the order of the routes is preserved. { my $obj = REST::Application::Routes->new(); $obj->resourceHooks(map {$_ => $_*$_} (1 .. 1000)); is_deeply([keys %{$obj->resourceHooks}], [(1 .. 1000)], 'resourceHooks contains ordered keys.'); is_deeply([values %{$obj->resourceHooks}], [map {$_*$_} (1 .. 1000)], 'resourceHooks contains ordered values.'); } # TEST: run, simple usage. { restoreENV(); CGI->initialize_globals(); my $rest = RoutesTestClass->new(); $ENV{PATH_INFO} = "foo"; $ENV{REST_APP_RETURN_ONLY} = 1; my $obj = REST::Application::Routes->new(); $obj->resourceHooks('/foo/:var/bar', => sub {shift; shift;}); is_deeply(${$obj->loadResource("/foo/42/bar")}, {var => 42}, "Testing parsing action"); is($obj->getLastMatchPath(), "/foo/42/bar", "Testing getLastMatchPath"); is($obj->getLastMatchPattern(), "/foo/:var/bar", "Testing getLastMatchPattern"); } # TEST: run, more advanced usage. { my $obj = REST::Application::Routes->new(); $obj->resourceHooks( '/data/tags/:tag', => sub {shift; shift}, '/data/tags', => sub {shift; shift;}, '/data/pages/:page/sections/:section', => sub {shift; shift;}, '/data/workspaces/:ws/pages/:page', => sub {shift; shift;}, '/data/workspaces/:ws', => sub {shift; shift;}, ); is_deeply(${$obj->loadResource("/data/tags")}, {}, '/data/tags matches'); is_deeply(${$obj->loadResource("/data/tags/foo")}, {tag => 'foo'}, '/data/tags/foo matches'); is_deeply(${$obj->loadResource("/data/pages/cows/sections/udder")}, {page => 'cows', section => 'udder'}, '/data/pages/cows/sections/udder/love/cakes matches'); is(${$obj->loadResource("/data/pages/cows/sections/udder/love/cakes")}, undef, '/data/pages/cows/sections/udder/love/cakes no match'); is_deeply(${$obj->loadResource("/data/workspaces/cows")}, {ws => 'cows'}, '/data/workspaces/cows matches'); } # TEST: Empty variable fix, from Chris Dent. { my $obj = REST::Application::Routes->new(); $obj->resourceHooks( '/data/tags/:tag', => sub {shift; shift}, '/data/tags', => sub {shift; "cow"}, ); is_deeply(${$obj->loadResource("/data/tags")}, "cow", '/data/tags matches'); is_deeply(${$obj->loadResource("/data/tags/")}, "cow", '/data/tags matches'); is_deeply(${$obj->loadResource("/data/tags/foo")}, {tag => 'foo'}, '/data/tags/foo matches'); } # TEST: make sure /foo does not match in the middle of something else { my $obj = REST::Application::Routes->new(); $obj->resourceHooks( '/foo', => sub { shift; "cow" }, ); is( ${$obj->loadResource("/fooCOW")}, undef, '/fowCOW is not matched' ); } REST-Application-0.992/t/TestClass.pm0000644000175000017500000000351610455267512017201 0ustar matthewmatthewpackage TestClass; use strict; use warnings; use lib 't/'; use base 'REST::Application'; sub foo { "foo" } sub barMethod { shift; return join(":", @_) } sub GET { "xAbC" } sub DELETE { "xAbC" } sub PUT { "xAbC" } sub POST { "xAbC" } sub getRepresentation { "qWeRtY" } sub preRun { shift->{preRun} = 1 } sub postRun { my ($self, $outputRef) = @_; $self->{postRun} = $$outputRef } sub getMatchText { my $self = shift; if ($self->{TEST_TEXT}) { return $self->{TEST_TEXT}; } return $self->getPathInfo(); } sub checkMatch { my $self = shift; my ($a, $b) = @_; if ($self->{TEST_MATCH}) { return ($a eq $b); } return $self->SUPER::checkMatch($a, $b); } sub preHandler { my $self = shift; my $args = shift; return if not $self->{TEST_PRE}; shift @$args; # drop the ref to the REST::Application object $self->{preHandler} = join(":", @$args); } sub postHandler { my ($self, $outputRef, $args) = @_; return if not $self->{TEST_POST}; shift @$args; # drop the ref to the REST::Application object $self->{postHandler} = $$outputRef . join(":", @$args); } sub callHandler { my $self = shift; if (not $self->{TEST_CALL}) { return $self->SUPER::callHandler(@_); } elsif ($self->{TEST_CALL_ERROR}) { my $handler = shift; $handler->(); } my ($handler, @extraArgs) = @_; return ref($handler) . join(":", @extraArgs); } sub makeHandlerFromClass { my $self = shift; return $self->SUPER::makeHandlerFromClass(@_) unless $self->{TEST_MHFC}; my ($class, $method) = @_; return sub { "$class $method" }; } sub makeHandlerFromRef { my $self = shift; return $self->SUPER::makeHandlerFromRef(@_) unless $self->{TEST_MHFR}; my ($obj, $method) = @_; return sub { "SMOKE " . ref($obj). " $method" }; } 1; REST-Application-0.992/t/01-basic.t0000644000175000017500000005161310567665540016432 0ustar matthewmatthew#!/usr/bin/perl use strict; use warnings; use Test::More tests => 80; use Data::Dumper; use lib 't/'; # Some special helpers to restove the environment my %ORIG_ENV = %ENV; sub restoreENV { %ENV = %ORIG_ENV } # TEST: use BEGIN { use_ok('REST::Application'); use_ok('Tie::IxHash'); use_ok('UNIVERSAL'); use_ok('CGI'); use_ok('Carp'); $SIG{'__WARN__'} = sub { &Carp::croak }; } # TEST: new() { my $rest = REST::Application->new(); is(ref($rest), 'REST::Application', "Object instantiation."); } # TEST: query() { my $rest = REST::Application->new(); my $query = $rest->defaultQueryObject(); is(ref($query), 'CGI', "Retrieving default query object."); } # TEST: query() { my $rest = REST::Application->new(); my $query = $rest->query(); is(ref($query), 'CGI', "Retrieving query object."); } # TEST: query($value) { my $rest = REST::Application->new(); my $query = $rest->query("x/a/b"); is($query, 'x/a/b', "Setting and retrieving a query object."); } # TEST: query(undef) { my $rest = REST::Application->new(); my $query = $rest->query(undef); is($query, undef, "Setting and retrieving a query object w/ undef."); } # TEST: defaultQueryObject($value) { my $rest = REST::Application->new(); my $query = $rest->defaultQueryObject("xxx"); is($query, 'xxx', "Setting and retrieving default query object."); } # TEST: defaultQueryObject(undef) { my $rest = REST::Application->new(); my $query = $rest->defaultQueryObject(undef); is($query, undef, "Setting and retrieving default query object w/ undef value."); } # TEST: resourceHooks() { my $rest = REST::Application->new(); my $resources = $rest->resourceHooks(); is_deeply($resources, {}, "Getting resource hooks when none are set."); } # TEST: resourceHooks() { my $rest = REST::Application->new(); my $sub = sub {}; my $regex = qr/foobar/; my $resources = $rest->resourceHooks($regex => $sub); is(ref($resources), 'HASH', "Resource hook using a code ref (data type check)"); is($resources->{$regex}, $sub, "Resource hook using a code ref (value check)"); } # TEST: resourceHooks() { my $rest = REST::Application->new(); my %uniq; my @keys = map { my $x = int(rand(100000)); qr/$x/ } (1 .. 10000); @uniq{@keys} = 1; @keys = keys(%uniq); # Make sure we have no duplicate keys my @k2 = @keys; my $resources = $rest->resourceHooks(map { $_ => "x" } @keys); my $keys = [ keys %$resources ]; is_deeply(\@k2, \@keys, "Resource hook regexes have their order preserved."); } # TEST: resourceHooks() { my $rest = REST::Application->new(); my $resources = $rest->resourceHooks({ foo => 1 }); is_deeply($resources, {foo => 1}, "Resource hook set from a hash ref"); } # TEST: resourceHooks() { my $rest = REST::Application->new(); $rest->resourceHooks(foo => 1); my $resources = $rest->resourceHooks(); is_deeply($resources, {foo => 1}, "Resource hook set and retrieved in 2 steps."); } # TEST: getPathInfo() { require_ok('CGI'); restoreENV(); CGI->initialize_globals(); $ENV{PATH_INFO} = "/blah/bar"; my $rest = REST::Application->new(); is_deeply($rest->getPathInfo(), "/blah/bar", "Retrieving path info."); } # TEST: getRequestMethod() { restoreENV(); CGI->initialize_globals(); $ENV{REQUEST_METHOD} = "PuT"; my $rest = REST::Application->new(); is_deeply($rest->getRequestMethod(), "PUT", "Retrieving request method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{PATH_INFO} = "/parts/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => sub { ref($_[0]) . $_[1].$_[2] }); is(${$rest->loadResource()}, "REST::Application12345xml", "Loading resource - code reference"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{PATH_INFO} = "/parts/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => undef); is(${$rest->loadResource()}, undef, "Loading resource - default hook via undef"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{PATH_INFO} = "/NOEXIST/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => sub {1}); is(${$rest->loadResource()}, undef, "Loading resource - default hook via non-match"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); use_ok('TestClass'); my $rest = TestClass->new(); $rest->resourceHooks(qr{parts} => "barMethod"); my $resource = $rest->loadResource("parts", "blah", "bar", "baz"); is($$resource, 'blah:bar:baz', "Loading resource - \"methodName\" hook"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{PATH_INFO} = "/parts/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => [$rest, "getPathInfo"]); my $resource = $rest->loadResource(); is($$resource, '/parts/12345.xml', "Loading resource - [\$object w/ \"methodName\"] hook"); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => { get => sub { "x" }, puT => [$rest, "getPathInfo"], POST => "getPathInfo", 'deLEte' => undef, }); my $resource = $rest->loadResource(); is($$resource, 'x', "Loading resource for GET HTTP method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "PuT"; $ENV{PATH_INFO} = "/parts/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => { get => sub { "x" }, puT => [$rest, "getPathInfo"], POST => "getPathInfo", 'deLEte' => undef, }); my $resource = $rest->loadResource(); is($$resource, '/parts/12345.xml', "Loading resource for PUT HTTP method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "PoSt"; $ENV{PATH_INFO} = "/parts/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => { get => sub { "x" }, puT => [$rest, "getPathInfo"], POST => "getPathInfo", 'deLEte' => undef, }); my $resource = $rest->loadResource(); is($$resource, '/parts/12345.xml', "Loading resource for POST HTTP method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "delete"; $ENV{PATH_INFO} = "/parts/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => { get => sub { "x" }, puT => [$rest, "getPathInfo"], POST => "getPathInfo", 'deLEte' => undef, }); my $resource = $rest->loadResource(); is($$resource, undef, "Loading resource for DELETE HTTP method."); } # TEST: loadResource() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "delete"; $ENV{PATH_INFO} = "/parts/12345.xml"; my $obj = TestClass->new(); $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => $obj); my $resource = $rest->loadResource(); is($$resource, "xAbC", "Loading resource - with \$object->DELETE() hook"); } # TEST: headerType { my $rest = REST::Application->new(); is($rest->headerType(), 'header', "Retrieving default header type"); $rest->headerType("redIRect"); is($rest->headerType(), 'redirect', "Setting header type to \"redirect\"."); $rest->headerType("nOne"); is($rest->headerType(), 'none', "Setting header type to \"none\"."); eval { $rest->headerType("blahblahlbha") }; ok($@, "Checking error for invalid header type"); } # TEST: header { my $rest = REST::Application->new(); my %hash = $rest->header(); is_deeply(\%hash, {}, "Retrieving default header values."); $rest->header(-type => 'text/html', -foobar => 5); %hash = $rest->header(); is_deeply(\%hash, {-type => 'text/html', -foobar => 5}, "Retrieving custom header values."); } # TEST: resetHeader() { my $rest = REST::Application->new(); my %hash1 = $rest->header(-type => 'text/html', -foobar => 5); my %hash2 = $rest->resetHeader(); my %hash3 = $rest->header(); is_deeply(\%hash1, \%hash2, "Resetting header, verifying return value."); is_deeply(\%hash3, {}, "Resetting header, verifying reset."); } # TEST: run() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345.xml"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { my ($app, $part) = @_; $app->header(-type => 'text/plain'); return "hubcap - $part - Honda"; }; $rest->resourceHooks(qr{^/parts/(\d+)\.\w+} => $resourceHook); my $output = $rest->run(); my $answer = "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nhubcap - 12345 - Honda"; is($output, $answer, "Running a REST Application"); } # TEST: run() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345.xml"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { "this is a test" }; $rest->resourceHooks(qr{^/parts/(\d+)\.\w+} => $resourceHook); my $answer = "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nthis is a test"; is($rest->run(), $answer, "Running a REST Application which has a resource being its own repr."); } # TEST: addRepresentation { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); my $x = "hi"; $rest->addRepresentation(" world", \$x); is($x, "hi world", "Adding representation w/ a string."); my $xx = "hi"; my $y = " world"; $rest->addRepresentation(\$y, \$xx); is ($xx, "hi world", "Adding representation w/ a scalar references."); } # TEST: getHeaders { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $rest->header(-type => "text/xml", -foop => "helloWorld"); like($rest->getHeaders(), qr{[Ff]oop: helloWorld\r\nContent-Type: text/xml; charset=ISO-8859-1\r\n\r\n}, "Sending representation."); } # TEST: BUG: run() produced warnings when sendRepresentation() returned an # undefined value. This test should exploit that and fail if it happens. { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345.xml"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { return }; $rest->resourceHooks(qr{^/parts/(\d+)\.\w+} => $resourceHook); my $output = $rest->run(); my $answer = "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"; is($output, $answer, "Running a REST Application"); } # TEST: preRun() and postRun() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345.xml"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { return "my resource" }; $rest->resourceHooks(qr{^/parts/(\d+)\.\w+} => $resourceHook); $rest->run(); is($rest->{preRun}, 1, "preRun() method."); is($rest->{postRun}, "my resource", "postRun() method."); } # TEST: setRedirect() { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $rest->setRedirect("http://www.google.com"); like($rest->getHeaders(), qr{^Status: 302 (Moved|Found)\r\n[lL]ocation: http://www\.google\.com\r\n\r\n$}, "Redirect header"); } # TEST: getMatchText() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345.xml"; $ENV{REST_APP_RETURN_ONLY} = 1; $rest->{TEST_TEXT} = "radio is friendly"; # See TestClass my $resourceHook = sub { my ($app, $is, $friendly) = @_; return "$friendly $is"; }; $rest->resourceHooks(qr{^radio\s+(\w+)\s+(\w+)$} => $resourceHook); my $output = $rest->run(); is($output, "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nfriendly is", "Using alternate matching text instead of PATH_INFO."); } # TEST: checkMatch() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{REQUEST_METHOD} = "GET"; $ENV{PATH_INFO} = "/parts/12345.xml"; $ENV{REST_APP_RETURN_ONLY} = 1; $rest->{TEST_TEXT} = "Quis hic locus?"; # See TestClass $rest->{TEST_MATCH} = 1; # See TestClass my $resourceHook = sub { return "I match" }; $rest->resourceHooks(q(Quis) => sub { undef }, q(Quis hic locus?) => $resourceHook); my $output = $rest->run(); is($output, "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nI match", "Using custom matching logic, checkMatch()."); } # TEST: extraHandlerArgs() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { shift; join(" ", @_) }; $rest->{TEST_TEXT} = "foo"; $rest->{TEST_MATCH} = 1; $rest->resourceHooks(q(foo) => $resourceHook); $rest->extraHandlerArgs(qw(hello jello world foo bar)); my $output = $rest->run(); is($output, "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nhello jello world foo bar", "Setting arguments for the handler."); } # TEST: extraHandlerArgs() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { shift; join(" ", @_) }; $rest->{TEST_TEXT} = "foo"; $rest->{TEST_MATCH} = 1; $rest->resourceHooks(q(foo) => $resourceHook); $rest->extraHandlerArgs([qw(hello jello world foo bar)]); my $output = $rest->run(); is($output, "Content-Type: text/html; charset=ISO-8859-1\r\n\r\nhello jello world foo bar", "Setting arguments for the handler w/ a reference."); } # TEST: preHandler() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { shift; join(" ", @_) }; $rest->{TEST_PRE} = 1; $rest->resourceHooks(q(foo) => $resourceHook); $rest->extraHandlerArgs(qw(hello jello world foo bar)); my $output = $rest->run(); is($rest->{preHandler}, "hello:jello:world:foo:bar", "Testing pre handler"); } # TEST: postHandler() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{PATH_INFO} = "foo"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { shift; return join(" ", @_) }; $rest->{TEST_POST} = 1; $rest->resourceHooks(qr(foo) => $resourceHook); $rest->extraHandlerArgs(qw(hello jello world foo bar)); my $output = $rest->run(); is($rest->{postHandler}, "hello jello world foo barhello:jello:world:foo:bar", "testing post handler"); } # TEST: callHandler() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{PATH_INFO} = "foo"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { }; $rest->{TEST_CALL} = 1; my $output = $rest->callHandler($resourceHook, "a", "b", "c"); is($output, "CODEa:b:c", "The handle caller w/o error."); } # TEST: callHandler() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{PATH_INFO} = "foo"; $ENV{REST_APP_RETURN_ONLY} = 1; my $resourceHook = sub { die "TEST ERROR" }; $rest->{TEST_CALL} = 1; $rest->{TEST_CALL_ERROR} = 1; eval { $rest->callHandler($resourceHook); }; like($@, qr/TEST ERROR/, "The handle caller with error."); } # TEST: '*' handler { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "PUT"; $ENV{PATH_INFO} = "/parts/12345.xml"; $rest->resourceHooks(qr{^/parts/(\d+)\.(\w+)} => { GET => sub { die }, POST => sub { die }, DELETE => sub { die }, '*' => sub { ref($_[0]) . $_[1].$_[2] } }); is(${$rest->loadResource()}, "REST::Application12345xml", "Loading resource - code reference"); } # TEST: simpleContentNegotiation { restoreENV(); CGI->initialize_globals(); my $rest = REST::Application->new(); $ENV{REQUEST_METHOD} = "PUT"; $ENV{PATH_INFO} = "/parts"; $ENV{HTTP_ACCEPT} = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; # Firefox default Accept header. my @types = qw(text/xml application/xml text/html text/json */*); my $hash = { '*/*' => sub { '*/*' }, 'text/json' => sub { '*/*' }, 'text/html' => sub { 'text/html' }, 'text/xml' => sub { 'text/xml' }, 'application/xml' => sub { 'application/xml' }, }; for my $type (@types, "") { $rest->resourceHooks(qr{/parts} => {PUT => $hash}); my $wanted_type = $type ? $type : '*/*'; $wanted_type = '*/*' if $type eq 'text/json'; my $msg = $type ? $type : "empty string"; is(${$rest->loadResource()}, $wanted_type, "con-neg on $msg"); delete $hash->{$type} unless $type eq '*/*'; } } # TEST: makeHandlerFromClass() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{REST_APP_RETURN_ONLY} = 1; $rest->{TEST_MHFC} = 1; $rest->resourceHooks(q(foo) => ["CowsLoveMe", "BecauseIhugThem"]); my $output = $rest->loadResource("foo"); is($$output, "CowsLoveMe BecauseIhugThem", "Testing makeHandlerFromClass"); } # TEST: makeHandlerFromRef() { restoreENV(); CGI->initialize_globals(); my $rest = TestClass->new(); $ENV{REST_APP_RETURN_ONLY} = 1; $rest->{TEST_MHFR} = 1; $rest->resourceHooks(qr/.*foo/ => [{}, "MAN"]); my $output = $rest->loadResource('foo'); is($$output, "SMOKE HASH MAN", "Testing makeHandlerFromRef"); is($rest->getLastMatchPattern(), qr/.*foo/, "Testing getLastMatchPattern"); is($rest->getLastMatchPath(), "foo", "Testing getLastMatchPath"); } # TEST: fake the http method { restoreENV(); CGI->initialize_globals(); $ENV{REQUEST_METHOD} = "POST"; $ENV{QUERY_STRING} = "http_method=PUT"; my $rest = REST::Application->new(); is_deeply( $rest->getRealRequestMethod(), "POST", "Test Real Method" ); is_deeply( $rest->getRequestMethod(), "PUT", "Tunnel PUT over POST via query param." ); } # TEST: fake the http method, again { restoreENV(); CGI->initialize_globals(); $ENV{REQUEST_METHOD} = "POST"; $ENV{QUERY_STRING} = "http_method=GET"; my $rest = REST::Application->new(); is_deeply( $rest->getRealRequestMethod(), "POST", "Test Real Method" ); is_deeply( $rest->getRequestMethod(), "GET", "Tunnel GET over POST via query param." ); } # TEST: fake the HTTP method { restoreENV(); CGI->initialize_globals(); $ENV{REQUEST_METHOD} = "POST"; $ENV{HTTP_X_HTTP_METHOD} = "DELETE"; my $rest = REST::Application->new(); is_deeply( $rest->getRealRequestMethod(), "POST", "Test Real Method" ); is_deeply( $rest->getRequestMethod(), "DELETE", "Tunnel DELETE over POST via header." ); } # TEST: fake the HTTP method { restoreENV(); CGI->initialize_globals(); $ENV{REQUEST_METHOD} = "GET"; $ENV{HTTP_X_HTTP_METHOD} = "POST"; my $rest = REST::Application->new(); is_deeply( $rest->getRealRequestMethod(), "GET", "Test Real Method" ); is_deeply( $rest->getRequestMethod(), "GET", "Tunnel POST over GET does not work" ); } # TEST: fake the HTTP method { restoreENV(); CGI->initialize_globals(); $ENV{REQUEST_METHOD} = "GET"; $ENV{HTTP_X_HTTP_METHOD} = "HEAD"; my $rest = REST::Application->new(); is_deeply( $rest->getRealRequestMethod(), "GET", "Test Real Method" ); is_deeply( $rest->getRequestMethod(), "HEAD", "Tunnel HEAD over GET does work" ); } # TEST: fake the HTTP method { restoreENV(); CGI->initialize_globals(); $ENV{REQUEST_METHOD} = "POST"; my $cgi = CGI->new; $cgi->param( "http_method", "PUT" ); my $rest = REST::Application->new(); $rest->query($cgi); is_deeply( $rest->getRealRequestMethod(), "POST", "Test Real Method" ); is_deeply( $rest->getRequestMethod(), "PUT", "Tunnel PUT over POST content" ); } REST-Application-0.992/t/RoutesTestClass.pm0000644000175000017500000000305310454344007020371 0ustar matthewmatthewpackage RoutesTestClass; use strict; use warnings; use lib 't/'; use base 'REST::Application::Routes'; sub foo { "foo" } sub barMethod { shift; shift; return join(":", @_) } sub GET { "xAbC" } sub PUT { "xAbC" } sub POST { "xAbC" } sub DELETE { "xAbC" } sub getRepresentation { "qWeRtY" } sub preRun { shift->{preRun} = 1 } sub postRun { my ($self, $outputRef) = @_; $self->{postRun} = $$outputRef } sub getMatchText { my $self = shift; if ($self->{TEST_TEXT}) { return $self->{TEST_TEXT}; } return $self->getPathInfo(); } sub checkMatch { my $self = shift; my ($a, $b) = @_; if ($self->{TEST_MATCH}) { return ($a eq $b); } return $self->SUPER::checkMatch($a, $b); } sub preHandler { my $self = shift; my $args = shift; return if not $self->{TEST_PRE}; shift @$args; # drop the ref to the REST::Application object shift @$args; # drop the variable args $self->{preHandler} = join(":", @$args); } sub postHandler { my ($self, $outputRef, $args) = @_; return if not $self->{TEST_POST}; shift @$args; # drop the ref to the REST::Application object shift @$args; # drop the variable args $self->{postHandler} = $$outputRef . join(":", @$args); } sub callHandler { my $self = shift; if (not $self->{TEST_CALL}) { return $self->SUPER::callHandler(@_); } elsif ($self->{TEST_CALL_ERROR}) { my $handler = shift; $handler->(); } my ($handler, $v, @extraArgs) = @_; return ref($handler) . join(":", @extraArgs); } 1; REST-Application-0.992/lib/0000755000175000017500000000000010656657711015243 5ustar matthewmatthewREST-Application-0.992/lib/REST/0000755000175000017500000000000010656657711016020 5ustar matthewmatthewREST-Application-0.992/lib/REST/Application/0000755000175000017500000000000010656657711020263 5ustar matthewmatthewREST-Application-0.992/lib/REST/Application/Routes.pm0000644000175000017500000001022410656657331022077 0ustar matthewmatthewpackage REST::Application::Routes; use strict; use warnings; use base 'REST::Application'; our $VERSION = $REST::Application::VERSION; sub loadResource { my ($self, $path, @extraArgs) = @_; $path ||= $self->getMatchText(); my $handler = sub { $self->defaultResourceHandler(@_) }; my %vars; # Loop through the keys of the hash returned by resourceHooks(). Each of # the keys is a URI template, see if the current path info matches that # template. Save the parent matches for passing into the handler. for my $template (keys %{ $self->resourceHooks() }) { my $regex = join "\\/", map {/^:/ ? '([^\/]+)' : quotemeta $_} split m{/}, $template; $regex = "^(?:$regex)\\/?\$"; if ($self->checkMatch($path, $regex)) { $self->{__last_match_pattern} = $template; %vars = $self->getTemplateVars($template); $handler = $self->_getHandlerFromHook($template); last; } } return $self->callHandler($handler, \%vars, @extraArgs); } sub getHandlerArgs { my ($self, @extraArgs) = @_; my @args = ($self, @extraArgs, $self->extraHandlerArgs()); # Don't make $self the first argument if the handler is a method on $self, # because in that case it'd be redundant. Also see _getHandlerFromHook(). shift @args if $self->{__handlerIsOurMethod}; return @args; } sub _get_template_vars { my $self = shift; return $self->getTemplateVars(@_); } sub getTemplateVars { my ($self, $route) = @_; my @matches = $self->_getLastRegexMatches(); my @vars = map {s/^://; $_} grep /^:/, split m{/}, $route; return map { $vars[$_] => $matches[$_] } (0 .. scalar(@matches)-1); } sub getLastMatchTemplate { my $self = shift; return $self->getLastMatchPattern(); } 1; __END__ =head1 NAME REST::Application::Routes - An implementation of Ruby on Rails type routes. =head1 SYNOPSIS package MyApp; use base 'REST::Application::Routes'; my $obj = REST::Application::Routes->new(); $obj->loadResource( '/data/workspaces/:ws/pages/:page', => \&do_thing, # ... other routes here ... ); sub do_thing { my %vars = @_; print $vars{ws} . " " . $vars{page} . "\n"; } # Now, in some other place. Maybe a CGI file or an Apache handler, do: use MyApp; MyApp->new->run("/data/workspaces/cows/pages/good"); # prints "cows good" =head1 DESCRIPTION Ruby on Rails has this concept of routes. Routes are URI path info templates which are tied to specific code (i.e. Controllers and Actions in Rails). That is routes consist of key value pairs, called the route map, where the key is the path info template and the value is a code reference. A template is of the form: C where variables are always prefaced with a colon. When a given path is passed to C the code reference which the template maps to will be passed a hash where the keys are the variable names (sans colon) and the values are what was specified in place of the variables. The route map is ordered, so the most specific matching template is used and so you should order your templates from least generic to most generic. See L for details. The only difference between this module and that one is that this one uses URI templates as keys in the C rather than regexes. =head1 METHODS These are methods which L has but its superclass does not. =head2 getTemplateVars() Returns a hash whose keys are the C<:symbols> from the URI template and whose values are what where matched to be there. It is assumed that this method is called either from within or after C is called. Otherwise you're likely to get an empty hash back. =head2 getLastMatchTemplate() This is an alias for C, since this class is about templates rather than regexes. =head1 AUTHORS Matthew O'Connor Ematthew@canonical.orgE =head1 LICENSE This program is free software. It is subject to the same license as Perl itself. =head1 SEE ALSO L, L =cut REST-Application-0.992/lib/REST/Application.pm0000644000175000017500000006741710656657441020640 0ustar matthewmatthew# vi:ai:sm:et:sw=4:ts=4:tw=0 # # REST::Application - A framework for building RESTful web-applications. # # Copyright 2005 Matthew O'Connor package REST::Application; use strict; use warnings; use Carp; use Tie::IxHash; use UNIVERSAL; use CGI; our $VERSION = '0.992'; #################### # Class Methods #################### sub new { my ($proto, %args) = @_; my $class = ref($proto) ? ref($proto) : $proto; my $self = bless({ __defaultQuery => CGI->new() }, $class); $self->setup(%args); return $self; } ################################## # Instance Methods - Object State ################################## sub query { my $self = shift; # Set default value if this method hasn't been called yet. if (not exists $self->{__query}) { $self->{__query} = $self->defaultQueryObject(); } # Set the field if we got any arguments. $self->{__query} = shift if @_; return $self->{__query}; } sub defaultQueryObject { my $self = shift; # Set the field if we got any arguments. if (@_) { $self->{__defaultQuery} = shift; } return $self->{__defaultQuery}; } sub resourceHooks { my $self = shift; # Set default value if this method hasn't been called yet. if (not exists $self->{__resourceHooks}) { my %hooks; tie(%hooks, "Tie::IxHash"); # For keeping hash key order preserved. $self->{__resourceHooks} = \%hooks; } # If we got arguments then they should be an even sized list, otherwise a # hash reference. if (@_ and @_%2 == 0) { %{ $self->{__resourceHooks} } = @_; } elsif (@_ == 1) { my $value = shift; if (ref($value) ne 'HASH') { croak "Expected hash reference or even-sized list."; } %{ $self->{__resourceHooks} } = %$value; } return $self->{__resourceHooks}; } sub extraHandlerArgs { my $self = shift; # Set default value for method if it hasn't been called yet. if (not exists $self->{__extraHandlerArgs}) { $self->{__extraHandlerArgs} = []; } # If got arguments then process them. We expect either a single array # reference or a list if (@_) { if (@_ == 1 and ref($_[0]) eq 'ARRAY') { $self->{__extraHandlerArgs} = shift; } else { $self->{__extraHandlerArgs} = [ @_ ]; } } return @{ $self->{__extraHandlerArgs} }; } ################################## # Instance Methods - Proxies ################################## sub getPathInfo { my $self = shift; return $self->query->path_info(); } sub getRealRequestMethod { my $self = shift; return uc( $self->query->request_method() || "" ); } sub getRequestMethod { my $self = shift; my $real_method = $self->getRealRequestMethod(); my $tunnel_method = uc( $self->query->http('X-HTTP-Method') || $self->query->url_param('http_method') || $self->query->param('http_method') || $real_method ); # POST can tunnel any method. return $tunnel_method if $real_method eq 'POST'; # GET can only tunnel GET/HEAD if ( $real_method eq 'GET' and $tunnel_method =~ /^(GET|HEAD)$/ ) { return $tunnel_method; } return $real_method; } ############################# # Instance Methods - Public ############################# sub loadResource { my ($self, $path, @extraArgs) = @_; $path ||= $self->getMatchText(); my $handler = sub { $self->defaultResourceHandler(@_) }; my $matches = []; # Loop through the keys of the hash returned by resourceHooks(). Each of # the keys is a regular expression, see if the current path info matches # that regular expression. Save the parent matches for passing into the # handler. for my $pathRegex (keys %{ $self->resourceHooks() }) { if ($self->checkMatch($path, $pathRegex)) { $handler = $self->_getHandlerFromHook($pathRegex); last; } } return $self->callHandler($handler, @extraArgs); } sub getHandlerArgs { my ($self, @extraArgs) = @_; my @args = ($self, $self->_getLastRegexMatches(), $self->extraHandlerArgs(), @extraArgs); # Don't make $self the first argument if the handler is a method on $self, # because in that case it'd be redundant. Also see _getHandlerFromHook(). shift @args if $self->{__handlerIsOurMethod}; return @args; } sub callHandler { my ($self, $handler, @extraArgs) = @_; my @args = $self->getHandlerArgs(@extraArgs); # Call the handler, carp if something goes wrong. my $result; eval { $self->preHandler(\@args); # no-op by default. $result = $handler->(@args); $self->postHandler(\$result, \@args); # no-op by default. }; carp "Handler failed: $@\n" if $@; # Convert the result to a scalar result if it isn't already. my $ref = (ref($result) eq 'scalar') ? $result : \$result; return $ref; } sub getMatchText { my $self = shift; return $self->getPathInfo(); } sub checkMatch { my ($self, $a, $b) = @_; my $match = 0; if ($a =~ /$b/) { $self->_setLastRegexMatches(); $self->{__last_match_path} = $a; $self->{__last_match_pattern} = $b; $match = 1; } return $match; } sub getLastMatchPath { my $self = shift; return $self->{__last_match_path}; } sub getLastMatchPattern { my $self = shift; return $self->{__last_match_pattern}; } sub run { my $self = shift; # Get resource. $self->preRun(); # A no-op by default. my $repr = $self->loadResource(@_); $self->postRun($repr); # A no-op by default. # Get the headers and then add the representation to to the output stream. my $output = $self->getHeaders(); $self->addRepresentation($repr, \$output); # Send the output unless we're told not to by the environment. print $output if not $ENV{REST_APP_RETURN_ONLY}; return $output; } sub getHeaders { my $self = shift; my $type = $self->headerType() || ""; my $header = ""; if ($type eq 'header') { $header = $self->query->header($self->header()); } elsif ($type eq 'redirect') { $header = $self->query->redirect($self->header()); } elsif ($type ne 'none') { croak "Unexpected header type: \"$type\"."; } return $header; } sub addRepresentation { my ($self, $repr, $outputRef) = @_; # Make sure $outputRef is a scalar ref and the scalar it references is # defined. return if ref($outputRef) ne 'SCALAR'; return if not defined $$outputRef; # If we're given a scalar reference then dereference it first, otherwise # just treat what we got as though it's a string. if (ref($repr) eq 'SCALAR') { $$outputRef .= $$repr if defined $$repr; } else { $$outputRef .= $repr if defined $repr; } } sub headerType { my $self = shift; # Set the default value if this method has not been called yet. if (not exists $self->{__headerType}) { $self->{__headerType} = "header"; } # If an argument was passed in then use them to set the header type. if (@_) { my $type = lc(shift || ""); if ($type =~ /^(redirect|header|none)$/) { $self->{__headerType} = $type; } else { croak "Invalid header type specified: \"$type\""; } } return $self->{__headerType}; } sub header { my $self = shift; # Set the default value if this method has not been called yet. if (not exists $self->{__header}) { $self->{__header} = {}; } # If arguments were passed in then use them to set the header type. # Arguments can be passed in as a hash-ref or as an even sized list. if (@_) { if (@_%2 == 0) { # even-sized list, must be hash %{ $self->{__header} } = @_; } elsif (ref($_[0]) eq 'HASH') { # First item must be a hash reference $self->{__header} = shift; } else { croak "Expected even-sized list or hash reference."; } } return %{$self->{__header}}; } sub resetHeader { my $self = shift; my %old = $self->header(); $self->headerType('header'); $self->{__header} = {}; return %old; } sub setRedirect { my ($self, $url) = @_; $self->headerType('redirect'); $self->header(-url => $url || ""); } ############################################## # Instance Methods - Intended for Overloading ############################################## sub setup { return } sub preRun { return } sub postRun{ return } sub preHandler { return } sub postHandler { return } sub defaultResourceHandler { return } ############################# # Instance Methods - Private ############################# # CodeRef _getHandlerFromHook(String $pathRegex) # # This method retrieves a code reference which will yield the resource of the # given $pathRegex, where $pathRegex is a key into the resource hooks hash (it # isn't used as a regex in this method, just a hash key). sub _getHandlerFromHook { my ($self, $pathRegex) = @_; my $ref = $self->resourceHooks()->{$pathRegex}; my $refType = ref($ref); my $handler = sub { $self->defaultResourceHandler(@_) }; my $method = $self->getRequestMethod() || "getResource"; # If we get a hash, then use the request method to narrow down the choice. # We do this first because we allow the same range of handler types for a # particular HTTP method that we do for a more generic handler. if ($refType eq 'HASH') { %$ref = map { uc($_) => $ref->{$_} } keys %$ref; # Uppercase the keys my $http_method = $self->getRequestMethod(); if (exists $ref->{$http_method}) { $ref = $ref->{$http_method} } elsif (exists $ref->{'*'}) { $ref = $ref->{'*'}; } else { return $handler; # Just bail now with the default handler. } $refType = ref($ref); } # If we still have a hash then assume we're doing Content Negotation if ($refType eq 'HASH') { my $type = $self->bestContentType(keys %$ref); $ref = $ref->{$type}; $refType = ref($ref); } # Based on the the reference's type construct the handler. if ($refType eq "CODE") { # A code reference $handler = $ref; } elsif ($refType eq "ARRAY") { # Array reference which holds a $object and "method name" pair. my ($thing, $smethod) = @$ref; $smethod ||= $method; if (ref $thing) { $handler = $self->makeHandlerFromRef($thing, $smethod); } else { $handler = $self->makeHandlerFromClass($thing, $smethod); } } elsif ($refType) { # Object with GET, PUT, etc, or getResource method. $handler = $self->makeHandlerFromRef($ref, $method); } elsif ($ref) { # A bare string signifying a method name $handler = sub { $self->$ref(@_) }; $self->{__handlerIsOurMethod} = 1; # See callHandler(). } return $handler; } sub makeHandlerFromRef { my ($self, $ref, $method) = @_; return sub { $ref->$method(@_) }; } sub makeHandlerFromClass { my ($self, $class, $method) = @_; return sub { $class->$method(@_) }; } sub bestContentType { my ($self, @types) = @_; return ($self->simpleContentNegotiation(@types))[0] || '*/*'; } # We don't support the full range of content negtiation because a) it's # overkill and b) it makes it hard to specify the hooks cleanly, also see (a). sub simpleContentNegotiation { my ($self, @types) = @_; my @accept_types = $self->getContentPrefs(); my $score = sub { $self->scoreType(shift, @accept_types) }; return sort {$score->($b) <=> $score->($a)} @types; } # The pattern matching stuff was stolen from CGI.pm sub scoreType { my ($self, $type, @accept_types) = @_; my $score = scalar(@accept_types); for my $accept_type (@accept_types) { return $score if $type eq $accept_type; my $pat; ($pat = $accept_type) =~ s/([^\w*])/\\$1/g; # escape meta characters $pat =~ s/\*/.*/g; # turn it into a pattern return $score if $type =~ /$pat/; $score--; } return 0; } # Basic idea stolen from CGI.pm. Its semantics made it hard to pull out the # information I wanted without a lot of trickery, so I improved upon the # original. Same with libwww's HTTP::Negotiate algorithim, it's also hard to # make go with what we want. sub getContentPrefs { my $self = shift; my $default_weight = 1; my @prefs; # Parse the Accept header, and save type name, score, and position. my @accept_types = split /,/, $self->getAcceptHeader(); my $order = 0; for my $accept_type (@accept_types) { my ($weight) = ($accept_type =~ /q=(\d\.\d+|\d+)/); my ($name) = ($accept_type =~ m#(\S+/[^;]+)#); next unless $name; push @prefs, { name => $name, order => $order++}; if (defined $weight) { $prefs[-1]->{score} = $weight; } else { $prefs[-1]->{score} = $default_weight; $default_weight -= 0.001; } } # Sort the types by score, subscore by order, and pull out just the name @prefs = map {$_->{name}} sort {$b->{score} <=> $a->{score} || $a->{order} <=> $b->{order}} @prefs; return @prefs, '*/*'; # Allows allow for */* } sub getAcceptHeader { my $self = shift; return $self->query->http('accept') || ""; } # List _getLastRegexMatches(void) # # Returns a list of all the paren matches in the last regular expression who's # matches were saved with _saveLastRegexMatches(). sub _getLastRegexMatches { my $self = shift; my $matches = $self->{__lastRegexMatches} || []; return @$matches; } # ArrayRef _setLastRegexMatches(void) # # Grabs the values of $1, $2, etc. as set by the last regular expression to run # in the current dyanamic scope. This of course exploits that $1, $2, etc. and # @+ are dynamically scoped. A reference to an array is returned where the # array values are $1, $2, $3, etc. _getLastRegexMatches() can also be used to # retrieve the values saved by this method. sub _setLastRegexMatches { my $self = shift; no strict 'refs'; # For the $$_ symbolic reference below. my @matches = map $$_, (1 .. scalar(@+)-1); # See "perlvar" for @+. $self->{__lastRegexMatches} = \@matches; } 1; __END__ =pod =head1 NAME L - A framework for building RESTful web-applications. =head1 SYNOPSIS # MyRESTApp L instance / mod_perl handler package MyRESTApp; use Apache; use Apache::Constants qw(:common); sub handler { __PACKAGE__->new(request => $r)->run(); return OK; } sub getMatchText { return Apache->uri } sub setup { my $self = shift; $self->resourceHooks( qr{/rest/parts/(\d+)} => 'get_part', # ... other handlers here ... ); } sub get_part { my ($self, $part_num) = @_; # Business logic to retrieve part num } # Apache conf perl-script .cgi PerlHandler MyRESTApp =head1 DESCRIPTION This module acts as a base class for applications which implement a RESTful interface. When an HTTP request is received some dispatching logic in L> is invoked, calling different handlers based on what the kind of HTTP request it was (i.e. GET, PUT, etc) and what resource it was trying to access. This module won't ensure that your API is RESTful but hopefully it will aid in developing a REST API. =head1 OVERVIEW The following list describes the basic way this module is intended to be used. It does not capture everything the module can do. =over 8 =item 1. Subclass Subclass L, i.e. C. =item 2. Overload C Overload the C method and set up some resource hooks with the C method. Hooks are mappings of the form: REGEX => handler where handler is either a method name, a code reference, an object which supports a method with the same name as the HTTP method (or C if no such method), or a reference to an array of the form: C<[$objectRef, "methodName"]> (C<$objectRef> can be a class name instead). The regular expressions are applied, by default, to the path info of the HTTP request. Anything captured by parens in the regex will be passed into the handler as arguments. For example: qr{/parts/(\d+)$} => "getPartByNumber", The above hook will call a method named C on the current object (i.e. $self, an instance of L) if the path info of the requested URI matches the above regular expression. The first argument to the method will be the part number, since that's the first element captured in the regular expression. =item 3. Write code. Write the code for the handler specified above. So here we'd define the C method. =item 4. Create a handler/loader. Create an Apache handler, for example: use MyRESTApp; sub handler { my $r = shift; my $app = MyRESTApp->new(request => $r); $app->run(); } or a small CGI script with the following code: #!/usr/bin/perl use MyRESTApp; MyRESTApp->new()->run(); In the second case, for a CGI script, you'll probably need to do something special to get Apache to load up your script unless you give it a .cgi extension. It would be unRESTful to allow your script to have a .cgi extension, so you should go the extra mile and configure Apache to run your script without it. For example, it'd be bad to have your users go to: http://www.foo.tld/parts.cgi/12345.html =item 5. Call the C method. When the C method is called the path info is extracted from the HTTP request. The regexes specified in step 2 are processed, in order, and if one matches then the handler is called. If the regex had paren. matching then the matched elements are passed into the handler. A handler is also passed a copy of the L object instance (except for the case when the handler is a method on the L object, in that case it'd be redundant). So, when writing a subroutine handler you'd do: sub rest_handler { my ($rest, @capturedArgs) = @_; ... } =item 6. Return a representation of the resource. The handler is processed and should return a string or a scalar reference to a string. Optionally the handler should set any header information via the C method on instance object pased in. =head1 CALLING ORDER The L base class provides a good number of methods, each of which can be overloaded. By default you only need to overload the C method but you may wish to overload others. To help with this the following outline is the calling order of the various methods in the base class. You can find detailed descriptions of each method in the METHODS section of this document. If a method is followed by the string NOOP then that means it does nothing by default and it exists only to be overloaded. new() setup() - NOOP run() preRun() - NOOP loadResource() getMatchText() getPathInfo() query() defaultQueryObject() defaultResourceHandler() - NOOP resourceHooks() checkMatch() _setLastRegexMatches() _getHandlerFromHook() resourceHooks() defaultResourceHandler() - NOOP getRequestMethod() query() defaultQueryObject() bestContentType() simpleContentNegotiation getContentPrefs getAcceptHeader scoreType() callHandler() getHandlerArgs _getLastRegexMatches() extraHandlerArgs() preHandler() - NOOP ... your handler called here ... postHandler() - NOOP postRun() - NOOP getHeaders() headerType() query() defaultQueryObject() header() addRepresentation() The only methods not called as part of the new() or run() methods are the helper methods C and C, both of which call the C and C methods. For example, if you wanted to have your code branch on the entire URI of the HTTP request rather than just the path info you'd merely overload C to return the URI rather than the path info. =back =head1 METHODS =head2 new(%args) This method creates a new L object and returns it. The arguments passed in via C<%args>, if any, are passed untouched to the C method. =head2 query([$newCGI]) This accessor/mutator retrieves the current CGI query object or sets it if one is passed in. =head2 defaultQueryObject([$newCGI]) This method retrieves/sets the default query object. This method is called if C is called for the first time and no query object has been set yet. =head2 resourceHooks([%hash]) This method is used to set the resource hooks. A L hook is a regex to handler mapping. The hooks are passed in as a hash (or a reference to one) and the keys are treated as regular expressions while the values are treated as handlers should B match the regex that maps to that handler. Handlers can be code references, methods on the current object, methods on other objects, or class methods. Also, handlers can be differ based on what the B was (e.g. GET, PUT, POST, DELETE, etc). The handler's types are as follows: =over 8 =item string The handler is considered to be a method on the current L instance. =item code ref The code ref is considered to be the handler. =item object ref The object is considered to have a method the same name as the HTTP method. That is, if the object is being called because of GET then C is called, if it is called because of a C then C is called. C method will be used if C returns false. =item array ref The array is expected to be two elements long, the first element is a class name or object instance. The 2nd element is a method name on that class/instance. IF the 2nd element is ommitted then the method name is assumed to be the same as the B, e.g. C, C, whatever. =item hash ref The current B is used as a key to the hash, the value should be one the four above handler types. In this way you can specify different handlers for each of the request types. The request method can also be specified as '*', in which case that is used if a more specific match is not found. It is possible for the value of the handler to be another hash ref, rather than one of the four above types. In this case it is assumed content-negotion is wanted. The keys of this second hash are MIME types and the values are one of the four above types. For example: $self->resourceHooks( qr{/parts/(\d+)} => { GET => { 'text/json' => 'get_json', 'text/xml', => 'get_xml', 'text/xml' => 'get_html', '*/*' => 'get_html', }, '*' => sub { die "Bad Method!" }, } ); =back The return value of the handler is expected to be a string, which L will then send to the browser with the C method. If no argument is supplied to C then the current set of hooks is returned. The returned hash referces is a tied IxHash, so the keys are kept sorted. =head2 loadResource([$path]) This method will take the value of B, iterate through the path regex's set in C and if it finds a match call the associated handler and return the handler's value, which should be a scalar. If $path is passed in then that is used instead of B. =head2 run() This method calls C with no arguments and then takes that output and sends it to the remote client. Headers are sent with C and the representation is sent with C. If the environment variable B is set then output isn't sent to the client. The return value of this method is the text output it sends (or would've sent). =head2 sendHeaders() This method returns the headers as a string. =head2 sendRepresentation($representation) This method just returns C<$representation>. It is provided soely for overloading purposes. =head2 headerType([$type]) This accessor/mutator controls the type of header to be returned. This method returns one of "header, redirect, or none." If C<$type> is passed in then that is used to set the header type. =head2 header([%args]) This accessor/mutator controls the header values sent. If called without arguments then it simply returns the current header values as a hash, where the keys are the header fields and the values are the header field values. If this method is called multiple times then the values of %args are additive. So calling C<$self->header(-type => "text/html")> and C<$self->header(-foo => "bar")> results in both the content-type header being set and the "foo" header being set. =head2 resetHeader() This header causes the current header values to be reset. The previous values are returned. =head2 defaultResourceHandler() This method is called by C if no regex in C matches the current B. It returns undef by default, it exists for overloading. =head2 bestContentType(@types) Given a list of MIME types this function returns the best matching type considering the Accept header of the current request (as returned by C. =head2 simpleContentNegotiation(@types) Given a list of MIME types this function returns the same list sorted from best match to least considering the Accept header as returned by C. =head2 getContentPrefs() Returns the list of MIME types in the Accept header form most preferred to least preferred. Quality weights are taken into account. =head2 getAcceptHeader() Returns the value of the Accept header as a single string. =head2 scoreType($type, @accept_types) Returns an integer, only good for sorting, for where C<$type> fits among the C<@accept_types>. This method takes wildcards into account. So C matches C. The integer returned is the position in C<@accept_types> of the matching MIME type. It assumped @accept_types is already sorted from best to worst. =head2 getLastMatchPath() Returns the last path passed to C which successfully matched against. Unless you're overloading things in funny ways the value returned will be the path that caused the current handler to be invoked. =head2 getLastMatchPattern() Similar to C except this is the pattern that was applied to the path. =head2 getRequestMethod() This method tries to be smart and allow tunneling of the other HTTP methods over GET or PUT. You can tunnel three ways with the higher up taking precedence: 1) Pass an X-HTTP-Method header 2) Pass the 'http_method' query parameter 3) Pass a parameter via POST Only POST and GET, being the most common, can be used to tunnel. In an attempt to prevent people from being bad, GET can only be used to tunnel GET or HEAD. POST can be used to tunnel anything. =head1 AUTHORS Matthew O'Connor Ematthew@canonical.org =head1 LICENSE This program is free software. It is subject to the same license as Perl itself. =head1 SEE ALSO L, L, L, L =cut REST-Application-0.992/META.yml0000644000175000017500000000060710656657711015751 0ustar matthewmatthew# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: REST-Application version: 0.992 version_from: lib/REST/Application.pm installdirs: site requires: CGI: 0 Tie::IxHash: 0 distribution_type: module generated_by: ExtUtils::MakeMaker version 6.17 REST-Application-0.992/MANIFEST0000644000175000017500000000034510553576300015616 0ustar matthewmatthewbin/server lib/REST/Application/Routes.pm lib/REST/Application.pm Makefile.PL MANIFEST t/01-basic.t t/02-routes.t t/RoutesTestClass.pm t/TestClass.pm META.yml Module meta-data (added by MakeMaker) REST-Application-0.992/bin/0000755000175000017500000000000010656657711015245 5ustar matthewmatthewREST-Application-0.992/bin/server0000755000175000017500000007276110553577052016511 0ustar matthewmatthew#!/usr/bin/env perl use strict; use warnings; use Getopt::Long; ######################## # Command Line Options ######################## my %opts = ( port => 8080, host => 'localhost', bg => 0 ); GetOptions( "port|p=s" => \$opts{port}, "host|H=s" => \$opts{host}, "background|b" => \$opts{bg}, "help|h" => \$opts{help}, "include|I=s" => \$opts{include}, ) || ( $opts{help} = 1 ); die_with_usage() if $opts{help}; $opts{ra_class} = shift || die_with_usage("No class given"); ######################### # Setup the Environment ######################### extend_INC( $opts{include} ); load_class( $opts{ra_class} ); ###################### # Run the Web Server ###################### my $server = WebServer->new( $opts{port} ); $server->host( $opts{host} ); my $run_method = $opts{bg} ? "background" : "run"; $server->{ra_class} = $opts{ra_class}; $server->$run_method(); exit; ########### # Helpers ########### sub extend_INC { my $include = shift || ""; my @dirs = split /,/, $include; unshift @INC, reverse(@dirs); } sub load_class { my $klass = shift; eval "require $klass;"; die "Unable to load $klass: $@\n" if $@; } sub die_with_usage { my $msg = shift || ""; warn "$msg\n\n" if $msg; die <{ra_class}; my $ra = $klass->new(); $ra->run(); } sub print_banner { my $self = shift; print "Server is serving class " . $self->{ra_class} . " at http://" . $self->host . ":" . $self->port . "/\n\n"; } 1; package HTTP::Server::Simple; use strict; use warnings; use FileHandle; use Socket; use Carp; use URI::Escape; no warnings 'redefine'; use vars qw($VERSION $bad_request_doc); $VERSION = '0.26'; =head1 NAME HTTP::Server::Simple - Lightweight HTTP server =head1 SYNOPSIS use warnings; use strict; use HTTP::Server::Simple; my $server = HTTP::Server::Simple->new(); $server->run(); However, normally you will sub-class the HTTP::Server::Simple::CGI module (see L); package Your::Web::Server; use base qw(HTTP::Server::Simple::CGI); sub handle_request { my ($self, $cgi) = @_; #... do something, print output to default # selected filehandle... } 1; =head1 DESCRIPTION This is a simple standalone HTTP server. By default, it doesn't thread or fork. It does, however, act as a simple frontend which can be used to build a standalone web-based application or turn a CGI into one. (It's possible to use Net::Server to get threading, forking, preforking and so on. Autrijus Tang wrote the functionality and owes docs for that ;) By default, the server traps a few signals: =over =item HUP When you C the server, it does its best to rexec itself. Please note that in order to provide restart-on-SIGHUP, HTTP::Server::Simple sets a SIGHUP handler during initialisation. If your request handling code forks you need to make sure you reset this or unexpected things will happen if somebody sends a HUP to all running processes spawned by your app (e.g. by "kill -HUP