AnyEvent-HTTPD-ExtDirect
view release on metacpan or search on metacpan
lib/AnyEvent/HTTPD/ExtDirect.pm view on Meta::CPAN
### 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)
if ( !$is_form ) {
my $postdata = $req->content;
return $postdata ne '' ? $postdata
: undef
;
};
# If any files are attached, extUpload field will be set to 'true'
my $has_uploads = $req->param('extUpload') eq 'true';
# Outgoing hash
my %keyword;
# Pluck all parameters from the Request object
for my $param ( $req->params ) {
my @values = $req->param($param);
$keyword{ $param } = @values == 0 ? undef
: @values == 1 ? $values[0]
: [ @values ]
;
};
# Find all file uploads
for ( $has_uploads ? ($has_uploads) : () ) {
# The list of fields that contain file uploads
my @upload_fields = $req->upload_fields;
last unless @upload_fields;
my @uploaded_files;
for my $field_name ( @upload_fields ) {
my $uploads = $req->raw_param($field_name);
# We need files as a formatted list
my @field_uploads = map { $self->_format_upload($_) } @$uploads;
push @uploaded_files, @field_uploads;
# Now remove the field that contained files
delete @keyword{ $field_name };
}
$keyword{ '_uploads' } = \@uploaded_files if @uploaded_files;
};
# Metadata is JSON encoded; decode_metadata lives by side effects!
if ( exists $keyword{metadata} ) {
RPC::ExtDirect::Util::decode_metadata($self, \%keyword);
}
# Remove extType because it's meaningless later on
delete $keyword{ extType };
# Fix TID so that it comes as number (JavaScript is picky)
$keyword{ extTID } += 0 if exists $keyword{ extTID };
return \%keyword;
}
### PRIVATE INSTANCE METHOD ###
#
# Take the file content and metadata and format it in a way
# that RPC::ExtDirect handlers expect
#
sub _format_upload {
my ($self, $upload) = @_;
( run in 2.049 seconds using v1.01-cache-2.11-cpan-e1769b4cff6 )