| 1 | |||
| 2 | package Data::Decode::Encode::HTTP::Response::Parser; | ||
| 3 | use Moose; | ||
| 4 | use MooseX::NonMoose; | ||
| 5 | |||
| 6 | extends 'HTML::Parser'; | ||
| 7 | |||
| 8 | sub FOREIGNBUILDARGS { | ||
| 9 | my ($class, %args) = @_; | ||
| 10 | |||
| 11 | $args{api_version} ||= 3; | ||
| 12 | $args{start_h} ||= [ \&_parse_meta, "self, tagname, attr" ]; | ||
| 13 | return %args; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub extract_encodings { | ||
| 17 | my $self = shift; | ||
| 18 | |||
| 19 | my @encodings; | ||
| 20 | $self->utf8_mode(1); | ||
| 21 | $self->{encodings} = \@encodings; | ||
| 22 | $self->parse($_[0]); | ||
| 23 | $self->eof; | ||
| 24 | delete $self->{encodings}; | ||
| 25 | return wantarray ? @encodings : \@encodings; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub _parse_meta | ||
| 29 | { | ||
| 30 | my ($self, $tag, $attrs) = @_; | ||
| 31 | return unless $tag eq 'meta'; | ||
| 32 | return unless $attrs->{'http-equiv'} && | ||
| 33 | lc($attrs->{'http-equiv'}) eq 'content-type'; | ||
| 34 | |||
| 35 | my $content = $attrs->{content}; | ||
| 36 | if (defined $content && $content =~ /charset=([A-Za-z0-9_\-]+)/i) { | ||
| 37 | push @{ $self->{encodings} }, $1; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | __END__ |