| 1 | package Coro::Twiggy; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use 5.008008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Twiggy::Server; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Coro; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Coro::Twiggy - Coro interface for L<Twiggy> | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | use Coro::Twiggy; | ||||||||||||||||||||||||||||
| 20 | use Plack::Request; | ||||||||||||||||||||||||||||
| 21 | use Coro::AnyEvent; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $application = sub { | ||||||||||||||||||||||||||||
| 24 | my ($env) = @_; | ||||||||||||||||||||||||||||
| 25 | my $req = Plack::Request->new($env); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Coro::AnyEvent::sleep 10; | ||||||||||||||||||||||||||||
| 28 | ... | ||||||||||||||||||||||||||||
| 29 | return [ | ||||||||||||||||||||||||||||
| 30 | 200, | ||||||||||||||||||||||||||||
| 31 | [ 'Content-Type' => 'text/html' ], | ||||||||||||||||||||||||||||
| 32 | [ 'Twiggy response after 10 seconds' ] | ||||||||||||||||||||||||||||
| 33 | ] | ||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my $server = Coro::Twiggy->new(host => '127.0.0.1', port => 8080); | ||||||||||||||||||||||||||||
| 38 | $server->register_service( $application ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | The server starts Your application in L<Coro/async> coroutine and uses its | ||||||||||||||||||||||||||||
| 44 | return value to respond to client. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Application have to return an B<ARRAYREF> with the following items: | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =over | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =item * | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | HTTP-code; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =item * | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | an B<ARRAYREF> that contains headers for response; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =item * | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | an B<ARRAYREF> that contains body of response. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =back | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | To stop server destroy server object | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 METHODS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | use constant DEFAULT_SERVICE => sub { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | [ | ||||||||||||||||||||||||||||
| 72 | 1 | 503, | |||||||||||||||||||||||||||
| 73 | [ 'Content-Type' => 'text/plain' ], | ||||||||||||||||||||||||||||
| 74 | [ 'There is no registered PSGI service' ] | ||||||||||||||||||||||||||||
| 75 | ] | ||||||||||||||||||||||||||||
| 76 | 2 | }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head2 new | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Constructor. Returns server. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head3 Named arguments | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =over | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =item host | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =item port | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item service | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | PSGI application | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =back | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 1 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 101 | 1 | my $host = $opts{host}; | |||||||||||||||||||||||||||
| 102 | 1 | ▶ | my $port = $opts{port} || 8080; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'port'} || 8080
MC/DC0%0/1
$opts{'port'} || 8080
$opts{'port'}
Truth table50%1/2
A
A= $opts{'port'}
| |||||||||||||||||||||||||||||
| 103 | 1 | my $listen = $opts{listen}; | |||||||||||||||||||||||||||
| 104 | 1 | ▶ | my $app = $opts{service} || DEFAULT_SERVICE; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'service'} || sub {
[503, ['Content-Type', 'text/plain'], ['There is no registered PSGI service']];
}
MC/DC0%0/1
$opts{'service'} || sub {
[503, ['Content-Type', 'text/plain'], ['There is no registered PSGI service']];
}
$opts{'service'}
Truth table50%1/2
A
A= $opts{'service'}
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | my @args; | |||||||||||||||||||||||||||
| 107 | 1 | ▶ | if ($listen) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | 0 | push @args => listen => $listen; | |||||||||||||||||||||||||||
| 109 | } elsif ($port !~ /^\d+$/) { | ||||||||||||||||||||||||||||
| 110 | 1 | push @args => listen => [ $port ]; | |||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||
| 112 | 0 | push @args => | |||||||||||||||||||||||||||
| 113 | host => $host, | ||||||||||||||||||||||||||||
| 114 | port => $port; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | my $ts = Twiggy::Server->new(@args); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | my $self = bless { ts => $ts, app => $app } => ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | my $this = $self; | |||||||||||||||||||||||||||
| 122 | 1 | $ts->register_service( $this->_app ); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | return $self; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 129 | 1 | delete $self->{ts}{listen_guards}; # hack: Twiggy has no interface to stop | |||||||||||||||||||||||||||
| 130 | 1 | delete $self->{ts}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 register_service | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | (Re)register PSGI application. | ||||||||||||||||||||||||||||
| 137 | Until the event server will respond B<503 Service Unavailable>. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 5 | ▶ | sub register_service { | ||||||||||||||||||||||||||
|
Subroutine
register_service: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 5 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||
| 143 | 5 | ▶ | $self->{app} = $cb || DEFAULT_SERVICE; | ||||||||||||||||||||||||||
|
Condition50%1/2
$cb || sub {
[503, ['Content-Type', 'text/plain'], ['There is no registered PSGI service']];
}
MC/DC0%0/1
$cb || sub {
[503, ['Content-Type', 'text/plain'], ['There is no registered PSGI service']];
}
$cb
Truth table50%1/2
A
A= $cb
| |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | ▶ | sub _app { | ||||||||||||||||||||||||||
|
Subroutine
_app: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 148 | 1 | weaken $self; | |||||||||||||||||||||||||||
| 149 | 6 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 6 | my ($env) = @_; | |||||||||||||||||||||||||||
| 151 | 6 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 6 | my ($cb) = @_; | |||||||||||||||||||||||||||
| 153 | 6 | ▶ | async { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=26.1
| |||||||||||||||||||||||||||||
| 154 | 6 | ▶ | return DEFAULT_SERVICE->() unless $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 6 | my @res = eval { $self->{app}->($env, $self) }; | |||||||||||||||||||||||||||
| 156 | 6 | my $res = shift @res; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | utf8::encode($err) if utf8::is_utf8 $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1 | $cb->([ 500, [ 'Content-Type' => 'text/plain' ], [ $err ]]); | |||||||||||||||||||||||||||
| 161 | 1 | return; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 5 | my $msg; | |||||||||||||||||||||||||||
| 165 | 5 | ▶ | unless('ARRAY' eq ref $res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 1 | $msg = 'PSGI application have to return an ARRAYREF'; | |||||||||||||||||||||||||||
| 167 | 1 | goto WRONG_RES; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 4 | ▶ | goto WRONG_RES unless @$res >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 3 | ▶ | push @$res => [] unless @$res > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | goto WRONG_RES | ||||||||||||||||||||||||||||
| 174 | 3 | ▶ | unless defined($res->[0]) && $res->[0] =~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $res->[0] and $res->[0] =~ /^\d+$/
MC/DC0%0/2
defined $res->[0] and $res->[0] =~ /^\d+$/
defined $res->[0]
$res->[0] =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $res->[0]B= $res->[0] =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 175 | 3 | ▶ | goto WRONG_RES unless 'ARRAY' eq ref $res->[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 3 | ▶ | goto WRONG_RES unless 'ARRAY' eq ref $res->[2]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 3 | $cb->( $res ); | |||||||||||||||||||||||||||
| 179 | 3 | return; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 2 | ▶ | WRONG_RES: | ||||||||||||||||||||||||||
|
Condition100%2/2
$msg ||= "PSGI returned wrong response"
MC/DC100%1/1
$msg ||= "PSGI returned wrong response"
$msg
Truth table100%2/2
A
A= $msg
| |||||||||||||||||||||||||||||
| 183 | $msg ||= "PSGI returned wrong response"; | ||||||||||||||||||||||||||||
| 184 | 2 | $msg .= "\n\n"; | |||||||||||||||||||||||||||
| 185 | { | ||||||||||||||||||||||||||||
| 186 | 2 | local $Data::Dumper::Indent = 1; | |||||||||||||||||||||||||||
| 187 | 2 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 188 | 2 | local $Data::Dumper::Useqq = 1; | |||||||||||||||||||||||||||
| 189 | 2 | local $Data::Dumper::Deepcopy = 1; | |||||||||||||||||||||||||||
| 190 | 2 | local $Data::Dumper::Maxdepth = 0; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 2 | my $dump = Data::Dumper->Dump([ $res, @res ]); | |||||||||||||||||||||||||||
| 193 | 2 | ▶ | utf8::downgrade($dump) if utf8::is_utf8 $dump; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 2 | $msg .= $dump; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 2 | $cb->( [ 500, [ 'Content-Type', 'text/plain' ], [ $msg ]]); | |||||||||||||||||||||||||||
| 198 | 2 | return; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 6 | } | |||||||||||||||||||||||||||
| 201 | 6 | } | |||||||||||||||||||||||||||
| 202 | 1 | } | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 2 | 1; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head1 VCS | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | L<https://github.com/unera/coro-twiggy> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Dmitry E. Oboukhov, <unera@debian.org> | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Copyright (C) 2012 by Dmitry E. Oboukhov | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 220 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||
| 221 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||