| 1 | package Mojo::Server::PSGI; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojo::Server'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 5 | 0 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 0 | my $tx = $self->build_tx; | |||||||||||||||||||||||||||
| 8 | 0 | my $req = $tx->req->parse($env); | |||||||||||||||||||||||||||
| 9 | 0 | $tx->local_port($env->{SERVER_PORT})->remote_address($env->{REMOTE_ADDR}); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # Request body (may block if we try to read too much) | ||||||||||||||||||||||||||||
| 12 | 0 | my $len = $env->{CONTENT_LENGTH}; | |||||||||||||||||||||||||||
| 13 | 0 | ▶ | until ($req->is_finished) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my $chunk = ($len && $len < 131072) ? $len : 131072; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$len and $len < 131072
MC/DC0%0/2
$len and $len < 131072
$len
$len < 131072
Truth table0%0/3
A and B
A= $lenB= $len < 131072
| |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | last unless my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 0 | $req->parse($buffer); | |||||||||||||||||||||||||||
| 17 | 0 | ▶ | last if ($len -= $read) <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | $self->emit(request => $tx); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Response headers | ||||||||||||||||||||||||||||
| 23 | 0 | my $res = $tx->res->fix_headers; | |||||||||||||||||||||||||||
| 24 | 0 | my $hash = $res->headers->to_hash(1); | |||||||||||||||||||||||||||
| 25 | 0 | my @headers; | |||||||||||||||||||||||||||
| 26 | 0 | for my $name (keys %$hash) { push @headers, $name, $_ for @{$hash->{$name}} } | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # PSGI response | ||||||||||||||||||||||||||||
| 29 | 0 | my $io = Mojo::Server::PSGI::_IO->new(tx => $tx, empty => $tx->is_empty); | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | return [$res->code // 404, \@headers, $io]; | ||||||||||||||||||||||||||
|
Condition0%0/2
$res->code // 404
MC/DC0%0/1
$res->code // 404
$res->code
Truth table0%0/2
A
A= $res->code
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | sub to_psgi_app { | ||||||||||||||||||||||||||
|
Subroutine
to_psgi_app: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Preload application and wrap it | ||||||||||||||||||||||||||||
| 37 | 4 | $self->app->server($self); | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | return sub { $self->run(@_) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 4 | } | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | package Mojo::Server::PSGI::_IO; | ||||||||||||||||||||||||||||
| 42 | 1 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Finish transaction | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub close { shift->{tx}->closed } | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub getline { | ||||||||||||||||||||||||||
|
Subroutine
getline: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 48 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Empty | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | return undef if $self->{empty}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # No content yet, try again later | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $chunk = $self->{tx}->res->get_body_chunk($self->{offset} //= 0); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"offset"} //= 0
MC/DC0%0/1
$self->{"offset"} //= 0
$self->{"offset"}
Truth table0%0/2
A
A= $self->{"offset"}
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | return '' unless defined $chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # End of content | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return undef unless length $chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | $self->{offset} += length $chunk; | |||||||||||||||||||||||||||
| 61 | 0 | return $chunk; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | 1; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =encoding utf8 | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 NAME | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Mojo::Server::PSGI - PSGI server | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | use Mojo::Server::PSGI; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | my $psgi = Mojo::Server::PSGI->new; | ||||||||||||||||||||||||||||
| 77 | $psgi->unsubscribe('request')->on(request => sub ($psgi, $tx) { | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Request | ||||||||||||||||||||||||||||
| 80 | my $method = $tx->req->method; | ||||||||||||||||||||||||||||
| 81 | my $path = $tx->req->url->path; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Response | ||||||||||||||||||||||||||||
| 84 | $tx->res->code(200); | ||||||||||||||||||||||||||||
| 85 | $tx->res->headers->content_type('text/plain'); | ||||||||||||||||||||||||||||
| 86 | $tx->res->body("$method request for $path!"); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Resume transaction | ||||||||||||||||||||||||||||
| 89 | $tx->resume; | ||||||||||||||||||||||||||||
| 90 | }); | ||||||||||||||||||||||||||||
| 91 | my $app = $psgi->to_psgi_app; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | L<Mojo::Server::PSGI> allows L<Mojolicious> applications to run on all L<PSGI> compatible servers. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | L<Mojo::Server::PSGI> inherits all events from L<Mojo::Server>. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | L<Mojo::Server::PSGI> inherits all attributes from L<Mojo::Server>. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 METHODS | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | L<Mojo::Server::PSGI> inherits all methods from L<Mojo::Server> and implements the following new ones. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 run | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | my $res = $psgi->run($env); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Run L<PSGI>. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 to_psgi_app | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | my $app = $psgi->to_psgi_app; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Turn L<Mojolicious> application into L<PSGI> application. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||