hooks.xml revision a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd<?xml version="1.0" encoding="UTF-8" ?>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd<!DOCTYPE manualpage SYSTEM "/style/manualpage.dtd">
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
5f5d1b4cc970b7f06ff8ef6526128e9a27303d88nd<!-- $LastChangedRevision$ -->
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd<!--
031b91a62d25106ae69d4693475c79618dd5e884fielding Licensed to the Apache Software Foundation (ASF) under one or more
031b91a62d25106ae69d4693475c79618dd5e884fielding contributor license agreements. See the NOTICE file distributed with
031b91a62d25106ae69d4693475c79618dd5e884fielding this work for additional information regarding copyright ownership.
031b91a62d25106ae69d4693475c79618dd5e884fielding The ASF licenses this file to You under the Apache License, Version 2.0
031b91a62d25106ae69d4693475c79618dd5e884fielding (the "License"); you may not use this file except in compliance with
031b91a62d25106ae69d4693475c79618dd5e884fielding the License. You may obtain a copy of the License at
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd http://www.apache.org/licenses/LICENSE-2.0
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd Unless required by applicable law or agreed to in writing, software
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd distributed under the License is distributed on an "AS IS" BASIS,
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd See the License for the specific language governing permissions and
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd limitations under the License.
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd-->
816bc7965d58c92c0d02fd42d6ea58090f70c6bdnd
7db9f691a00ead175b03335457ca296a33ddf31bnd<manualpage metafile="hooks.xml.meta">
c82fca6d3f5608b946f18d37e8710b1d71e3478dnd<parentdocument href="./">Developer Documentation</parentdocument>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
c98b488eb0a76ff64674644913f4bbd1739ed03ahumbedooh<title>Hook Functions in the Apache HTTP Server 2.x</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd<summary>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <note type="warning"><title>Warning</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>This document is still in development and may be partially out of
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd date.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </note>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar <p>In general, a hook function is one that the Apache HTTP Server
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar will call at some point during the processing of a request.
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar Modules can provide functions that are called, and specify when
d6d9a8670a4620266fc9aa4958d8e42668a6bb68humbedooh they get called in comparison to other modules.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd</summary>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd<section id="create"><title>Creating a hook function</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>In order to create a new hook, four things need to be
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd done:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section id="create-declare"><title>Declare the hook function</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>Use the <code>AP_DECLARE_HOOK</code> macro, which needs to be given
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd the return type of the hook function, the name of the hook, and the
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd arguments. For example, if the hook returns an <code>int</code> and
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd takes a <code>request_rec *</code> and an <code>int</code> and is
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd called <code>do_something</code>, then declare it like this:</p>
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd AP_DECLARE_HOOK(int, do_something, (request_rec *r, int n))
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>This should go in a header which modules will include if
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd they want to use the hook.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section id="create-create"><title>Create the hook structure</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>Each source file that exports a hook has a private structure
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd which is used to record the module functions that use the hook.
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd This is declared as follows:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohAPR_HOOK_STRUCT(
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh APR_HOOK_LINK(do_something)
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh ...
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh)
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section id="create-implement"><title>Implement the hook caller</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>The source file that exports the hook has to implement a
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd function that will call the hook. There are currently three
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd possible ways to do this. In all cases, the calling function is
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd called <code>ap_run_<var>hookname</var>()</code>.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section><title>Void hooks</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>If the return value of a hook is <code>void</code>, then all the
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd hooks are called, and the caller is implemented like this:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd AP_IMPLEMENT_HOOK_VOID(do_something, (request_rec *r, int n), (r, n))
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>The second and third arguments are the dummy argument
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd declaration and the dummy arguments as they will be used when
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd calling the hook. In other words, this macro expands to
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd something like this:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohvoid ap_run_do_something(request_rec *r, int n)
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh{
bb10b4c35ab42a1950a733a36e4fb2dd1168b236jailletc ...
bb10b4c35ab42a1950a733a36e4fb2dd1168b236jailletc do_something(r, n);
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh}
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section><title>Hooks that return a value</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>If the hook returns a value, then it can either be run until
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd the first hook that does something interesting, like so:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd AP_IMPLEMENT_HOOK_RUN_FIRST(int, do_something, (request_rec *r, int n), (r, n), DECLINED)
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>The first hook that does <em>not</em> return <code>DECLINED</code>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd stops the loop and its return value is returned from the hook
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar caller. Note that <code>DECLINED</code> is the traditional
d6d9a8670a4620266fc9aa4958d8e42668a6bb68humbedooh hook return value meaning "I didn't do anything", but it can be
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd whatever suits you.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>Alternatively, all hooks can be run until an error occurs.
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd This boils down to permitting <em>two</em> return values, one of
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd which means "I did something, and it was OK" and the other
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd meaning "I did nothing". The first function that returns a
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd value other than one of those two stops the loop, and its
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd return is the return value. Declare these like so:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd AP_IMPLEMENT_HOOK_RUN_ALL(int, do_something, (request_rec *r, int n), (r, n), OK, DECLINED)
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>Again, <code>OK</code> and <code>DECLINED</code> are the traditional
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd values. You can use what you want.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section id="create-call"><title>Call the hook callers</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>At appropriate moments in the code, call the hook caller,
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd like so:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohint n, ret;
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohrequest_rec *r;
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohret=ap_run_do_something(r, n);
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd</section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd<section id="hooking"><title>Hooking the hook</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>A module that wants a hook to be called needs to do two
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd things.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section id="hooking-implement"><title>Implement the hook function</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>Include the appropriate header, and define a static function
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd of the correct type:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohstatic int my_something_doer(request_rec *r, int n)<br />
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh{
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh ...
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh return OK;
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh}
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section id="hooking-add"><title>Add a hook registering function</title>
d6d9a8670a4620266fc9aa4958d8e42668a6bb68humbedooh <p>During initialisation, the server will call each modules hook
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd registering function, which is included in the module
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd structure:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohstatic void my_register_hooks()
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh{
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh ap_hook_do_something(my_something_doer, NULL, NULL, APR_HOOK_MIDDLE);
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh}
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohmode MODULE_VAR_EXPORT my_module =
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh{
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh ...
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh my_register_hooks /* register hooks */
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh};
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <section id="hooking-order"><title>Controlling hook calling order</title>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>In the example above, we didn't use the three arguments in
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd the hook registration function that control calling order.
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd There are two mechanisms for doing this. The first, rather
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd crude, method, allows us to specify roughly where the hook is
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd run relative to other modules. The final argument control this.
1c14060ebaf0bab91387c4be294e0d3fddf2068cyoshiki There are three possible values: <code>APR_HOOK_FIRST</code>,
1c14060ebaf0bab91387c4be294e0d3fddf2068cyoshiki <code>APR_HOOK_MIDDLE</code> and <code>APR_HOOK_LAST</code>.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>All modules using any particular value may be run in any
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd order relative to each other, but, of course, all modules using
1c14060ebaf0bab91387c4be294e0d3fddf2068cyoshiki <code>APR_HOOK_FIRST</code> will be run before <code>APR_HOOK_MIDDLE</code>
1c14060ebaf0bab91387c4be294e0d3fddf2068cyoshiki which are before <code>APR_HOOK_LAST</code>. Modules that don't care
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar when they are run should use <code>APR_HOOK_MIDDLE</code>. <em>These
d6d9a8670a4620266fc9aa4958d8e42668a6bb68humbedooh values are spaced out, so that positions like <code>APR_HOOK_FIRST-2</code>
d6d9a8670a4620266fc9aa4958d8e42668a6bb68humbedooh are possible to hook slightly earlier than other functions.</em></p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>Note that there are two more values,
1c14060ebaf0bab91387c4be294e0d3fddf2068cyoshiki <code>APR_HOOK_REALLY_FIRST</code> and <code>APR_HOOK_REALLY_LAST</code>. These
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd should only be used by the hook exporter.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>The other method allows finer control. When a module knows
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd that it must be run before (or after) some other modules, it
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd can specify them by name. The second (third) argument is a
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd NULL-terminated array of strings consisting of the names of
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd modules that must be run before (after) the current module. For
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd example, suppose we want "mod_xyz.c" and "mod_abc.c" to run
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd before we do, then we'd hook as follows:</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh <highlight language="c">
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedoohstatic void register_hooks()
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh{
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh static const char * const aszPre[] = { "mod_xyz.c", "mod_abc.c", NULL };
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh ap_hook_do_something(my_something_doer, aszPre, NULL, APR_HOOK_MIDDLE);
94e836655ae06d0a14ec9aaaf6455bcaf2872d1dhumbedooh}
1050464f9f91f75e7a1c5c3daf3fb7b8aa74592ahumbedooh </highlight>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd <p>Note that the sort used to achieve this is stable, so
1c14060ebaf0bab91387c4be294e0d3fddf2068cyoshiki ordering set by <code>APR_HOOK_<var>ORDER</var></code> is preserved, as far
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd as is possible.</p>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd </section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd</section>
1eba1be63201689b2d3e651d3a35f0bc2057e2c0nd</manualpage>