blib/lib/ASP4/PSGI.pm

filter: Statementstmt 34.1% 14 / 41 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 4 / 6 Podpod 0.0% 0 / 1 totaltotal 29.0% 18 / 62 CC 8 SCAR 38.0
CC
8
Cov
27%
__ANON__
72.0
app
2.0
BEGIN
1.0
CRAP 44.8
↑ Summary
1  
2 package ASP4::PSGI;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use ASP4::SimpleCGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.004';
10  
11  
12 sub app
13 0 ▶{
Subroutine app: not called CC=1 SCAR=6.9
Pod undocumented
14 0 ▶ return sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
15 0 my $env = shift;
16 0 my $preq = Plack::Request->new( $env );
17  
18 0 local %ENV = %$env;
19 0 require ASP4::API;
20 0 my $api = ASP4::API->new();
21
22 # Parse cookies:
23 0 ▶ foreach my $cookie ( split /;\s*/, ($ENV{HTTP_COOKIE}||'') )
Condition0%0/2
$ENV{'HTTP_COOKIE'} || ''
l!l
00
MC/DC0%0/1
$ENV{'HTTP_COOKIE'} || ''
$ENV{'HTTP_COOKIE'}
Truth table0%0/2
A
A= $ENV{'HTTP_COOKIE'}
Aresult
11
00
24 {
25 0 my ($k,$v) = map { ASP4::SimpleCGI->unescape($_) } split /\=/, $cookie;
26 0 $api->ua->add_cookie( $k => $v );
27 }# end foreach()
28
29 # Execute the request:
30 0 my $method = lc( $ENV{REQUEST_METHOD} );
31 0 my $res = do {
32 # Is it a GET, POST or Upload?
33 0 ▶ if( $method eq 'get' )
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'get') { } 0 0 0
34 {
35 # GET
36 0 $api->ua->get( $env->{REQUEST_URI} );
37 }
38 else
39 {
40 0 ▶ if( $ENV{CONTENT_TYPE} =~ m{^multipart/form\-data;} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'CONTENT_TYPE'} =~ m[^multipart/form\-data;]) { } 0 0 0
41 {
42 # Upload:
43 0 my @pairs = $preq->parameters->flatten;
44 # Prepare the upload:
45 0 foreach my $up ( keys %{ $preq->uploads } )
46 {
47 0 my $upload = $preq->uploads->{$up};
48 push @pairs, $up => [
49 $upload->{tempname}, $upload->{filename},
50 0 'content-type' => $upload->{'content-type'}
51 ];
52 }# end foreach()
53
54 # Now we can upload:
55 0 $api->ua->upload( $env->{REQUEST_URI}, \@pairs );
56 }
57 else
58 {
59 # POST:
60 0 $api->ua->post( $env->{REQUEST_URI}, [ $preq->parameters->flatten ] );
61 }# end if()
62 }# end if()
63 };
64
65 # Check for a 404 response. If we got one, then see if we've got a /404.asp:
66 0 my ($status) = $res->status_line =~ m{^(\d+)};
67 0 ▶ if( $status eq 404 && -f $api->config->web->www_root . '/404.asp' )
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq 404 and -f $api->config->web->www_root . '/404.asp') 0 0 0
Condition0%0/3
$status eq 404 and -f $api->config->web->www_root . '/404.asp'
!ll&&!rl&&r
000
MC/DC0%0/2
$status eq 404 and -f $api->config->web->www_root . '/404.asp'
$status eq 404 -f $api->config->web->www_root . '/404.asp'
Truth table0%0/3
A and B
A= $status eq 404B= -f $api->config->web->www_root . '/404.asp'
ABresult
0X0
100
111
68 {
69 # Try to do the right thing:
70 0 $res = $api->ua->get( '/404.asp' );
71 }# end if()
72
73 # Return a PSGI-compliant response:
74 return [
75 $status,
76 [
77 0 %{ $res->headers }
78 ],
79 [
80 $res->content
81 ]
82 ];
83 0 };
84 }# end app()
85  
86 1 1;# return true:
87  
88 =pod
89  
90 =head1 NAME
91  
92 ASP4::PSGI - Run your ASP4 web application under PSGI/Plack.
93  
94 =head1 SYNOPSIS
95  
96 In your C<app.psgi> file:
97  
98 use ASP4::PSGI;
99
100 ASP4::PSGI->app;
101  
102 That's it!
103  
104 =head1 DESCRIPTION
105  
106 L<ASP4> is a great way to build web applications. L<PSGI>/L<Plack> is a great way
107 to abstract a web application from the environment in which it is run.
108  
109 While I wouldn't B<yet> run a high-traffic ASP4 web application in this way, it works
110 very well for smaller tools and apps.
111  
112 =head1 SEE ALSO
113  
114 L<Plack>, L<PSGI>, L<http://plackperl.org/>
115  
116 =head1 SPECIAL THANKS TO
117  
118 * Tatsuhiko Miyagawa - The man behind Plack and PSGI.
119  
120 * Everyone else who has worked on the Plack and PSGI projects.
121  
122 =head1 BUGS
123  
124 It's possible that some bugs have found their way into this release.
125  
126 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4-PSGI> to submit bug reports.
127  
128 =head1 HOMEPAGE
129  
130 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
131 of ASP4 in action.
132  
133 =head1 AUTHOR
134  
135 John Drago <jdrago_999@yahoo.com>
136  
137 =head1 COPYRIGHT
138  
139 This software is Free software and may be used and redistributed under the same
140 terms as perl itself.
141  
142 =cut
143  
144  
↑ Summary