… | |
… | |
496 | callback. |
496 | callback. |
497 | |
497 | |
498 | This callback will only be called on TLS shutdowns, not when the |
498 | This callback will only be called on TLS shutdowns, not when the |
499 | underlying handle signals EOF. |
499 | underlying handle signals EOF. |
500 | |
500 | |
501 | =item json => JSON or JSON::XS object |
501 | =item json => L<JSON> or L<JSON::XS> object |
502 | |
502 | |
503 | This is the json coder object used by the C<json> read and write types. |
503 | This is the json coder object used by the C<json> read and write types. |
504 | |
504 | |
505 | If you don't supply it, then AnyEvent::Handle will create and use a |
505 | 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 |
506 | suitable one (on demand), which will write and expect UTF-8 encoded JSON |
507 | texts. |
507 | texts. |
508 | |
508 | |
509 | Note that you are responsible to depend on the JSON module if you want to |
509 | Note that you are responsible to depend on the L<JSON> module if you want |
510 | use this functionality, as AnyEvent does not have a dependency itself. |
510 | to use this functionality, as AnyEvent does not have a dependency on it |
|
|
511 | itself. |
|
|
512 | |
|
|
513 | =item cbor => L<CBOR::XS> object |
|
|
514 | |
|
|
515 | This is the cbor coder object used by the C<cbor> read and write types. |
|
|
516 | |
|
|
517 | If you don't supply it, then AnyEvent::Handle will create and use a |
|
|
518 | suitable one (on demand), which will write CBOR without using extensions, |
|
|
519 | if possible. texts. |
|
|
520 | |
|
|
521 | Note that you are responsible to depend on the L<CBOR::XS> module if you |
|
|
522 | want to use this functionality, as AnyEvent does not have a dependency on |
|
|
523 | it itself. |
511 | |
524 | |
512 | =back |
525 | =back |
513 | |
526 | |
514 | =cut |
527 | =cut |
515 | |
528 | |
… | |
… | |
1053 | |
1066 | |
1054 | The generated JSON text is guaranteed not to contain any newlines: While |
1067 | The generated JSON text is guaranteed not to contain any newlines: While |
1055 | this module doesn't need delimiters after or between JSON texts to be |
1068 | this module doesn't need delimiters after or between JSON texts to be |
1056 | able to read them, many other languages depend on that. |
1069 | able to read them, many other languages depend on that. |
1057 | |
1070 | |
1058 | A simple RPC protocol that interoperates easily with others is to send |
1071 | A simple RPC protocol that interoperates easily with other languages is |
1059 | JSON arrays (or objects, although arrays are usually the better choice as |
1072 | to send JSON arrays (or objects, although arrays are usually the better |
1060 | they mimic how function argument passing works) and a newline after each |
1073 | choice as they mimic how function argument passing works) and a newline |
1061 | JSON text: |
1074 | after each JSON text: |
1062 | |
1075 | |
1063 | $handle->push_write (json => ["method", "arg1", "arg2"]); # whatever |
1076 | $handle->push_write (json => ["method", "arg1", "arg2"]); # whatever |
1064 | $handle->push_write ("\012"); |
1077 | $handle->push_write ("\012"); |
1065 | |
1078 | |
1066 | An AnyEvent::Handle receiver would simply use the C<json> read type and |
1079 | An AnyEvent::Handle receiver would simply use the C<json> read type and |
… | |
… | |
1069 | $handle->push_read (json => sub { my $array = $_[1]; ... }); |
1082 | $handle->push_read (json => sub { my $array = $_[1]; ... }); |
1070 | |
1083 | |
1071 | Other languages could read single lines terminated by a newline and pass |
1084 | Other languages could read single lines terminated by a newline and pass |
1072 | this line into their JSON decoder of choice. |
1085 | this line into their JSON decoder of choice. |
1073 | |
1086 | |
|
|
1087 | =item cbor => $perl_scalar |
|
|
1088 | |
|
|
1089 | Encodes the given scalar into a CBOR value. Unless you provide your own |
|
|
1090 | L<CBOR::XS> object, this means it will be encoded to a CBOR string not |
|
|
1091 | using any extensions, if possible. |
|
|
1092 | |
|
|
1093 | CBOR values are self-delimiting, so you can write CBOR at one end of |
|
|
1094 | a handle and read them at the other end without using any additional |
|
|
1095 | framing. |
|
|
1096 | |
|
|
1097 | A simple nd very very fast RPC protocol that interoperates with |
|
|
1098 | other languages is to send CBOR and receive CBOR values (arrays are |
|
|
1099 | recommended): |
|
|
1100 | |
|
|
1101 | $handle->push_write (cbor => ["method", "arg1", "arg2"]); # whatever |
|
|
1102 | |
|
|
1103 | An AnyEvent::Handle receiver would simply use the C<cbor> read type: |
|
|
1104 | |
|
|
1105 | $handle->push_read (cbor => sub { my $array = $_[1]; ... }); |
|
|
1106 | |
1074 | =cut |
1107 | =cut |
1075 | |
1108 | |
1076 | sub json_coder() { |
1109 | sub json_coder() { |
1077 | eval { require JSON::XS; JSON::XS->new->utf8 } |
1110 | eval { require JSON::XS; JSON::XS->new->utf8 } |
1078 | || do { require JSON; JSON->new->utf8 } |
1111 | || do { require JSON; JSON->new->utf8 } |
1079 | } |
1112 | } |
1080 | |
1113 | |
1081 | register_write_type json => sub { |
1114 | register_write_type json => sub { |
1082 | my ($self, $ref) = @_; |
1115 | my ($self, $ref) = @_; |
1083 | |
1116 | |
1084 | my $json = $self->{json} ||= json_coder; |
1117 | ($self->{json} ||= json_coder) |
1085 | |
|
|
1086 | $json->encode ($ref) |
1118 | ->encode ($ref) |
|
|
1119 | }; |
|
|
1120 | |
|
|
1121 | sub cbor_coder() { |
|
|
1122 | require CBOR::XS; |
|
|
1123 | CBOR::XS->new |
|
|
1124 | } |
|
|
1125 | |
|
|
1126 | register_write_type cbor => sub { |
|
|
1127 | my ($self, $scalar) = @_; |
|
|
1128 | |
|
|
1129 | ($self->{cbor} ||= cbor_coder) |
|
|
1130 | ->encode ($scalar) |
1087 | }; |
1131 | }; |
1088 | |
1132 | |
1089 | =item storable => $reference |
1133 | =item storable => $reference |
1090 | |
1134 | |
1091 | Freezes the given reference using L<Storable> and writes it to the |
1135 | Freezes the given reference using L<Storable> and writes it to the |
… | |
… | |
1485 | |
1529 | |
1486 | register_read_type line => sub { |
1530 | register_read_type line => sub { |
1487 | my ($self, $cb, $eol) = @_; |
1531 | my ($self, $cb, $eol) = @_; |
1488 | |
1532 | |
1489 | if (@_ < 3) { |
1533 | if (@_ < 3) { |
1490 | # this is more than twice as fast as the generic code below |
1534 | # this is faster then the generic code below |
1491 | sub { |
1535 | sub { |
1492 | $_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return; |
1536 | (my $pos = index $_[0]{rbuf}, "\012") >= 0 |
|
|
1537 | or return; |
1493 | |
1538 | |
|
|
1539 | (my $str = substr $_[0]{rbuf}, 0, $pos + 1, "") =~ s/(\015?\012)\Z// or die; |
1494 | $cb->($_[0], "$1", "$2"); |
1540 | $cb->($_[0], $str, "$1"); |
1495 | 1 |
1541 | 1 |
1496 | } |
1542 | } |
1497 | } else { |
1543 | } else { |
1498 | $eol = quotemeta $eol unless ref $eol; |
1544 | $eol = quotemeta $eol unless ref $eol; |
1499 | $eol = qr|^(.*?)($eol)|s; |
1545 | $eol = qr|^(.*?)($eol)|s; |
… | |
… | |
1682 | my ($self, $cb) = @_; |
1728 | my ($self, $cb) = @_; |
1683 | |
1729 | |
1684 | my $json = $self->{json} ||= json_coder; |
1730 | my $json = $self->{json} ||= json_coder; |
1685 | |
1731 | |
1686 | my $data; |
1732 | my $data; |
1687 | my $rbuf = \$self->{rbuf}; |
|
|
1688 | |
1733 | |
1689 | sub { |
1734 | sub { |
1690 | my $ref = eval { $json->incr_parse ($_[0]{rbuf}) }; |
1735 | my $ref = eval { $json->incr_parse ($_[0]{rbuf}) }; |
1691 | |
1736 | |
1692 | if ($ref) { |
1737 | if ($ref) { |
… | |
… | |
1706 | |
1751 | |
1707 | () |
1752 | () |
1708 | } else { |
1753 | } else { |
1709 | $_[0]{rbuf} = ""; |
1754 | $_[0]{rbuf} = ""; |
1710 | |
1755 | |
|
|
1756 | () |
|
|
1757 | } |
|
|
1758 | } |
|
|
1759 | }; |
|
|
1760 | |
|
|
1761 | =item cbor => $cb->($handle, $scalar) |
|
|
1762 | |
|
|
1763 | Reads a CBOR value, decodes it and passes it to the callback. When a parse |
|
|
1764 | error occurs, an C<EBADMSG> error will be raised. |
|
|
1765 | |
|
|
1766 | If a L<CBOR::XS> object was passed to the constructor, then that will be |
|
|
1767 | used for the final decode, otherwise it will create a CBOR coder without |
|
|
1768 | enabling any options. |
|
|
1769 | |
|
|
1770 | You have to provide a dependency to L<CBOR::XS> on your own: this module |
|
|
1771 | will load the L<CBOR::XS> module, but AnyEvent does not depend on it |
|
|
1772 | itself. |
|
|
1773 | |
|
|
1774 | Since CBOR values are fully self-delimiting, the C<cbor> read and write |
|
|
1775 | types are an ideal simple RPC protocol: just exchange CBOR datagrams. See |
|
|
1776 | the C<cbor> write type description, above, for an actual example. |
|
|
1777 | |
|
|
1778 | =cut |
|
|
1779 | |
|
|
1780 | register_read_type cbor => sub { |
|
|
1781 | my ($self, $cb) = @_; |
|
|
1782 | |
|
|
1783 | my $cbor = $self->{cbor} ||= cbor_coder; |
|
|
1784 | |
|
|
1785 | my $data; |
|
|
1786 | |
|
|
1787 | sub { |
|
|
1788 | my (@value) = eval { $cbor->incr_parse ($_[0]{rbuf}) }; |
|
|
1789 | |
|
|
1790 | if (@value) { |
|
|
1791 | $cb->($_[0], @value); |
|
|
1792 | |
|
|
1793 | 1 |
|
|
1794 | } elsif ($@) { |
|
|
1795 | # error case |
|
|
1796 | $cbor->incr_reset; |
|
|
1797 | |
|
|
1798 | $_[0]->_error (Errno::EBADMSG); |
|
|
1799 | |
|
|
1800 | () |
|
|
1801 | } else { |
1711 | () |
1802 | () |
1712 | } |
1803 | } |
1713 | } |
1804 | } |
1714 | }; |
1805 | }; |
1715 | |
1806 | |
… | |
… | |
1753 | }); |
1844 | }); |
1754 | } |
1845 | } |
1755 | |
1846 | |
1756 | 1 |
1847 | 1 |
1757 | } |
1848 | } |
|
|
1849 | }; |
|
|
1850 | |
|
|
1851 | =item tls_detect => $cb->($handle, $detect, $major, $minor) |
|
|
1852 | |
|
|
1853 | Checks the input stream for a valid SSL or TLS handshake TLSPaintext |
|
|
1854 | record without consuming anything. Only SSL version 3 or higher |
|
|
1855 | is handled, up to the fictituous protocol 4.x (but both SSL3+ and |
|
|
1856 | SSL2-compatible framing is supported). |
|
|
1857 | |
|
|
1858 | If it detects that the input data is likely TLS, it calls the callback |
|
|
1859 | with a true value for C<$detect> and the (on-wire) TLS version as second |
|
|
1860 | and third argument (C<$major> is C<3>, and C<$minor> is 0..3 for SSL |
|
|
1861 | 3.0, TLS 1.0, 1.1 and 1.2, respectively). If it detects the input to |
|
|
1862 | be definitely not TLS, it calls the callback with a false value for |
|
|
1863 | C<$detect>. |
|
|
1864 | |
|
|
1865 | The callback could use this information to decide whether or not to start |
|
|
1866 | TLS negotiation. |
|
|
1867 | |
|
|
1868 | In all cases the data read so far is passed to the following read |
|
|
1869 | handlers. |
|
|
1870 | |
|
|
1871 | Usually you want to use the C<tls_autostart> read type instead. |
|
|
1872 | |
|
|
1873 | If you want to design a protocol that works in the presence of TLS |
|
|
1874 | dtection, make sure that any non-TLS data doesn't start with the octet 22 |
|
|
1875 | (ASCII SYN, 16 hex) or 128-255 (i.e. highest bit set). The checks this |
|
|
1876 | read type does are a bit more strict, but might losen in the future to |
|
|
1877 | accomodate protocol changes. |
|
|
1878 | |
|
|
1879 | This read type does not rely on L<AnyEvent::TLS> (and thus, not on |
|
|
1880 | L<Net::SSLeay>). |
|
|
1881 | |
|
|
1882 | =item tls_autostart => $tls[, $tls_ctx] |
|
|
1883 | |
|
|
1884 | Tries to detect a valid SSL or TLS handshake. If one is detected, it tries |
|
|
1885 | to start tls by calling C<starttls> with the given arguments. |
|
|
1886 | |
|
|
1887 | In practise, C<$tls> must be C<accept>, or a Net::SSLeay context that has |
|
|
1888 | been configured to accept, as servers do not normally send a handshake on |
|
|
1889 | their own and ths cannot be detected in this way. |
|
|
1890 | |
|
|
1891 | See C<tls_detect> above for more details. |
|
|
1892 | |
|
|
1893 | Example: give the client a chance to start TLS before accepting a text |
|
|
1894 | line. |
|
|
1895 | |
|
|
1896 | $hdl->push_read (tls_detect => "accept"); |
|
|
1897 | $hdl->push_read (line => sub { |
|
|
1898 | print "received ", ($_[0]{tls} ? "encrypted" : "cleartext"), " <$_[1]>\n"; |
|
|
1899 | }); |
|
|
1900 | |
|
|
1901 | =cut |
|
|
1902 | |
|
|
1903 | register_read_type tls_detect => sub { |
|
|
1904 | my ($self, $cb) = @_; |
|
|
1905 | |
|
|
1906 | sub { |
|
|
1907 | # this regex matches a full or partial tls record |
|
|
1908 | if ( |
|
|
1909 | # ssl3+: type(22=handshake) major(=3) minor(any) length_hi |
|
|
1910 | $self->{rbuf} =~ /^(?:\z| \x16 (\z| [\x03\x04] (?:\z| . (?:\z| [\x00-\x40] ))))/xs |
|
|
1911 | # ssl2 comapatible: len_hi len_lo type(1) major minor dummy(forlength) |
|
|
1912 | or $self->{rbuf} =~ /^(?:\z| [\x80-\xff] (?:\z| . (?:\z| \x01 (\z| [\x03\x04] (?:\z| . (?:\z| . ))))))/xs |
|
|
1913 | ) { |
|
|
1914 | return if 3 != length $1; # partial match, can't decide yet |
|
|
1915 | |
|
|
1916 | # full match, valid TLS record |
|
|
1917 | my ($major, $minor) = unpack "CC", $1; |
|
|
1918 | $cb->($self, "accept", $major + $minor * 0.1); |
|
|
1919 | } else { |
|
|
1920 | # mismatch == guaranteed not TLS |
|
|
1921 | $cb->($self, undef); |
|
|
1922 | } |
|
|
1923 | |
|
|
1924 | 1 |
|
|
1925 | } |
|
|
1926 | }; |
|
|
1927 | |
|
|
1928 | register_read_type tls_autostart => sub { |
|
|
1929 | my ($self, @tls) = @_; |
|
|
1930 | |
|
|
1931 | $RH{tls_detect}($self, sub { |
|
|
1932 | return unless $_[1]; |
|
|
1933 | $_[0]->starttls (@tls); |
|
|
1934 | }) |
1758 | }; |
1935 | }; |
1759 | |
1936 | |
1760 | =back |
1937 | =back |
1761 | |
1938 | |
1762 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
1939 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
… | |
… | |
2240 | handles requests until the server gets some QUIT command, causing it to |
2417 | handles requests until the server gets some QUIT command, causing it to |
2241 | close the connection first (highly desirable for a busy TCP server). A |
2418 | close the connection first (highly desirable for a busy TCP server). A |
2242 | client dropping the connection is an error, which means this variant can |
2419 | client dropping the connection is an error, which means this variant can |
2243 | detect an unexpected detection close. |
2420 | detect an unexpected detection close. |
2244 | |
2421 | |
2245 | To handle this case, always make sure you have a on-empty read queue, by |
2422 | To handle this case, always make sure you have a non-empty read queue, by |
2246 | pushing the "read request start" handler on it: |
2423 | pushing the "read request start" handler on it: |
2247 | |
2424 | |
2248 | # we assume a request starts with a single line |
2425 | # we assume a request starts with a single line |
2249 | my @start_request; @start_request = (line => sub { |
2426 | my @start_request; @start_request = (line => sub { |
2250 | my ($hdl, $line) = @_; |
2427 | my ($hdl, $line) = @_; |