… | |
… | |
14 | on_error => sub { |
14 | on_error => sub { |
15 | my ($hdl, $fatal, $msg) = @_; |
15 | my ($hdl, $fatal, $msg) = @_; |
16 | warn "got error $msg\n"; |
16 | warn "got error $msg\n"; |
17 | $hdl->destroy; |
17 | $hdl->destroy; |
18 | $cv->send; |
18 | $cv->send; |
19 | ); |
19 | }; |
20 | |
20 | |
21 | # send some request line |
21 | # send some request line |
22 | $hdl->push_write ("getinfo\015\012"); |
22 | $hdl->push_write ("getinfo\015\012"); |
23 | |
23 | |
24 | # read the response line |
24 | # read the response line |
… | |
… | |
79 | |
79 | |
80 | =head1 METHODS |
80 | =head1 METHODS |
81 | |
81 | |
82 | =over 4 |
82 | =over 4 |
83 | |
83 | |
84 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
84 | =item $handle = B<new> AnyEvent::Handle fh => $filehandle, key => value... |
85 | |
85 | |
86 | The constructor supports these arguments (all as C<< key => value >> pairs). |
86 | The constructor supports these arguments (all as C<< key => value >> pairs). |
87 | |
87 | |
88 | =over 4 |
88 | =over 4 |
89 | |
89 | |
… | |
… | |
710 | |
710 | |
711 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
711 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
712 | |
712 | |
713 | =cut |
713 | =cut |
714 | |
714 | |
715 | sub on_starttls { |
715 | sub on_stoptls { |
716 | $_[0]{on_stoptls} = $_[1]; |
716 | $_[0]{on_stoptls} = $_[1]; |
717 | } |
717 | } |
718 | |
718 | |
719 | =item $handle->rbuf_max ($max_octets) |
719 | =item $handle->rbuf_max ($max_octets) |
720 | |
720 | |
… | |
… | |
903 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
903 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
904 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
904 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
905 | ->($self, @_); |
905 | ->($self, @_); |
906 | } |
906 | } |
907 | |
907 | |
|
|
908 | # we downgrade here to avoid hard-to-track-down bugs, |
|
|
909 | # and diagnose the problem earlier and better. |
|
|
910 | |
908 | if ($self->{tls}) { |
911 | if ($self->{tls}) { |
909 | $self->{_tls_wbuf} .= $_[0]; |
912 | utf8::downgrade $self->{_tls_wbuf} .= $_[0]; |
910 | &_dotls ($self) if $self->{fh}; |
913 | &_dotls ($self) if $self->{fh}; |
911 | } else { |
914 | } else { |
912 | $self->{wbuf} .= $_[0]; |
915 | utf8::downgrade $self->{wbuf} .= $_[0]; |
913 | $self->_drain_wbuf if $self->{fh}; |
916 | $self->_drain_wbuf if $self->{fh}; |
914 | } |
917 | } |
915 | } |
918 | } |
916 | |
919 | |
917 | =item $handle->push_write (type => @args) |
920 | =item $handle->push_write (type => @args) |
… | |
… | |
1711 | } |
1714 | } |
1712 | |
1715 | |
1713 | sub start_read { |
1716 | sub start_read { |
1714 | my ($self) = @_; |
1717 | my ($self) = @_; |
1715 | |
1718 | |
1716 | unless ($self->{_rw} || $self->{_eof}) { |
1719 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1717 | Scalar::Util::weaken $self; |
1720 | Scalar::Util::weaken $self; |
1718 | |
1721 | |
1719 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1722 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1720 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1723 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1721 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1724 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
… | |
… | |
1814 | && ($tmp != $ERROR_SYSCALL || $!); |
1817 | && ($tmp != $ERROR_SYSCALL || $!); |
1815 | |
1818 | |
1816 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1819 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1817 | $self->{wbuf} .= $tmp; |
1820 | $self->{wbuf} .= $tmp; |
1818 | $self->_drain_wbuf; |
1821 | $self->_drain_wbuf; |
|
|
1822 | $self->{tls} or return; # tls session might have gone away in callback |
1819 | } |
1823 | } |
1820 | |
1824 | |
1821 | $self->{_on_starttls} |
1825 | $self->{_on_starttls} |
1822 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1826 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1823 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1827 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
… | |
… | |
1922 | |
1926 | |
1923 | =item $handle->stoptls |
1927 | =item $handle->stoptls |
1924 | |
1928 | |
1925 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1929 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1926 | sending a close notify to the other side, but since OpenSSL doesn't |
1930 | sending a close notify to the other side, but since OpenSSL doesn't |
1927 | support non-blocking shut downs, it is not guarenteed that you can re-use |
1931 | support non-blocking shut downs, it is not guaranteed that you can re-use |
1928 | the stream afterwards. |
1932 | the stream afterwards. |
1929 | |
1933 | |
1930 | =cut |
1934 | =cut |
1931 | |
1935 | |
1932 | sub stoptls { |
1936 | sub stoptls { |
1933 | my ($self) = @_; |
1937 | my ($self) = @_; |
1934 | |
1938 | |
1935 | if ($self->{tls}) { |
1939 | if ($self->{tls} && $self->{fh}) { |
1936 | Net::SSLeay::shutdown ($self->{tls}); |
1940 | Net::SSLeay::shutdown ($self->{tls}); |
1937 | |
1941 | |
1938 | &_dotls; |
1942 | &_dotls; |
1939 | |
1943 | |
1940 | # # we don't give a shit. no, we do, but we can't. no...#d# |
1944 | # # we don't give a shit. no, we do, but we can't. no...#d# |
… | |
… | |
2017 | |
2021 | |
2018 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2022 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2019 | #nop |
2023 | #nop |
2020 | } |
2024 | } |
2021 | |
2025 | |
|
|
2026 | =item $handle->destroyed |
|
|
2027 | |
|
|
2028 | Returns false as long as the handle hasn't been destroyed by a call to C<< |
|
|
2029 | ->destroy >>, true otherwise. |
|
|
2030 | |
|
|
2031 | Can be useful to decide whether the handle is still valid after some |
|
|
2032 | callback possibly destroyed the handle. For example, C<< ->push_write >>, |
|
|
2033 | C<< ->starttls >> and other methods can call user callbacks, which in turn |
|
|
2034 | can destroy the handle, so work can be avoided by checking sometimes: |
|
|
2035 | |
|
|
2036 | $hdl->starttls ("accept"); |
|
|
2037 | return if $hdl->destroyed; |
|
|
2038 | $hdl->push_write (... |
|
|
2039 | |
|
|
2040 | Note that the call to C<push_write> will silently be ignored if the handle |
|
|
2041 | has been destroyed, so often you can just ignore the possibility of the |
|
|
2042 | handle being destroyed. |
|
|
2043 | |
|
|
2044 | =cut |
|
|
2045 | |
|
|
2046 | sub destroyed { 0 } |
|
|
2047 | sub AnyEvent::Handle::destroyed::destroyed { 1 } |
|
|
2048 | |
2022 | =item AnyEvent::Handle::TLS_CTX |
2049 | =item AnyEvent::Handle::TLS_CTX |
2023 | |
2050 | |
2024 | This function creates and returns the AnyEvent::TLS object used by default |
2051 | This function creates and returns the AnyEvent::TLS object used by default |
2025 | for TLS mode. |
2052 | for TLS mode. |
2026 | |
2053 | |