hooks.html revision 4f1f0f9a56771adea88c7b3c1061f33d3dfe5c2c
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<html>
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<head>
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<title>Apache 2.0 Hook Functions</title>
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek</head>
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<BODY
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek BGCOLOR="#FFFFFF"
03532fb1cbb7e8c1d5cf2e93aa3719f926631cabStephen Gallagher TEXT="#000000"
8a2a49333b7df3a4b86db42cd20ec8286d2788d3Pavel Březina LINK="#0000FF"
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek VLINK="#000080"
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek ALINK="#FF0000"
b69cb1787209e85cc246eb9a944242689bfe0c46Pavel Březina>
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<!--#include virtual="header.html" -->
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<H1 align="center">Apache Hook Functions</H1>
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<P>In general, a hook function is one that Apache will call at some
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozekpoint during the processing of a request. Modules can provide
46e44f86067b7f77c3f71cb7ac57456434e354dcStephen Gallagherfunctions that are called, and specify when they get called in
46e44f86067b7f77c3f71cb7ac57456434e354dcStephen Gallaghercomparison to other modules.</P>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
191e45ede8b9210d253dc041d5f0432d2ed2f4e3Jakub Hrozek<H2>Creating a hook function</H2>
53b6d6e82dbe52aa655f910e87eb3595584aa9c9Jakub Hrozek
46e44f86067b7f77c3f71cb7ac57456434e354dcStephen Gallagher<P>In order to create a new hook, four things need to be done:</P>
46e44f86067b7f77c3f71cb7ac57456434e354dcStephen Gallagher
1dab34228cefddcd6bedef2d2cee70a620c8ace8Jakub Hrozek<H3>Declare the hook function</H3>
dc6dd1ef6a70a0a07017d362e13c7680e83c4fc8Pavel Březina
54c64aad71e6792edb7cf99988d9a7f4bc2b0c61Jakub Hrozek<P>Use the AP_DECLARE_HOOK macro, which needs to be given the return
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozektype of the hook function, the name of the hook, and the arguments. For
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozekexample, if the hook returns an <TT>int</TT> and takes a
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek<TT>request_rec *</TT> and an <TT>int</TT> and is called
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher"do_something", then declare it like this:</P>
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek
813086b16adabc6c1a0f37d7518c6a709ae1c57aJakub Hrozek<TT>AP_DECLARE_HOOK(int,do_something,(request_rec *r,int n))</TT>
46e44f86067b7f77c3f71cb7ac57456434e354dcStephen Gallagher
11540d9efb85b9ed0341e8a1fc97fc078c6ce418Pavel Březina<P>This should go in a header which modules will include if they want
46e44f86067b7f77c3f71cb7ac57456434e354dcStephen Gallagherto use the hook.</P>
9b3f37cb0c70c7b18c49b657e3799094a8711cadJakub Hrozek
544525ee1fc54d744c08465066e2b4a521f78224Stephen Gallagher<H3>Create the hook structure</H3>
5ef295d1cf410ceaa92c03a7843df8a36409f465Stephen Gallagher
002dfe55ef258b73ca85eb813b1a156789b7702aJakub Hrozek<P>Each source file that exports a hook has a private structure which
b49d753b5a17c4092d8c24e84eea4400de39d341Sumit Boseis used to record the module functions that use the hook. This is
ae0a5011e2644eaa482ea1b9e1451eff05c676b9Lukas Slebodnikdeclared as follows:</P>
6e27e8572f671de575d9ac2a34a677d9efc24fbcJustin Stephenson
<PRE>
APR_HOOK_STRUCT(
APR_HOOK_LINK(do_something)
...
)
</PRE>
<H3>Implement the hook caller</H3>
<P>The source file that exports the hook has to implement a function
that will call the hook. There are currently three possible ways to do
this. In all cases, the calling function is called
<TT>ap_run_<I>hookname</I>()</TT>.</P>
<H4>Void hooks</H4>
<P>If the return value of a hook is <TT>void</TT>, then all the hooks are
called, and the caller is implemented like this:</P>
<TT>AP_IMPLEMENT_HOOK_VOID(do_something,(request_rec *r,int
n),(r,n))</TT>
<P>The second and third arguments are the dummy argument declaration and
the dummy arguments as they will be used when calling the hook. In
other words, this macro expands to something like this:</P>
<PRE>
void ap_run_do_something(request_rec *r,int n)
{
...
do_something(r,n);
}
</PRE>
<H4>Hooks that return a value</H4>
<P>If the hook returns a value, then it can either be run until the first
hook that does something interesting, like so:</P>
<TT>AP_IMPLEMENT_HOOK_RUN_FIRST(int,do_something,(request_rec *r,int n),(r,n),DECLINED)</TT>
<P>The first hook that <I>doesn't</I> return <TT>DECLINED</TT> stops
the loop and its return value is returned from the hook caller. Note
that <TT>DECLINED</TT> is the tradition Apache hook return meaning "I
didn't do anything", but it can be whatever suits you.</P>
<P>Alternatively, all hooks can be run until an error occurs. This
boils down to permitting <I>two</I> return values, one of which means
"I did something, and it was OK" and the other meaning "I did
nothing". The first function that returns a value other than one of
those two stops the loop, and its return is the return value. Declare
these like so:</P>
<TT>AP_IMPLEMENT_HOOK_RUN_ALL(int,do_something,(request_rec *r,int
n),(r,n),OK,DECLINED)</TT>
<P>Again, <TT>OK</TT> and <TT>DECLINED</TT> are the traditional
values. You can use what you want.</P>
<H3>Call the hook callers</H3>
<P>At appropriate moments in the code, call the hook caller, like
so:</P>
<PRE>
int n,ret;
request_rec *r;
ret=ap_run_do_something(r,n);
</PRE>
<H2>Hooking the hook</H2>
<P>A module that wants a hook to be called needs to do two
things.</P>
<H3>Implement the hook function</H3>
<P>Include the appropriate header, and define a static function of the
correct type:</P>
<PRE>
static int my_something_doer(request_rec *r,int n)
{
...
return OK;
}
</PRE>
<H3>Add a hook registering function</H3>
<P>During initialisation, Apache will call each modules hook
registering function, which is included in the module structure:</P>
<PRE>
static void my_register_hooks()
{
ap_hook_do_something(my_something_doer,NULL,NULL,HOOK_MIDDLE);
}
mode MODULE_VAR_EXPORT my_module =
{
...
my_register_hooks /* register hooks */
};
</PRE>
<H3>Controlling hook calling order</H3>
<P>In the example above, we didn't use the three arguments in the hook
registration function that control calling order. There are two
mechanisms for doing this. The first, rather crude, method, allows us
to specify roughly where the hook is run relative to other
modules. The final argument control this. There are three possible
values:</P>
<PRE>
HOOK_FIRST
HOOK_MIDDLE
HOOK_LAST
</PRE>
<P>All modules using any particular value may be run in any order
relative to each other, but, of course, all modules using
<TT>HOOK_FIRST</TT> will be run before <TT>HOOK_MIDDLE</TT> which are
before <TT>HOOK_LAST</TT>. Modules that don't care when they are run
should use <TT>HOOK_MIDDLE</TT>. <I>(I spaced these out so people
could do stuff like <TT>HOOK_FIRST-2</TT> to get in slightly earlier,
but is this wise? - Ben)</I></P>
<P>Note that there are two more values, <TT>HOOK_REALLY_FIRST</TT> and
<TT>HOOK_REALLY_LAST</TT>. These should only be used by the hook
exporter.</P>
<P>The other method allows finer control. When a module knows that it
must be run before (or after) some other modules, it can specify them
by name. The second (third) argument is a NULL-terminated array of
strings consisting of the names of modules that must be run before
(after) the current module. For example, suppose we want "mod_xyz.c"
and "mod_abc.c" to run before we do, then we'd hook as follows:</P>
<PRE>
static void register_hooks()
{
static const char * const aszPre[]={ "mod_xyz.c", "mod_abc.c", NULL };
ap_hook_do_something(my_something_doer,aszPre,NULL,HOOK_MIDDLE);
}
</PRE>
<P>Note that the sort used to achieve this is stable, so ordering set
by <TT>HOOK_<I>ORDER</I></TT> is preserved, as far as is
possible.</P>
<I>Ben Laurie, 15th August 1999</I>
<!--#include virtual="footer.html" -->
</body>
</html>