blib/lib/Net/SSLeay/Handle.pm

filter: Statementstmt 19.6% 21 / 107 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 26.3% 5 / 19 Podpod 100% 3 / 3 totaltotal 15.1% 29 / 191 CC 30 SCAR 33.9
CC
30
Cov
13%
make_socket
90.0
TIEHANDLE
42.0
PRINT
20.0
CRAP 29.6
↑ Summary blib/lib/Net/SSLeay.pm »
1 package Net::SSLeay::Handle;
2  
3 1 ▶use 5.8.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::SSLeay;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11  
12 =encoding utf-8
13  
14 =head1 NAME
15  
16 Net::SSLeay::Handle - Perl module that lets SSL (HTTPS) sockets be
17 handled as standard file handles.
18  
19 =head1 SYNOPSIS
20  
21 use Net::SSLeay::Handle qw/shutdown/;
22 my ($host, $port) = ("localhost", 443);
23  
24 tie(*SSL, "Net::SSLeay::Handle", $host, $port);
25  
26 print SSL "GET / HTTP/1.0\r\n";
27 shutdown(\*SSL, 1);
28 print while (<SSL>);
29 close SSL;
30  
31 =head1 DESCRIPTION
32  
33 Net::SSLeay::Handle allows you to request and receive HTTPS web pages
34 using "old-fashion" file handles as in:
35  
36 print SSL "GET / HTTP/1.0\r\n";
37  
38 and
39  
40 print while (<SSL>);
41  
42 If you export the shutdown routine, then the only extra code that
43 you need to add to your program is the tie function as in:
44  
45 my $socket;
46 if ($scheme eq "https") {
47 tie(*S2, "Net::SSLeay::Handle", $host, $port);
48 $socket = \*S2;
49 else {
50 $socket = Net::SSLeay::Handle->make_socket($host, $port);
51 }
52 print $socket $request_headers;
53 ...
54  
55 =cut
56  
57 1 ▶use vars qw(@ISA @EXPORT_OK $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 @ISA = qw(Exporter);
59 1 @EXPORT_OK = qw(shutdown);
60 1 $VERSION = '1.96';
61  
62 1 my $Initialized; #-- only _initialize() once
63 1 my $Debug = 0; #-- pretty hokey
64  
65 #== Tie Handle Methods ========================================================
66 #
67 # see perldoc perltie for details.
68 #
69 #==============================================================================
70  
71 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=6 SCAR=37.4
72 0 my ($class, $socket, $port) = @_;
73 0 ▶ $Debug > 10 and print "TIEHANDLE(@{[join ', ', @_]})\n";
Branch: 1 branch
BranchTrueFalseTotal
$Debug > 10 and print qq[TIEHANDLE(@{[join(", ", @_)];})\n] 0 0 0
74  
75 0 ▶ ref $socket eq "GLOB" or $socket = $class->make_socket($socket, $port);
Branch: 1 branch
BranchTrueFalseTotal
ref $socket eq "GLOB" or $socket = $class->make_socket($socket, $port) 0 0 0
76  
77 0 $class->_initialize();
78  
79 0 ▶ my $ctx = Net::SSLeay::CTX_new() or die_now("Failed to create SSL_CTX $!");
Branch: 1 branch
BranchTrueFalseTotal
my $ctx = &Net::SSLeay::CTX_new() or die_now("Failed to create SSL_CTX $!") 0 0 0
80 0 ▶ my $ssl = Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!");
Branch: 1 branch
BranchTrueFalseTotal
my $ssl = &Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!") 0 0 0
81  
82 0 my $fileno = fileno($socket);
83  
84 0 Net::SSLeay::set_fd($ssl, $fileno); # Must use fileno
85  
86 0 my $resp = Net::SSLeay::connect($ssl);
87  
88 0 ▶ $Debug and print "Cipher '" . Net::SSLeay::get_cipher($ssl) . "'\n";
Branch: 1 branch
BranchTrueFalseTotal
$Debug and print q[Cipher '] . &Net::SSLeay::get_cipher($ssl) . "'\n" 0 0 0
89  
90 0 my $self = bless {
91 ssl => $ssl,
92 ctx => $ctx,
93 socket => $socket,
94 fileno => $fileno,
95 }, $class;
96  
97 0 return $self;
98 }
99  
100 0 ▶sub PRINT {
Subroutine PRINT: not called CC=4 SCAR=30.0
101 0 my $self = shift;
102  
103 0 my $ssl = _get_ssl($self);
104 0 my $resp = 0;
105 0 for my $msg (@_) {
106 0 ▶ defined $msg or last;
Branch: 1 branch
BranchTrueFalseTotal
defined $msg or last 0 0 0
107 0 ▶ $resp = Net::SSLeay::write($ssl, $msg) or last;
Branch: 1 branch
BranchTrueFalseTotal
$resp = &Net::SSLeay::write($ssl, $msg) or last 0 0 0
108 }
109 0 return $resp;
110 }
111  
112 0 ▶sub READLINE {
Subroutine READLINE: not called CC=4 SCAR=30.0
113 0 my $self = shift;
114 0 my $ssl = _get_ssl($self);
115 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
116 0 my @lines;
117 0 ▶ while (my $line = Net::SSLeay::ssl_read_until($ssl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = &Net::SSLeay::ssl_read_until($ssl)) 0 0 0
118 0 push @lines, $line;
119 }
120 0 return @lines;
121 } else {
122 0 my $line = Net::SSLeay::ssl_read_until($ssl);
123 0 ▶ return $line ? $line : undef;
Branch: 1 branch
BranchTrueFalseTotal
$line ? : 0 0 0
124 }
125 }
126  
127 0 ▶sub READ {
Subroutine READ: not called CC=4 SCAR=30.0
128 0 my ($self, $buf, $len, $offset) = \ (@_);
129 0 my $ssl = _get_ssl($$self);
130 0 ▶ defined($$offset) or
Branch: 1 branch
BranchTrueFalseTotal
defined $$offset or return length($$buf = Net::SSLeay::ssl_read_all($ssl, $$len)) 0 0 0
131 return length($$buf = Net::SSLeay::ssl_read_all($ssl, $$len));
132  
133 0 ▶ defined(my $read = Net::SSLeay::ssl_read_all($ssl, $$len))
Branch: 1 branch
BranchTrueFalseTotal
defined(my $read = Net::SSLeay::ssl_read_all($ssl, $$len)) or return undef 0 0 0
134 or return undef;
135  
136 0 my $buf_len = length($$buf);
137 0 ▶ $$offset > $buf_len and $$buf .= chr(0) x ($$offset - $buf_len);
Branch: 1 branch
BranchTrueFalseTotal
$$offset > $buf_len and $$buf .= "\0" x ($$offset - $buf_len) 0 0 0
138 0 substr($$buf, $$offset) = $read;
139 0 return length($read);
140 }
141  
142 0 ▶sub WRITE {
Subroutine WRITE: not called CC=3 SCAR=24.8
143 0 my $self = shift;
144 0 my ($buf, $len, $offset) = @_;
145 0 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
146  
147 # Return number of characters written.
148 0 my $ssl = $self->_get_ssl();
149 0 ▶ return $len if Net::SSLeay::write($ssl, substr($buf, $offset, $len));
Branch: 1 branch
BranchTrueFalseTotal
if &Net::SSLeay::write($ssl, substr($buf, $offset, $len)) 0 0 0
150 0 return undef;
151 }
152  
153 0 ▶sub CLOSE {
Subroutine CLOSE: not called CC=2 SCAR=17.9
154 0 my $self = shift;
155 0 my $fileno = $self->{fileno};
156 0 ▶ $Debug > 10 and print "close($fileno)\n";
Branch: 1 branch
BranchTrueFalseTotal
$Debug > 10 and print "close($fileno)\n" 0 0 0
157 0 Net::SSLeay::free ($self->{ssl});
158 0 Net::SSLeay::CTX_free ($self->{ctx});
159 0 close $self->{socket};
160 }
161  
162 0 ▶sub FILENO { $_[0]->{fileno} }
Subroutine FILENO: not called CC=1 SCAR=6.9
163  
164  
165 =head1 FUNCTIONS
166  
167 =over
168  
169 =item shutdown
170  
171 shutdown(\*SOCKET, $mode)
172  
173 Calls to the main shutdown() don't work with tied sockets created with this
174 module. This shutdown should be able to distinquish between tied and untied
175 sockets and do the right thing.
176  
177 =cut
178  
179 0 ▶sub shutdown {
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod documented
180 0 my ($obj, @params) = @_;
181  
182 my $socket = UNIVERSAL::isa($obj, 'Net::SSLeay::Handle') ?
183 0 ▶ $obj->{socket} : $obj;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($obj, 'Net::SSLeay::Handle') ? : 0 0 0
184 0 return shutdown($socket, @params);
185 }
186  
187 =item debug
188  
189 my $debug = Net::SSLeay::Handle->debug()
190 Net::SSLeay::Handle->debug(1)
191  
192 Get/set debugging mode. Always returns the debug value before the function call.
193 if an additional argument is given the debug option will be set to this value.
194  
195 =cut
196  
197 0 ▶sub debug {
Subroutine debug: not called CC=3 SCAR=24.8
Pod documented
198 0 my ($class, $debug) = @_;
199 0 my $old_debug = $Debug;
200 0 ▶ @_ >1 and $Debug = $debug || 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and $Debug = $debug || 0 0 0 0
Condition0%0/2
$debug || 0
l!l
00
MC/DC0%0/1
$debug || 0
$debug
Truth table0%0/2
A
A= $debug
Aresult
11
00
201 0 return $old_debug;
202 }
203  
204 #=== Internal Methods =========================================================
205  
206 =item make_socket
207  
208 my $sock = Net::SSLeay::Handle->make_socket($host, $port);
209  
210 Creates a socket that is connected to $post using $port. It uses
211 $Net::SSLeay::proxyhost and proxyport if set and authentificates itself against
212 this proxy depending on $Net::SSLeay::proxyauth. It also turns autoflush on for
213 the created socket.
214  
215 =cut
216  
217 0 ▶sub make_socket {
Subroutine make_socket: not called CC=9 SCAR=45.0
Pod documented
218 0 my ($class, $host, $port) = @_;
219 0 ▶ $Debug > 10 and print "_make_socket(@{[join ', ', @_]})\n";
Branch: 1 branch
BranchTrueFalseTotal
$Debug > 10 and print qq[_make_socket(@{[join(", ", @_)];})\n] 0 0 0
220 0 ▶ $host ||= 'localhost';
Condition0%0/2
$host ||= "localhost"
l!l
00
MC/DC0%0/1
$host ||= "localhost"
$host
Truth table0%0/2
A
A= $host
Aresult
11
00
221 0 ▶ $port ||= 443;
Condition0%0/2
$port ||= 443
l!l
00
MC/DC0%0/1
$port ||= 443
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
222  
223 0 my $phost = $Net::SSLeay::proxyhost;
224 0 ▶ my $pport = $Net::SSLeay::proxyhost ? $Net::SSLeay::proxyport : $port;
Branch: 1 branch
BranchTrueFalseTotal
$Net::SSLeay::proxyhost ? : 0 0 0
225  
226 0 ▶ my $dest_ip = gethostbyname($phost || $host);
Condition0%0/3
$phost || $host
l!l&&r!l&&!r
000
MC/DC0%0/2
$phost || $host
$phost $host
Truth table0%0/3
A || B
A= $phostB= $host
ABresult
1X1
011
000
227 0 my $host_params = sockaddr_in($pport, $dest_ip);
228
229 0 ▶ socket(my $socket, &PF_INET(), &SOCK_STREAM(), 0) or die "socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket my $socket, &PF_INET(), &SOCK_STREAM(), 0 or die "socket: $!" 0 0 0
230 0 ▶ connect($socket, $host_params) or die "connect: $!";
Branch: 1 branch
BranchTrueFalseTotal
connect $socket, $host_params or die "connect: $!" 0 0 0
231  
232 0 my $old_select = select($socket); $| = 1; select($old_select);
233 0 ▶ $phost and do {
Branch: 1 branch
BranchTrueFalseTotal
$phost and do { my $auth = $Net::SSLeay::proxyauth; my $CRLF = $Net::SSLeay::CRLF; print $socket "CONNECT ${host}:$port HTTP/1.0$auth$CRLF$CRLF"; my $line = readline $socket } 0 0 0
234 0 my $auth = $Net::SSLeay::proxyauth;
235 0 my $CRLF = $Net::SSLeay::CRLF;
236 0 print $socket "CONNECT $host:$port HTTP/1.0$auth$CRLF$CRLF";
237 0 my $line = <$socket>;
238 };
239 0 return $socket;
240 }
241  
242 =back
243  
244 =cut
245  
246 0 ▶sub _initialize {
Subroutine _initialize: not called CC=2 SCAR=17.9
247 0 ▶ $Initialized++ and return;
Branch: 1 branch
BranchTrueFalseTotal
$Initialized++ and return 0 0 0
248 0 Net::SSLeay::load_error_strings();
249 0 Net::SSLeay::SSLeay_add_ssl_algorithms();
250 0 Net::SSLeay::randomize();
251 }
252  
253 0 ▶sub __dummy {
Subroutine __dummy: not called CC=1 SCAR=6.9
254 0 my $host = $Net::SSLeay::proxyhost;
255 0 my $port = $Net::SSLeay::proxyport;
256 0 my $auth = $Net::SSLeay::proxyauth;
257 }
258  
259 #--- _get_self($socket) -------------------------------------------------------
260 # Returns a hash containing attributes for $socket (= \*SOMETHING) based
261 # on fileno($socket). Will return undef if $socket was not created here.
262 #------------------------------------------------------------------------------
263  
264 0 ▶sub _get_self { return $_[0]; }
Subroutine _get_self: not called CC=1 SCAR=6.9
265  
266 #--- _get_ssl($socket) --------------------------------------------------------
267 # Returns a the "ssl" attribute for $socket (= \*SOMETHING) based
268 # on fileno($socket). Will cause a warning and return undef if $socket was not
269 # created here.
270 #------------------------------------------------------------------------------
271  
272 0 ▶sub _get_ssl {
Subroutine _get_ssl: not called CC=1 SCAR=6.9
273 0 return $_[0]->{ssl};
274 }
275  
276 1 1;
277  
278 __END__
↑ Summary blib/lib/Net/SSLeay.pm »