| 1 | package Limper::Engine::PSGI; | ||||||||||||||||||||
| 2 | 2 | $Limper::Engine::PSGI::VERSION = '0.002'; | |||||||||||||||||||
| 3 | 2 | ▶ | use base 'Limper'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use 5.10.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | package # newline because Dist::Zilla::Plugin::PkgVersion and PAUSE indexer | ||||||||||||||||||||
| 9 | Limper; | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | sub get_psgi { | ||||||||||||||||||||
| 12 | 3 | ▶ | my ($env) = @_; | ||||||||||||||||||
|
Subroutine
get_psgi: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 3 | delete response->{$_} for keys %{&response}; | |||||||||||||||||||
| 15 | 3 | response->{headers} = {}; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 3 | delete request->{$_} for keys %{&request}; | |||||||||||||||||||
| 18 | 3 | request->{method} = $env->{REQUEST_METHOD}; | |||||||||||||||||||
| 19 | 3 | request->{uri} = $env->{REQUEST_URI}; | |||||||||||||||||||
| 20 | 3 | request->{version} = $env->{SERVER_PROTOCOL}; | |||||||||||||||||||
| 21 | 3 | request->{remote_host} = $env->{REMOTE_HOST}; | |||||||||||||||||||
| 22 | 3 | request->{headers} = {}; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 3 | ▶ | request->{headers}{'content-length'} = $env->{CONTENT_LENGTH} if exists $env->{CONTENT_LENGTH}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 3 | ▶ | request->{headers}{'content-type'} = $env->{CONTENT_TYPE} if exists $env->{CONTENT_TYPE}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 3 | for my $header (grep { /^HTTP_/ } keys %$env) { | |||||||||||||||||||
| 27 | 3 | my $name = lc $header; | |||||||||||||||||||
| 28 | 3 | $name =~ s/^http_//; | |||||||||||||||||||
| 29 | 3 | $name =~ s/_/-/g; | |||||||||||||||||||
| 30 | 3 | my @values = split /, /, $env->{$header}; | |||||||||||||||||||
| 31 | 3 | ▶ | request->{headers}{$name} = @values > 1 ? \@values : $values[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | # this covers both requests with Content-Length: <INT> and Tranfer-Encoding: chunked | ||||||||||||||||||||
| 34 | 3 | ▶ | $env->{'psgi.input'}->read(request->{body}, $env->{CONTENT_LENGTH}) if exists $env->{CONTENT_LENGTH}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | hook request_handler => sub { | ||||||||||||||||||||
| 38 | 3 | ▶ | eval { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=21.1
| |||||||||||||||||||||
| 39 | 3 | get_psgi @_; | |||||||||||||||||||
| 40 | 3 | $_ = handle_request; | |||||||||||||||||||
| 41 | }; | ||||||||||||||||||||
| 42 | 3 | ▶ | return $_ unless $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | warning $@; | |||||||||||||||||||
| 44 | 0 | status 500; | |||||||||||||||||||
| 45 | 0 | ▶ | response->{body} = options->{debug} // 0 ? $@ : 'Internal Server Error'; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
options()->{"debug"} // 0
MC/DC0%0/1
options()->{"debug"} // 0
options()->{"debug"}
Truth table0%0/2
A
A= options()->{"debug"}
| |||||||||||||||||||||
| 46 | 0 | send_response; | |||||||||||||||||||
| 47 | 2 | }; | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | hook response_handler => sub { | ||||||||||||||||||||
| 50 | [ | ||||||||||||||||||||
| 51 | response->{status}, | ||||||||||||||||||||
| 52 | [ headers ], | ||||||||||||||||||||
| 53 | 3 | ▶ | defined response->{body} ? (ref response->{body} ? response->{body} : [response->{body}]) : [], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
| |||||||||||||||||||||
| 54 | ]; | ||||||||||||||||||||
| 55 | 2 | }; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 2 | 1; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =for Pod::Coverage get_psgi | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =head1 NAME | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | Limper::Engine::PSGI - PSGI engine for Limper | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head1 VERSION | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | version 0.002 | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head1 SYNOPSIS | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | use Limper::Engine::PSGI; # all you need to do is add this line | ||||||||||||||||||||
| 72 | use Limper; # this must come after all extensions | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | # routes and whatnot | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | limp; | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 DESCRIPTION | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | B<Limper::Engine::PSGI> extends L<Limper> to use L<PSGI> instead of the built-in web | ||||||||||||||||||||
| 81 | server. | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | All you need to do in order to use L<PSGI> is add C<use Limper::Engine::PSGI;> | ||||||||||||||||||||
| 84 | somewhere before C<use Limper;> in your app. | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | This package sets a B<request_handler> and B<response_handler> for | ||||||||||||||||||||
| 87 | L<Limper>, as well as defining a non-exportable sub that turns a PSGI | ||||||||||||||||||||
| 88 | request into one that B<Limper> understands. | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | Note that unlike other hooks, only the first B<request_handler> and | ||||||||||||||||||||
| 91 | B<response_handler> is used, so care should be taken to load this first and | ||||||||||||||||||||
| 92 | not load another B<Limper::Engine::> that also expects to make use of these | ||||||||||||||||||||
| 93 | hooks. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =head1 EXPORTS | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | Nothing additional is exported. | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Copyright (C) 2014 by Ashley Willis E<lt>ashley+perl@gitable.orgE<gt> | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 104 | it under the same terms as Perl itself, either Perl version 5.12.4 or, | ||||||||||||||||||||
| 105 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head1 SEE ALSO | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | L<Limper> | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | L<Limper::SendFile> | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | L<Limper::SendJSON> | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | L<PSGI> | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | L<Plack> | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | L<Starman> | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | L<The uWSGI project|https://uwsgi-docs.readthedocs.org/en/latest/> | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||