| 1 | # $Id: HTTPS.pm,v 1.14 2012/03/13 03:23:24 dk Exp $ | ||
| 2 | package IO::Lambda::HTTP::HTTPS; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Socket; | ||
| 7 | use IO::Socket::SSL; | ||
| 8 | use IO::Lambda qw(:lambda :stream :dev :constants); | ||
| 9 | use Errno qw(EWOULDBLOCK EAGAIN); | ||
| 10 | |||
| 11 | our $DEBUG = $IO::Lambda::DEBUG{https}; | ||
| 12 | |||
| 13 | # check for SSL error condition, wait for read or write if necessary | ||
| 14 | # return ioresult | ||
| 15 | sub https_wrapper | ||
| 16 | { | ||
| 17 | my ($sock, $deadline) = @_; | ||
| 18 | tail { | ||
| 19 | my ( $bytes, $error) = @_; | ||
| 20 | warn | ||
| 21 | "SSL on fh(", fileno($sock), ") = ", | ||
| 22 | (defined($bytes) ? "$bytes bytes" : "error $error"), | ||
| 23 | "\n" if $DEBUG; | ||
| 24 | return $bytes if defined $bytes; | ||
| 25 | return undef, $error if $error eq 'timeout'; | ||
| 26 | |||
| 27 | if ( $error == SSL_WANT_READ) { | ||
| 28 | warn "SSL_WANT_READ on fh(", fileno($sock), ")\n" if $DEBUG; | ||
| 29 | my @ctx = context; | ||
| 30 | context $sock, $deadline; | ||
| 31 | readable { | ||
| 32 | return 'timeout' unless shift; | ||
| 33 | context @ctx; | ||
| 34 | https_wrapper($sock, $deadline) | ||
| 35 | } | ||
| 36 | } elsif ( $error == SSL_WANT_WRITE) { | ||
| 37 | warn "SSL_WANT_WRITE on fh(", fileno($sock), ")\n" if $DEBUG; | ||
| 38 | my @ctx = context; | ||
| 39 | context $sock, $deadline; | ||
| 40 | writable { | ||
| 41 | return 'timeout' unless shift; | ||
| 42 | context @ctx; | ||
| 43 | https_wrapper($sock, $deadline) | ||
| 44 | } | ||
| 45 | } else { | ||
| 46 | warn | ||
| 47 | "SSL retry on fh(", fileno($sock), ") = ", | ||
| 48 | (defined($bytes) ? "$bytes bytes" : "error $error"), | ||
| 49 | "\n" if $DEBUG; | ||
| 50 | return $bytes, $error; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub https_connect | ||
| 56 | { | ||
| 57 | my ($sock, $host, $port, $deadline) = @_; | ||
| 58 | my $ssl_ok = IO::Socket::SSL-> start_SSL( $sock, | ||
| 59 | SSL_startHandshake => 0, | ||
| 60 | SSL_verify_mode => SSL_VERIFY_NONE(), | ||
| 61 | PeerAddr => $host, | ||
| 62 | PeerPort => $port, | ||
| 63 | Blocking => 0, | ||
| 64 | ); | ||
| 65 | return lambda { (undef, $SSL_ERROR ) } unless $ssl_ok; | ||
| 66 | |||
| 67 | lambda { | ||
| 68 | # emulate sysreader/syswriter to be able to | ||
| 69 | # reuse https_wrapper | ||
| 70 | context lambda { $sock-> connect_SSL ? 1 : (undef, $SSL_ERROR) }; | ||
| 71 | https_wrapper( $sock, $deadline ); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub https_syscall | ||
| 76 | { | ||
| 77 | my ( $read, $fh, $buf, $length, $offset) = @_; | ||
| 78 | $$buf = '' unless defined $$buf; | ||
| 79 | local $SIG{PIPE} = 'IGNORE'; | ||
| 80 | my $n = $read ? | ||
| 81 | sysread( $fh, $$buf, $length, $offset) : | ||
| 82 | syswrite( $fh, $$buf, $length, $offset); | ||
| 83 | unless ( defined $n ) { | ||
| 84 | my $err = $!; | ||
| 85 | warn "fh(", fileno($fh), ") ", ( $read ? 'read' : 'write'), " error $err\n" if $DEBUG; | ||
| 86 | return undef, $err; | ||
| 87 | } | ||
| 88 | if ( $DEBUG ) { | ||
| 89 | warn "fh(", fileno($fh), ") ", ( $read ? 'read' : 'wrote'), "$n bytes\n"; | ||
| 90 | warn substr( $$buf, length($$buf) - $n), "\n" if $DEBUG > 1 and $n > 0; | ||
| 91 | } | ||
| 92 | return $n; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub https_syscall_watcher | ||
| 96 | { | ||
| 97 | my $read = shift; | ||
| 98 | lambda { | ||
| 99 | my ( $fh, $buf, $length, $offset, $deadline) = @_; | ||
| 100 | |||
| 101 | ($deadline, $offset) = ($offset, length($$buf) || 0) if $read; | ||
| 102 | |||
| 103 | my ( $n, $err ) = https_syscall( $read, $fh, $buf, $length, $offset ); | ||
| 104 | return ($n, $err) if defined($n) || ($err != EAGAIN && $err != EWOULDBLOCK); | ||
| 105 | |||
| 106 | this-> watch_io( $read ? IO_READ : IO_WRITE, $fh, $deadline, _subname https_syscall_watcher => sub { | ||
| 107 | return undef, 'timeout' unless $_[1]; | ||
| 108 | my ( $n, $err ) = https_syscall( $read, $fh, $buf, $length, $offset ); | ||
| 109 | return $n if defined $n; | ||
| 110 | $err = $SSL_ERROR if $err == EWOULDBLOCK || $err == EAGAIN; | ||
| 111 | return undef, $err; | ||
| 112 | }); | ||
| 113 | }; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub https_writer | ||
| 117 | { | ||
| 118 | my ($cached, $host, $port) = @_; | ||
| 119 | my $writer = https_syscall_watcher(0); | ||
| 120 | |||
| 121 | lambda { | ||
| 122 | my ( $sock, $req, $length, $offset, $deadline) = @_; | ||
| 123 | if ( $cached ) { | ||
| 124 | context $writer, $sock, $req, $length, $offset, $deadline; | ||
| 125 | return https_wrapper($sock, $deadline); | ||
| 126 | } | ||
| 127 | context https_connect($sock, $host, $port, $deadline); | ||
| 128 | tail { | ||
| 129 | my ( $bytes, $error) = @_; | ||
| 130 | return @_ if defined $error; | ||
| 131 | |||
| 132 | $cached++; | ||
| 133 | context $writer, $sock, $req, $length, $offset, $deadline; | ||
| 134 | https_wrapper($sock, $deadline); | ||
| 135 | }} | ||
| 136 | } | ||
| 137 | |||
| 138 | sub https_reader | ||
| 139 | { | ||
| 140 | my $reader = https_syscall_watcher(1); | ||
| 141 | lambda { | ||
| 142 | my ( $sock, $buf, $length, $deadline) = @_; | ||
| 143 | context $reader, $sock, $buf, $length, $deadline; | ||
| 144 | https_wrapper($sock, $deadline); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __DATA__ |