I need to play with some misconfigured web servers, so I started to process HTML meta tags to feed the information back to the agent web user object. I tried various ways to do this in Mojolicious and decided to find the "finish" event in the response. My goal was to make this mostly invisible to the rest of the code, so the process didn’t even know that this was happening.
However, it just doesn’t sit right with me for some reason that I cannot rely on. Apart from the specific code in process_meta_options, is there a more moholic way to do this? For example, a Mojo :: UserAgent get () with a custom callback uses an event read, but I tend to think that this may interfere with the work. Or I could just change my mind.
use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);
use Data::Dumper;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $tx = $ua->build_tx( GET => 'http://blogs.perl.org' );
$tx->res->on(
finish => \&process_meta_options
);
$tx = $ua->start( $tx );
say "At end, charset is ", $tx->res->content->charset;
sub process_meta_options ( $res ) {
$res
->dom
->find( 'head meta[charset]' )
->map( sub {
my $content_type = $res->headers->header( 'Content-type' );
return unless my $meta_charset = $_->{charset};
$content_type =~ s/;.*//;
$res->headers->header( 'Content-type', "$content_type; charset=$_->{charset}" );
} );
}
source
share