TOD has asked for the wisdom of the Perl Monks concerning the following question:
Rather simple, quick and clean. ;)package Foo::FloodBlocker; use strict; use warnings; use Apache2::Connection; use Apache2::Const -compile => qw/FORBIDDEN OK/; use IPC::Shareable ':all'; use constant { FILTERGLUE => 'PIFt', FILTERSHMEM => 1048576 }; sub handler { my $c = shift; return Apache2::Const::OK if $c->keepalives; my $ip = $c->remote_ip; my $time = time; no warnings 'untie'; my ($shmem, %hash); $shmem = tie %hash, 'IPC::Shareable', FILTERGLUE, { create => 0, mode => 0666, size => FILTERSHMEM, exclusive => 0, destroy => 0 }; $ip =~ s/\./_/g; my $block; $shmem->shlock(LOCK_EX); if (exists $hash{$ip}) { if ($hash{$ip} < $time) { delete $hash{$ip}; } else { $block = 1; } } else { $hash{$ip} = $time; } $shmem->shunlock; untie %hash; undef $shmem; return Apache2::Const::FORBIDDEN if $block; Apache2::Const::OK; }
use APR::Const -compile => qw/SUCCESS/; use APR::Brigade; use APR::Bucket; use APR::Status; use APR::Error; [...] sub handler { [...] my $bb_in = APR::Brigade->new($c->pool, $c->bucket_alloc); my $bb_out = APR::Brigade->new($c->pool, $c->bucket_alloc); while (1) { my $rc = $c->input_filters->get_brigade($bb_in, Apache2::Const::MODE_GETLINE); last if APR::Status::is_EOF($rc); die APR::Error::strerror($rc) unless $rc == APR::Const::SUCCESS; my $last = 0; while (!$bb_in->is_empty) { my $b = $bb_in->first; $b->remove; if ($b->is_eos) { $bb_out->insert_tail($b); $last++; last; } if ($b->read(my $data)) { warn $data; # testing line } $bb_out->insert_tail($b); } if ($last) { $bb_out->insert_tail(APR::Bucket::flush_create($c->bucket_all +oc)); $c->output_filters->pass_brigade($bb_out); last; } } [...]
Edit: g0n - readmore tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Apache2 Perl(Pre|Process)ConnectionHandler
by perrin (Chancellor) on May 26, 2006 at 18:18 UTC | |
by TOD (Friar) on May 27, 2006 at 03:22 UTC | |
by perrin (Chancellor) on May 27, 2006 at 04:35 UTC | |
by TOD (Friar) on May 27, 2006 at 04:55 UTC |