… | |
… | |
90 | |
90 | |
91 | The object will not be in a usable state when this callback has been |
91 | The object will not be in a usable state when this callback has been |
92 | called. |
92 | called. |
93 | |
93 | |
94 | On callback entrance, the value of C<$!> contains the operating system |
94 | On callback entrance, the value of C<$!> contains the operating system |
95 | error (or C<ENOSPC> or C<EPIPE>). |
95 | error (or C<ENOSPC>, C<EPIPE> or C<EBADMSG>). |
96 | |
96 | |
97 | While not mandatory, it is I<highly> recommended to set this callback, as |
97 | While not mandatory, it is I<highly> recommended to set this callback, as |
98 | you will not be notified of errors otherwise. The default simply calls |
98 | you will not be notified of errors otherwise. The default simply calls |
99 | die. |
99 | die. |
100 | |
100 | |
… | |
… | |
145 | |
145 | |
146 | When this parameter is given, it enables TLS (SSL) mode, that means it |
146 | When this parameter is given, it enables TLS (SSL) mode, that means it |
147 | will start making tls handshake and will transparently encrypt/decrypt |
147 | will start making tls handshake and will transparently encrypt/decrypt |
148 | data. |
148 | data. |
149 | |
149 | |
|
|
150 | TLS mode requires Net::SSLeay to be installed (it will be loaded |
|
|
151 | automatically when you try to create a TLS handle). |
|
|
152 | |
150 | For the TLS server side, use C<accept>, and for the TLS client side of a |
153 | For the TLS server side, use C<accept>, and for the TLS client side of a |
151 | connection, use C<connect> mode. |
154 | connection, use C<connect> mode. |
152 | |
155 | |
153 | You can also provide your own TLS connection object, but you have |
156 | You can also provide your own TLS connection object, but you have |
154 | to make sure that you call either C<Net::SSLeay::set_connect_state> |
157 | to make sure that you call either C<Net::SSLeay::set_connect_state> |
155 | or C<Net::SSLeay::set_accept_state> on it before you pass it to |
158 | or C<Net::SSLeay::set_accept_state> on it before you pass it to |
156 | AnyEvent::Handle. |
159 | AnyEvent::Handle. |
157 | |
160 | |
|
|
161 | See the C<starttls> method if you need to start TLs negotiation later. |
|
|
162 | |
158 | =item tls_ctx => $ssl_ctx |
163 | =item tls_ctx => $ssl_ctx |
159 | |
164 | |
160 | Use the given Net::SSLeay::CTX object to create the new TLS connection |
165 | Use the given Net::SSLeay::CTX object to create the new TLS connection |
161 | (unless a connection object was specified directly). If this parameter is |
166 | (unless a connection object was specified directly). If this parameter is |
162 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
167 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
163 | |
168 | |
164 | =back |
169 | =back |
165 | |
170 | |
166 | =cut |
171 | =cut |
|
|
172 | |
|
|
173 | our (%RH, %WH); |
|
|
174 | |
|
|
175 | sub register_read_type($$) { |
|
|
176 | $RH{$_[0]} = $_[1]; |
|
|
177 | } |
|
|
178 | |
|
|
179 | sub register_write_type($$) { |
|
|
180 | $WH{$_[0]} = $_[1]; |
|
|
181 | } |
167 | |
182 | |
168 | sub new { |
183 | sub new { |
169 | my $class = shift; |
184 | my $class = shift; |
170 | |
185 | |
171 | my $self = bless { @_ }, $class; |
186 | my $self = bless { @_ }, $class; |
… | |
… | |
206 | } |
221 | } |
207 | |
222 | |
208 | if ($self->{on_error}) { |
223 | if ($self->{on_error}) { |
209 | $self->{on_error}($self); |
224 | $self->{on_error}($self); |
210 | } else { |
225 | } else { |
211 | die "AnyEvent::Handle uncaught fatal error: $!"; |
226 | Carp::croak "AnyEvent::Handle uncaught fatal error: $!"; |
212 | } |
227 | } |
213 | } |
228 | } |
214 | |
229 | |
215 | =item $fh = $handle->fh |
230 | =item $fh = $handle->fh |
216 | |
231 | |
… | |
… | |
282 | =cut |
297 | =cut |
283 | |
298 | |
284 | sub _drain_wbuf { |
299 | sub _drain_wbuf { |
285 | my ($self) = @_; |
300 | my ($self) = @_; |
286 | |
301 | |
287 | unless ($self->{ww}) { |
302 | if (!$self->{ww} && length $self->{wbuf}) { |
288 | Scalar::Util::weaken $self; |
303 | Scalar::Util::weaken $self; |
289 | my $cb = sub { |
304 | my $cb = sub { |
290 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
305 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
291 | |
306 | |
292 | if ($len > 0) { |
307 | if ($len >= 0) { |
293 | substr $self->{wbuf}, 0, $len, ""; |
308 | substr $self->{wbuf}, 0, $len, ""; |
294 | |
309 | |
295 | $self->{on_drain}($self) |
310 | $self->{on_drain}($self) |
296 | if $self->{low_water_mark} >= length $self->{wbuf} |
311 | if $self->{low_water_mark} >= length $self->{wbuf} |
297 | && $self->{on_drain}; |
312 | && $self->{on_drain}; |
… | |
… | |
309 | } |
324 | } |
310 | |
325 | |
311 | sub push_write { |
326 | sub push_write { |
312 | my $self = shift; |
327 | my $self = shift; |
313 | |
328 | |
|
|
329 | if (@_ > 1) { |
|
|
330 | my $type = shift; |
|
|
331 | |
|
|
332 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
|
|
333 | ->($self, @_); |
|
|
334 | } |
|
|
335 | |
314 | if ($self->{filter_w}) { |
336 | if ($self->{filter_w}) { |
315 | $self->{filter_w}->($self, \$_[0]); |
337 | $self->{filter_w}->($self, \$_[0]); |
316 | } else { |
338 | } else { |
317 | $self->{wbuf} .= $_[0]; |
339 | $self->{wbuf} .= $_[0]; |
318 | $self->_drain_wbuf; |
340 | $self->_drain_wbuf; |
319 | } |
341 | } |
320 | } |
342 | } |
|
|
343 | |
|
|
344 | =item $handle->push_write (type => @args) |
|
|
345 | |
|
|
346 | =item $handle->unshift_write (type => @args) |
|
|
347 | |
|
|
348 | Instead of formatting your data yourself, you can also let this module do |
|
|
349 | the job by specifying a type and type-specific arguments. |
|
|
350 | |
|
|
351 | Predefined types are: |
|
|
352 | |
|
|
353 | =over 4 |
|
|
354 | |
|
|
355 | =item netstring => $string |
|
|
356 | |
|
|
357 | Formats the given value as netstring |
|
|
358 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
|
|
359 | |
|
|
360 | =cut |
|
|
361 | |
|
|
362 | register_write_type netstring => sub { |
|
|
363 | my ($self, $string) = @_; |
|
|
364 | |
|
|
365 | sprintf "%d:%s,", (length $string), $string |
|
|
366 | }; |
|
|
367 | |
|
|
368 | =back |
|
|
369 | |
|
|
370 | =cut |
|
|
371 | |
|
|
372 | |
321 | |
373 | |
322 | ############################################################################# |
374 | ############################################################################# |
323 | |
375 | |
324 | =back |
376 | =back |
325 | |
377 | |
… | |
… | |
413 | local $self->{in_drain} = 1; |
465 | local $self->{in_drain} = 1; |
414 | |
466 | |
415 | while (my $len = length $self->{rbuf}) { |
467 | while (my $len = length $self->{rbuf}) { |
416 | no strict 'refs'; |
468 | no strict 'refs'; |
417 | if (my $cb = shift @{ $self->{queue} }) { |
469 | if (my $cb = shift @{ $self->{queue} }) { |
418 | if (!$cb->($self)) { |
470 | unless ($cb->($self)) { |
419 | if ($self->{eof}) { |
471 | if ($self->{eof}) { |
420 | # no progress can be made (not enough data and no data forthcoming) |
472 | # no progress can be made (not enough data and no data forthcoming) |
421 | $! = &Errno::EPIPE; return $self->error; |
473 | $! = &Errno::EPIPE; return $self->error; |
422 | } |
474 | } |
423 | |
475 | |
… | |
… | |
501 | true, it will be removed from the queue. |
553 | true, it will be removed from the queue. |
502 | |
554 | |
503 | =cut |
555 | =cut |
504 | |
556 | |
505 | sub push_read { |
557 | sub push_read { |
506 | my ($self, $cb) = @_; |
558 | my $self = shift; |
|
|
559 | my $cb = pop; |
|
|
560 | |
|
|
561 | if (@_) { |
|
|
562 | my $type = shift; |
|
|
563 | |
|
|
564 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
|
|
565 | ->($self, $cb, @_); |
|
|
566 | } |
507 | |
567 | |
508 | push @{ $self->{queue} }, $cb; |
568 | push @{ $self->{queue} }, $cb; |
509 | $self->_drain_rbuf; |
569 | $self->_drain_rbuf; |
510 | } |
570 | } |
511 | |
571 | |
512 | sub unshift_read { |
572 | sub unshift_read { |
513 | my ($self, $cb) = @_; |
573 | my $self = shift; |
|
|
574 | my $cb = pop; |
514 | |
575 | |
|
|
576 | if (@_) { |
|
|
577 | my $type = shift; |
|
|
578 | |
|
|
579 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
|
|
580 | ->($self, $cb, @_); |
|
|
581 | } |
|
|
582 | |
|
|
583 | |
515 | push @{ $self->{queue} }, $cb; |
584 | unshift @{ $self->{queue} }, $cb; |
516 | $self->_drain_rbuf; |
585 | $self->_drain_rbuf; |
517 | } |
586 | } |
518 | |
587 | |
519 | =item $handle->push_read_chunk ($len, $cb->($self, $data)) |
588 | =item $handle->push_read (type => @args, $cb) |
520 | |
589 | |
521 | =item $handle->unshift_read_chunk ($len, $cb->($self, $data)) |
590 | =item $handle->unshift_read (type => @args, $cb) |
522 | |
591 | |
523 | Append the given callback to the end of the queue (C<push_read_chunk>) or |
592 | Instead of providing a callback that parses the data itself you can chose |
524 | prepend it (C<unshift_read_chunk>). |
593 | between a number of predefined parsing formats, for chunks of data, lines |
|
|
594 | etc. |
525 | |
595 | |
526 | The callback will be called only once C<$len> bytes have been read, and |
596 | The types currently supported are: |
527 | these C<$len> bytes will be passed to the callback. |
|
|
528 | |
597 | |
529 | =cut |
598 | =over 4 |
530 | |
599 | |
531 | sub _read_chunk($$) { |
600 | =item chunk => $octets, $cb->($self, $data) |
|
|
601 | |
|
|
602 | Invoke the callback only once C<$octets> bytes have been read. Pass the |
|
|
603 | data read to the callback. The callback will never be called with less |
|
|
604 | data. |
|
|
605 | |
|
|
606 | Example: read 2 bytes. |
|
|
607 | |
|
|
608 | $handle->push_read (chunk => 2, sub { |
|
|
609 | warn "yay ", unpack "H*", $_[1]; |
|
|
610 | }); |
|
|
611 | |
|
|
612 | =cut |
|
|
613 | |
|
|
614 | register_read_type chunk => sub { |
532 | my ($self, $len, $cb) = @_; |
615 | my ($self, $cb, $len) = @_; |
533 | |
616 | |
534 | sub { |
617 | sub { |
535 | $len <= length $_[0]{rbuf} or return; |
618 | $len <= length $_[0]{rbuf} or return; |
536 | $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); |
619 | $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); |
537 | 1 |
620 | 1 |
538 | } |
621 | } |
539 | } |
622 | }; |
540 | |
623 | |
|
|
624 | # compatibility with older API |
541 | sub push_read_chunk { |
625 | sub push_read_chunk { |
542 | $_[0]->push_read (&_read_chunk); |
626 | $_[0]->push_read (chunk => $_[1], $_[2]); |
543 | } |
627 | } |
544 | |
|
|
545 | |
628 | |
546 | sub unshift_read_chunk { |
629 | sub unshift_read_chunk { |
547 | $_[0]->unshift_read (&_read_chunk); |
630 | $_[0]->unshift_read (chunk => $_[1], $_[2]); |
548 | } |
631 | } |
549 | |
632 | |
550 | =item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol)) |
633 | =item line => [$eol, ]$cb->($self, $line, $eol) |
551 | |
|
|
552 | =item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol)) |
|
|
553 | |
|
|
554 | Append the given callback to the end of the queue (C<push_read_line>) or |
|
|
555 | prepend it (C<unshift_read_line>). |
|
|
556 | |
634 | |
557 | The callback will be called only once a full line (including the end of |
635 | The callback will be called only once a full line (including the end of |
558 | line marker, C<$eol>) has been read. This line (excluding the end of line |
636 | line marker, C<$eol>) has been read. This line (excluding the end of line |
559 | marker) will be passed to the callback as second argument (C<$line>), and |
637 | marker) will be passed to the callback as second argument (C<$line>), and |
560 | the end of line marker as the third argument (C<$eol>). |
638 | the end of line marker as the third argument (C<$eol>). |
… | |
… | |
571 | Partial lines at the end of the stream will never be returned, as they are |
649 | Partial lines at the end of the stream will never be returned, as they are |
572 | not marked by the end of line marker. |
650 | not marked by the end of line marker. |
573 | |
651 | |
574 | =cut |
652 | =cut |
575 | |
653 | |
576 | sub _read_line($$) { |
654 | register_read_type line => sub { |
577 | my $self = shift; |
655 | my ($self, $cb, $eol) = @_; |
578 | my $cb = pop; |
|
|
579 | my $eol = @_ ? shift : qr|(\015?\012)|; |
|
|
580 | my $pos; |
|
|
581 | |
656 | |
|
|
657 | $eol = qr|(\015?\012)| if @_ < 3; |
582 | $eol = quotemeta $eol unless ref $eol; |
658 | $eol = quotemeta $eol unless ref $eol; |
583 | $eol = qr|^(.*?)($eol)|s; |
659 | $eol = qr|^(.*?)($eol)|s; |
584 | |
660 | |
585 | sub { |
661 | sub { |
586 | $_[0]{rbuf} =~ s/$eol// or return; |
662 | $_[0]{rbuf} =~ s/$eol// or return; |
587 | |
663 | |
588 | $cb->($_[0], $1, $2); |
664 | $cb->($_[0], $1, $2); |
589 | 1 |
665 | 1 |
590 | } |
666 | } |
591 | } |
667 | }; |
592 | |
668 | |
|
|
669 | # compatibility with older API |
593 | sub push_read_line { |
670 | sub push_read_line { |
594 | $_[0]->push_read (&_read_line); |
671 | my $self = shift; |
|
|
672 | $self->push_read (line => @_); |
595 | } |
673 | } |
596 | |
674 | |
597 | sub unshift_read_line { |
675 | sub unshift_read_line { |
598 | $_[0]->unshift_read (&_read_line); |
676 | my $self = shift; |
|
|
677 | $self->unshift_read (line => @_); |
599 | } |
678 | } |
|
|
679 | |
|
|
680 | =item netstring => $cb->($string) |
|
|
681 | |
|
|
682 | A netstring (http://cr.yp.to/proto/netstrings.txt, this is not an endorsement). |
|
|
683 | |
|
|
684 | Throws an error with C<$!> set to EBADMSG on format violations. |
|
|
685 | |
|
|
686 | =cut |
|
|
687 | |
|
|
688 | register_read_type netstring => sub { |
|
|
689 | my ($self, $cb) = @_; |
|
|
690 | |
|
|
691 | sub { |
|
|
692 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
|
|
693 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
|
|
694 | $! = &Errno::EBADMSG; |
|
|
695 | $self->error; |
|
|
696 | } |
|
|
697 | return; |
|
|
698 | } |
|
|
699 | |
|
|
700 | my $len = $1; |
|
|
701 | |
|
|
702 | $self->unshift_read (chunk => $len, sub { |
|
|
703 | my $string = $_[1]; |
|
|
704 | $_[0]->unshift_read (chunk => 1, sub { |
|
|
705 | if ($_[1] eq ",") { |
|
|
706 | $cb->($_[0], $string); |
|
|
707 | } else { |
|
|
708 | $! = &Errno::EBADMSG; |
|
|
709 | $self->error; |
|
|
710 | } |
|
|
711 | }); |
|
|
712 | }); |
|
|
713 | |
|
|
714 | 1 |
|
|
715 | } |
|
|
716 | }; |
|
|
717 | |
|
|
718 | =back |
600 | |
719 | |
601 | =item $handle->stop_read |
720 | =item $handle->stop_read |
602 | |
721 | |
603 | =item $handle->start_read |
722 | =item $handle->start_read |
604 | |
723 | |
… | |
… | |
673 | |
792 | |
674 | # all others are fine for our purposes |
793 | # all others are fine for our purposes |
675 | } |
794 | } |
676 | } |
795 | } |
677 | |
796 | |
|
|
797 | =item $handle->starttls ($tls[, $tls_ctx]) |
|
|
798 | |
|
|
799 | Instead of starting TLS negotiation immediately when the AnyEvent::Handle |
|
|
800 | object is created, you can also do that at a later time by calling |
|
|
801 | C<starttls>. |
|
|
802 | |
|
|
803 | The first argument is the same as the C<tls> constructor argument (either |
|
|
804 | C<"connect">, C<"accept"> or an existing Net::SSLeay object). |
|
|
805 | |
|
|
806 | The second argument is the optional C<Net::SSLeay::CTX> object that is |
|
|
807 | used when AnyEvent::Handle has to create its own TLS connection object. |
|
|
808 | |
|
|
809 | =cut |
|
|
810 | |
678 | # TODO: maybe document... |
811 | # TODO: maybe document... |
679 | sub starttls { |
812 | sub starttls { |
680 | my ($self, $ssl, $ctx) = @_; |
813 | my ($self, $ssl, $ctx) = @_; |
|
|
814 | |
|
|
815 | $self->stoptls; |
681 | |
816 | |
682 | if ($ssl eq "accept") { |
817 | if ($ssl eq "accept") { |
683 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
818 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
684 | Net::SSLeay::set_accept_state ($ssl); |
819 | Net::SSLeay::set_accept_state ($ssl); |
685 | } elsif ($ssl eq "connect") { |
820 | } elsif ($ssl eq "connect") { |
… | |
… | |
691 | |
826 | |
692 | # basically, this is deep magic (because SSL_read should have the same issues) |
827 | # basically, this is deep magic (because SSL_read should have the same issues) |
693 | # but the openssl maintainers basically said: "trust us, it just works". |
828 | # but the openssl maintainers basically said: "trust us, it just works". |
694 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
829 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
695 | # and mismaintained ssleay-module doesn't even offer them). |
830 | # and mismaintained ssleay-module doesn't even offer them). |
|
|
831 | # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html |
696 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
832 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
697 | (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
833 | (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
698 | | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
834 | | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
699 | |
835 | |
700 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
836 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
… | |
… | |
710 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
846 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
711 | &_dotls; |
847 | &_dotls; |
712 | }; |
848 | }; |
713 | } |
849 | } |
714 | |
850 | |
|
|
851 | =item $handle->stoptls |
|
|
852 | |
|
|
853 | Destroys the SSL connection, if any. Partial read or write data will be |
|
|
854 | lost. |
|
|
855 | |
|
|
856 | =cut |
|
|
857 | |
|
|
858 | sub stoptls { |
|
|
859 | my ($self) = @_; |
|
|
860 | |
|
|
861 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
|
|
862 | delete $self->{tls_rbio}; |
|
|
863 | delete $self->{tls_wbio}; |
|
|
864 | delete $self->{tls_wbuf}; |
|
|
865 | delete $self->{filter_r}; |
|
|
866 | delete $self->{filter_w}; |
|
|
867 | } |
|
|
868 | |
715 | sub DESTROY { |
869 | sub DESTROY { |
716 | my $self = shift; |
870 | my $self = shift; |
717 | |
871 | |
718 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
872 | $self->stoptls; |
719 | } |
873 | } |
720 | |
874 | |
721 | =item AnyEvent::Handle::TLS_CTX |
875 | =item AnyEvent::Handle::TLS_CTX |
722 | |
876 | |
723 | This function creates and returns the Net::SSLeay::CTX object used by |
877 | This function creates and returns the Net::SSLeay::CTX object used by |