From 2f54bf7f33d60e83a16774d077c0b4df067d43fd Mon Sep 17 00:00:00 2001
From: Daniel Gruno
apxs -i -a -c mod_example.c
Every module starts with the same declaration, or name tag if you will,
that defines a module as a separate entity within Apache:
@@ -106,13 +107,17 @@ module AP_MODULE_DECLARE_DATA example_module =
This bit of code lets the server know that we have now registered a new module
in the system, and that its name is
example_module
. The name
of the module is used primarily for two things:
+
For now, we're only concerned with the first purpose of the module name,
-which comes into play when we need to load the module:
+which comes into play when we need to load the module:
+
LoadModule example_module modules/mod_example.so
In essence, this tells the server to open up mod_example.so
and look for a module
called example_module
.
mod_example
, so we'll add a configuration directive that tells
the server to do just that:
+
AddHandler example-handler .sum
What this tells the server is the following: Whenever we receive a request for a URI ending in .sum, we are to let all modules know that we are looking for whoever goes by the name of "example-handler" . @@ -693,17 +700,22 @@ advanced configurations for your modules.
+If you are reading this, then you probably already know
what a configuration directive is. Simply put, a directive is a way of
telling an individual module (or a set of modules) how to behave, such as
these directives control how mod_rewrite
works:
+
RewriteEngine On RewriteCond %{REQUEST_URI} ^/foo/bar RewriteRule ^/foo/bar/(.*)$ /foobar?page=$1
Each of these configuration directives are handled by a separate function, that parses the parameters given and sets up a configuration accordingly. +
As you can see, each directive needs at least 5 parameters set:
=
In our httpd.conf file, we can now change the hard-coded configuration by
adding a few lines:
+
ExampleEnabled On
ExamplePath "/usr/bin/foo"
ExampleAction file allow
+
And thus we apply the configuration, visit /example
on our
web site, and we see the configuration has adapted to what we wrote in our
configuration file.
@@ -1041,6 +1055,7 @@ In Apache HTTP Server 2.4, different URLs, virtual hosts, directories etc can ha
different meanings to the user of the server, and thus different contexts
within which modules must operate. For example, let's assume you have this
configuration set up for mod_rewrite:
+
<Directory "/var/www">
RewriteCond %{HTTP_HOST} ^example.com$
@@ -1050,6 +1065,7 @@ configuration set up for mod_rewrite:
RewriteRule ^foobar$ index.php?foobar=true
</Directory>
+
In this example, you will have set up two different contexts for
mod_rewrite:
@@ -1179,7 +1195,7 @@ module AP_MODULE_DECLARE_DATA example_module =
Now that we have told the server to help us create and manage configurations, our first step is to make a function for creating new, blank @@ -1206,11 +1222,12 @@ our name tag as the Per-directory configuration handler:
Our next step in creating a context aware configuration is merging configurations. This part of the process particularly apply to scenarios where you have a parent configuration and a child, such as the following: +
<Directory "/var/www"> ExampleEnable On @@ -1221,6 +1238,7 @@ where you have a parent configuration and a child, such as the following: ExampleAction file deny </Directory>
In this example, it is natural to assume that the directory
/var/www/subdir
should inherit the value set for the /var/www
directory, as we did not specify a ExampleEnable
nor
@@ -1257,11 +1275,12 @@ two configurations and decide how they are to be merged:
Now, let's try putting it all together to create a new module that is context aware. First off, we'll create a configuration that lets us test how the module works: +
<Location "/a"> SetHandler example-handler @@ -1281,6 +1300,7 @@ how the module works: ExampleEnabled on </Location>
Then we'll assemble our module code. Note, that since we are now using our name tag as reference when fetching configurations in our handler, I have added some prototypes to keep the compiler happy: -- 2.47.2