--- AnyEvent/lib/AnyEvent/Handle.pm 2008/04/28 08:01:05 1.5 +++ AnyEvent/lib/AnyEvent/Handle.pm 2008/05/24 15:11:22 1.23 @@ -1,23 +1,24 @@ package AnyEvent::Handle; -use warnings; +no warnings; use strict; -use AnyEvent; -use IO::Handle; +use AnyEvent (); +use AnyEvent::Util (); +use Scalar::Util (); +use Carp (); +use Fcntl (); use Errno qw/EAGAIN EINTR/; =head1 NAME -AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent +AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent -=head1 VERSION - -Version 0.01 +This module is experimental. =cut -our $VERSION = '0.01'; +our $VERSION = '0.04'; =head1 SYNOPSIS @@ -28,15 +29,7 @@ my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN); - $ae_fh->on_eof (sub { $cv->broadcast }); - - $ae_fh->readlines (sub { - my ($ae_fh, @lines) = @_; - for (@lines) { - chomp; - print "Line: $_"; - } - }); + #TODO # or use the constructor to pass the callback: @@ -46,23 +39,23 @@ on_eof => sub { $cv->broadcast; }, - on_readline => sub { - my ($ae_fh, @lines) = @_; - for (@lines) { - chomp; - print "Line: $_"; - } - } + #TODO ); $cv->wait; =head1 DESCRIPTION -This module is a helper module to make it easier to do non-blocking I/O -on filehandles (and sockets, see L). +This module is a helper module to make it easier to do event-based I/O on +filehandles. For utility functions for doing non-blocking connects and accepts +on sockets see L. + +In the following, when the documentation refers to of "bytes" then this +means characters. As sysread and syswrite are used for all I/O, their +treatment of characters applies to this module as well. -The event loop is provided by L. +All callbacks will be invoked with the handle object as their first +argument. =head1 METHODS @@ -70,304 +63,702 @@ =item B -The constructor has these arguments: +The constructor supports these arguments (all as key => value pairs). =over 4 -=item fh => $filehandle +=item fh => $filehandle [MANDATORY] The filehandle this L object will operate on. -NOTE: The filehandle will be set to non-blocking. +NOTE: The filehandle will be set to non-blocking (using +AnyEvent::Util::fh_nonblocking). + +=item on_eof => $cb->($self) + +Set the callback to be called on EOF. + +While not mandatory, it is highly recommended to set an eof callback, +otherwise you might end up with a closed socket while you are still +waiting for data. + +=item on_error => $cb->($self) + +This is the fatal error callback, that is called when, well, a fatal error +occurs, such as not being able to resolve the hostname, failure to connect +or a read error. + +The object will not be in a usable state when this callback has been +called. + +On callback entrance, the value of C<$!> contains the operating system +error (or C or C). + +While not mandatory, it is I recommended to set this callback, as +you will not be notified of errors otherwise. The default simply calls +die. + +=item on_read => $cb->($self) + +This sets the default read callback, which is called when data arrives +and no read request is in the queue. + +To access (and remove data from) the read buffer, use the C<< ->rbuf >> +method or access the C<$self->{rbuf}> member directly. + +When an EOF condition is detected then AnyEvent::Handle will first try to +feed all the remaining data to the queued callbacks and C before +calling the C callback. If no progress can be made, then a fatal +error will be raised (with C<$!> set to C). + +=item on_drain => $cb->() + +This sets the callback that is called when the write buffer becomes empty +(or when the callback is set and the buffer is empty already). + +To append to the write buffer, use the C<< ->push_write >> method. + +=item rbuf_max => + +If defined, then a fatal error will be raised (with C<$!> set to C) +when the read buffer ever (strictly) exceeds this size. This is useful to +avoid denial-of-service attacks. + +For example, a server accepting connections from untrusted sources should +be configured to accept only so-and-so much data that it cannot act on +(for example, when expecting a line, an attacker could send an unlimited +amount of data without a callback ever being called as long as the line +isn't finished). -=item read_block_size => $size +=item read_size => -The default read block size use for reads via the C -method. +The default read block size (the amount of bytes this module will try to read +on each [loop iteration). Default: C<4096>. -=item on_read => $cb +=item low_water_mark => -=item on_eof => $cb +Sets the amount of bytes (default: C<0>) that make up an "empty" write +buffer: If the write reaches this size or gets even samller it is +considered empty. -=item on_error => $cb +=item tls => "accept" | "connect" | Net::SSLeay::SSL object -These are shortcuts, that will call the corresponding method and set the callback to C<$cb>. +When this parameter is given, it enables TLS (SSL) mode, that means it +will start making tls handshake and will transparently encrypt/decrypt +data. -=item on_readline => $cb +For the TLS server side, use C, and for the TLS client side of a +connection, use C mode. -The C method is called with the default seperator and C<$cb> as callback -for you. +You can also provide your own TLS connection object, but you have +to make sure that you call either C +or C on it before you pass it to +AnyEvent::Handle. + +=item tls_ctx => $ssl_ctx + +Use the given Net::SSLeay::CTX object to create the new TLS connection +(unless a connection object was specified directly). If this parameter is +missing, then AnyEvent::Handle will use C. =back =cut sub new { - my $this = shift; - my $class = ref($this) || $this; - my $self = { - read_block_size => 4096, - rbuf => '', - @_ - }; - bless $self, $class; + my $class = shift; + + my $self = bless { @_ }, $class; + + $self->{fh} or Carp::croak "mandatory argument fh is missing"; + + AnyEvent::Util::fh_nonblocking $self->{fh}, 1; - $self->{fh}->blocking (0) if $self->{fh}; + if ($self->{tls}) { + require Net::SSLeay; + $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); + } + + $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; + $self->on_error (delete $self->{on_error}) if $self->{on_error}; + $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; + $self->on_read (delete $self->{on_read} ) if $self->{on_read}; + + $self->start_read; - if ($self->{on_read}) { - $self->on_read ($self->{on_read}); + $self +} - } elsif ($self->{on_readline}) { - $self->readlines ($self->{on_readline}); +sub _shutdown { + my ($self) = @_; - } elsif ($self->{on_eof}) { - $self->on_eof ($self->{on_eof}); + delete $self->{rw}; + delete $self->{ww}; + delete $self->{fh}; +} - } elsif ($self->{on_error}) { - $self->on_eof ($self->{on_error}); +sub error { + my ($self) = @_; + + { + local $!; + $self->_shutdown; } - return $self + if ($self->{on_error}) { + $self->{on_error}($self); + } else { + die "AnyEvent::Handle uncaught fatal error: $!"; + } } -=item B +=item $fh = $handle->fh -This method returns the filehandle of the L object. +This method returns the file handle of the L object. =cut sub fh { $_[0]->{fh} } -=item B +=item $handle->on_error ($cb) -This method installs a C<$callback> that will be called -when new data arrived. You can access the read buffer via the C -method (see below). +Replace the current C callback (see the C constructor argument). -The first argument of the C<$callback> will be the L object. +=cut + +sub on_error { + $_[0]{on_error} = $_[1]; +} + +=item $handle->on_eof ($cb) + +Replace the current C callback (see the C constructor argument). =cut -sub on_read { +sub on_eof { + $_[0]{on_eof} = $_[1]; +} + +############################################################################# + +=back + +=head2 WRITE QUEUE + +AnyEvent::Handle manages two queues per handle, one for writing and one +for reading. + +The write queue is very simple: you can add data to its end, and +AnyEvent::Handle will automatically try to get rid of it for you. + +When data could be written and the write buffer is shorter then the low +water mark, the C callback will be invoked. + +=over 4 + +=item $handle->on_drain ($cb) + +Sets the C callback or clears it (see the description of +C in the constructor). + +=cut + +sub on_drain { my ($self, $cb) = @_; - $self->{on_read} = $cb; - unless (defined $self->{on_read}) { - delete $self->{on_read_w}; - return; - } - - $self->{on_read_w} = - AnyEvent->io (poll => 'r', fh => $self->{fh}, cb => sub { - #d# warn "READ:[$self->{read_size}] $self->{read_block_size} : ".length ($self->{rbuf})."\n"; - my $rbuf_len = length $self->{rbuf}; - my $l; - if (defined $self->{read_size}) { - $l = sysread $self->{fh}, $self->{rbuf}, - ($self->{read_size} - $rbuf_len), $rbuf_len; - } else { - $l = sysread $self->{fh}, $self->{rbuf}, $self->{read_block_size}, $rbuf_len; - } - #d# warn "READL $l [$self->{rbuf}]\n"; + $self->{on_drain} = $cb; + + $cb->($self) + if $cb && $self->{low_water_mark} >= length $self->{wbuf}; +} + +=item $handle->push_write ($data) + +Queues the given scalar to be written. You can push as much data as you +want (only limited by the available memory), as C +buffers it independently of the kernel. + +=cut - if (not defined $l) { - return if $! == EAGAIN || $! == EINTR; - $self->{on_error}->($self) if $self->{on_error}; - delete $self->{on_read_w}; - - } elsif ($l == 0) { - $self->{on_eof}->($self) if $self->{on_eof}; - delete $self->{on_read_w}; +sub _drain_wbuf { + my ($self) = @_; - } else { - $self->{on_read}->($self); + unless ($self->{ww}) { + Scalar::Util::weaken $self; + my $cb = sub { + my $len = syswrite $self->{fh}, $self->{wbuf}; + + if ($len > 0) { + substr $self->{wbuf}, 0, $len, ""; + + $self->{on_drain}($self) + if $self->{low_water_mark} >= length $self->{wbuf} + && $self->{on_drain}; + + delete $self->{ww} unless length $self->{wbuf}; + } elsif ($! != EAGAIN && $! != EINTR) { + $self->error; } - }); + }; + + $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); + + $cb->($self); + }; +} + +sub push_write { + my $self = shift; + + if ($self->{filter_w}) { + $self->{filter_w}->($self, \$_[0]); + } else { + $self->{wbuf} .= $_[0]; + $self->_drain_wbuf; + } } -=item B +############################################################################# -Whenever a read or write operation resulted in an error the C<$callback> -will be called. +=back + +=head2 READ QUEUE -The first argument of C<$callback> will be the L object itself. -The error is given as errno in C<$!>. +AnyEvent::Handle manages two queues per handle, one for writing and one +for reading. + +The read queue is more complex than the write queue. It can be used in two +ways, the "simple" way, using only C and the "complex" way, using +a queue. + +In the simple case, you just install an C callback and whenever +new data arrives, it will be called. You can then remove some data (if +enough is there) from the read buffer (C<< $handle->rbuf >>) if you want +or not. + +In the more complex case, you want to queue multiple callbacks. In this +case, AnyEvent::Handle will call the first queued callback each time new +data arrives and removes it when it has done its job (see C, +below). + +This way you can, for example, push three line-reads, followed by reading +a chunk of data, and AnyEvent::Handle will execute them in order. + +Example 1: EPP protocol parser. EPP sends 4 byte length info, followed by +the specified number of bytes which give an XML datagram. + + # in the default state, expect some header bytes + $handle->on_read (sub { + # some data is here, now queue the length-header-read (4 octets) + shift->unshift_read_chunk (4, sub { + # header arrived, decode + my $len = unpack "N", $_[1]; + + # now read the payload + shift->unshift_read_chunk ($len, sub { + my $xml = $_[1]; + # handle xml + }); + }); + }); + +Example 2: Implement a client for a protocol that replies either with +"OK" and another line or "ERROR" for one request, and 64 bytes for the +second request. Due tot he availability of a full queue, we can just +pipeline sending both requests and manipulate the queue as necessary in +the callbacks: + + # request one + $handle->push_write ("request 1\015\012"); + + # we expect "ERROR" or "OK" as response, so push a line read + $handle->push_read_line (sub { + # if we got an "OK", we have to _prepend_ another line, + # so it will be read before the second request reads its 64 bytes + # which are already in the queue when this callback is called + # we don't do this in case we got an error + if ($_[1] eq "OK") { + $_[0]->unshift_read_line (sub { + my $response = $_[1]; + ... + }); + } + }); + + # request two + $handle->push_write ("request 2\015\012"); + + # simply read 64 bytes, always + $handle->push_read_chunk (64, sub { + my $response = $_[1]; + ... + }); + +=over 4 =cut -sub on_error { - $_[0]->{on_error} = $_[1]; +sub _drain_rbuf { + my ($self) = @_; + + if ( + defined $self->{rbuf_max} + && $self->{rbuf_max} < length $self->{rbuf} + ) { + $! = &Errno::ENOSPC; return $self->error; + } + + return if $self->{in_drain}; + local $self->{in_drain} = 1; + + while (my $len = length $self->{rbuf}) { + no strict 'refs'; + if (my $cb = shift @{ $self->{queue} }) { + if (!$cb->($self)) { + if ($self->{eof}) { + # no progress can be made (not enough data and no data forthcoming) + $! = &Errno::EPIPE; return $self->error; + } + + unshift @{ $self->{queue} }, $cb; + return; + } + } elsif ($self->{on_read}) { + $self->{on_read}($self); + + if ( + $self->{eof} # if no further data will arrive + && $len == length $self->{rbuf} # and no data has been consumed + && !@{ $self->{queue} } # and the queue is still empty + && $self->{on_read} # and we still want to read data + ) { + # then no progress can be made + $! = &Errno::EPIPE; return $self->error; + } + } else { + # read side becomes idle + delete $self->{rw}; + return; + } + } + + if ($self->{eof}) { + $self->_shutdown; + $self->{on_eof}($self) + if $self->{on_eof}; + } } -=item B +=item $handle->on_read ($cb) -Installs the C<$callback> that will be called when the end of file is -encountered in a read operation this C<$callback> will be called. The first -argument will be the L object itself. +This replaces the currently set C callback, or clears it (when +the new callback is C). See the description of C in the +constructor. =cut -sub on_eof { - $_[0]->{on_eof} = $_[1]; +sub on_read { + my ($self, $cb) = @_; + + $self->{on_read} = $cb; } -=item B +=item $handle->rbuf + +Returns the read buffer (as a modifiable lvalue). -Returns a reference to the read buffer. +You can access the read buffer directly as the C<< ->{rbuf} >> member, if +you want. -NOTE: The read buffer should only be used or modified if the C -method is used directly. The C and C methods will provide -the read data to their callbacks. +NOTE: The read buffer should only be used or modified if the C, +C or C methods are used. The other read methods +automatically manage the read buffer. =cut sub rbuf : lvalue { - $_[0]->{rbuf} + $_[0]{rbuf} } -=item B +=item $handle->push_read ($cb) + +=item $handle->unshift_read ($cb) + +Append the given callback to the end of the queue (C) or +prepend it (C). + +The callback is called each time some additional read data arrives. -Will read exactly C<$len> bytes from the filehandle and call the C<$callback> -if done so. The first argument to the C<$callback> will be the L -object itself and the second argument the read data. +It must check whether enough data is in the read buffer already. -NOTE: This method will override any callbacks installed via the C method. +If not enough data is available, it must return the empty list or a false +value, in which case it will be called repeatedly until enough data is +available (or an error condition is detected). + +If enough data was available, then the callback must remove all data it is +interested in (which can be none at all) and return a true value. After returning +true, it will be removed from the queue. =cut -sub read { - my ($self, $len, $cb) = @_; +sub push_read { + my ($self, $cb) = @_; - $self->{read_cb} = $cb; - my $old_blk_size = $self->{read_block_size}; - $self->{read_block_size} = $len; - - $self->on_read (sub { - #d# warn "OFOFO $len || ".length($_[0]->{rbuf})."||\n"; - - if ($len == length $_[0]->{rbuf}) { - $_[0]->{read_block_size} = $old_blk_size; - $_[0]->on_read (undef); - $_[0]->{read_cb}->($_[0], (substr $self->{rbuf}, 0, $len, '')); - } - }); + push @{ $self->{queue} }, $cb; + $self->_drain_rbuf; } -=item B +sub unshift_read { + my ($self, $cb) = @_; + + push @{ $self->{queue} }, $cb; + $self->_drain_rbuf; +} -=item B +=item $handle->push_read_chunk ($len, $cb->($self, $data)) -This method will read lines from the filehandle, seperated by C<$sep> or C<"\n"> -if C<$sep> is not provided. C<$sep> will be used as "line" seperator. +=item $handle->unshift_read_chunk ($len, $cb->($self, $data)) -The C<$callback> will be called when at least one -line could be read. The first argument to the C<$callback> will be the L -object itself and the rest of the arguments will be the read lines. +Append the given callback to the end of the queue (C) or +prepend it (C). -NOTE: This method will override any callbacks installed via the C method. +The callback will be called only once C<$len> bytes have been read, and +these C<$len> bytes will be passed to the callback. =cut -sub readlines { - my ($self, $sep, $cb) = @_; - - if (ref $sep) { - $cb = $sep; - $sep = "\n"; +sub _read_chunk($$) { + my ($self, $len, $cb) = @_; - } elsif (not defined $sep) { - $sep = "\n"; + sub { + $len <= length $_[0]{rbuf} or return; + $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); + 1 } +} - my $sep_len = length $sep; +sub push_read_chunk { + $_[0]->push_read (&_read_chunk); +} - $self->{on_readline} = $cb; - $self->on_read (sub { - my @lines; - my $rb = \$_[0]->{rbuf}; - my $pos; - while (($pos = index ($$rb, $sep)) >= 0) { - push @lines, substr $$rb, 0, $pos + $sep_len, ''; - } - $self->{on_readline}->($_[0], @lines); - }); +sub unshift_read_chunk { + $_[0]->unshift_read (&_read_chunk); +} + +=item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol)) + +=item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol)) + +Append the given callback to the end of the queue (C) or +prepend it (C). + +The callback will be called only once a full line (including the end of +line marker, C<$eol>) has been read. This line (excluding the end of line +marker) will be passed to the callback as second argument (C<$line>), and +the end of line marker as the third argument (C<$eol>). + +The end of line marker, C<$eol>, can be either a string, in which case it +will be interpreted as a fixed record end marker, or it can be a regex +object (e.g. created by C), in which case it is interpreted as a +regular expression. + +The end of line marker argument C<$eol> is optional, if it is missing (NOT +undef), then C is used (which is good for most internet +protocols). + +Partial lines at the end of the stream will never be returned, as they are +not marked by the end of line marker. + +=cut + +sub _read_line($$) { + my $self = shift; + my $cb = pop; + my $eol = @_ ? shift : qr|(\015?\012)|; + my $pos; + + $eol = quotemeta $eol unless ref $eol; + $eol = qr|^(.*?)($eol)|s; + + sub { + $_[0]{rbuf} =~ s/$eol// or return; + + $cb->($_[0], $1, $2); + 1 + } } -=item B +sub push_read_line { + $_[0]->push_read (&_read_line); +} + +sub unshift_read_line { + $_[0]->unshift_read (&_read_line); +} -=item B +=item $handle->stop_read -=item B +=item $handle->start_read -This method will write C<$data> to the filehandle and call the C<$callback> -afterwards. If only C<$callback> is provided it will be called when the -write buffer becomes empty the next time (or immediately if it already is empty). +In rare cases you actually do not want to read anything from the +socket. In this case you can call C. Neither C no +any queued callbacks will be executed then. To start reading again, call +C. =cut -sub write { - my ($self, $data, $cb) = @_; - if (ref $data) { $cb = $data; undef $data } - push @{$self->{write_bufs}}, [$data, $cb]; - $self->_check_writer; +sub stop_read { + my ($self) = @_; + + delete $self->{rw}; } -sub _check_writer { +sub start_read { my ($self) = @_; - if ($self->{write_w}) { - unless ($self->{write_cb}) { - while (@{$self->{write_bufs}} && not defined $self->{write_bufs}->[0]->[1]) { - my $wba = shift @{$self->{write_bufs}}; - $self->{wbuf} .= $wba->[0]; + unless ($self->{rw} || $self->{eof}) { + Scalar::Util::weaken $self; + + $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { + my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; + my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; + + if ($len > 0) { + $self->{filter_r} + ? $self->{filter_r}->($self, $rbuf) + : $self->_drain_rbuf; + + } elsif (defined $len) { + delete $self->{rw}; + $self->{eof} = 1; + $self->_drain_rbuf; + + } elsif ($! != EAGAIN && $! != EINTR) { + return $self->error; } + }); + } +} + +sub _dotls { + my ($self) = @_; + + if (length $self->{tls_wbuf}) { + while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf})) > 0) { + substr $self->{tls_wbuf}, 0, $len, ""; } - return; } - my $wba = shift @{$self->{write_bufs}} - or return; + if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { + $self->{wbuf} .= $buf; + $self->_drain_wbuf; + } + + while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { + $self->{rbuf} .= $buf; + $self->_drain_rbuf; + } + + if ( + (my $err = Net::SSLeay::get_error ($self->{tls}, -1)) + != Net::SSLeay::ERROR_WANT_READ () + ) { + if ($err == Net::SSLeay::ERROR_SYSCALL ()) { + $self->error; + } elsif ($err == Net::SSLeay::ERROR_SSL ()) { + $! = &Errno::EIO; + $self->error; + } + + # all others are fine for our purposes + } +} - unless (defined $wba->[0]) { - $wba->[1]->($self) if $wba->[1]; - $self->_check_writer; - return; +# TODO: maybe document... +sub starttls { + my ($self, $ssl, $ctx) = @_; + + if ($ssl eq "accept") { + $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); + Net::SSLeay::set_accept_state ($ssl); + } elsif ($ssl eq "connect") { + $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); + Net::SSLeay::set_connect_state ($ssl); } - $self->{wbuf} = $wba->[0]; - $self->{write_cb} = $wba->[1]; + $self->{tls} = $ssl; + + # basically, this is deep magic (because SSL_read should have the same issues) + # but the openssl maintainers basically said: "trust us, it just works". + # (unfortunately, we have to hardcode constants because the abysmally misdesigned + # and mismaintained ssleay-module doesn't even offer them). + Net::SSLeay::CTX_set_mode ($self->{tls}, + (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) + | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); + + $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); + $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); + + Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); + + $self->{filter_w} = sub { + $_[0]{tls_wbuf} .= ${$_[1]}; + &_dotls; + }; + $self->{filter_r} = sub { + Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); + &_dotls; + }; +} - $self->{write_w} = - AnyEvent->io (poll => 'w', fh => $self->{fh}, cb => sub { - my $l = syswrite $self->{fh}, $self->{wbuf}, length $self->{wbuf}; +sub DESTROY { + my $self = shift; - if (not defined $l) { - return if $! == EAGAIN || $! == EINTR; - delete $self->{write_w}; - $self->{on_error}->($self) if $self->{on_error}; + Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; +} - } else { - substr $self->{wbuf}, 0, $l, ''; +=item AnyEvent::Handle::TLS_CTX - if (length ($self->{wbuf}) == 0) { - $self->{write_cb}->($self) if $self->{write_cb}; +This function creates and returns the Net::SSLeay::CTX object used by +default for TLS mode. - delete $self->{write_w}; - delete $self->{wbuf}; - delete $self->{write_cb}; +The context is created like this: - $self->_check_writer; - } - } - }); + Net::SSLeay::load_error_strings; + Net::SSLeay::SSLeay_add_ssl_algorithms; + Net::SSLeay::randomize; + + my $CTX = Net::SSLeay::CTX_new; + + Net::SSLeay::CTX_set_options $CTX, Net::SSLeay::OP_ALL + +=cut + +our $TLS_CTX; + +sub TLS_CTX() { + $TLS_CTX || do { + require Net::SSLeay; + + Net::SSLeay::load_error_strings (); + Net::SSLeay::SSLeay_add_ssl_algorithms (); + Net::SSLeay::randomize (); + + $TLS_CTX = Net::SSLeay::CTX_new (); + + Net::SSLeay::CTX_set_options ($TLS_CTX, Net::SSLeay::OP_ALL ()); + + $TLS_CTX + } } =back =head1 AUTHOR -Robin Redeker, C<< >> +Robin Redeker C<< >>, Marc Lehmann . =cut