***** infoCopter.com/perl *****

Apache::HttpEquiv

Customizing the Fixup Phase

Apache::HttpEquiv, a module written by Rob Hartill.
Patched to run under mod_perl 2.0 (1.99) by me

package Apache::HttpEquiv;
use strict;

use Apache::Const
 -compile
 => qw(:common REDIRECT HTTP_NO_CONTENT DIR_MAGIC_TYPE HTTP_NOT_MODIFIED);

use Apache::RequestRec;
use APR::Table;

sub handler {
   my $r = shift;
   local(*FILE);

	open(FILE, $r->filename);

   while(<FILE>) {
	last if m!<BODY>|</HEAD>!i; # exit early if in BODY
	if (m/META HTTP-EQUIV="([^"]+)"\s+CONTENT="([^"]+)"/i) {
	   $r->headers_out->{$1} = $2;
	}
   }
   close(FILE);
   return Apache::OK;
}

1;

Test / Output

# telnet localhost 81
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /httpequiv/index.html HTTP/1.0
Host: www.example.com

HTTP/1.1 200 OK
Date: Mon, 05 Apr 2004 20:21:55 GMT
Server: Apache/2.0.48 (Unix) mod_perl/1.99_13 Perl/v5.6.1 mod_ssl/2.0.48 OpenSSL/0.9.7d
Expires: Wed, 31 Jul 1998 16:40:00 GMT
Set-Cookie: open=sesame
Last-Modified: Mon, 05 Apr 2004 20:19:18 GMT
ETag: "9eeaa-d2-a4d9dd80"
Accept-Ranges: bytes
Content-Length: 210
Connection: close
Content-Type: text/html; charset=ISO-8859-1

<HTML>
<HEAD>
<TITLE>My Page</TITLE>

<META HTTP-EQUIV="Expires" CONTENT="Wed, 31 Jul 1998 16:40:00 GMT">
<META HTTP-EQUIV="Set-Cookie" CONTENT="open=sesame">
</HEAD>
<body>
</body>
</HTML>
Connection closed by foreign host.


Tip

  • To set a custom header My-Header you should call:
    use Apache::RequestRec ();
    use APR::Table;
    $r->headers_out->set(My-Header => "SomeValue");
    $r->headers_out->add('Set-Cookie' => $_); # Use add to set one key multiple times
    
  • Update mod_perl 1.0 header_out statements to mod_perl 2.0:
    perl -pi -e 's|header_out\((.*?)\s*=>\s*(.*?)\);|headers_out->{$1} = $2;|g' HttpEquiv.pm
© reto :)