Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Last active September 8, 2015 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandfoy/a20c4259f81cbb4cb38d to your computer and use it in GitHub Desktop.
Save briandfoy/a20c4259f81cbb4cb38d to your computer and use it in GitHub Desktop.
Mojo::UserAgent post-request processing to add meta charset
#!/Users/brian/bin/perls/perl5.22.0
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]' ) # HTML 5
->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}" );
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment