Advertisement
Guest User

start_tls patch for Mojo::IOLoop::Server

a guest
Jun 4th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.12 KB | None | 0 0
  1. @@ -22,8 +22,8 @@ use constant TLS_WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;
  2.  
  3.  # To regenerate the certificate run this command (18.04.2012)
  4.  # openssl req -new -x509 -keyout server.key -out server.crt -nodes -days 7300
  5. -my $CERT = catfile dirname(__FILE__), 'server.crt';
  6. -my $KEY  = catfile dirname(__FILE__), 'server.key';
  7. +our $CERT = catfile dirname(__FILE__), 'server.crt';
  8. +our $KEY  = catfile dirname(__FILE__), 'server.key';
  9.  
  10.  has multi_accept => 50;
  11.  has reactor      => sub {
  12. @@ -109,6 +109,23 @@ sub start {
  13.      $self->{handle} => sub { $self->_accept for 1 .. $self->multi_accept });
  14.  }
  15.  
  16. +sub start_tls {
  17. +  my ($self, $handle, $args) = @_;
  18. +  weaken $self;
  19. +  my %opts = (
  20. +    $self->{tls} ? %{$self->{tls}} : (),
  21. +    $args ? %$args : (),
  22. +  );
  23. +  $opts{SSL_error_trap} = sub {
  24. +    return unless my $handle = delete $self->{handles}{shift()};
  25. +    $self->reactor->remove($handle);
  26. +    close $handle;
  27. +  };
  28. +  return unless $handle = IO::Socket::SSL->start_SSL($handle, %opts);
  29. +  $self->reactor->io($handle => sub { $self->_tls($handle) });
  30. +  $self->{handles}{$handle} = $handle;
  31. +}
  32. +
  33.  sub stop { $_[0]->reactor->remove($_[0]{handle}) }
  34.  
  35.  sub _accept {
  36. @@ -121,16 +138,8 @@ sub _accept {
  37.    setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1;
  38.  
  39.    # Start TLS handshake
  40. -  return $self->emit_safe(accept => $handle) unless my $tls = $self->{tls};
  41. -  weaken $self;
  42. -  $tls->{SSL_error_trap} = sub {
  43. -    return unless my $handle = delete $self->{handles}{shift()};
  44. -    $self->reactor->remove($handle);
  45. -    close $handle;
  46. -  };
  47. -  return unless $handle = IO::Socket::SSL->start_SSL($handle, %$tls);
  48. -  $self->reactor->io($handle => sub { $self->_tls($handle) });
  49. -  $self->{handles}{$handle} = $handle;
  50. +  return $self->emit_safe(accept => $handle) unless $self->{tls};
  51. +  $self->start_tls($handle);
  52.  }
  53.  
  54.  sub _tls {
  55. @@ -273,6 +282,12 @@ Find a free TCP port, this is a utility function primarily used for tests.
  56.  
  57.  Start accepting connections.
  58.  
  59. +=head2 start_tls
  60. +
  61. +  $server->start_tls($handle);
  62. +
  63. +Perform TLS handshake for handle.
  64. +
  65.  =head2 stop
  66.  
  67.    $server->stop;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement