AnyEvent-HTTPD-ExtDirect
view release on metacpan or search on metacpan
lib/AnyEvent/HTTPD/ExtDirect.pm view on Meta::CPAN
);
# No need for eval here, Router won't throw exceptions
my $result = $router->route($router_input, $env);
# Router result is Plack-compatible arrayref; there's not much
# difference in what AnyEvent::HTTPD expects so we just convert it
# in place
$req->respond([
200,
'OK',
+{ @{ $result->[1] } },
$result->[2]->[0],
]);
$self->stop_request;
}
### PUBLIC INSTANCE METHOD ###
#
# Polls Event handlers for events, returning serialized stream
#
sub handle_events {
my ($self, $req) = @_;
# Only GET and POST methods are supported for polling
my $method = $req->method;
if ( $method ne 'GET' && $method ne 'POST' ) {
$req->respond( $self->_error_response );
$self->stop_request;
return;
}
my $config = $self->config;
my $api = $self->api;
my $env = bless $req, __PACKAGE__.'::Env';
my $provider_class = $config->eventprovider_class_anyevent;
eval "require $provider_class";
my $provider = $provider_class->new(
config => $config,
api => $api,
);
# Polling for Events is safe from exceptions
my $http_body = $provider->poll($env);
my $content_length
= do { no warnings 'void'; use bytes; length $http_body };
$req->respond([
200,
'OK',
{
'Content-Type' => 'application/json; charset=utf-8',
'Content-Length' => $content_length,
},
$http_body,
]);
$self->stop_request;
}
### PUBLIC INSTANCE METHOD ###
#
# Register the callbacks for Ext.Direct handlers.
# This effectively "primes" the server but does not make it
# enter a blocking wait.
#
sub set_callbacks {
my ($self, %arg) = @_;
my $config = $self->config;
my $api_path = $arg{api_path} || $config->api_path;
my $router_path = $arg{router_path} || $config->router_path;
my $poll_path = $arg{poll_path} || $config->poll_path;
$self->reg_cb(
$api_path => $self->can('handle_api'),
$router_path => $self->can('handle_router'),
$poll_path => $self->can('handle_events'),
);
}
### PUBLIC INSTANCE METHODS ###
#
# Read-write accessors.
#
RPC::ExtDirect::Util::Accessor::mk_accessors(
simple => [qw/ api config /],
);
############## PRIVATE METHODS BELOW ##############
### PRIVATE INSTANCE METHOD ###
#
# Deals with intricacies of POST-fu and returns something suitable to
# feed to Router (string or hashref, really). Or undef if something
# goes too wrong to recover.
#
# This code was mostly copied from the Plack gateway and adapted
# for AnyEvent::HTTPD.
#
sub _extract_post_data {
my ($self, $req) = @_;
# The smartest way to tell if a form was submitted that *I* know of
# is to look for 'extAction' and 'extMethod' keywords in form params.
my $is_form = $req->param('extAction') && $req->param('extMethod');
# If form is not involved, it's easy: just return raw POST (or undef)
( run in 2.185 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )