Manipulating HTTP requests through Apache Filters
I have been performing an architectural assessment and re-mediation of a large enterprise application for a Government of India Financial Services Firm. This is a huge system integration project consisting of 12+ applications, including Core Insurance, Financials, HRMS, CRM and others, integrated using a commercial SOA platform.
Apache Web servers are used as a proxy into the various application servers. We had implemented a structured logging mechanism and I had written a Python program that processes all the logs in various application servers to produce summarized data to understand performance and scalability issues.
One very critical feature we needed was to be able to print a particular XML element, that is part of the SOAP request in the Apache access logs. While it is easy to print the http headers in the Apache access log through the %{element-name} feature in httpd.conf, Apache does not have any native capability to parse XML and print an element in its access log.
I developed an Apache Input filter using its C-API. This filter applies its logic only when the requests are POST requests and the Content-Type is text/xml, which is always the case with SOAP requests. It then extracts a particular element from within the SOAP payload and injects a HTTP header into the request. This enables this element to be logged as part of the Apache access log.
Since there is no IP restriction on this program (being a simple utility to get some data as part of the Performance Testing environment), I have uploaded it in github (https://github.com/umakanthdiwakar/ApacheAuditFilter). The program will "mostly" work, with one small quirk to make it interesting. If anyone has a need for custom content manipulation in Apache, feel free to use it as a base. I will be glad to help you out if you get stuck.
Here are some simple steps to create a skeleton Apache module. Let us assume that apache is installed in /usr/local/apache2. Let us also assume that you want to create a filter called as myfilter.
$ cd /usr/local/apache2
$ bin/apxs -g -n myfilter
$ cd myfilter
$ vi mod_myfilter.c
Perform your edits of the file here. Please refer apache documentation to understand the purpose of each function in the skeleton.
$ ../bin/apxs -c -i mod_myfilter.c
This will compile and deploy your module as a shared library in the "modules" directory.
Now add the following line to httpd.conf
LoadModule module_myfilter modules/mod_myfilter.so
Nice !!