| 1 | # -*- Perl -*- | ||||||||||
| 2 | # | ||||||||||
| 3 | # a Gemini protocol server, mostly to test the Net::Gemini client with | ||||||||||
| 4 | # | ||||||||||
| 5 | # "They are great warriors: their greatness is like the empty desert | ||||||||||
| 6 | # wastes. They are both the lords of the River, the River of the | ||||||||||
| 7 | # Ordeal which clears the just man. They weigh upon the evil man like | ||||||||||
| 8 | # a neck-stock. In Kisiga, their very anciently founded city, the | ||||||||||
| 9 | # trustworthy does not get caught, but the evil cannot pass through." | ||||||||||
| 10 | |||||||||||
| 11 | package Net::Gemini::Server; | ||||||||||
| 12 | 33 | our $VERSION = '0.08'; | |||||||||
| 13 | 33 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 33 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | # the below code mostly cargo culted from example/ssl_server.pl | ||||||||||
| 16 | 33 | ▶ | use IO::Socket::IP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 33 | ▶ | use IO::Socket::SSL; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 85 | ▶ | DESTROY { undef $_[0]{_context}; undef $_[0]{_socket} } | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | sub new { | ||||||||||
| 22 | 86 | ▶ | my ( $class, %param ) = @_; | ||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 23 | $param{listen}{LocalPort} = 1965 | ||||||||||
| 24 | 86 | ▶ | unless defined $param{listen}{LocalPort}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 86 | my %obj; | |||||||||
| 26 | $obj{_socket} = IO::Socket::IP->new( | ||||||||||
| 27 | Listen => 5, | ||||||||||
| 28 | Reuse => 1, | ||||||||||
| 29 | 86 | ▶ | %{ $param{listen} }, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | ) or die "server failed: $!"; | ||||||||||
| 31 | # server default is not to perform any verification | ||||||||||
| 32 | $obj{_context} = | ||||||||||
| 33 | 85 | ▶ | IO::Socket::SSL::SSL_Context->new( %{ $param{context} }, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | SSL_server => 1, ) | ||||||||||
| 35 | or die "context failed: $SSL_ERROR"; | ||||||||||
| 36 | 85 | $obj{_port} = $obj{_socket}->sockport; | |||||||||
| 37 | 85 | bless \%obj, $class; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | sub context { $_[0]{_context} } | ||||||||
|
Subroutine
context: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 41 | 85 | ▶ | sub port { $_[0]{_port} } | ||||||||
|
Subroutine
port: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | 55 | ▶ | sub socket { $_[0]{_socket} } | ||||||||
|
Subroutine
socket: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | |||||||||||
| 44 | # this, as noted elsewhere, is mostly for testing the client | ||||||||||
| 45 | sub withforks { | ||||||||||
| 46 | 29 | ▶ | my ( $self, $callback, %param ) = @_; | ||||||||
|
Subroutine
withforks: called CC=9 SCAR=23.2
Pod
documented
| |||||||||||
| 47 | 29 | my $server = $self->{_socket}; | |||||||||
| 48 | 29 | while (1) { | |||||||||
| 49 | 204 | ▶ | my $client = $server->accept or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | warn "accept failed: $!\n"; | |||||||||
| 51 | 0 | next; | |||||||||
| 52 | }; | ||||||||||
| 53 | 204 | ▶ | if ( $param{close_on_accept} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | close $client; | |||||||||
| 55 | 0 | next; | |||||||||
| 56 | } | ||||||||||
| 57 | 204 | my $parent = fork; | |||||||||
| 58 | 204 | ▶ | die "fork failed: $!" unless defined $parent; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 204 | ▶ | if ($parent) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 175 | close $client; | |||||||||
| 61 | 175 | next; | |||||||||
| 62 | } | ||||||||||
| 63 | 29 | ▶ | unless ( $param{no_ssl} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 29 | ▶ | unless ( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | IO::Socket::SSL->start_SSL( | ||||||||||
| 66 | $client, | ||||||||||
| 67 | SSL_server => 1, | ||||||||||
| 68 | SSL_reuse_ctx => $self->{_context} | ||||||||||
| 69 | ) | ||||||||||
| 70 | ) { | ||||||||||
| 71 | 1 | warn "ssl handshake failed: $SSL_ERROR\n"; | |||||||||
| 72 | 1 | close $client; | |||||||||
| 73 | 1 | exit; | |||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | 28 | ▶ | if ( $param{close_before_read} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | close $client; | |||||||||
| 78 | 0 | exit; | |||||||||
| 79 | } | ||||||||||
| 80 | 28 | binmode $client, ':raw'; | |||||||||
| 81 | # NOTE this assumes the client isn't sending bytes one by one slow | ||||||||||
| 82 | 28 | my $n = sysread( $client, my $buf, 1024 ); | |||||||||
| 83 | # does not suss out any new client edge cases | ||||||||||
| 84 | #if ( $param{close_after_read} ) { | ||||||||||
| 85 | # close $client; | ||||||||||
| 86 | # exit; | ||||||||||
| 87 | #} | ||||||||||
| 88 | eval { | ||||||||||
| 89 | # NOTE the buffer is raw bytes and may need a decode | ||||||||||
| 90 | 28 | $callback->( $client, $n, $buf ); | |||||||||
| 91 | 27 | 1; | |||||||||
| 92 | 28 | ▶ | } or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | # KLUGE random stderr from a fork can confuse TAP and get | ||||||||||
| 94 | # the tests out of sequence? | ||||||||||
| 95 | #warn "callback error: $@"; | ||||||||||
| 96 | 1 | close $client; | |||||||||
| 97 | }; | ||||||||||
| 98 | 28 | exit; | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 33 | 1; | |||||||||
| 103 | __END__ | ||||||||||