| 1 | package Langertha::Virtual::Server; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: HTTP endpoint in front of a Langertha::Engine::Virtual | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use HTTP::Status (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | has engine => ( | |||||||||||||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 17 | isa => 'Langertha::Engine::Virtual', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | has port => ( | |||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 4 | has loop => ( | |||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | predicate => 'has_loop', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | has debug => ( | |||||||||||||||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | has delay => ( | |||||||||||||||||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 44 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 4 | has _listener => ( is => 'rw', init_arg => undef ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | has _handle => ( is => 'rw', init_arg => undef ); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | has _clients => ( is => 'ro', init_arg => undef, default => sub { {} } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | has _json => ( | ||||||||||||||||||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 7 | ▶ | default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | my $MAX_REQUEST = 16 * 1024 * 1024; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 18 | ▶ | sub url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 18 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 18 | ▶ | my $listener = $self->_listener or croak ref($self).'->url: the server is not started'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 17 | return 'http://127.0.0.1:'.$listener->sockport.'/v1'; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | ▶ | croak ref($self).'->start: already started' if $self->_listener; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 7 | ▶ | my $listener = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | LocalAddr => '127.0.0.1', LocalPort => $self->port, Listen => 16, ReuseAddr => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ) or croak ref($self).'->start: cannot listen on 127.0.0.1:'.$self->port.": $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 7 | $listener->blocking(0); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | $self->_listener($listener); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | ▶ | if ( $self->has_loop ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | require IO::Async::Handle; | |||||||||||||||||||||||||||||||||||||||||
| 76 | my $handle = IO::Async::Handle->new( | ||||||||||||||||||||||||||||||||||||||||||
| 77 | read_handle => $listener, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 19 | ▶ | on_read_ready => sub { $self->_accept }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 6 | $self->loop->add($handle); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | $self->_handle($handle); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 9 | ▶ | sub stop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 9 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 9 | $self->_drop($_) for values %{ $self->_clients }; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 9 | ▶ | if ( my $handle = $self->_handle ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 6 | $self->loop->remove($handle); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 6 | $self->_handle(undef); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 9 | ▶ | if ( my $listener = $self->_listener ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 7 | close $listener; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 7 | $self->_listener(undef); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | ▶ | sub DEMOLISH { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | eval { $_[0]->stop }; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | croak ref($self).'->run: not started' unless $self->_listener; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | croak ref($self).'->run: the server is bound to a loop' if $self->has_loop; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $parent = $opts{parent}; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | last if defined $parent && getppid() != $parent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $parent and getppid != $parent
MC/DC0%0/2
defined $parent and getppid != $parent
defined $parent
getppid != $parent
Truth table0%0/3
A and B
A= defined $parentB= getppid != $parent
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $select = IO::Select->new( $self->_listener, map { $_->{socket} } values %{ $self->_clients } ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | for my $ready ( $select->can_read(1) ) { | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( $ready == $self->_listener ) { $self->_accept } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | else { $self->_read( $self->_clients->{ fileno $ready } ) } | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | sub spawn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=3 SCAR=12.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | my %args = @args; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | delete $args{loop}; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | my $server = $class->new(%args); | |||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | $server->start; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | my $url = $server->url; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | my $parent = $$; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | ▶ | my $pid = fork // croak "$class->spawn: fork failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
fork // croak("$class->spawn: fork failed: $!")
MC/DC0%0/1
fork // croak("$class->spawn: fork failed: $!")
fork
Truth table50%1/2
A
A= fork
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | unless ($pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | $SIG{TERM} = $SIG{INT} = sub { POSIX::_exit(0) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | eval { $server->run( parent => $parent ) }; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | $server->stop; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | return ( $pid, $url ); | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | sub reap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reap: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | my ( $class, $pid ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | return unless $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | kill 'TERM', $pid; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | waitpid $pid, 0; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 19 | ▶ | sub _accept { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_accept: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 19 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 19 | ▶ | while ( my $socket = $self->_listener->accept ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 19 | $socket->blocking(0); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 19 | my $client = { socket => $socket, buffer => '' }; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 19 | ▶ | if ( $self->has_loop ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | my $handle = IO::Async::Handle->new( | ||||||||||||||||||||||||||||||||||||||||||
| 160 | read_handle => $socket, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 19 | ▶ | on_read_ready => sub { $self->_read($client) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 19 | ); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 19 | $self->loop->add($handle); | |||||||||||||||||||||||||||||||||||||||||
| 164 | 19 | $client->{handle} = $handle; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 19 | $self->_clients->{ fileno $socket } = $client; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 19 | return; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 19 | ▶ | sub _drop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_drop: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 19 | my ( $self, $client ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 19 | ▶ | return unless $client; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 19 | ▶ | delete $self->_clients->{ fileno $client->{socket} } if defined fileno $client->{socket}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 19 | ▶ | $self->loop->remove( $client->{handle} ) if $client->{handle} && $self->has_loop; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$$client{"handle"} and $self->has_loop
MC/DC0%0/2
$$client{"handle"} and $self->has_loop
$$client{"handle"}
$self->has_loop
Truth table33%1/3
A and B
A= $$client{"handle"}B= $self->has_loop
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 19 | close $client->{socket}; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 19 | return; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 19 | ▶ | sub _read { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read: called CC=11 SCAR=31.0
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 19 | my ( $self, $client ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 19 | my $count = sysread $client->{socket}, $client->{buffer}, 65536, length $client->{buffer}; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 19 | ▶ | unless ($count) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | return if !defined $count && ( $!{EAGAIN} || $!{EWOULDBLOCK} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $count and $!{"EAGAIN"} || $!{"EWOULDBLOCK"}
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
MC/DC0%0/3
not defined $count and $!{"EAGAIN"} || $!{"EWOULDBLOCK"}
not defined $count
$!{"EAGAIN"}
$!{"EWOULDBLOCK"}
Truth table0%0/4
not(A) and B or C
A= not defined $countB= $!{"EAGAIN"}C= $!{"EWOULDBLOCK"}
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | return $self->_drop($client); | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 19 | ▶ | return if $client->{responding}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 19 | ▶ | my $request = $self->_parse( $client ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 19 | $client->{responding} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 19 | ▶ | if ( ref $request eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 19 | ▶ | my $respond = sub { $self->_send( $client, $self->_route($request) ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 19 | ▶ | if ( $self->delay && $request->{path} =~ m{/chat/completions\z} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->delay and $$request{"path"} =~ m[/chat/completions\z]
MC/DC50%1/2
$self->delay and $$request{"path"} =~ m[/chat/completions\z]
$self->delay
$$request{"path"} =~ m[/chat/completions\z]
Truth table66%2/3
A and B
A= $self->delayB= $$request{"path"} =~ m[/chat/completions\z]
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | ▶ | if ( $self->has_loop ) { $self->loop->watch_time( after => $self->delay, code => $respond ) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | else { Time::HiRes::sleep( $self->delay ); $respond->() } | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 18 | else { $respond->() } | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $self->_send( $client, $request ); | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 19 | return; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | # A parsed request (HashRef), a ready-made error response (ArrayRef) or | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # nothing while the request is incomplete. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 19 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=11 SCAR=27.2
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 19 | my ( $self, $client ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 19 | my $buffer = \$client->{buffer}; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 19 | my $end = index $$buffer, "\r\n\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 19 | ▶ | if ( $end < 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | return $self->_error_response( 431, 'request header too large' ) if length $$buffer > 65536; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 19 | my ( $line, @header_lines ) = split /\r\n/, substr( $$buffer, 0, $end ); | |||||||||||||||||||||||||||||||||||||||||
| 215 | 19 | ▶ | my ( $method, $target ) = $line =~ m{\A([A-Z]+) (\S+) HTTP/1\.[01]\z} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | or return $self->_error_response( 400, 'malformed request line' ); | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 19 | ▶ | my %header = map { /\A([^:]+):\s*(.*)\z/ ? ( lc $1 => $2 ) : () } @header_lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | return $self->_error_response( 411, 'chunked request bodies are not supported' ) | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 19 | ▶ | if lc( $header{'transfer-encoding'} // '' ) =~ /chunked/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$header{"transfer-encoding"} // ""
MC/DC0%0/1
$header{"transfer-encoding"} // ""
$header{"transfer-encoding"}
Truth table50%1/2
A
A= $header{"transfer-encoding"}
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 19 | ▶ | my $length = $header{'content-length'} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$header{"content-length"} // 0
MC/DC100%1/1
$header{"content-length"} // 0
$header{"content-length"}
Truth table100%2/2
A
A= $header{"content-length"}
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 19 | ▶ | return $self->_error_response( 400, 'malformed Content-Length' ) unless $length =~ /\A\d+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 19 | ▶ | return $self->_error_response( 413, 'request body too large' ) if $length > $MAX_REQUEST; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 19 | ▶ | return if length($$buffer) - $end - 4 < $length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 19 | my ( $path ) = $target =~ m{\A([^?]*)}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 19 | return { method => $method, path => $path, body => substr( $$buffer, $end + 4, $length ) }; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | # A response is [ $status, \@headers, $body ] or, for server-sent events, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # [ $status, \@headers, undef, \@chunks ]. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 19 | ▶ | sub _route { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_route: called CC=8 SCAR=21.7
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 19 | my ( $self, $request ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 19 | my ( $method, $path ) = @{$request}{qw( method path )}; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 19 | ( my $route = $path ) =~ s{\A/v1(?=/)}{}; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 19 | ▶ | if ( $route eq '/chat/completions' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 16 | ▶ | return $self->_error_response( 405, "$method is not allowed on $path" ) unless $method eq 'POST'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 15 | return $self->_chat($request); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 3 | ▶ | if ( $route eq '/models' && $method eq 'GET' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$route eq "/models" and $method eq "GET"
MC/DC50%1/2
$route eq "/models" and $method eq "GET"
$route eq "/models"
$method eq "GET"
Truth table66%2/3
A and B
A= $route eq "/models"B= $method eq "GET"
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | return $self->_json_response( 200, { | ||||||||||||||||||||||||||||||||||||||||||
| 240 | object => 'list', | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | data => [ map { { id => $_->{id}, object => 'model', created => 0, owned_by => 'virtual' } } | |||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | @{ $self->engine->list_models( full => 1 ) } ], | |||||||||||||||||||||||||||||||||||||||||
| 243 | } ); | ||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 2 | ▶ | if ( $self->debug && $path eq '/_virtual/requests' && $method eq 'GET' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$self->debug and $path eq "/_virtual/requests" and $method eq "GET"
$self->debug and $path eq "/_virtual/requests"
MC/DC0%0/3
$self->debug and $path eq "/_virtual/requests" and $method eq "GET"
$self->debug
$path eq "/_virtual/requests"
$method eq "GET"
Truth table25%1/4
A and B and C
A= $self->debugB= $path eq "/_virtual/requests"C= $method eq "GET"
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | return $self->_json_response( 200, { | |||||||||||||||||||||||||||||||||||||||||
| 247 | requests => $self->engine->requests, | ||||||||||||||||||||||||||||||||||||||||||
| 248 | calls => $self->engine->calls, | ||||||||||||||||||||||||||||||||||||||||||
| 249 | } ); | ||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 2 | return $self->_error_response( 404, "no route for $method $path" ); | |||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 15 | ▶ | sub _chat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_chat: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 15 | my ( $self, $request ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 15 | my $body = eval { $self->_json->decode( $request->{body} ) }; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 15 | ▶ | return $self->_error_response( 400, 'the request body is not a JSON object' ) unless ref $body eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 14 | my $reply = $self->engine->virtual_reply( $request->{method}, $request->{path}, $request->{body} ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 14 | ▶ | return $self->_error_response( $reply->{code}, $reply->{content} ) unless $reply->{code} == 200; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 12 | ▶ | return $body->{stream} ? $self->_sse_response( $reply->{content}, $body ) : $self->_json_response( 200, $reply->{content} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 6 | ▶ | sub _sse_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sse_response: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 6 | my ( $self, $completion, $body ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 265 | my $events = $self->engine->virtual_stream_chunks( $completion, | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 6 | ▶ | include_usage => ref $body->{stream_options} eq 'HASH' && $body->{stream_options}{include_usage} ? 1 : 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $$body{"stream_options"} eq "HASH" and $$body{"stream_options"}{"include_usage"}
MC/DC50%1/2
ref $$body{"stream_options"} eq "HASH" and $$body{"stream_options"}{"include_usage"}
ref $$body{"stream_options"} eq "HASH"
$$body{"stream_options"}{"include_usage"}
Truth table66%2/3
A and B
A= ref $$body{"stream_options"} eq "HASH"B= $$body{"stream_options"}{"include_usage"}
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 6 | my @chunks = map { 'data: '.$self->_json->encode($_)."\n\n" } @$events; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 6 | push @chunks, "data: [DONE]\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 6 | return [ 200, [ 'Content-Type' => 'text/event-stream', 'Cache-Control' => 'no-cache' ], undef, \@chunks ]; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 13 | ▶ | sub _json_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_response: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 13 | my ( $self, $status, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 13 | return [ $status, [ 'Content-Type' => 'application/json' ], $self->_json->encode($data) ]; | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 6 | ▶ | sub _error_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_error_response: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 6 | my ( $self, $status, $message ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 6 | ▶ | return $self->_json_response( $status, { error => { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | message => $message, type => $status >= 500 ? 'server_error' : 'invalid_request_error', code => $status, | ||||||||||||||||||||||||||||||||||||||||||
| 281 | } } ); | ||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 19 | ▶ | sub _send { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_send: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 19 | my ( $self, $client, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 19 | my ( $status, $headers, $body, $chunks ) = @$response; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 19 | ▶ | my $reason = HTTP::Status::status_message($status) // 'Unknown'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
&HTTP::Status::status_message($status) // "Unknown"
MC/DC0%0/1
&HTTP::Status::status_message($status) // "Unknown"
&HTTP::Status::status_message($status)
Truth table50%1/2
A
A= &HTTP::Status::status_message($status)
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 19 | ▶ | $body = Encode::encode( 'UTF-8', $body ) if defined $body && Encode::is_utf8($body); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $body and &Encode::is_utf8($body)
MC/DC0%0/2
defined $body and &Encode::is_utf8($body)
defined $body
&Encode::is_utf8($body)
Truth table66%2/3
A and B
A= defined $bodyB= &Encode::is_utf8($body)
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | my $head = "HTTP/1.1 $status $reason\r\n" | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 19 | ▶ | .join( '', map { "$headers->[$_]: $headers->[$_ + 1]\r\n" } grep { !( $_ % 2 ) } 0 .. $#$headers ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | .( defined $body ? 'Content-Length: '.length($body)."\r\n" : '' ) | ||||||||||||||||||||||||||||||||||||||||||
| 292 | ."Connection: close\r\n\r\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 19 | local $SIG{PIPE} = 'IGNORE'; | |||||||||||||||||||||||||||||||||||||||||
| 294 | 19 | ▶ | $self->_write( $client, $head.( $body // '' ).join( '', @{ $chunks // [] } ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%4/4
$body // ""
$chunks // []
MC/DC100%1/1
$body // ""
$body
MC/DC100%1/1
$chunks // []
$chunks
Truth table100%2/2
A
A= $body
Truth table100%2/2
A
A= $chunks
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 19 | $self->_drop($client); | |||||||||||||||||||||||||||||||||||||||||
| 296 | 19 | return; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | # Blocking write of a small buffer to a socket that is otherwise non-blocking. | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 19 | ▶ | sub _write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write: called CC=6 SCAR=21.9
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 19 | my ( $self, $client, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 19 | my $socket = $client->{socket}; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 19 | my $offset = 0; | |||||||||||||||||||||||||||||||||||||||||
| 304 | 19 | my $select = IO::Select->new($socket); | |||||||||||||||||||||||||||||||||||||||||
| 305 | 19 | ▶ | while ( $offset < length $data ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 19 | my $written = syswrite $socket, $data, 65536, $offset; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 19 | ▶ | if ( defined $written ) { $offset += $written; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | return unless $!{EAGAIN} || $!{EWOULDBLOCK}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"}
$!{"EWOULDBLOCK"}
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | $select->can_write(5) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 19 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | __END__ | ||||||||||||||||||||||||||||||||||||||||||