Apache-ExtDirect

 view release on metacpan or  search on metacpan

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

    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 = ();

    # Now if the form IS involved, it gets a little bit complicated
    PARAM:
    for my $param ( keys %keyword ) {
        # Defang CGI's idiosyncratic way to return multi-valued params
        my @values = $cgi->param( $param );
        $keyword{ $param } = @values == 0 ? undef
                           : @values == 1 ? $values[0]
                           :                [ @values ]
                           ;

        # Try to see if $param is a field with associated file upload
        # Skip the standard ones first, of course
        next PARAM if $STANDARD_KEYWORD{ $param } || !$has_uploads;

        # Look for file uploads in this field
        my @field_uploads = $class->_parse_uploads($cgi, $param);

        # Found some, add them to general stash and kill the field
        if ( @field_uploads ) {
            push @_uploads, @field_uploads;
            delete $keyword{ $param };
        };
    };

    # 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 };

    # Now add files to hash, if any
    $keyword{ '_uploads' } = \@_uploads if @_uploads;

    return \%keyword;
}

### PRIVATE INSTANCE METHOD ###
#
# Parses CGI form input field looking for file uploads
#

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

    # CGI returns "lightweight file handles", or undef
    my @file_handles = $cgi->upload($param);

    # Empty list means no uploads for this field
    return unless grep { defined $_ } @file_handles;

    # Despite what CGI documentation says, the values returned
    # as "file names" are actually some kind of key handles
    my @file_keys = $cgi->param($param);

    # Here file uploads get collected



( run in 0.746 second using v1.01-cache-2.11-cpan-e1769b4cff6 )