| 1 | package Mojo::IOLoop::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 33 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 33 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 33 | ▶ | use IO::Socket::IP; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 33 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 33 | ▶ | use Mojo::File qw(path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 33 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 33 | ▶ | use Mojo::IOLoop::TLS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 33 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 33 | ▶ | use Socket qw(IPPROTO_TCP TCP_NODELAY); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | has reactor => sub { Mojo::IOLoop->singleton->reactor }, weak => 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 41 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 41 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 41 | ▶ | $ENV{MOJO_REUSE} =~ s/(?:^|\,)\Q$self->{reuse}\E// if $self->{reuse}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 41 | ▶ | $self->stop if $self->{handle} && $self->reactor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"handle"} and $self->reactor
MC/DC50%1/2
$self->{"handle"} and $self->reactor
$self->{"handle"}
$self->reactor
Truth table66%2/3
A and B
A= $self->{"handle"}B= $self->reactor
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub generate_port { IO::Socket::IP->new(Listen => 5, LocalAddr => '127.0.0.1')->sockport } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_port: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub handle { shift->{handle} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | sub is_accepting { !!shift->{active} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_accepting: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 51 | ▶ | sub listen { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
listen: called CC=21 SCAR=35.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 51 | ▶ | my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # Look for reusable file descriptor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 51 | my $path = $args->{path}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 51 | ▶ | my $address = $args->{address} || '0.0.0.0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'address'} || '0.0.0.0'
MC/DC0%0/1
$args->{'address'} || '0.0.0.0'
$args->{'address'}
Truth table50%1/2
A
A= $args->{'address'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 51 | my $port = $args->{port}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 51 | ▶ | $ENV{MOJO_REUSE} ||= ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{"MOJO_REUSE"} ||= ""
MC/DC100%1/1
$ENV{"MOJO_REUSE"} ||= ""
$ENV{"MOJO_REUSE"}
Truth table100%2/2
A
A= $ENV{"MOJO_REUSE"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | my $fd = ($path && $ENV{MOJO_REUSE} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 51 | ▶ | || ($port && $ENV{MOJO_REUSE} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/) ? $1 : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$path and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u or $port and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
$path and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u
$port and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
MC/DC0%0/4
$path and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u or $port and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
$path
$ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u
$port
$ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
Truth table14%1/7
A and B or C and D
A= $pathB= $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/uC= $portD= $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # Allow file descriptor inheritance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 51 | local $^F = 1023; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Reuse file descriptor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 51 | my $handle; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 51 | ▶ | my $class = $path ? 'IO::Socket::UNIX' : 'IO::Socket::IP'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 51 | ▶ | if (defined($fd //= $args->{fd})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$fd //= $args->{"fd"}
MC/DC50%1/2
$fd //= $args->{"fd"}
$fd
$args->{"fd"}
Truth table66%2/3
A //= B
A= $fdB= $args->{"fd"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | $handle = $class->new_from_fd($fd, 'r') or croak "Can't open file descriptor $fd: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 50 | ▶ | my %options = (Listen => $args->{backlog} // SOMAXCONN, Type => SOCK_STREAM); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"backlog"} // 4096
MC/DC0%0/1
$args->{"backlog"} // 4096
$args->{"backlog"}
Truth table50%1/2
A
A= $args->{"backlog"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # UNIX domain socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 50 | my $reuse; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 50 | ▶ | if ($path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | path($path)->remove if -S $path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $options{Local} = $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $handle = $class->new(%options) or croak "Can't create listen socket: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $reuse = $self->{reuse} = join ':', 'unix', $path, fileno $handle; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # IP socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 50 | $options{LocalAddr} = $address; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 50 | $options{LocalAddr} =~ y/[]//d; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 50 | ▶ | $options{LocalPort} = $port if $port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 50 | $options{ReuseAddr} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 50 | $options{ReusePort} = $args->{reuse}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 50 | ▶ | $handle = $class->new(%options) or croak "Can't create listen socket: $@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 50 | $fd = fileno $handle; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 50 | $reuse = $self->{reuse} = join ':', $address, $handle->sockport, $fd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 50 | ▶ | $ENV{MOJO_REUSE} .= length $ENV{MOJO_REUSE} ? ",$reuse" : "$reuse"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 51 | $handle->blocking(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 51 | @$self{qw(args handle)} = ($args, $handle); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 51 | ▶ | croak 'IO::Socket::SSL 2.009+ required for TLS support' if !Mojo::IOLoop::TLS->can_tls && $args->{tls}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not "Mojo::IOLoop::TLS"->can_tls and $args->{"tls"}
MC/DC100%2/2
not "Mojo::IOLoop::TLS"->can_tls and $args->{"tls"}
not "Mojo::IOLoop::TLS"->can_tls
$args->{"tls"}
Truth table100%3/3
not(A) and B
A= not "Mojo::IOLoop::TLS"->can_tlsB= $args->{"tls"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 50 | ▶ | sub port { shift->{handle}->sockport } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
port: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 111 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 111 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 111 | weaken $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 111 | ▶ | ++$self->{active} and $self->reactor->io($self->{handle} => sub { $self->_accept })->watch($self->{handle}, 1, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 135 | ▶ | sub stop { delete($_[0]{active}) and $_[0]->reactor->remove($_[0]{handle}) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 39 | ▶ | sub _accept { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_accept: called CC=7 SCAR=21.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 39 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # Greedy accept | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 39 | my $args = $self->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 39 | my $accepted = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 39 | ▶ | while ($self->{active} && !($args->{single_accept} && $accepted++)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$self->{"active"} and not $args->{"single_accept"} && $accepted++
$args->{'single_accept'} && $accepted++
MC/DC100%3/3
$self->{"active"} and not $args->{"single_accept"} && $accepted++
$self->{"active"}
$args->{'single_accept'}
$accepted++
Truth table100%4/4
A and not(B && C)
A= $self->{"active"}B= $args->{'single_accept'}C= $accepted++
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 73 | ▶ | return unless my $handle = $self->{handle}->accept; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 39 | $handle->blocking(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # Disable Nagle's algorithm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 39 | setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 39 | ▶ | $self->emit(accept => $handle) and next unless $args->{tls}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
$args->{'tls'} || $self->emit('accept', $handle) && (next)
$self->emit("accept", $handle) and next
MC/DC0%0/2
$args->{'tls'} || $self->emit('accept', $handle) && (next)
$args->{'tls'}
$self->emit("accept", $handle)
Truth table33%1/3
A || B
A= $args->{'tls'}B= $self->emit("accept", $handle)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # Start TLS handshake | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $tls = Mojo::IOLoop::TLS->new($handle)->reactor($self->reactor); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $tls->on(upgrade => sub { $self->emit(accept => pop) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $tls->on(error => sub { }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $tls->negotiate(%$args, server => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 33 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | Mojo::IOLoop::Server - Non-blocking TCP and UNIX domain socket server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | use Mojo::IOLoop::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # Create listen socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | my $server = Mojo::IOLoop::Server->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | $server->on(accept => sub ($server, $handle) {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | $server->listen(port => 3000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # Start and stop accepting connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | $server->start; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | $server->stop; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # Start reactor if necessary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | $server->reactor->start unless $server->reactor->is_running; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | L<Mojo::IOLoop::Server> accepts TCP/IP and UNIX domain socket connections for L<Mojo::IOLoop>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | =head1 EVENTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | L<Mojo::IOLoop::Server> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | =head2 accept | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | $server->on(accept => sub ($server, $handle) {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | Emitted for each accepted connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | L<Mojo::IOLoop::Server> implements the following attributes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | =head2 reactor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | my $reactor = $server->reactor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | $server = $server->reactor(Mojo::Reactor::Poll->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | Low-level event reactor, defaults to the C<reactor> attribute value of the global L<Mojo::IOLoop> singleton. Note that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | this attribute is weakened. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | L<Mojo::IOLoop::Server> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | =head2 generate_port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | my $port = Mojo::IOLoop::Server->generate_port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | Find a free TCP port, primarily used for tests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | =head2 handle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | my $handle = $server->handle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | Get handle for server, usually an L<IO::Socket::IP> object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | =head2 is_accepting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | my $bool = $server->is_accepting; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | Check if connections are currently being accepted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | =head2 listen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | $server->listen(port => 3000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | $server->listen({port => 3000}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | Create a new listen socket. Note that TLS support depends on L<IO::Socket::SSL> (2.009+). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | These options are currently available: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | =item address | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | address => '127.0.0.1' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | Local address to listen on, defaults to C<0.0.0.0>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | =item backlog | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | backlog => 128 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | Maximum backlog size, defaults to C<SOMAXCONN>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | =item fd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | fd => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | File descriptor with an already prepared listen socket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | =item path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | path => '/tmp/myapp.sock' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | Path for UNIX domain socket to listen on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | =item port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | port => 80 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | Port to listen on, defaults to a random port. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | =item reuse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | reuse => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | Allow multiple servers to use the same port with the C<SO_REUSEPORT> socket option. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | =item single_accept | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | single_accept => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | Only accept one connection at a time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | =item tls | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | tls => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | Enable TLS. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | =item tls_ca | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | tls_ca => '/etc/tls/ca.crt' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | Path to TLS certificate authority file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | =item tls_cert | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | tls_cert => '/etc/tls/server.crt' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | tls_cert => {'mojolicious.org' => '/etc/tls/mojo.crt'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | Path to the TLS cert file, defaults to a built-in test certificate. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | =item tls_key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | tls_key => '/etc/tls/server.key' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | tls_key => {'mojolicious.org' => '/etc/tls/mojo.key'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | Path to the TLS key file, defaults to a built-in test key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | =item tls_options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | tls_options => {SSL_alpn_protocols => ['foo', 'bar'], SSL_verify_mode => 0x00} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | Additional options for L<IO::Socket::SSL>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | =head2 port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | my $port = $server->port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | Get port this server is listening on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | =head2 start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | $server->start; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | Start or resume accepting connections. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | =head2 stop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | $server->stop; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | Stop accepting connections. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||