1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="modules.xml.meta">
24 <parentdocument href="./">Developer Documentation</parentdocument>
26 <title>Converting Modules from Apache 1.3 to Apache 2.0</title>
29 <p>This is a first attempt at writing the lessons I learned
30 when trying to convert the <code>mod_mmap_static</code> module to Apache
31 2.0. It's by no means definitive and probably won't even be
32 correct in some ways, but it's a start.</p>
35 <section id="easy"><title>The easier changes ...</title>
37 <section id="cleanup"><title>Cleanup Routines</title>
38 <p>These now need to be of type <code>apr_status_t</code> and return a
39 value of that type. Normally the return value will be
40 <code>APR_SUCCESS</code> unless there is some need to signal an error in
41 the cleanup. Be aware that even though you signal an error not all code
42 yet checks and acts upon the error.</p>
45 <section id="init"><title>Initialisation Routines</title>
46 <p>These should now be renamed to better signify where they sit
47 in the overall process. So the name gets a small change from
48 <code>mmap_init</code> to <code>mmap_post_config</code>. The arguments
49 passed have undergone a radical change and now look like</p>
52 <li><code>apr_pool_t *p</code></li>
53 <li><code>apr_pool_t *plog</code></li>
54 <li><code>apr_pool_t *ptemp</code></li>
55 <li><code>server_rec *s</code></li>
59 <section id="datatypes"><title>Data Types</title>
60 <p>A lot of the data types have been moved into the <a
61 href="http://apr.apache.org/">APR</a>. This means that some have had
62 a name change, such as the one shown above. The following is a brief
63 list of some of the changes that you are likely to have to make.</p>
66 <li><code>pool</code> becomes <code>apr_pool_t</code></li>
67 <li><code>table</code> becomes <code>apr_table_t</code></li>
72 <section id="messy"><title>The messier changes...</title>
74 <section id="register-hooks"><title>Register Hooks</title>
75 <p>The new architecture uses a series of hooks to provide for
76 calling your functions. These you'll need to add to your module
77 by way of a new function, <code>static void register_hooks(void)</code>.
78 The function is really reasonably straightforward once you
79 understand what needs to be done. Each function that needs
80 calling at some stage in the processing of a request needs to
81 be registered, handlers do not. There are a number of phases
82 where functions can be added, and for each you can specify with
83 a high degree of control the relative order that the function
84 will be called in.</p>
86 <p>This is the code that was added to <code>mod_mmap_static</code>:</p>
88 static void register_hooks(void)
90 static const char * const aszPre[]={ "http_core.c",NULL };
91 ap_hook_post_config(mmap_post_config,NULL,NULL,HOOK_MIDDLE);
92 ap_hook_translate_name(mmap_static_xlat,aszPre,NULL,HOOK_LAST);
96 <p>This registers 2 functions that need to be called, one in
97 the <code>post_config</code> stage (virtually every module will need this
98 one) and one for the <code>translate_name</code> phase. note that while
99 there are different function names the format of each is
100 identical. So what is the format?</p>
103 ap_hook_<var>phase_name</var>(<var>function_name</var>,
104 <var>predecessors</var>, <var>successors</var>, <var>position</var>);
107 <p>There are 3 hook positions defined...</p>
110 <li><code>HOOK_FIRST</code></li>
111 <li><code>HOOK_MIDDLE</code></li>
112 <li><code>HOOK_LAST</code></li>
115 <p>To define the position you use the position and then modify
116 it with the predecessors and successors. Each of the modifiers
117 can be a list of functions that should be called, either before
118 the function is run (predecessors) or after the function has
119 run (successors).</p>
121 <p>In the <code>mod_mmap_static</code> case I didn't care about the
122 <code>post_config</code> stage, but the <code>mmap_static_xlat</code>
123 <strong>must</strong> be called after the core module had done it's name
124 translation, hence the use of the aszPre to define a modifier to the
125 position <code>HOOK_LAST</code>.</p>
128 <section id="moddef"><title>Module Definition</title>
129 <p>There are now a lot fewer stages to worry about when
130 creating your module definition. The old definition looked
134 module MODULE_VAR_EXPORT <var>module_name</var>_module =
136 STANDARD_MODULE_STUFF,
138 /* dir config creater */
139 /* dir merger --- default is to override */
141 /* merge server config */
142 /* command handlers */
144 /* filename translation */
154 /* post read-request */
158 <p>The new structure is a great deal simpler...</p>
160 module MODULE_VAR_EXPORT <var>module_name</var>_module =
162 STANDARD20_MODULE_STUFF,
163 /* create per-directory config structures */
164 /* merge per-directory config structures */
165 /* create per-server config structures */
166 /* merge per-server config structures */
167 /* command handlers */
173 <p>Some of these read directly across, some don't. I'll try to
174 summarise what should be done below.</p>
176 <p>The stages that read directly across :</p>
179 <dt><code>/* dir config creater */</code></dt>
180 <dd><code>/* create per-directory config structures */</code></dd>
182 <dt><code>/* server config */</code></dt>
183 <dd><code>/* create per-server config structures */</code></dd>
185 <dt><code>/* dir merger */</code></dt>
186 <dd><code>/* merge per-directory config structures */</code></dd>
188 <dt><code>/* merge server config */</code></dt>
189 <dd><code>/* merge per-server config structures */</code></dd>
191 <dt><code>/* command table */</code></dt>
192 <dd><code>/* command apr_table_t */</code></dd>
194 <dt><code>/* handlers */</code></dt>
195 <dd><code>/* handlers */</code></dd>
198 <p>The remainder of the old functions should be registered as
199 hooks. There are the following hook stages defined so
203 <dt><code>ap_hook_pre_config</code></dt>
204 <dd>do any setup required prior to processing configuration
207 <dt><code>ap_hook_check_config</code></dt>
208 <dd>review configuration directive interdependencies</dd>
210 <dt><code>ap_hook_test_config</code></dt>
211 <dd>executes only with <code>-t</code> option</dd>
213 <dt><code>ap_hook_open_logs</code></dt>
214 <dd>open any specified logs</dd>
216 <dt><code>ap_hook_post_config</code></dt>
217 <dd>this is where the old <code>_init</code> routines get
220 <dt><code>ap_hook_http_method</code></dt>
221 <dd>retrieve the http method from a request. (legacy)</dd>
223 <dt><code>ap_hook_auth_checker</code></dt>
224 <dd>check if the resource requires authorization</dd>
226 <dt><code>ap_hook_access_checker</code></dt>
227 <dd>check for module-specific restrictions</dd>
229 <dt><code>ap_hook_check_user_id</code></dt>
230 <dd>check the user-id and password</dd>
232 <dt><code>ap_hook_default_port</code></dt>
233 <dd>retrieve the default port for the server</dd>
235 <dt><code>ap_hook_pre_connection</code></dt>
236 <dd>do any setup required just before processing, but after
239 <dt><code>ap_hook_process_connection</code></dt>
240 <dd>run the correct protocol</dd>
242 <dt><code>ap_hook_child_init</code></dt>
243 <dd>call as soon as the child is started</dd>
245 <dt><code>ap_hook_create_request</code></dt>
248 <dt><code>ap_hook_fixups</code></dt>
249 <dd>last chance to modify things before generating content</dd>
251 <dt><code>ap_hook_handler</code></dt>
252 <dd>generate the content</dd>
254 <dt><code>ap_hook_header_parser</code></dt>
255 <dd>lets modules look at the headers, not used by most modules, because
256 they use <code>post_read_request</code> for this</dd>
258 <dt><code>ap_hook_insert_filter</code></dt>
259 <dd>to insert filters into the filter chain</dd>
261 <dt><code>ap_hook_log_transaction</code></dt>
262 <dd>log information about the request</dd>
264 <dt><code>ap_hook_optional_fn_retrieve</code></dt>
265 <dd>retrieve any functions registered as optional</dd>
267 <dt><code>ap_hook_post_read_request</code></dt>
268 <dd>called after reading the request, before any other phase</dd>
270 <dt><code>ap_hook_quick_handler</code></dt>
271 <dd>called before any request processing, used by cache modules.</dd>
273 <dt><code>ap_hook_translate_name</code></dt>
274 <dd>translate the URI into a filename</dd>
276 <dt><code>ap_hook_type_checker</code></dt>
277 <dd>determine and/or set the doc type</dd>