Apache-ExtDirect

 view release on metacpan or  search on metacpan

lib/Apache/ExtDirect/Router.pm  view on Meta::CPAN

package Apache::ExtDirect::Router;

use 5.012000;
use strict;
use warnings;
no  warnings 'uninitialized';       ## no critic

use CGI;
use File::Basename qw(basename);
use IO::Handle;

use Apache2::Const -compile => qw(OK SERVER_ERROR DECLINED);

use RPC::ExtDirect ();
use RPC::ExtDirect::Router;

### PACKAGE GLOBAL VARIABLE ###
#
# Debugging; off by default
#

our $DEBUG = 0;

### PUBLIC MOD_PERL HANDLER ###
#
# Handle Ext.Direct routing requests
#

sub handler {
    my ($r) = @_;

    local $RPC::ExtDirect::Router::DEBUG = $DEBUG;

    # If anything but POST method is used, throw an error
    return Apache2::Const::DECLINED
        if $r->method ne 'POST';

    my $cgi = CGI->new($r);

    my $router_input = Apache::ExtDirect::Router->_extract_post_data($cgi);

    # If input is undefined, extraction have failed
    return Apache2::Const::SERVER_ERROR
        unless defined $router_input;

    # Routing requests is safe
    my $result = RPC::ExtDirect::Router->route($router_input, $cgi);

    # Router returns PSGI array with fixed items
    my $content_type   = $result->[1]->[1];
    my $content_length = $result->[1]->[3];
    my $http_body      = $result->[2]->[0];

    $r->content_type($content_type);
    $r->headers_out->{'Content-Length'} = $content_length;

    $r->print($http_body);

    return Apache2::Const::OK;
}

############## PRIVATE METHODS BELOW ##############

### PRIVATE INSTANCE METHOD ###
#
# Extract Ext.Direct request information from POST body
#

my @STANDARD_KEYWORDS
    = qw(action method extAction extMethod extTID extUpload extType);
my %STANDARD_KEYWORD = map { $_ => 1 } @STANDARD_KEYWORDS;

sub _extract_post_data {
    my ($class, $cgi) = @_;

    # The smartest way to tell if a form was submitted that *I* know of
    # is to look for 'extAction' and 'extMethod' keywords in CGI params.
    my %keyword = map { $_ => 1 } $cgi->param();
    my $is_form = exists $keyword{ extAction } &&
                  exists $keyword{ extMethod };

    # If form is not involved, it's easy: just return POSTDATA (or undef)
    if ( !$is_form ) {
        my $postdata = $cgi->param('POSTDATA') || join '', $cgi->keywords;
        return $postdata ne '' ? $postdata
               :                 undef
               ;
    };

    # If any files are attached, extUpload will contain 'true'
    my $has_uploads = $cgi->param('extUpload') eq 'true';

    # Here file uploads data is stored
    my @_uploads = ();



( run in 1.469 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )