HTTP::...lparser(3pm)
π HTTP::Proxy::BodyFilter::htmlparser β the manual, but make it cute π
okay bestie, so this page lives in the User Contributed Perl Documentation corner of the man-page universe, and it's called HTTP::Proxy::BodyFilter::htmlparser (the header shortens it to HTTP::...lparser(3pm) because man pages are dramatic and love an ellipsis β¨). buckle up, we're filtering HTML with style π
β¨ NAME β¨
HTTP::Proxy::BodyFilter::htmlparser β a filter that uses HTML::Parser, no cap π
πΈ SYNOPSIS πΈ
use HTTP::Proxy::BodyFilter::htmlparser;
# $parser is a HTML::Parser object
$proxy->push_filter(
mime => 'text/html',
response => HTTP::Proxy::BodyFilter::htmlparser->new( $parser );
);
π¦ DESCRIPTION π¦
so basically, HTTP::Proxy::BodyFilter::htmlparser lets you build a filter around whatever HTML::Parser object your heart desires π
the filter takes a HTML::Parser object as the argument to its constructor. and here's the vibe check: the filter is either read-only or read-write. a read-only filter is like, hands off β it will not let you change the data on the fly. but if you ask for a read-write filter, bestie, you're signing up to rewrite the entire response body. completely. all of it. no shortcuts π
with a read-write filter you must recreate the whole body data. why?? because the HTML::Parser has its own buffering system going on, and there's literally no easy way to match up the data that triggered an HTML::Parser event with its original position in the chunk that the origin server sent. it's giving chaos. see below for the details π
ngl, if all you wanna do is modify the HTML text (not the tags), there's a way easier route: HTTP::Proxy::BodyFilter::htmltext. just saying π«Ά
π§ Creating a HTML::Parser that rewrites pages π§
to declare a read-write filter, you pass rw => 1 to the constructor, like so:
HTTP::Proxy::BodyFilter::htmlparser->new( $parser, rw => 1 );
to actually be able to modify the body of a message, a filter made with HTTP::Proxy::BodyFilter::htmlparser has to rewrite it completely β periodt. the HTML::Parser object can update a special attribute named output. to do that, the HTML::Parser handler has to request the self attribute (translation: it needs access to the parser itself πͺ) and then update its output key.
here are the attributes this filter adds to the HTML::Parser object:
outputβ a string that will hold the data sent back by the proxy. π this string gets used as a replacement for the body data only if the filter is read-write, aka only if it was initialised withrw => 1. data should always be appended to$parser->{output}. always always. πmessageβ a reference to theHTTP::Messagethat triggered the filter. π¨protocolβ a reference to theHTTP::Protocolobject. π
π METHODS π
this filter defines three methods, and they're all called automatically β you don't have to poke them, they just show up like the main character β¨
filter()β thefilter()method handles all the interactions with theHTML::Parserobject. it's the social butterfly of the trio π¦init()β initialises the filter with theHTML::Parserobject passed to the constructor. the setup era πwill_modify()β returns a boolean value that tells the system whether it's gonna modify the data passing through. and the value? it's literally just the value of therwparameter you passed to the constructor. iconic, honestly π
π₯ SEE ALSO π₯
HTTP::Proxy, HTTP::Proxy::Bodyfilter, HTTP::Proxy::BodyFilter::htmltext.
π AUTHOR π
Philippe "BooK" Bruhat, <book@cpan.org>.
β COPYRIGHT β
Copyright 2003-2015, Philippe Bruhat.
π LICENSE π
This module is free software; you can redistribute it or modify it under the same terms as Perl itself.
and that's the whole page, bestie π«Ά this lil doc shipped as part of perl v5.36.0, dated 2022-12-04, so go forth and filter your HTML with main character energy πβ¨ππ¦
π¦ where this page comes from
- debian bookworm: libhttp-proxy-perl 0.304-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libhttp-proxy-perl 0.304-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libhttp-proxy-perl 0.304-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms