File Coverage

blib/lib/Net/Server/INET.pm
Criterion Covered Total %
statement 47 88 53.4
branch 2 14 14.2
condition 4 24 16.6
subroutine 15 34 44.1
pod 5 9 55.5
total 73 169 43.2


line stmt bran cond sub pod time code
1             # -*- perl -*-
2             #
3             # Net::Server::INET - Net::Server personality
4             #
5             # Copyright (C) 2001-2022
6             #
7             # Paul Seamons
8             #
9             # This package may be distributed under the terms of either the
10             # GNU General Public License
11             # or the
12             # Perl Artistic License
13             #
14             # All rights reserved.
15             #
16             ################################################################
17              
18             package Net::Server::INET;
19              
20 2     2   8314 use strict;
  2         4  
  2         122  
21 2     2   16 use base qw(Net::Server);
  2         6  
  2         1654  
22 2     2   26 use Scalar::Util qw(blessed);
  2         2  
  2         564  
23              
24 0     0 0 0 sub net_server_type { __PACKAGE__ }
25              
26             sub post_configure {
27 1     1 1 1 my $self = shift;
28 1         10 $self->{'server'}->{'_is_inet'} = 1;
29 1         28 $self->SUPER::post_configure();
30 1         2 delete $self->{'server'}->{'_is_inet'};
31             }
32              
33       1 1   sub pre_bind {} # no need to prepare bind
34              
35       1 1   sub bind {} # inet has no port to bind
36              
37             sub accept { # connection is already accepted
38 1     1 0 2 my $self = shift;
39 1         2 my $prop = $self->{'server'};
40              
41             ### Net::Server::INET will not do any determination of TCP,UDP,Unix
42             ### it is up to the programmer to keep these as separate processes
43 1         2 delete $prop->{'udp_true'}; # not sure if we can do UDP on INET
44              
45 1         3 1;
46             }
47              
48             sub get_client_info {
49 1     1 1 10 my $self = shift;
50 1         2 my $prop = $self->{'server'};
51 1   33     4 my $sock = shift || $prop->{'client'};
52              
53 1 50 33     26 if (blessed($sock) && $sock->can('NS_proto') && $sock->NS_proto eq 'UNIX') {
      33        
54 0         0 $self->log(3, $self->log_time." CONNECT UNIX Socket: \"".$sock->NS_port."\"");
55 0         0 return;
56             }
57              
58 1   50     12 $prop->{'sockaddr'} = $ENV{'REMOTE_HOST'} || '0.0.0.0';
59 1         9 $prop->{'peeraddr'} = '0.0.0.0';
60 1         15 $prop->{'sockhost'} = $prop->{'peerhost'} = 'inetd.server';
61 1         20 $prop->{'sockport'} = $prop->{'peerport'} = 0;
62 1         2 return;
63             }
64              
65              
66 0     0 0 0 sub done { 1 } # accept only one connection per process
67              
68             sub post_accept { # set up handles
69 1     1 1 2 my $self = shift;
70              
71             ### STDIN and STDOUT are already bound
72              
73             ### create a handle for those who want to use
74             ### an IO::Socket'ish handle - more portable
75             ### to just use STDIN and STDOUT though
76 1         19 $self->{'server'}->{'client'} = Net::Server::INET::Handle->new();
77              
78             }
79              
80             ### can't hup single process
81       0 0   sub hup_server {}
82              
83             ################################################################
84             ### the rest are methods to tie STDIN and STDOUT to a GLOB
85             ### this most likely isn't necessary, but the methods are there
86             ### support for this is experimental and may go away
87             ################################################################
88             package Net::Server::INET::Handle;
89              
90 2     2   14 use strict;
  2         4  
  2         68  
91 2     2   8 use Carp qw(croak);
  2         2  
  2         84  
92 2     2   6 use base qw(IO::Handle);
  2         4  
  2         1398  
93              
94             sub new {
95 1     1   2 my $class = shift;
96 1         2 local *HAND;
97 1         4 STDIN->autoflush(1);
98 1         37 STDOUT->autoflush(1);
99 1 50       21 tie *HAND, $class, *STDIN, *STDOUT or die "can't tie *HAND: $!";
100 1         3 bless \*HAND, $class;
101 1         8 return \*HAND;
102             }
103              
104 2     2   7 sub NS_proto { '' }
105              
106             sub TIEHANDLE {
107 1     1   12 my ($class, $in, $out) = @_;
108 1         20 bless [ \$in, \$out ], $class;
109             }
110              
111             sub PRINT {
112 0     0     my $handle = shift()->[1];
113 0           local *FH = $$handle;
114 0           CORE::print FH @_;
115             }
116              
117             sub PRINTF {
118 0     0     my $handle = shift()->[1];
119 0           local *FH = $$handle;
120 0           CORE::printf FH @_;
121             }
122              
123             sub WRITE {
124 0     0     my $handle = shift()->[1];
125 0           local *FH = $$handle;
126 0           local ($\) = "";
127 0 0         $_[1] = length($_[0]) unless defined $_[1];
128 0   0       CORE::print FH substr($_[0], $_[2] || 0, $_[1]);
129             }
130              
131             sub READ {
132 0     0     my $handle = shift()->[0];
133 0           local *FH = $$handle;
134 0   0       CORE::read(FH, $_[0], $_[1], $_[2] || 0);
135             }
136              
137             sub READLINE {
138 0     0     my $handle = shift()->[0];
139 0           local *FH = $$handle;
140 0           return scalar ;
141             }
142              
143             sub GETC {
144 0     0     my $handle = shift()->[0];
145 0           local *FH = $$handle;
146 0           return CORE::getc(FH);
147             }
148              
149             sub EOF {
150 0     0     my $handle = shift()->[0];
151 0           local *FH = $$handle;
152 0           return CORE::eof(FH);
153             }
154              
155       0     sub OPEN {}
156              
157             sub CLOSE {
158 0     0     my $self = shift;
159 0           $self = undef;
160             }
161              
162       0     sub BINMODE {}
163              
164       0     sub TELL {}
165              
166       0     sub SEEK {}
167              
168       0     sub DESTROY {}
169              
170       0     sub FILENO {}
171              
172       0     sub FETCH {}
173              
174             sub read_until { # only sips the data - but it allows for compatibility with SSLEAY
175 0     0     my ($client, $bytes, $end_qr) = @_;
176 0 0 0       croak "One of bytes or end_qr should be defined for TCP read_until" if !defined($bytes) && !defined($end_qr);
177 0           my $content = '';
178 0           my $ok = 0;
179 0           while (1) {
180 0           $client->read($content, 1, length($content));
181 0 0 0       if (defined($bytes) && length($content) >= $bytes) {
    0 0        
182 0           $ok = 2;
183 0           last;
184             }
185             elsif (defined($end_qr) && $content =~ $end_qr) {
186 0           $ok = 1;
187 0           last;
188             }
189             }
190 0 0         return wantarray ? ($ok, $content) : $content;
191             }
192              
193             1;
194              
195              
196             __END__