ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.235 by root, Tue May 8 19:41:22 2012 UTC vs.
Revision 1.238 by root, Tue Dec 10 15:54:51 2013 UTC

496callback. 496callback.
497 497
498This callback will only be called on TLS shutdowns, not when the 498This callback will only be called on TLS shutdowns, not when the
499underlying handle signals EOF. 499underlying 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
503This is the json coder object used by the C<json> read and write types. 503This is the json coder object used by the C<json> read and write types.
504 504
505If you don't supply it, then AnyEvent::Handle will create and use a 505If you don't supply it, then AnyEvent::Handle will create and use a
506suitable one (on demand), which will write and expect UTF-8 encoded JSON 506suitable one (on demand), which will write and expect UTF-8 encoded JSON
507texts. 507texts.
508 508
509Note that you are responsible to depend on the JSON module if you want to 509Note that you are responsible to depend on the L<JSON> module if you want
510use this functionality, as AnyEvent does not have a dependency itself. 510to use this functionality, as AnyEvent does not have a dependency on it
511itself.
512
513=item cbor => L<CBOR::XS> object
514
515This is the cbor coder object used by the C<cbor> read and write types.
516
517If you don't supply it, then AnyEvent::Handle will create and use a
518suitable one (on demand), which will write CBOR without using extensions,
519if possible. texts.
520
521Note that you are responsible to depend on the L<CBOR::XS> module if you
522want to use this functionality, as AnyEvent does not have a dependency on
523it itself.
511 524
512=back 525=back
513 526
514=cut 527=cut
515 528
1053 1066
1054The generated JSON text is guaranteed not to contain any newlines: While 1067The generated JSON text is guaranteed not to contain any newlines: While
1055this module doesn't need delimiters after or between JSON texts to be 1068this module doesn't need delimiters after or between JSON texts to be
1056able to read them, many other languages depend on that. 1069able to read them, many other languages depend on that.
1057 1070
1058A simple RPC protocol that interoperates easily with others is to send 1071A simple RPC protocol that interoperates easily with other languages is
1059JSON arrays (or objects, although arrays are usually the better choice as 1072to send JSON arrays (or objects, although arrays are usually the better
1060they mimic how function argument passing works) and a newline after each 1073choice as they mimic how function argument passing works) and a newline
1061JSON text: 1074after 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
1066An AnyEvent::Handle receiver would simply use the C<json> read type and 1079An 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
1071Other languages could read single lines terminated by a newline and pass 1084Other languages could read single lines terminated by a newline and pass
1072this line into their JSON decoder of choice. 1085this line into their JSON decoder of choice.
1073 1086
1087=item cbor => $perl_scalar
1088
1089Encodes the given scalar into a CBOR value. Unless you provide your own
1090L<CBOR::XS> object, this means it will be encoded to a CBOR string not
1091using any extensions, if possible.
1092
1093CBOR values are self-delimiting, so you can write CBOR at one end of
1094a handle and read them at the other end without using any additional
1095framing.
1096
1097A simple nd very very fast RPC protocol that interoperates with
1098other languages is to send CBOR and receive CBOR values (arrays are
1099recommended):
1100
1101 $handle->push_write (cbor => ["method", "arg1", "arg2"]); # whatever
1102
1103An 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
1076sub json_coder() { 1109sub 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
1081register_write_type json => sub { 1114register_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
1121sub cbor_coder() {
1122 require CBOR::XS;
1123 CBOR::XS->new
1124}
1125
1126register_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
1091Freezes the given reference using L<Storable> and writes it to the 1135Freezes the given reference using L<Storable> and writes it to the
1485 1529
1486register_read_type line => sub { 1530register_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
1763Reads a CBOR value, decodes it and passes it to the callback. When a parse
1764error occurs, an C<EBADMSG> error will be raised.
1765
1766If a L<CBOR::XS> object was passed to the constructor, then that will be
1767used for the final decode, otherwise it will create a CBOR coder without
1768enabling any options.
1769
1770You have to provide a dependency to L<CBOR::XS> on your own: this module
1771will load the L<CBOR::XS> module, but AnyEvent does not depend on it
1772itself.
1773
1774Since CBOR values are fully self-delimiting, the C<cbor> read and write
1775types are an ideal simple RPC protocol: just exchange CBOR datagrams. See
1776the C<cbor> write type description, above, for an actual example.
1777
1778=cut
1779
1780register_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
1853Checks the input stream for a valid SSL or TLS handshake TLSPaintext
1854record without consuming anything. Only SSL version 3 or higher
1855is handled, up to the fictituous protocol 4.x (but both SSL3+ and
1856SSL2-compatible framing is supported).
1857
1858If it detects that the input data is likely TLS, it calls the callback
1859with a true value for C<$detect> and the (on-wire) TLS version as second
1860and third argument (C<$major> is C<3>, and C<$minor> is 0..3 for SSL
18613.0, TLS 1.0, 1.1 and 1.2, respectively). If it detects the input to
1862be definitely not TLS, it calls the callback with a false value for
1863C<$detect>.
1864
1865The callback could use this information to decide whether or not to start
1866TLS negotiation.
1867
1868In all cases the data read so far is passed to the following read
1869handlers.
1870
1871Usually you want to use the C<tls_autostart> read type instead.
1872
1873If you want to design a protocol that works in the presence of TLS
1874dtection, 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
1876read type does are a bit more strict, but might losen in the future to
1877accomodate protocol changes.
1878
1879This read type does not rely on L<AnyEvent::TLS> (and thus, not on
1880L<Net::SSLeay>).
1881
1882=item tls_autostart => $tls[, $tls_ctx]
1883
1884Tries to detect a valid SSL or TLS handshake. If one is detected, it tries
1885to start tls by calling C<starttls> with the given arguments.
1886
1887In practise, C<$tls> must be C<accept>, or a Net::SSLeay context that has
1888been configured to accept, as servers do not normally send a handshake on
1889their own and ths cannot be detected in this way.
1890
1891See C<tls_detect> above for more details.
1892
1893Example: give the client a chance to start TLS before accepting a text
1894line.
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
1903register_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
1928register_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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines