use IO::Socket::INET;
use IO::Socket::SSL;
use IO::Select;
use strict;
my $sock = IO::Socket::INET->new(
PeerHost => 'encrypted.google.com', PeerPort => 443, Blocking => 0
) or die $@; # first create simple N-B socket with IO::Socket::INET
my $sel = IO::Select->new($sock); # wait until it connected
if ($sel->can_write) {
print "IO::Socket::INET connected\n";
}
# upgrade socket to IO::Socket::SSL
IO::Socket::SSL->start_SSL($sock, SSL_startHandshake => 0);
# make non-blocking SSL handshake
while (1) {
if ($sock->connect_SSL) { # will not block
print "IO::Socket::SSL connected\n";
last;
}
else { # handshake still incomplete
print "IO::Socket::SSL not connected yet\n";
if ($SSL_ERROR == SSL_WANT_READ) {
$sel->can_read;
}
elsif ($SSL_ERROR == SSL_WANT_WRITE) {
$sel->can_write;
}
else {
die "IO::Socket::SSL unknown error: ", $SSL_ERROR;
}
}
}
# sucessfully connected, we can write to socket
$sock->syswrite(
join(
"\015\012",
"GET / HTTP/1.0",
"\015\012"
)
) or die $!;
# and then read from it
while ($sel->can_read && $sock->sysread(my $buf, 1024)) {
print $buf;
}
$sock->close();
1 comments:
Nice post. I used to be checking continuously this blog and I am inspired! Extremely helpful info specially the closing part :) I maintain such information much. I was looking for this certain info for a long time. Thanks and best of luck.
Post a Comment