| 1 |
package POE::Filter::SSL::PreFilter; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use POE qw (Filter::HTTPD Filter::Stackable Wheel::ReadWrite); |
| 6 |
use POE; |
| 7 |
|
| 8 |
our $globalinfos = {}; |
| 9 |
|
| 10 |
BEGIN { |
| 11 |
our $PATCH = 18; |
| 12 |
no warnings 'redefine'; |
| 13 |
my $old_set_input_filter = \&POE::Wheel::ReadWrite::set_input_filter; |
| 14 |
*POE::Wheel::ReadWrite::set_input_filter = sub { |
| 15 |
my $self = shift; |
| 16 |
my $new_filter = shift; |
| 17 |
my $unique_id = $self->[POE::Wheel::ReadWrite::UNIQUE_ID()]; |
| 18 |
my $ret = $old_set_input_filter->($self, $new_filter, @_); |
| 19 |
$poe_kernel->yield(ref($self) . "($unique_id) -> ssl patch" => "input" => $self); |
| 20 |
return $ret; |
| 21 |
}; |
| 22 |
my $old_set_output_filter = \&POE::Wheel::ReadWrite::set_output_filter; |
| 23 |
*POE::Wheel::ReadWrite::set_output_filter = sub { |
| 24 |
my $self = shift; |
| 25 |
my $new_filter = shift; |
| 26 |
my $unique_id = $self->[POE::Wheel::ReadWrite::UNIQUE_ID()]; |
| 27 |
my $ret = $old_set_output_filter->($self, $new_filter, @_); |
| 28 |
$poe_kernel->yield(ref($self) . "($unique_id) -> ssl patch" => "output" => $self); |
| 29 |
return $ret; |
| 30 |
}; |
| 31 |
my $old_new = \&POE::Wheel::ReadWrite::new; |
| 32 |
*POE::Wheel::ReadWrite::new = sub { |
| 33 |
my $class = shift; |
| 34 |
my %arg = @_; |
| 35 |
my $self = $old_new->($class,%arg); |
| 36 |
my $unique_id = $self->[POE::Wheel::ReadWrite::UNIQUE_ID]; |
| 37 |
$poe_kernel->state( |
| 38 |
$self->[$PATCH] = ref($self) . "($unique_id) -> ssl patch", |
| 39 |
sub { |
| 40 |
my $type = $_[ARG0]; |
| 41 |
my $self = $_[ARG1]; |
| 42 |
if ($_[HEAP]->{self}->{PreFilter}) { |
| 43 |
$_[HEAP]->{self}->{"PreFilter".ref($self).$self->[POE::Wheel::ReadWrite::UNIQUE_ID()]} = $_[HEAP]->{self}->{PreFilter}->clone() |
| 44 |
unless ($_[HEAP]->{self}->{"PreFilter".ref($self).$self->[POE::Wheel::ReadWrite::UNIQUE_ID()]}); |
| 45 |
if ($type eq "input") { |
| 46 |
$old_set_input_filter->($self, POE::Filter::Stackable->new( |
| 47 |
Filters => [ |
| 48 |
$_[HEAP]->{self}->{"PreFilter".ref($self).$self->[POE::Wheel::ReadWrite::UNIQUE_ID()]}, |
| 49 |
$self->get_input_filter() |
| 50 |
] |
| 51 |
)); |
| 52 |
} else { |
| 53 |
$old_set_output_filter->($self, POE::Filter::Stackable->new( |
| 54 |
Filters => [ |
| 55 |
$_[HEAP]->{self}->{"PreFilter".ref($self).$self->[POE::Wheel::ReadWrite::UNIQUE_ID()]}, |
| 56 |
$self->get_output_filter() |
| 57 |
] |
| 58 |
)); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
); |
| 63 |
$poe_kernel->yield(ref($self) . "($unique_id) -> ssl patch" => "input" => $self); |
| 64 |
$poe_kernel->yield(ref($self) . "($unique_id) -> ssl patch" => "output" => $self); |
| 65 |
return $self; |
| 66 |
}; |
| 67 |
my $old_set_filter = \&POE::Wheel::ReadWrite::set_filter; |
| 68 |
*POE::Wheel::ReadWrite::set_filter = sub { |
| 69 |
my $self = shift; |
| 70 |
my $new_filter = shift; |
| 71 |
my $unique_id = $self->[POE::Wheel::ReadWrite::UNIQUE_ID()]; |
| 72 |
my $ret = $old_set_filter->($self, $new_filter, @_); |
| 73 |
$poe_kernel->yield(ref($self) . "($unique_id) -> ssl patch" => "input" => $self); |
| 74 |
$poe_kernel->yield(ref($self) . "($unique_id) -> ssl patch" => "output" => $self); |
| 75 |
return $ret; |
| 76 |
}; |
| 77 |
my $old_destroy = \&POE::Wheel::ReadWrite::DESTROY; |
| 78 |
*POE::Wheel::ReadWrite::DESTROY = sub { |
| 79 |
my $self = shift; |
| 80 |
if ($self->[$PATCH]) { |
| 81 |
$poe_kernel->state($self->[$PATCH]); |
| 82 |
$self->[$PATCH] = undef; |
| 83 |
} |
| 84 |
return $old_destroy->($self, @_); |
| 85 |
}; |
| 86 |
*POE::Filter::HTTPD::get_pending = sub { |
| 87 |
return undef; |
| 88 |
}; |
| 89 |
use warnings 'redefine'; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
=head1 NAME |
| 94 |
|
| •95 |
POE::Filter::SSL::Prefilter - Allow one to add a PreFilter on Components using Wheels |
| 96 |
|
| 97 |
=head1 VERSION |
| 98 |
|
| 99 |
Version 0.39 |
| 100 |
|
| 101 |
=head1 DESCRIPTION |
| 102 |
|
| 103 |
This is an extension for some POE::Component::Server modules to use POE::Filters in front of their own used Filters |
| 104 |
|
| 105 |
=head1 SYNOPSIS |
| 106 |
|
| 107 |
By default filters like I<POE::Filter::SSL> can only be used if you specify the filters by |
| •108 |
manualy creating the POE::Wheel by our own. This allows this to be done if a component is doing this. |
| 109 |
|
| 110 |
=over 2 |
| 111 |
|
| 112 |
=item HTTPS-Server |
| 113 |
|
| 114 |
use POE::Filter::SSL; |
| 115 |
use POE::Component::Server::HTTP; |
| 116 |
use HTTP::Status; |
| 117 |
my $aliases = POE::Component::Server::HTTP->new( |
| 118 |
Port => 443, |
| 119 |
ContentHandler => { |
| 120 |
'/' => \&handler, |
| 121 |
'/dir/' => sub { return; }, |
| 122 |
'/file' => sub { return; } |
| 123 |
}, |
| 124 |
Headers => { Server => 'My Server' }, |
| 125 |
PreFilter => POE::Filter::SSL->new( |
| 126 |
crt => 'server.crt', |
| 127 |
key => 'server.key', |
| 128 |
cacrt => 'ca.crt' |
| 129 |
) |
| 130 |
); |
| 131 |
|
| 132 |
sub handler { |
| 133 |
my ($request, $response) = @_; |
| 134 |
$response->code(RC_OK); |
| 135 |
$response->content("Hi, you fetched ". $request->uri); |
| 136 |
return RC_OK; |
| 137 |
} |
| 138 |
|
| 139 |
POE::Kernel->run(); |
| 140 |
POE::Kernel->call($aliases->{httpd}, "shutdown"); |
| 141 |
# next line isn't really needed |
| 142 |
POE::Kernel->call($aliases->{tcp}, "shutdown"); |
| 143 |
|
| 144 |
=back |
| 145 |
|
| 146 |
=head1 COPYRIGHT & LICENSE |
| 147 |
|
| 148 |
Copyright 2010-2017 Markus Schraeder, CryptoMagic GmbH, all rights reserved. |
| 149 |
|
| 150 |
This program is free software; you can redistribute it and/or modify it |
| 151 |
under the same terms as Perl itself. |
| 152 |
|
| 153 |
=cut |
| 154 |
|
| 155 |
1; # End of POE::Filter::SSL::PreFilter |