| 1 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Plack::App::DAIA; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | #ABSTRACT: DAIA Server as Plack application | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 9 | our $VERSION = '0.55'; #VERSION | |||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use v5.10.1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use parent 'Plack::Component'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use LWP::Simple qw(get); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use DAIA; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use Plack::Util::Accessor qw(xslt root warnings errors code idformat initialized safe); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use Plack::Middleware::Static; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 9 | ▶ | use File::ShareDir qw(dist_dir); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | our %FORMATS = DAIA->formats; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 27 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 27 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 27 | ▶ | return if $self->initialized; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 16 | $self->init; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 16 | ▶ | $self->errors(0) unless defined $self->errors; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 16 | ▶ | $self->warnings(1) if $self->errors or not defined $self->warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->errors or not defined $self->warnings
MC/DC100%2/2
$self->errors or not defined $self->warnings
$self->errors
not defined $self->warnings
Truth table100%3/3
A or not(B)
A= $self->errorsB= not defined $self->warnings
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 16 | ▶ | $self->idformat( qr{^.*$} ) unless defined $self->idformat; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 16 | ▶ | $self->safe(1) unless defined $self->safe; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 16 | ▶ | $self->xslt('daia.xsl') if ($self->xslt // 1) eq 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->xslt // 1
MC/DC100%1/1
$self->xslt // 1
$self->xslt
Truth table100%2/2
A
A= $self->xslt
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 16 | ▶ | $self->{client} = Plack::Middleware::Static->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->root || dist_dir('Plack-App-DAIA')
MC/DC0%0/2
$self->root || dist_dir('Plack-App-DAIA')
$self->root
dist_dir('Plack-App-DAIA')
Truth table33%1/3
A || B
A= $self->rootB= dist_dir('Plack-App-DAIA')
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | path => qr{daia\.(xsl|css|xsd)$|xmlverbatim\.xsl$|icons/[a-z0-9_-]+\.png$}, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | root => ($self->root || dist_dir('Plack-App-DAIA')) | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ) if $self->xslt; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 16 | $self->initialized(1); | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 9 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | # initialization hook | ||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 29 | ▶ | sub call_client { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call_client: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 29 | my ($self, $req) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 29 | ▶ | if ( $self->{client} and $req->path ne '/' and !keys %{$req->parameters} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$self->{"client"} and $req->path ne "/" and not keys %{$req->parameters;}
$self->{"client"} and $req->path ne "/"
MC/DC66%2/3
$self->{"client"} and $req->path ne "/" and not keys %{$req->parameters;}
$self->{"client"}
$req->path ne "/"
not keys %{$req->parameters;}
Truth table75%3/4
A and B and not(C)
A= $self->{"client"}B= $req->path ne "/"C= not keys %{$req->parameters;}
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | return $self->{client}->_handle_static( $req->env ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 26 | return; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 27 | ▶ | sub call { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 27 | my ($self, $env) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 27 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 27 | ▶ | my $id = $req->param('id') // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$req->param("id") // ""
MC/DC100%1/1
$req->param("id") // ""
$req->param("id")
Truth table100%2/2
A
A= $req->param("id")
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 27 | ▶ | my $format = lc($req->param('format') // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$req->param("format") // ""
MC/DC100%1/1
$req->param("format") // ""
$req->param("format")
Truth table100%2/2
A
A= $req->param("format")
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # serve parts of the XSLT client | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 27 | my $res = $self->call_client($req); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 27 | ▶ | return $res if $res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # validate identifier | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 26 | my ($invalid_id, $error, %parts) = ('',undef); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 26 | ▶ | if ( $id ne '' and ref $self->idformat ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$id ne "" and ref $self->idformat
MC/DC50%1/2
$id ne "" and ref $self->idformat
$id ne ""
ref $self->idformat
Truth table66%2/3
A and B
A= $id ne ""B= ref $self->idformat
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 20 | ▶ | if ( ref $self->idformat eq 'Regexp' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 20 | ▶ | if ( $id =~ $self->idformat ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 17 | %parts = %+; # named capturing groups | |||||||||||||||||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | $invalid_id = $id; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | $id = ""; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 26 | ▶ | if ( $self->warnings ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 25 | ▶ | if ( $invalid_id ne '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | $error = 'unknown identifier format'; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } elsif ( $id eq '' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | $error = 'please provide a document identifier'; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # retrieve and construct response | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 26 | my ($status, $daia) = (200, undef); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 26 | ▶ | if ( $error and $self->errors ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$error and $self->errors
MC/DC100%2/2
$error and $self->errors
$error
$self->errors
Truth table100%3/3
A and B
A= $errorB= $self->errors
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | $daia = DAIA::Response->new; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 25 | ▶ | if ($self->safe) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 24 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 24 | $daia = $self->retrieve( $id, %parts ); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | } catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | chomp($error = "request method died: $_"); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | $status = 500; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 24 | } else { | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | $daia = $self->retrieve( $id, %parts ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 24 | ▶ | if (!$daia or !blessed $daia or !$daia->isa('DAIA::Response')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$daia and builtin::blessed($daia) and $daia->isa("DAIA::Response")
$daia and builtin::blessed($daia)
MC/DC66%2/3
$daia and builtin::blessed($daia) and $daia->isa("DAIA::Response")
$daia
builtin::blessed($daia)
$daia->isa("DAIA::Response")
Truth table75%3/4
A and B and C
A= $daiaB= builtin::blessed($daia)C= $daia->isa("DAIA::Response")
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 14 | $daia = DAIA::Response->new; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 14 | ▶ | $error = 'request method did not return a DAIA response' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | unless $error; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 14 | $status = 500; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 25 | ▶ | if ( $error and $self->warnings ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$error and $self->warnings
MC/DC50%1/2
$error and $self->warnings
$error
$self->warnings
Truth table66%2/3
A and B
A= $errorB= $self->warnings
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 15 | $daia->addMessage( 'en' => $error, errno => 400 ); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 25 | $self->as_psgi( $status, $daia, $format, $req->param('callback') ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 21 | ▶ | sub retrieve { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 21 | ▶ | return $self->code ? $self->code->(@_) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 25 | ▶ | sub as_psgi { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_psgi: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 25 | my ($self, $status, $daia, $format, $callback) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 25 | my ($content, $type); | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 25 | ▶ | $type = $FORMATS{$format} unless $format eq 'xml'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 25 | ▶ | $content = $daia->serialize($format) if $type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 25 | ▶ | if (!$content) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 21 | $type = "application/xml; charset=utf-8"; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 21 | ▶ | if ( $self->warnings ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 20 | ▶ | if ( not $format ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 8 | $daia->addMessage( 'en' => 'please provide an explicit parameter format=xml', 300 ); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } elsif ( $format ne 'xml' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $daia->addMessage( 'en' => 'unknown or unsupported format', 300 ); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 21 | ▶ | $content = $daia->xml( header => 1, xmlns => 1, ( $self->xslt ? (xslt => $self->xslt) : () ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | ▶ | } elsif ( $type =~ qr{^application/javascript} and ($callback || '') =~ /^[\w\.\[\]]+$/ ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
$type =~ qr"^application/javascript" and ($callback || "") =~ /^[\w\.\[\]]+$/
$callback || ''
MC/DC50%1/2
$type =~ qr"^application/javascript" and ($callback || "") =~ /^[\w\.\[\]]+$/
$type =~ qr"^application/javascript"
$callback
Truth table66%2/3
A and B
A= $type =~ qr"^application/javascript"B= $callback
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | $content = "$callback($content)"; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 25 | return [ $status, [ "Content-Type" => $type ], [ encode('utf8',$content) ] ]; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||||||||||||||||