1<?xml version="1.0" encoding="ISO-8859-1"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!-- 4 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 5 This file is generated from xml source: DO NOT EDIT 6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 7 --> 8<title>Converting Modules from Apache 1.3 to Apache 2.0 - Apache HTTP Server</title> 9<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" /> 10<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" /> 11<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="/style/css/prettify.css" /> 12<script src="/style/scripts/prettify.min.js" type="text/javascript"> 13</script> 14 15<link href="/images/favicon.ico" rel="shortcut icon" /></head> 16<body id="manual-page"><div id="page-header"> 17<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p> 18<p class="apache">Apache HTTP Server Version 2.4</p> 19<img alt="" src="/images/feather.gif" /></div> 20<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div> 21<div id="path"> 22<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.4</a> > <a href="./">Developer Documentation</a></div><div id="page-content"><div id="preamble"><h1>Converting Modules from Apache 1.3 to Apache 2.0</h1> 23<div class="toplang"> 24<p><span>Available Languages: </span><a href="/en/developer/modules.html" title="English"> en </a> | 25<a href="/ja/developer/modules.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a></p> 26</div> 27 28 <p>This is a first attempt at writing the lessons I learned 29 when trying to convert the <code>mod_mmap_static</code> module to Apache 30 2.0. It's by no means definitive and probably won't even be 31 correct in some ways, but it's a start.</p> 32</div> 33<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#easy">The easier changes ...</a></li> 34<li><img alt="" src="/images/down.gif" /> <a href="#messy">The messier changes...</a></li> 35</ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div> 36<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div> 37<div class="section"> 38<h2><a name="easy" id="easy">The easier changes ...</a></h2> 39 40 <h3><a name="cleanup" id="cleanup">Cleanup Routines</a></h3> 41 <p>These now need to be of type <code>apr_status_t</code> and return a 42 value of that type. Normally the return value will be 43 <code>APR_SUCCESS</code> unless there is some need to signal an error in 44 the cleanup. Be aware that even though you signal an error not all code 45 yet checks and acts upon the error.</p> 46 47 48 <h3><a name="init" id="init">Initialisation Routines</a></h3> 49 <p>These should now be renamed to better signify where they sit 50 in the overall process. So the name gets a small change from 51 <code>mmap_init</code> to <code>mmap_post_config</code>. The arguments 52 passed have undergone a radical change and now look like</p> 53 54 <ul> 55 <li><code>apr_pool_t *p</code></li> 56 <li><code>apr_pool_t *plog</code></li> 57 <li><code>apr_pool_t *ptemp</code></li> 58 <li><code>server_rec *s</code></li> 59 </ul> 60 61 62 <h3><a name="datatypes" id="datatypes">Data Types</a></h3> 63 <p>A lot of the data types have been moved into the <a href="http://apr.apache.org/">APR</a>. This means that some have had 64 a name change, such as the one shown above. The following is a brief 65 list of some of the changes that you are likely to have to make.</p> 66 67 <ul> 68 <li><code>pool</code> becomes <code>apr_pool_t</code></li> 69 <li><code>table</code> becomes <code>apr_table_t</code></li> 70 </ul> 71 72</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div> 73<div class="section"> 74<h2><a name="messy" id="messy">The messier changes...</a></h2> 75 76 <h3><a name="register-hooks" id="register-hooks">Register Hooks</a></h3> 77 <p>The new architecture uses a series of hooks to provide for 78 calling your functions. These you'll need to add to your module 79 by way of a new function, <code>static void register_hooks(void)</code>. 80 The function is really reasonably straightforward once you 81 understand what needs to be done. Each function that needs 82 calling at some stage in the processing of a request needs to 83 be registered, handlers do not. There are a number of phases 84 where functions can be added, and for each you can specify with 85 a high degree of control the relative order that the function 86 will be called in.</p> 87 88 <p>This is the code that was added to <code>mod_mmap_static</code>:</p> 89 <div class="example"><pre>static void register_hooks(void) 90{ 91 static const char * const aszPre[]={ "http_core.c",NULL }; 92 ap_hook_post_config(mmap_post_config,NULL,NULL,HOOK_MIDDLE); 93 ap_hook_translate_name(mmap_static_xlat,aszPre,NULL,HOOK_LAST); 94};</pre></div> 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 <div class="example"><p><code> 103 ap_hook_<var>phase_name</var>(<var>function_name</var>, 104 <var>predecessors</var>, <var>successors</var>, <var>position</var>); 105 </code></p></div> 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 its name 124 translation, hence the use of the aszPre to define a modifier to the 125 position <code>HOOK_LAST</code>.</p> 126 127 128 <h3><a name="moddef" id="moddef">Module Definition</a></h3> 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 <div class="example"><pre>module MODULE_VAR_EXPORT <var>module_name</var>_module = 134{ 135 STANDARD_MODULE_STUFF, 136 /* initializer */ 137 /* dir config creater */ 138 /* dir merger --- default is to override */ 139 /* server config */ 140 /* merge server config */ 141 /* command handlers */ 142 /* handlers */ 143 /* filename translation */ 144 /* check_user_id */ 145 /* check auth */ 146 /* check access */ 147 /* type_checker */ 148 /* fixups */ 149 /* logger */ 150 /* header parser */ 151 /* child_init */ 152 /* child_exit */ 153 /* post read-request */ 154};</pre></div> 155 156 <p>The new structure is a great deal simpler...</p> 157 <div class="example"><pre>module MODULE_VAR_EXPORT <var>module_name</var>_module = 158{ 159 STANDARD20_MODULE_STUFF, 160 /* create per-directory config structures */ 161 /* merge per-directory config structures */ 162 /* create per-server config structures */ 163 /* merge per-server config structures */ 164 /* command handlers */ 165 /* handlers */ 166 /* register hooks */ 167};</pre></div> 168 169 <p>Some of these read directly across, some don't. I'll try to 170 summarise what should be done below.</p> 171 172 <p>The stages that read directly across :</p> 173 174 <dl> 175 <dt><code>/* dir config creater */</code></dt> 176 <dd><code>/* create per-directory config structures */</code></dd> 177 178 <dt><code>/* server config */</code></dt> 179 <dd><code>/* create per-server config structures */</code></dd> 180 181 <dt><code>/* dir merger */</code></dt> 182 <dd><code>/* merge per-directory config structures */</code></dd> 183 184 <dt><code>/* merge server config */</code></dt> 185 <dd><code>/* merge per-server config structures */</code></dd> 186 187 <dt><code>/* command table */</code></dt> 188 <dd><code>/* command apr_table_t */</code></dd> 189 190 <dt><code>/* handlers */</code></dt> 191 <dd><code>/* handlers */</code></dd> 192 </dl> 193 194 <p>The remainder of the old functions should be registered as 195 hooks. There are the following hook stages defined so 196 far...</p> 197 198 <dl> 199 <dt><code>ap_hook_pre_config</code></dt> 200 <dd>do any setup required prior to processing configuration 201 directives</dd> 202 203 <dt><code>ap_hook_check_config</code></dt> 204 <dd>review configuration directive interdependencies</dd> 205 206 <dt><code>ap_hook_test_config</code></dt> 207 <dd>executes only with <code>-t</code> option</dd> 208 209 <dt><code>ap_hook_open_logs</code></dt> 210 <dd>open any specified logs</dd> 211 212 <dt><code>ap_hook_post_config</code></dt> 213 <dd>this is where the old <code>_init</code> routines get 214 registered</dd> 215 216 <dt><code>ap_hook_http_method</code></dt> 217 <dd>retrieve the http method from a request. (legacy)</dd> 218 219 <dt><code>ap_hook_auth_checker</code></dt> 220 <dd>check if the resource requires authorization</dd> 221 222 <dt><code>ap_hook_access_checker</code></dt> 223 <dd>check for module-specific restrictions</dd> 224 225 <dt><code>ap_hook_check_user_id</code></dt> 226 <dd>check the user-id and password</dd> 227 228 <dt><code>ap_hook_default_port</code></dt> 229 <dd>retrieve the default port for the server</dd> 230 231 <dt><code>ap_hook_pre_connection</code></dt> 232 <dd>do any setup required just before processing, but after 233 accepting</dd> 234 235 <dt><code>ap_hook_process_connection</code></dt> 236 <dd>run the correct protocol</dd> 237 238 <dt><code>ap_hook_child_init</code></dt> 239 <dd>call as soon as the child is started</dd> 240 241 <dt><code>ap_hook_create_request</code></dt> 242 <dd>??</dd> 243 244 <dt><code>ap_hook_fixups</code></dt> 245 <dd>last chance to modify things before generating content</dd> 246 247 <dt><code>ap_hook_handler</code></dt> 248 <dd>generate the content</dd> 249 250 <dt><code>ap_hook_header_parser</code></dt> 251 <dd>lets modules look at the headers, not used by most modules, because 252 they use <code>post_read_request</code> for this</dd> 253 254 <dt><code>ap_hook_insert_filter</code></dt> 255 <dd>to insert filters into the filter chain</dd> 256 257 <dt><code>ap_hook_log_transaction</code></dt> 258 <dd>log information about the request</dd> 259 260 <dt><code>ap_hook_optional_fn_retrieve</code></dt> 261 <dd>retrieve any functions registered as optional</dd> 262 263 <dt><code>ap_hook_post_read_request</code></dt> 264 <dd>called after reading the request, before any other phase</dd> 265 266 <dt><code>ap_hook_quick_handler</code></dt> 267 <dd>called before any request processing, used by cache modules.</dd> 268 269 <dt><code>ap_hook_translate_name</code></dt> 270 <dd>translate the URI into a filename</dd> 271 272 <dt><code>ap_hook_type_checker</code></dt> 273 <dd>determine and/or set the doc type</dd> 274 </dl> 275 276</div></div> 277<div class="bottomlang"> 278<p><span>Available Languages: </span><a href="/en/developer/modules.html" title="English"> en </a> | 279<a href="/ja/developer/modules.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a></p> 280</div><div class="top"><a href="#page-header"><img src="/images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div> 281<script type="text/javascript"><!--//--><![CDATA[//><!-- 282var comments_shortname = 'httpd'; 283var comments_identifier = 'http://httpd.apache.org/docs/2.4/developer/modules.html'; 284(function(w, d) { 285 if (w.location.hostname.toLowerCase() == "httpd.apache.org") { 286 d.write('<div id="comments_thread"><\/div>'); 287 var s = d.createElement('script'); 288 s.type = 'text/javascript'; 289 s.async = true; 290 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier; 291 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); 292 } 293 else { 294 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>'); 295 } 296})(window, document); 297//--><!]]></script></div><div id="footer"> 298<p class="apache">Copyright 2014 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p> 299<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!-- 300if (typeof(prettyPrint) !== 'undefined') { 301 prettyPrint(); 302} 303//--><!]]></script> 304</body></html>