The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Apache2::Filter::HTTPHeadersFixup - Manipulate Apache 2 HTTP Headers

Synopsis

  # MyApache/FixupInputHTTPHeaders.pm
  package MyApache::FixupInputHTTPHeaders;
  
  use strict;
  use warnings FATAL => 'all';
  
  use base qw(Apache2::Filter::HTTPHeadersFixup);
  
  sub manip {
      my ($class, $ra_headers) = @_;
  
      # modify a header
      for (@$ra_headers) {
          s/^(Foo).*/$1: Moahaha/;
      }
  
      # push header (don't forget "\n"!)
      push @$ra_headers, "Bar: MidBar\n";
  }
  1;

  # httpd.conf
  <VirtualHost Zoot>
      PerlModule MyApache::FixupInputHTTPHeaders
      PerlInputFilterHandler MyApache::FixupInputHTTPHeaders
  </VirtualHost>

  # similar for output headers

Description

Apache2::Filter::HTTPHeadersFixup is a super class which provides an easy way to manipulate HTTP headers without invoking any mod_perl HTTP handlers. This is accomplished by using input and/or output connection filters.

It supports KeepAlive connections.

This class cannot be used as is. It has to be sub-classed. Read on.

Usage

A new class inheriting from Apache2::Filter::HTTPHeadersFixup needs to be created. That class needs to include a single function manip(). This function is invoked with two arguments, the package it was invoked from and a reference to an array of headers, each terminated with a new line.

That function can manipulate the values in that array. It shouldn't return anything. That means you can't assign to the reference itself or the headers will be lost.

Now you can modify, add or remove headers.

The function works identically for input and output HTTP headers.

See the Synopsis section for an example. More examples can be seen in the test suite.

Debug

Apache2::Filter::HTTPHeadersFixup includes internal tracing calls, which make it easy to debug the parsing of the headers.

First change the constant DEBUG to 1 in Apache2::Filter::HTTPHeadersFixup. Then enable Apache-Test debug tracing. For example to run a test with tracing enabled do:

  % t/TEST -trace=debug -v manip/out_append

Or you can set the APACHE_TEST_TRACE_LEVEL environment variable to debug at the server startup:

  APACHE_TEST_TRACE_LEVEL=debug apachectl start

All the tracing goes into error_log.

Bugs

See Also

Apache2, mod_perl, Apache2::Filter

Author

Philip M. Gollucci <pgollucci@p6m7g8.com>

Previously developed by Stas Bekman.

Copyright

The Apache2::Filter::HTTPHeadersFixup module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.