]> granicus.if.org Git - apache/blob - docs/manual/developer/modules.xml
Add some initial example scripts and function reference for what will be a lengthy...
[apache] / docs / manual / developer / modules.xml
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$ -->
5
6 <!--
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
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
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.
21 -->
22
23 <manualpage metafile="modules.xml.meta">
24 <parentdocument href="./">Developer Documentation</parentdocument>
25
26 <title>Converting Modules from Apache 1.3 to Apache 2.0</title>
27
28 <summary>
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>
33 </summary>
34
35 <section id="easy"><title>The easier changes ...</title>
36
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>
43     </section>
44
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>
50
51       <ul>
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>
56       </ul>
57     </section>
58
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>
64
65       <ul>
66         <li><code>pool</code> becomes <code>apr_pool_t</code></li>
67         <li><code>table</code> becomes <code>apr_table_t</code></li>
68       </ul>
69     </section>
70 </section>
71
72 <section id="messy"><title>The messier changes...</title>
73
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>
85
86       <p>This is the code that was added to <code>mod_mmap_static</code>:</p>
87       <example><pre>
88 static void register_hooks(void)
89 {
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);
93 };</pre>
94       </example>
95
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>
101
102       <example>
103         ap_hook_<var>phase_name</var>(<var>function_name</var>,
104         <var>predecessors</var>, <var>successors</var>, <var>position</var>);
105       </example>
106
107       <p>There are 3 hook positions defined...</p>
108
109       <ul>
110         <li><code>HOOK_FIRST</code></li>
111         <li><code>HOOK_MIDDLE</code></li>
112         <li><code>HOOK_LAST</code></li>
113       </ul>
114
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>
120
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>
126     </section>
127
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
131       like</p>
132
133       <example><pre>
134 module MODULE_VAR_EXPORT <var>module_name</var>_module =
135 {
136     STANDARD_MODULE_STUFF,
137     /* initializer */
138     /* dir config creater */
139     /* dir merger --- default is to override */
140     /* server config */
141     /* merge server config */
142     /* command handlers */
143     /* handlers */
144     /* filename translation */
145     /* check_user_id */
146     /* check auth */
147     /* check access */
148     /* type_checker */
149     /* fixups */
150     /* logger */
151     /* header parser */
152     /* child_init */
153     /* child_exit */
154     /* post read-request */
155 };</pre>
156       </example>
157
158       <p>The new structure is a great deal simpler...</p>
159       <example><pre>
160 module MODULE_VAR_EXPORT <var>module_name</var>_module =
161 {
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 */
168     /* handlers */
169     /* register hooks */
170 };</pre>
171       </example>
172
173       <p>Some of these read directly across, some don't. I'll try to
174       summarise what should be done below.</p>
175
176       <p>The stages that read directly across :</p>
177
178       <dl>
179         <dt><code>/* dir config creater */</code></dt>
180         <dd><code>/* create per-directory config structures */</code></dd>
181
182         <dt><code>/* server config */</code></dt>
183         <dd><code>/* create per-server config structures */</code></dd>
184
185         <dt><code>/* dir merger */</code></dt>
186         <dd><code>/* merge per-directory config structures */</code></dd>
187
188         <dt><code>/* merge server config */</code></dt>
189         <dd><code>/* merge per-server config structures */</code></dd>
190
191         <dt><code>/* command table */</code></dt>
192         <dd><code>/* command apr_table_t */</code></dd>
193
194         <dt><code>/* handlers */</code></dt>
195         <dd><code>/* handlers */</code></dd>
196       </dl>
197
198       <p>The remainder of the old functions should be registered as
199       hooks. There are the following hook stages defined so
200       far...</p>
201
202       <dl>
203         <dt><code>ap_hook_pre_config</code></dt>
204         <dd>do any setup required prior to processing configuration
205         directives</dd>
206
207         <dt><code>ap_hook_check_config</code></dt>
208         <dd>review configuration directive interdependencies</dd>
209
210         <dt><code>ap_hook_test_config</code></dt>
211         <dd>executes only with <code>-t</code> option</dd>
212
213         <dt><code>ap_hook_open_logs</code></dt>
214         <dd>open any specified logs</dd>
215
216         <dt><code>ap_hook_post_config</code></dt>
217         <dd>this is where the old <code>_init</code> routines get
218         registered</dd>
219
220         <dt><code>ap_hook_http_method</code></dt>
221         <dd>retrieve the http method from a request. (legacy)</dd>
222
223         <dt><code>ap_hook_auth_checker</code></dt>
224         <dd>check if the resource requires authorization</dd>
225
226         <dt><code>ap_hook_access_checker</code></dt>
227         <dd>check for module-specific restrictions</dd>
228
229         <dt><code>ap_hook_check_user_id</code></dt>
230         <dd>check the user-id and password</dd>
231
232         <dt><code>ap_hook_default_port</code></dt>
233         <dd>retrieve the default port for the server</dd>
234
235         <dt><code>ap_hook_pre_connection</code></dt>
236         <dd>do any setup required just before processing, but after
237         accepting</dd>
238
239         <dt><code>ap_hook_process_connection</code></dt>
240         <dd>run the correct protocol</dd>
241
242         <dt><code>ap_hook_child_init</code></dt>
243         <dd>call as soon as the child is started</dd>
244
245         <dt><code>ap_hook_create_request</code></dt>
246         <dd>??</dd>
247
248         <dt><code>ap_hook_fixups</code></dt>
249         <dd>last chance to modify things before generating content</dd>
250
251         <dt><code>ap_hook_handler</code></dt>
252         <dd>generate the content</dd>
253
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>
257
258         <dt><code>ap_hook_insert_filter</code></dt>
259         <dd>to insert filters into the filter chain</dd>
260
261         <dt><code>ap_hook_log_transaction</code></dt>
262         <dd>log information about the request</dd>
263
264         <dt><code>ap_hook_optional_fn_retrieve</code></dt>
265         <dd>retrieve any functions registered as optional</dd>
266
267         <dt><code>ap_hook_post_read_request</code></dt>
268         <dd>called after reading the request, before any other phase</dd>
269
270         <dt><code>ap_hook_quick_handler</code></dt>
271         <dd>called before any request processing, used by cache modules.</dd>
272
273         <dt><code>ap_hook_translate_name</code></dt>
274         <dd>translate the URI into a filename</dd>
275
276         <dt><code>ap_hook_type_checker</code></dt>
277         <dd>determine and/or set the doc type</dd>
278       </dl>
279     </section>
280 </section>
281 </manualpage>
282