| 1 | package Plack::Middleware::ExtDirect; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use parent 'Plack::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | no warnings 'uninitialized'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use RPC::ExtDirect::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use RPC::ExtDirect::Util::Accessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use RPC::ExtDirect::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use RPC::ExtDirect::API; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use RPC::ExtDirect; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # This module is not compatible with RPC::ExtDirect < 3.0 | ||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | croak __PACKAGE__." requires RPC::ExtDirect 3.0+" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | if $RPC::ExtDirect::VERSION lt '3.0'; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | ### PACKAGE GLOBAL VARIABLE ### | ||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||
| 30 | # Version of the module | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | our $VERSION = '3.20'; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | ### PUBLIC INSTANCE METHOD (CONSTRUCTOR) ### | ||||||||||||||||||||||||||||
| 36 | # | ||||||||||||||||||||||||||||
| 37 | # Instantiates a new Plack::Middleware::ExtDirect object | ||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 35 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 35 | my $class = shift; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 35 | ▶ | my %params = @_ == 1 && 'HASH' eq ref($_[0]) ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and "HASH" eq ref $_[0]
MC/DC0%0/2
@_ == 1 and "HASH" eq ref $_[0]
@_ == 1
"HASH" eq ref $_[0]
Truth table33%1/3
A and B
A= @_ == 1B= "HASH" eq ref $_[0]
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 35 | ▶ | my $api = delete $params{api} || RPC::ExtDirect->get_api(); | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $params{'api'} || 'RPC::ExtDirect'->get_api
MC/DC0%0/2
delete $params{'api'} || 'RPC::ExtDirect'->get_api
delete $params{'api'}
'RPC::ExtDirect'->get_api
Truth table33%1/3
A || B
A= delete $params{'api'}B= 'RPC::ExtDirect'->get_api
| |||||||||||||||||||||||||||||
| 46 | 35 | ▶ | my $config = delete $params{config} || $api->config; | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $params{'config'} || $api->config
MC/DC0%0/2
delete $params{'config'} || $api->config
delete $params{'config'}
$api->config
Truth table33%1/3
A || B
A= delete $params{'config'}B= $api->config
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # These two are not method calls, they need to do their stuff *before* | ||||||||||||||||||||||||||||
| 49 | # we have found $self | ||||||||||||||||||||||||||||
| 50 | 35 | _decorate_config($config); | |||||||||||||||||||||||||||
| 51 | 35 | _process_params($api, $config, \%params); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 35 | my $self = $class->SUPER::new(%params); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 35 | $self->config($config); | |||||||||||||||||||||||||||
| 56 | 35 | $self->api($api); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 35 | return $self; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | ### PUBLIC INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||
| 63 | # Dispatch calls to Ext.Direct handlers | ||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 35 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 35 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 35 | my $config = $self->config; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Run the relevant handler. Router calls are the most frequent | ||||||||||||||||||||||||||||
| 72 | # so we test for them first | ||||||||||||||||||||||||||||
| 73 | 35 | for ( $env->{PATH_INFO} ) { | |||||||||||||||||||||||||||
| 74 | 35 | ▶ | return $self->_handle_router($env) if $_ =~ $config->router_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 8 | ▶ | return $self->_handle_events($env) if $_ =~ $config->poll_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 3 | ▶ | return $self->_handle_api($env) if $_ =~ $config->api_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Not our URI, fall through | ||||||||||||||||||||||||||||
| 80 | 0 | return $self->app->($env); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | ### PUBLIC INSTANCE METHODS ### | ||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||
| 85 | # Read-write accessors | ||||||||||||||||||||||||||||
| 86 | # | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | RPC::ExtDirect::Util::Accessor->mk_accessors( | |||||||||||||||||||||||||||
| 89 | simple => [qw/ api config /], | ||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | ############## PRIVATE METHODS BELOW ############## | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | ### PRIVATE PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 95 | # | ||||||||||||||||||||||||||||
| 96 | # Decorate a Config object with __PACKAGE__-specific accessors | ||||||||||||||||||||||||||||
| 97 | # | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 35 | ▶ | sub _decorate_config { | ||||||||||||||||||||||||||
|
Subroutine
_decorate_config: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 100 | 35 | my ($config) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 35 | $config->add_accessors( | |||||||||||||||||||||||||||
| 103 | overwrite => 1, | ||||||||||||||||||||||||||||
| 104 | complex => [{ | ||||||||||||||||||||||||||||
| 105 | accessor => 'router_class_plack', | ||||||||||||||||||||||||||||
| 106 | fallback => 'router_class', | ||||||||||||||||||||||||||||
| 107 | }, { | ||||||||||||||||||||||||||||
| 108 | accessor => 'eventprovider_class_plack', | ||||||||||||||||||||||||||||
| 109 | fallback => 'eventprovider_class', | ||||||||||||||||||||||||||||
| 110 | }], | ||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | ### PRIVATE PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||
| 116 | # Process parameters directly passed to the constructor | ||||||||||||||||||||||||||||
| 117 | # and set the Config/API options accordingly | ||||||||||||||||||||||||||||
| 118 | # | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 35 | ▶ | sub _process_params { | ||||||||||||||||||||||||||
|
Subroutine
_process_params: called CC=8 SCAR=21.5
| |||||||||||||||||||||||||||||
| 121 | 35 | my ($api, $config, $params) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # We used to accept these parameters directly in the constructor; | ||||||||||||||||||||||||||||
| 124 | # this behavior is not recommended now but it doesn't make much sense | ||||||||||||||||||||||||||||
| 125 | # to deprecate it either | ||||||||||||||||||||||||||||
| 126 | 35 | my @compat_params = qw/ | |||||||||||||||||||||||||||
| 127 | api_path router_path poll_path namespace remoting_var polling_var | ||||||||||||||||||||||||||||
| 128 | auto_connect debug no_polling | ||||||||||||||||||||||||||||
| 129 | /; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 35 | for my $var ( @compat_params ) { | |||||||||||||||||||||||||||
| 132 | 315 | ▶ | $config->$var( delete $params->{$var} ) if exists $params->{$var}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | $config->router_class_plack( delete $params->{router} ) | ||||||||||||||||||||||||||||
| 136 | 35 | ▶ | if exists $params->{router}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | $config->eventprovider_class_plack( delete $params->{event_provider} ) | ||||||||||||||||||||||||||||
| 139 | 35 | ▶ | if exists $params->{event_provider}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 35 | for my $type ( $api->HOOK_TYPES ) { | |||||||||||||||||||||||||||
| 142 | 105 | ▶ | my $code = delete $params->{ $type } if exists $params->{ $type }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 105 | ▶ | $api->add_hook( type => $type, code => $code ) if defined $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 149 | # | ||||||||||||||||||||||||||||
| 150 | # Handles Ext.Direct API calls | ||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 3 | ▶ | sub _handle_api { | ||||||||||||||||||||||||||
|
Subroutine
_handle_api: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 154 | 3 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Get the API JavaScript chunk | ||||||||||||||||||||||||||||
| 157 | 3 | my $js = eval { | |||||||||||||||||||||||||||
| 158 | 3 | $self->api->get_remoting_api( config => $self->config ) | |||||||||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # If JS API call failed, return error | ||||||||||||||||||||||||||||
| 162 | 3 | ▶ | return $self->_error_response if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # We need content length, in octets | ||||||||||||||||||||||||||||
| 165 | 4 | ▶ | my $content_length = do { use bytes; my $len = length $js }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | return [ | ||||||||||||||||||||||||||||
| 168 | 3 | 200, | |||||||||||||||||||||||||||
| 169 | [ | ||||||||||||||||||||||||||||
| 170 | 'Content-Type' => 'application/javascript', | ||||||||||||||||||||||||||||
| 171 | 'Content-Length' => $content_length, | ||||||||||||||||||||||||||||
| 172 | ], | ||||||||||||||||||||||||||||
| 173 | [ $js ], | ||||||||||||||||||||||||||||
| 174 | ]; | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 178 | # | ||||||||||||||||||||||||||||
| 179 | # Dispatches Ext.Direct method requests | ||||||||||||||||||||||||||||
| 180 | # | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 27 | ▶ | sub _handle_router { | ||||||||||||||||||||||||||
|
Subroutine
_handle_router: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 183 | 27 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # Throw an error if any method but POST is used | ||||||||||||||||||||||||||||
| 186 | return $self->_error_response | ||||||||||||||||||||||||||||
| 187 | 27 | ▶ | unless $env->{REQUEST_METHOD} eq 'POST'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 27 | my $config = $self->config; | |||||||||||||||||||||||||||
| 190 | 27 | my $api = $self->api; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # Now we need a Request object | ||||||||||||||||||||||||||||
| 193 | 27 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Try to distinguish between raw POST and form call | ||||||||||||||||||||||||||||
| 196 | 27 | my $router_input = $self->_extract_post_data($req); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # When extraction fails, undef is returned by method above | ||||||||||||||||||||||||||||
| 199 | 27 | ▶ | return $self->_error_response unless defined $router_input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # Rebless request as our environment object for compatibility | ||||||||||||||||||||||||||||
| 202 | 27 | bless $req, __PACKAGE__.'::Env'; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 27 | my $router_class = $config->router_class_plack; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 27 | eval "require $router_class"; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 27 | my $router = $router_class->new( | |||||||||||||||||||||||||||
| 209 | config => $config, | ||||||||||||||||||||||||||||
| 210 | api => $api, | ||||||||||||||||||||||||||||
| 211 | ); | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # Routing requests is safe (Router won't croak under torture) | ||||||||||||||||||||||||||||
| 214 | 27 | my $result = $router->route($router_input, $req); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 27 | return $result; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 220 | # | ||||||||||||||||||||||||||||
| 221 | # Polls Event handlers for events, returning serialized stream | ||||||||||||||||||||||||||||
| 222 | # | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 5 | ▶ | sub _handle_events { | ||||||||||||||||||||||||||
|
Subroutine
_handle_events: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 225 | 5 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # Only GET and POST methods are supported for polling | ||||||||||||||||||||||||||||
| 228 | return $self->_error_response | ||||||||||||||||||||||||||||
| 229 | 5 | ▶ | if $env->{REQUEST_METHOD} !~ / \A (GET|POST) \z /xms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 5 | my $req = Plack::Middleware::ExtDirect::Env->new($env); | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 5 | my $config = $self->config; | |||||||||||||||||||||||||||
| 234 | 5 | my $api = $self->api; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 5 | my $provider_class = $config->eventprovider_class_plack; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 5 | eval "require $provider_class"; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 5 | my $provider = $provider_class->new( | |||||||||||||||||||||||||||
| 241 | config => $config, | ||||||||||||||||||||||||||||
| 242 | api => $api, | ||||||||||||||||||||||||||||
| 243 | ); | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # Polling for Events is safe | ||||||||||||||||||||||||||||
| 246 | 5 | my $http_body = $provider->poll($req); | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # We need content length, in octets | ||||||||||||||||||||||||||||
| 249 | my $content_length | ||||||||||||||||||||||||||||
| 250 | 4 | ▶ | = do { no warnings 'void'; use bytes; length $http_body }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | return [ | ||||||||||||||||||||||||||||
| 253 | 5 | 200, | |||||||||||||||||||||||||||
| 254 | [ | ||||||||||||||||||||||||||||
| 255 | 'Content-Type' => 'application/json; charset=utf-8', | ||||||||||||||||||||||||||||
| 256 | 'Content-Length' => $content_length, | ||||||||||||||||||||||||||||
| 257 | ], | ||||||||||||||||||||||||||||
| 258 | [ $http_body ], | ||||||||||||||||||||||||||||
| 259 | ]; | ||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 263 | # | ||||||||||||||||||||||||||||
| 264 | # Deals with intricacies of POST-fu and returns something suitable to | ||||||||||||||||||||||||||||
| 265 | # feed to Router (string or hashref, really). Or undef if something | ||||||||||||||||||||||||||||
| 266 | # goes too wrong to recover. | ||||||||||||||||||||||||||||
| 267 | # | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 27 | ▶ | sub _extract_post_data { | ||||||||||||||||||||||||||
|
Subroutine
_extract_post_data: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||
| 270 | 27 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | # The smartest way to tell if a form was submitted that *I* know of | ||||||||||||||||||||||||||||
| 273 | # is to look for 'extAction' and 'extMethod' keywords in form params. | ||||||||||||||||||||||||||||
| 274 | 27 | ▶ | my $is_form = $req->param('extAction') && $req->param('extMethod'); | ||||||||||||||||||||||||||
|
Condition66%2/3
$req->param('extAction') && $req->param('extMethod')
MC/DC50%1/2
$req->param('extAction') && $req->param('extMethod')
$req->param('extAction')
$req->param('extMethod')
Truth table66%2/3
A && B
A= $req->param('extAction')B= $req->param('extMethod')
| |||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # If form is not involved, it's easy: just return raw POST (or undef) | ||||||||||||||||||||||||||||
| 277 | 27 | ▶ | if ( !$is_form ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 21 | my $postdata = $req->content; | |||||||||||||||||||||||||||
| 279 | 21 | ▶ | return $postdata ne '' ? $postdata | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | : undef | ||||||||||||||||||||||||||||
| 281 | ; | ||||||||||||||||||||||||||||
| 282 | }; | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | # If any files are attached, extUpload field will be set to 'true' | ||||||||||||||||||||||||||||
| 285 | 6 | my $has_uploads = $req->param('extUpload') eq 'true'; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # Outgoing hash | ||||||||||||||||||||||||||||
| 288 | 6 | my %keyword; | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | # Pluck all parameters from Plack::Request | ||||||||||||||||||||||||||||
| 291 | 6 | for my $param ( $req->param ) { | |||||||||||||||||||||||||||
| 292 | 52 | my @values = $req->param($param); | |||||||||||||||||||||||||||
| 293 | 52 | ▶ | $keyword{ $param } = @values == 0 ? undef | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 294 | : @values == 1 ? $values[0] | ||||||||||||||||||||||||||||
| 295 | : [ @values ] | ||||||||||||||||||||||||||||
| 296 | ; | ||||||||||||||||||||||||||||
| 297 | }; | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # Find all file uploads | ||||||||||||||||||||||||||||
| 300 | 6 | ▶ | if ( $has_uploads ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | 3 | my $uploads = $req->uploads; # Hash::MultiValue | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # We need files as plain list (keys %$uploads is by design) | ||||||||||||||||||||||||||||
| 304 | my @field_uploads | ||||||||||||||||||||||||||||
| 305 | 3 | = map { $self->_format_uploads( $uploads->get_all($_) ) } | |||||||||||||||||||||||||||
| 306 | keys %$uploads; | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # Now remove fields that contained files | ||||||||||||||||||||||||||||
| 309 | 3 | delete @keyword{ $uploads->keys }; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 3 | ▶ | $keyword{ '_uploads' } = \@field_uploads if @field_uploads; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | }; | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | # Metadata is JSON encoded; decode_metadata lives by side effects! | ||||||||||||||||||||||||||||
| 315 | 6 | ▶ | if ( exists $keyword{metadata} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 3 | RPC::ExtDirect::Util::decode_metadata($self, \%keyword); | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | # Remove extType because it's meaningless later on | ||||||||||||||||||||||||||||
| 320 | 6 | delete $keyword{ extType }; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # Fix TID so that it comes as a number (JavaScript is picky) | ||||||||||||||||||||||||||||
| 323 | 6 | ▶ | $keyword{ extTID } += 0 if exists $keyword{ extTID }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 6 | return \%keyword; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 329 | # | ||||||||||||||||||||||||||||
| 330 | # Takes info from Plack::Request::Upload and formats it as needed | ||||||||||||||||||||||||||||
| 331 | # | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 2 | ▶ | sub _format_uploads { | ||||||||||||||||||||||||||
|
Subroutine
_format_uploads: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 334 | 2 | my ($self, @uploads) = @_; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | my @result = map { | ||||||||||||||||||||||||||||
| 337 | 2 | { | |||||||||||||||||||||||||||
| 338 | 4 | filename => $_->filename, | |||||||||||||||||||||||||||
| 339 | basename => $_->basename, | ||||||||||||||||||||||||||||
| 340 | type => $_->content_type, | ||||||||||||||||||||||||||||
| 341 | size => $_->size, | ||||||||||||||||||||||||||||
| 342 | path => $_->path, | ||||||||||||||||||||||||||||
| 343 | handle => IO::File->new($_->path, 'r'), | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | @uploads; | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 2 | return @result; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 352 | # | ||||||||||||||||||||||||||||
| 353 | # Returns error response in Plack format | ||||||||||||||||||||||||||||
| 354 | # | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 0 | ▶ | sub _error_response { [ 500, [ 'Content-Type' => 'text/html' ], [] ] } | ||||||||||||||||||||||||||
|
Subroutine
_error_response: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # Small utility class | ||||||||||||||||||||||||||||
| 359 | package | ||||||||||||||||||||||||||||
| 360 | Plack::Middleware::ExtDirect::Env; | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 4 | ▶ | use parent 'Plack::Request'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 2 | ▶ | sub http { | ||||||||||||||||||||||||||
|
Subroutine
http: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 365 | 2 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 2 | my $hdr = $self->headers; | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 2 | ▶ | return $name ? $hdr->header($name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | : $hdr->header_field_names | ||||||||||||||||||||||||||||
| 371 | ; | ||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 2 | ▶ | sub param { | ||||||||||||||||||||||||||
|
Subroutine
param: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 375 | 2 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 2 | ▶ | return $name eq 'POSTDATA' ? $self->content | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 378 | : $name eq '' ? ( $self->SUPER::param(), 'POSTDATA' ) | ||||||||||||||||||||||||||||
| 379 | : $self->SUPER::param($name) | ||||||||||||||||||||||||||||
| 380 | ; | ||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 2 | ▶ | sub cookie { | ||||||||||||||||||||||||||
|
Subroutine
cookie: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 384 | 2 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | return $name ? $self->cookies()->{ $name } | ||||||||||||||||||||||||||||
| 387 | 2 | ▶ | : keys %{ $self->cookies() } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | ; | ||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 4 | 1; | |||||||||||||||||||||||||||