29/38

Live example: dragongoserver (7)

    my $seen_title;
    $proxy->push_filter(
        host     => 'www.dragongoserver.net',
        path     => '^/status.php',
        response => HTTP::Proxy::BodyFilter::tags->new,   # protect tags
        response => HTTP::Proxy::BodyFilter::simple->new(
            start  => sub { $seen_title = 0; }, # initialise the filter
            filter => sub {
                 my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
                 # pass everything until <TITLE> (included)
                 if( !$seen_title ) {
                     if( $$dataref =~ /<TITLE>/ ) {
                         $seen_title++;
                         $$dataref =~ s/(.*<TITLE>)(.*)/$1/s;
                         $$buffer .= $2;
                     }
                     return;
                 }
                 # store the rest of the page
                 if( defined $buffer ) {
                     $$buffer = $$dataref;
                     $$dataref = '';
                 }
                 # finally, count the games and change the title
                 else {
                     my $n; $n++ while $$dataref =~ /game\.php\?gid=\d+/g;
                     my $s = $n > 1 ? "s" : ""; $n ||= "No";
                     $$dataref =~ s!.*</TITLE>!Go - $n game$s pending</TITLE>!s;
                 };
            },
        ),
    );