How to create an Apache module – Where to start?

Apache logo
Recently I came across an issue that required me to modify the incoming requests to the web server before it was handled by the application behind it. Since the module created by the web application was blocking all OPTIONS requests and only allowing GET and POST requests, I had to find a way to make it so that the module believes that those requests were actually a GET.

Where to start?

The main issue with Apache modules, is that there doesn’t seem to be that much help available through Google when you have a specific problem. Only general solutions that try to help you on your way, which is fine off course… but that does require one to dig deep and experiment quite a lot.
I ran across some books like The Apache Modules Book, it contains a lot of useful information. But since I was on the clock I really didn’t have the time available to me to read it all. 
I mean, reading 600 pages on a budget is not the way to go…

Development by example

The obvious way to go for me, since I like to work like that myself: find a full example and just go with that! And luckily I found an answer really quickly: a complete module containing (almost) all the different possibilities available, and with working code! That module is called “mod_example“. No real surprises there right?
Using this file as your base, you can basically create anything if you know C or like me refresh your C skills a bit. Since everything is in there you can start to write your own code, remove the things you don’t need and adapt what is in there to your own needs.

Molding it to your image

Now that I had the example module, I started to adapt the code to what I wanted to achieve ( changing the method of the request). So I started to look at all the different functions that were available and finally ended up working with ap_hook_fixups. This is basically the last chance you get to modify a request before it is sent off to generate content.
This is the perfect spot to make some adaptions to the request.
It also has a positioning system allowing you to determine when the module needs to fire:
  • APR_HOOK_FIRST: First in the sequence
  • APR_HOOK_MIDDLE: In the middle of the sequence
  • APR_HOOK_LAST: Last in the sequence
Since I had to run it before the other modules I decided to use APR_HOOK_FIRST and ended up with the following code:
  ap_hook_fixups(method_fixer_upper, NULL, NULL,  APR_HOOK_FIRST);

When I looked at that method I saw that I had an object available called request_rec, which had the attribute method. Now that I knew where the data was that I needed to change, all I had to do was change it to something else:
request->method = “WHATEVER METHOD I WANT”;

This is in a nutshell how I ended up getting my module to work. I made it possible to use a directive in the httpd config. 

How I did all this would take a bit too long to explain, but I made the source code available to you so you can see for yourself and maybe experiment with it.

You can get it here!

Hope this helps you on your way to create your very own Apache Module!

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.