Last active
September 8, 2015 17:45
-
-
Save briandfoy/a20c4259f81cbb4cb38d to your computer and use it in GitHub Desktop.
Mojo::UserAgent post-request processing to add meta charset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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