… | |
… | |
30 | |
30 | |
31 | $cv->recv; |
31 | $cv->recv; |
32 | |
32 | |
33 | =head1 DESCRIPTION |
33 | =head1 DESCRIPTION |
34 | |
34 | |
35 | This is a helper module to make it easier to do event-based I/O on |
35 | This is a helper module to make it easier to do event-based I/O |
36 | stream-based filehandles (sockets, pipes, and other stream things). |
36 | on stream-based filehandles (sockets, pipes, and other stream |
|
|
37 | things). Specifically, it doesn't work as expected on files, packet-based |
|
|
38 | sockets or similar things. |
37 | |
39 | |
38 | The L<AnyEvent::Intro> tutorial contains some well-documented |
40 | The L<AnyEvent::Intro> tutorial contains some well-documented |
39 | AnyEvent::Handle examples. |
41 | AnyEvent::Handle examples. |
40 | |
42 | |
41 | In the following, where the documentation refers to "bytes", it means |
43 | In the following, where the documentation refers to "bytes", it means |
… | |
… | |
53 | package AnyEvent::Handle; |
55 | package AnyEvent::Handle; |
54 | |
56 | |
55 | use Scalar::Util (); |
57 | use Scalar::Util (); |
56 | use List::Util (); |
58 | use List::Util (); |
57 | use Carp (); |
59 | use Carp (); |
58 | use Errno qw(EAGAIN EINTR); |
60 | use Errno qw(EAGAIN EWOULDBLOCK EINTR); |
59 | |
61 | |
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
62 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
63 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
62 | |
64 | |
63 | our $VERSION = $AnyEvent::VERSION; |
65 | our $VERSION = $AnyEvent::VERSION; |
… | |
… | |
91 | |
93 | |
92 | =item fh => $filehandle [C<fh> or C<connect> MANDATORY] |
94 | =item fh => $filehandle [C<fh> or C<connect> MANDATORY] |
93 | |
95 | |
94 | The filehandle this L<AnyEvent::Handle> object will operate on. |
96 | The filehandle this L<AnyEvent::Handle> object will operate on. |
95 | NOTE: The filehandle will be set to non-blocking mode (using |
97 | NOTE: The filehandle will be set to non-blocking mode (using |
96 | C<AnyEvent::Util::fh_nonblocking>) by the constructor and needs to stay in |
98 | C<AnyEvent::fh_unblock>) by the constructor and needs to stay in |
97 | that mode. |
99 | that mode. |
98 | |
100 | |
99 | =item connect => [$host, $service] [C<fh> or C<connect> MANDATORY] |
101 | =item connect => [$host, $service] [C<fh> or C<connect> MANDATORY] |
100 | |
102 | |
101 | Try to connect to the specified host and service (port), using |
103 | Try to connect to the specified host and service (port), using |
… | |
… | |
131 | |
133 | |
132 | The peer's numeric host and port (the socket peername) are passed as |
134 | The peer's numeric host and port (the socket peername) are passed as |
133 | parameters, together with a retry callback. At the time it is called the |
135 | parameters, together with a retry callback. At the time it is called the |
134 | read and write queues, EOF status, TLS status and similar properties of |
136 | read and write queues, EOF status, TLS status and similar properties of |
135 | the handle will have been reset. |
137 | the handle will have been reset. |
136 | |
|
|
137 | It is not allowed to use the read or write queues while the handle object |
|
|
138 | is connecting. |
|
|
139 | |
138 | |
140 | If, for some reason, the handle is not acceptable, calling C<$retry> will |
139 | If, for some reason, the handle is not acceptable, calling C<$retry> will |
141 | continue with the next connection target (in case of multi-homed hosts or |
140 | continue with the next connection target (in case of multi-homed hosts or |
142 | SRV records there can be multiple connection endpoints). The C<$retry> |
141 | SRV records there can be multiple connection endpoints). The C<$retry> |
143 | callback can be invoked after the connect callback returns, i.e. one can |
142 | callback can be invoked after the connect callback returns, i.e. one can |
… | |
… | |
501 | =item json => L<JSON>, L<JSON::PP> or L<JSON::XS> object |
500 | =item json => L<JSON>, L<JSON::PP> or L<JSON::XS> object |
502 | |
501 | |
503 | This is the json coder object used by the C<json> read and write types. |
502 | This is the json coder object used by the C<json> read and write types. |
504 | |
503 | |
505 | If you don't supply it, then AnyEvent::Handle will create and use a |
504 | If you don't supply it, then AnyEvent::Handle will create and use a |
506 | suitable one (on demand), which will write and expect UTF-8 encoded JSON |
505 | suitable one (on demand), which will write and expect UTF-8 encoded |
|
|
506 | JSON texts (either using L<JSON::XS> or L<JSON>). The written texts are |
|
|
507 | guaranteed not to contain any newline character. |
|
|
508 | |
|
|
509 | For security reasons, this encoder will likely I<not> handle numbers and |
|
|
510 | strings, only arrays and objects/hashes. The reason is that originally |
|
|
511 | JSON was self-delimited, but Dougles Crockford thought it was a splendid |
|
|
512 | idea to redefine JSON incompatibly, so this is no longer true. |
|
|
513 | |
|
|
514 | For protocols that used back-to-back JSON texts, this might lead to |
|
|
515 | run-ins, where two or more JSON texts will be interpreted as one JSON |
507 | texts. |
516 | text. |
|
|
517 | |
|
|
518 | For this reason, if the default encoder uses L<JSON::XS>, it will default |
|
|
519 | to not allowing anything but arrays and objects/hashes, at least for the |
|
|
520 | forseeable future (it will change at some point). This might or might not |
|
|
521 | be true for the L<JSON> module, so this might cause a security issue. |
|
|
522 | |
|
|
523 | If you depend on either behaviour, you should create your own json object |
|
|
524 | and pass it in explicitly. |
508 | |
525 | |
509 | =item cbor => L<CBOR::XS> object |
526 | =item cbor => L<CBOR::XS> object |
510 | |
527 | |
511 | This is the cbor coder object used by the C<cbor> read and write types. |
528 | This is the cbor coder object used by the C<cbor> read and write types. |
512 | |
529 | |
513 | If you don't supply it, then AnyEvent::Handle will create and use a |
530 | If you don't supply it, then AnyEvent::Handle will create and use a |
514 | suitable one (on demand), which will write CBOR without using extensions, |
531 | suitable one (on demand), which will write CBOR without using extensions, |
515 | if possible. texts. |
532 | if possible. |
516 | |
533 | |
517 | Note that you are responsible to depend on the L<CBOR::XS> module if you |
534 | Note that you are responsible to depend on the L<CBOR::XS> module if you |
518 | want to use this functionality, as AnyEvent does not have a dependency on |
535 | want to use this functionality, as AnyEvent does not have a dependency on |
519 | it itself. |
536 | it itself. |
520 | |
537 | |
… | |
… | |
596 | # with AnyEvent::Handle, do them a favour. |
613 | # with AnyEvent::Handle, do them a favour. |
597 | my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE (); |
614 | my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE (); |
598 | Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!" |
615 | Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!" |
599 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
616 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
600 | |
617 | |
601 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
618 | AnyEvent::fh_unblock $self->{fh}; |
602 | |
619 | |
603 | $self->{_activity} = |
620 | $self->{_activity} = |
604 | $self->{_ractivity} = |
621 | $self->{_ractivity} = |
605 | $self->{_wactivity} = AE::now; |
622 | $self->{_wactivity} = AE::now; |
606 | |
623 | |
… | |
… | |
738 | $_[0]{oobinline} = $_[1]; |
755 | $_[0]{oobinline} = $_[1]; |
739 | |
756 | |
740 | eval { |
757 | eval { |
741 | local $SIG{__DIE__}; |
758 | local $SIG{__DIE__}; |
742 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_OOBINLINE (), int $_[1] |
759 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_OOBINLINE (), int $_[1] |
743 | if $_[0]{fh}; |
|
|
744 | }; |
|
|
745 | } |
|
|
746 | |
|
|
747 | =item $handle->keepalive ($boolean) |
|
|
748 | |
|
|
749 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
750 | the same name for details). |
|
|
751 | |
|
|
752 | =cut |
|
|
753 | |
|
|
754 | sub keepalive { |
|
|
755 | $_[0]{keepalive} = $_[1]; |
|
|
756 | |
|
|
757 | eval { |
|
|
758 | local $SIG{__DIE__}; |
|
|
759 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
760 | if $_[0]{fh}; |
760 | if $_[0]{fh}; |
761 | }; |
761 | }; |
762 | } |
762 | } |
763 | |
763 | |
764 | =item $handle->on_starttls ($cb) |
764 | =item $handle->on_starttls ($cb) |
… | |
… | |
956 | $self->{on_drain}($self) |
956 | $self->{on_drain}($self) |
957 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
957 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
958 | && $self->{on_drain}; |
958 | && $self->{on_drain}; |
959 | |
959 | |
960 | delete $self->{_ww} unless length $self->{wbuf}; |
960 | delete $self->{_ww} unless length $self->{wbuf}; |
961 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
961 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
962 | $self->_error ($!, 1); |
962 | $self->_error ($!, 1); |
963 | } |
963 | } |
964 | }; |
964 | }; |
965 | |
965 | |
966 | # try to write data immediately |
966 | # try to write data immediately |
… | |
… | |
1054 | |
1054 | |
1055 | Encodes the given hash or array reference into a JSON object. Unless you |
1055 | Encodes the given hash or array reference into a JSON object. Unless you |
1056 | provide your own JSON object, this means it will be encoded to JSON text |
1056 | provide your own JSON object, this means it will be encoded to JSON text |
1057 | in UTF-8. |
1057 | in UTF-8. |
1058 | |
1058 | |
|
|
1059 | The default encoder might or might not handle every type of JSON value - |
|
|
1060 | it might be limited to arrays and objects for security reasons. See the |
|
|
1061 | C<json> constructor attribute for more details. |
|
|
1062 | |
1059 | JSON objects (and arrays) are self-delimiting, so you can write JSON at |
1063 | JSON objects (and arrays) are self-delimiting, so if you only use arrays |
1060 | one end of a handle and read them at the other end without using any |
1064 | and hashes, you can write JSON at one end of a handle and read them at the |
1061 | additional framing. |
1065 | other end without using any additional framing. |
1062 | |
1066 | |
1063 | The generated JSON text is guaranteed not to contain any newlines: While |
1067 | The JSON text generated by the default encoder is guaranteed not to |
1064 | this module doesn't need delimiters after or between JSON texts to be |
1068 | contain any newlines: While this module doesn't need delimiters after or |
1065 | able to read them, many other languages depend on that. |
1069 | between JSON texts to be able to read them, many other languages depend on |
|
|
1070 | them. |
1066 | |
1071 | |
1067 | A simple RPC protocol that interoperates easily with other languages is |
1072 | A simple RPC protocol that interoperates easily with other languages is |
1068 | to send JSON arrays (or objects, although arrays are usually the better |
1073 | to send JSON arrays (or objects, although arrays are usually the better |
1069 | choice as they mimic how function argument passing works) and a newline |
1074 | choice as they mimic how function argument passing works) and a newline |
1070 | after each JSON text: |
1075 | after each JSON text: |
… | |
… | |
1550 | }; |
1555 | }; |
1551 | |
1556 | |
1552 | =item regex => $accept[, $reject[, $skip], $cb->($handle, $data) |
1557 | =item regex => $accept[, $reject[, $skip], $cb->($handle, $data) |
1553 | |
1558 | |
1554 | Makes a regex match against the regex object C<$accept> and returns |
1559 | Makes a regex match against the regex object C<$accept> and returns |
1555 | everything up to and including the match. |
1560 | everything up to and including the match. All the usual regex variables |
|
|
1561 | ($1, %+ etc.) from the regex match are available in the callback. |
1556 | |
1562 | |
1557 | Example: read a single line terminated by '\n'. |
1563 | Example: read a single line terminated by '\n'. |
1558 | |
1564 | |
1559 | $handle->push_read (regex => qr<\n>, sub { ... }); |
1565 | $handle->push_read (regex => qr<\n>, sub { ... }); |
1560 | |
1566 | |
… | |
… | |
1872 | accomodate protocol changes. |
1878 | accomodate protocol changes. |
1873 | |
1879 | |
1874 | This read type does not rely on L<AnyEvent::TLS> (and thus, not on |
1880 | This read type does not rely on L<AnyEvent::TLS> (and thus, not on |
1875 | L<Net::SSLeay>). |
1881 | L<Net::SSLeay>). |
1876 | |
1882 | |
1877 | =item tls_autostart => $tls[, $tls_ctx] |
1883 | =item tls_autostart => [$tls_ctx, ]$tls |
1878 | |
1884 | |
1879 | Tries to detect a valid SSL or TLS handshake. If one is detected, it tries |
1885 | Tries to detect a valid SSL or TLS handshake. If one is detected, it tries |
1880 | to start tls by calling C<starttls> with the given arguments. |
1886 | to start tls by calling C<starttls> with the given arguments. |
1881 | |
1887 | |
1882 | In practise, C<$tls> must be C<accept>, or a Net::SSLeay context that has |
1888 | In practise, C<$tls> must be C<accept>, or a Net::SSLeay context that has |
… | |
… | |
1886 | See C<tls_detect> above for more details. |
1892 | See C<tls_detect> above for more details. |
1887 | |
1893 | |
1888 | Example: give the client a chance to start TLS before accepting a text |
1894 | Example: give the client a chance to start TLS before accepting a text |
1889 | line. |
1895 | line. |
1890 | |
1896 | |
1891 | $hdl->push_read (tls_detect => "accept"); |
1897 | $hdl->push_read (tls_autostart => "accept"); |
1892 | $hdl->push_read (line => sub { |
1898 | $hdl->push_read (line => sub { |
1893 | print "received ", ($_[0]{tls} ? "encrypted" : "cleartext"), " <$_[1]>\n"; |
1899 | print "received ", ($_[0]{tls} ? "encrypted" : "cleartext"), " <$_[1]>\n"; |
1894 | }); |
1900 | }); |
1895 | |
1901 | |
1896 | =cut |
1902 | =cut |
… | |
… | |
2018 | } elsif (defined $len) { |
2024 | } elsif (defined $len) { |
2019 | delete $self->{_rw}; |
2025 | delete $self->{_rw}; |
2020 | $self->{_eof} = 1; |
2026 | $self->{_eof} = 1; |
2021 | $self->_drain_rbuf; |
2027 | $self->_drain_rbuf; |
2022 | |
2028 | |
2023 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
2029 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
2024 | return $self->_error ($!, 1); |
2030 | return $self->_error ($!, 1); |
2025 | } |
2031 | } |
2026 | }; |
2032 | }; |
2027 | } |
2033 | } |
2028 | } |
2034 | } |
… | |
… | |
2187 | $self->{tls} = $tls = $self->{tls_ctx}->_get_session ($tls, $self, $self->{peername}); |
2193 | $self->{tls} = $tls = $self->{tls_ctx}->_get_session ($tls, $self, $self->{peername}); |
2188 | |
2194 | |
2189 | # basically, this is deep magic (because SSL_read should have the same issues) |
2195 | # basically, this is deep magic (because SSL_read should have the same issues) |
2190 | # but the openssl maintainers basically said: "trust us, it just works". |
2196 | # but the openssl maintainers basically said: "trust us, it just works". |
2191 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
2197 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
2192 | # and mismaintained ssleay-module doesn't even offer them). |
2198 | # and mismaintained ssleay-module didn't offer them for a decade or so). |
2193 | # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html |
2199 | # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html |
2194 | # |
2200 | # |
2195 | # in short: this is a mess. |
2201 | # in short: this is a mess. |
2196 | # |
2202 | # |
2197 | # note that we do not try to keep the length constant between writes as we are required to do. |
2203 | # note that we do not try to keep the length constant between writes as we are required to do. |
2198 | # we assume that most (but not all) of this insanity only applies to non-blocking cases, |
2204 | # we assume that most (but not all) of this insanity only applies to non-blocking cases, |
2199 | # and we drive openssl fully in blocking mode here. Or maybe we don't - openssl seems to |
2205 | # and we drive openssl fully in blocking mode here. Or maybe we don't - openssl seems to |
2200 | # have identity issues in that area. |
2206 | # have identity issues in that area. |
2201 | # Net::SSLeay::CTX_set_mode ($ssl, |
2207 | # Net::SSLeay::set_mode ($ssl, |
2202 | # (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
2208 | # (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
2203 | # | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
2209 | # | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
2204 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
2210 | Net::SSLeay::set_mode ($tls, 1|2); |
2205 | |
2211 | |
2206 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
2212 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
2207 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
2213 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
2208 | |
2214 | |
2209 | Net::SSLeay::BIO_write ($self->{_rbio}, $self->{rbuf}); |
2215 | Net::SSLeay::BIO_write ($self->{_rbio}, $self->{rbuf}); |
… | |
… | |
2284 | push @linger, AE::io $fh, 1, sub { |
2290 | push @linger, AE::io $fh, 1, sub { |
2285 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2291 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2286 | |
2292 | |
2287 | if ($len > 0) { |
2293 | if ($len > 0) { |
2288 | substr $wbuf, 0, $len, ""; |
2294 | substr $wbuf, 0, $len, ""; |
2289 | } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK)) { |
2295 | } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK)) { |
2290 | @linger = (); # end |
2296 | @linger = (); # end |
2291 | } |
2297 | } |
2292 | }; |
2298 | }; |
2293 | push @linger, AE::timer $linger, 0, sub { |
2299 | push @linger, AE::timer $linger, 0, sub { |
2294 | @linger = (); |
2300 | @linger = (); |