Apache2-HttpEquiv
view release on metacpan or search on metacpan
Apache2::HttpEquiv version 1.00, released January 4, 2014
This module provides a PerlFixupHandler for mod_perl 2 that turns
<meta http-equiv="Header-Name" content="Header Value"> into an actual
HTTP header. It also looks for <meta charset="..."> and uses it to
set the Content-Type.
INSTALLATION
To install this module, run the following commands:
perl Makefile.PL
make
lib/Apache2/HttpEquiv.pm view on Meta::CPAN
my ($p, $token, $header) = HTML::PullParser->new(
file => $file,
start => 'tag, attr',
end => 'tag',
);
my $content_type;
while ($token = $p->get_token) {
if ($token->[0] eq 'meta') {
if ($header = $token->[1]{'charset'} and not defined $content_type) {
$content_type = "text/html; charset=$header";
} # end if <meta charset=...>
elsif ($header = $token->[1]{'http-equiv'}) {
if ($header eq 'Content-Type' and not defined $content_type) {
$content_type = $token->[1]{content};
# text/xhtml is not a valid content type:
$content_type =~ s!^text/xhtml(?=\s|;|\z)!text/html!i;
} else {
$r->headers_out->set($header => $token->[1]{content});
}
} # end elsif <meta http-equiv=...>
} # end if <meta> tag
lib/Apache2/HttpEquiv.pm view on Meta::CPAN
In your Apache config:
<Location />
PerlFixupHandler Apache2::HttpEquiv
</Location>
=head1 DESCRIPTION
Apache2::HttpEquiv provides a PerlFixupHandler for mod_perl 2 that turns
C<< <meta http-equiv="Header-Name" content="Header Value"> >> into an actual
HTTP header. It also looks for C<< <meta charset="..."> >> and uses it to
set the Content-Type to C<text/html; charset=...>.
If the file claims its Content-Type is 'text/xhtml', the Content-Type
is set to 'text/html' instead. 'text/xhtml' is not a valid
Content-Type, and any file that claims it is probably too broken to
parse as 'application/xhtml+xml'.
This works only for static HTML files (that Apache has identified as
'text/html'). If you're generating dynamic content, you should be
generating the appropriate Content-Type and other headers at the same
time.
t/10-headers.t view on Meta::CPAN
$exResult = OK unless defined $exResult;
is(Apache2::HttpEquiv::handler($r), $exResult, "$name result");
is_deeply($r->_test_results, $expected, "$name headers");
} # end test
#---------------------------------------------------------------------
test('meta charset', {}, <<'END',
<html><head><meta charset="UTF-8"></head>
END
[ 'text/html; charset=UTF-8' ]);
test('meta charset and http-equiv', {}, <<'END',
<html><head><meta charset="UTF-8">
<meta http-equiv="New-Header" content="value">
</head><body></body></html>
END
[ 'text/html; charset=UTF-8',
[ 'New-Header' => 'value' ] ]);
test('http-equiv in body', {}, <<'END',
<html><head><meta charset="UTF-8"></head><body>
<meta http-equiv="New-Header" content="value">
</body></html>
END
[ 'text/html; charset=UTF-8' ]);
test('not initial request', {is_initial_req => 0}, <<'END',
<html><head><meta charset="UTF-8">
<meta http-equiv="New-Header" content="value">
</head><body></body></html>
END
[ 'text/html'], DECLINED);
test('not text/html', {content_type => 'text/plain'}, <<'END',
<html><head><meta charset="UTF-8">
<meta http-equiv="New-Header" content="value">
</head><body></body></html>
END
[ 'text/plain'], DECLINED);
test('multiple headers', {}, <<'END',
<html><head><meta charset="Windows-1252">
<meta http-equiv="New-Header" content="header 1">
<meta content="header 2" http-equiv="Another-Header">
</head><body></body></html>
END
[ 'text/html; charset=Windows-1252',
[ 'New-Header' => 'header 1' ],
[ 'Another-Header' => 'header 2' ] ]);
test('repeated header', {}, <<'END',
<html><head><meta charset="ISO-8859-1">
<meta http-equiv="New-Header" content="header 1">
<meta content="header 2" http-equiv="New-Header">
</head><body></body></html>
END
[ 'text/html; charset=ISO-8859-1',
[ 'New-Header' => 'header 1' ],
[ 'New-Header' => 'header 2' ] ]);
test('http-equiv content-type', {}, <<'END',
<html><head>
<meta http-equiv="New-Header" content="header 1">
<meta content="header 2" http-equiv="New-Header">
<meta http-equiv="Content-Type" content="text/html; charset='Windows-1252'">
</head><body></body></html>
END
[ "text/html; charset='Windows-1252'",
[ 'New-Header' => 'header 1' ],
[ 'New-Header' => 'header 2' ] ]);
test('claims text/xhtml', {}, <<'END',
<html><head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8">
</head><body></body></html>
END
[ "text/html; charset=UTF-8" ]);
{
my $r = Mock_Request->new(filename => 'does_not_exist');
is(Apache2::HttpEquiv::handler($r), DECLINED, 'missing file is not error');
is_deeply($r->_test_results, [ 'text/html' ], 'missing file no changes');
}
done_testing;
( run in 0.271 second using v1.01-cache-2.11-cpan-4d50c553e7e )