| 1 | package Plack::Middleware::AMF; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | our $VERSION = '0.02'; | |||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use parent "Plack::Middleware"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use Data::AMF::Remoting; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use Plack::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use Plack::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use Plack::Util::Accessor qw/path headers_handler message_handler/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 3 | ▶ | use UNIVERSAL::require; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | sub prepare_app { | ||||||||
|
Subroutine
prepare_app: called CC=5 SCAR=18.1
Pod
documented
| |||||||||||
| 17 | 1 | my $self = shift; | |||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | unless (defined $self->headers_handler) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 1 | $self->headers_handler(\&_default_headers_handler); | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | unless (defined $self->message_handler) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 1 | $self->message_handler(\&_default_message_handler); | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | if (ref $self->headers_handler ne 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | die 'headers_handler should be a code reference'; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | if (ref $self->message_handler ne 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | die 'message_handler should be a code reference'; | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 2 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 37 | 2 | my $self = shift; | |||||||||
| 38 | 2 | my $env = shift; | |||||||||
| 39 | |||||||||||
| 40 | 2 | my $res = $self->_handle_amf($env); | |||||||||
| 41 | |||||||||||
| 42 | 2 | ▶ | return $res if $res; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | |||||||||||
| 44 | 1 | return $self->app->($env); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | sub _handle_amf { | ||||||||
|
Subroutine
_handle_amf: called CC=6 SCAR=18.1
| |||||||||||
| 48 | 2 | my ($self, $env) = @_; | |||||||||
| 49 | |||||||||||
| 50 | 2 | ▶ | my $path_match = $self->path or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 2 | my $path = $env->{PATH_INFO}; | |||||||||
| 52 | |||||||||||
| 53 | 2 | for ($path) { | |||||||||
| 54 | 2 | ▶ | my $matched = 'CODE' eq ref $path_match ? $path_match->($_) : $_ =~ $path_match; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 2 | ▶ | return unless $matched; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 1 | my $req = Plack::Request->new($env); | |||||||||
| 59 | 1 | my $res = $req->new_response(200); | |||||||||
| 60 | |||||||||||
| 61 | 1 | my $remoting = Data::AMF::Remoting->new( | |||||||||
| 62 | source => $req->raw_body, | ||||||||||
| 63 | headers_did_process => $self->headers_handler, | ||||||||||
| 64 | message_did_process => $self->message_handler, | ||||||||||
| 65 | ); | ||||||||||
| 66 | 1 | ▶ | $remoting->run if $remoting->{source}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | |||||||||||
| 68 | 1 | $res->content_type('application/x-amf'); | |||||||||
| 69 | 1 | $res->body($remoting->data); | |||||||||
| 70 | |||||||||||
| 71 | 1 | return $res->finalize; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | sub _default_headers_handler {} | ||||||||
|
Subroutine
_default_headers_handler: not called CC=1 SCAR=0.0
| |||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sub _default_message_handler { | ||||||||
|
Subroutine
_default_message_handler: not called CC=3 SCAR=24.8
| |||||||||||
| 77 | 0 | my $message = shift; | |||||||||
| 78 | |||||||||||
| 79 | 0 | my ($controller_name, $action_name) = split '\.', $message->target_uri; | |||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | $controller_name->require or die $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | |||||||||||
| 83 | 0 | my $controller = $controller_name->new; | |||||||||
| 84 | 0 | my $action = $controller->can($action_name); | |||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | if (defined $action) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | return $controller->$action(@{ $message->value }); | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 3 | 1; | |||||||||
| 92 | __END__ | ||||||||||