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.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.2</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.2</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> 90static void register_hooks(void) 91{ 92 static const char * const aszPre[]={ "http_core.c",NULL }; 93 ap_hook_post_config(mmap_post_config,NULL,NULL,HOOK_MIDDLE); 94 ap_hook_translate_name(mmap_static_xlat,aszPre,NULL,HOOK_LAST); 95};</pre></div> 96 97 <p>This registers 2 functions that need to be called, one in 98 the <code>post_config</code> stage (virtually every module will need this 99 one) and one for the <code>translate_name</code> phase. note that while 100 there are different function names the format of each is 101 identical. So what is the format?</p> 102 103 <div class="example"><p><code> 104 ap_hook_<var>phase_name</var>(<var>function_name</var>, 105 <var>predecessors</var>, <var>successors</var>, <var>position</var>); 106 </code></p></div> 107 108 <p>There are 3 hook positions defined...</p> 109 110 <ul> 111 <li><code>HOOK_FIRST</code></li> 112 <li><code>HOOK_MIDDLE</code></li> 113 <li><code>HOOK_LAST</code></li> 114 </ul> 115 116 <p>To define the position you use the position and then modify 117 it with the predecessors and successors. Each of the modifiers 118 can be a list of functions that should be called, either before 119 the function is run (predecessors) or after the function has 120 run (successors).</p> 121 122 <p>In the <code>mod_mmap_static</code> case I didn't care about the 123 <code>post_config</code> stage, but the <code>mmap_static_xlat</code> 124 <strong>must</strong> be called after the core module had done its name 125 translation, hence the use of the aszPre to define a modifier to the 126 position <code>HOOK_LAST</code>.</p> 127 128 129 <h3><a name="moddef" id="moddef">Module Definition</a></h3> 130 <p>There are now a lot fewer stages to worry about when 131 creating your module definition. The old defintion looked 132 like</p> 133 134 <div class="example"><pre> 135module MODULE_VAR_EXPORT <var>module_name</var>_module = 136{ 137 STANDARD_MODULE_STUFF, 138 /* initializer */ 139 /* dir config creater */ 140 /* dir merger --- default is to override */ 141 /* server config */ 142 /* merge server config */ 143 /* command handlers */ 144 /* handlers */ 145 /* filename translation */ 146 /* check_user_id */ 147 /* check auth */ 148 /* check access */ 149 /* type_checker */ 150 /* fixups */ 151 /* logger */ 152 /* header parser */ 153 /* child_init */ 154 /* child_exit */ 155 /* post read-request */ 156};</pre></div> 157 158 <p>The new structure is a great deal simpler...</p> 159 <div class="example"><pre> 160module 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></div> 171 172 <p>Some of these read directly across, some don't. I'll try to 173 summarise what should be done below.</p> 174 175 <p>The stages that read directly across :</p> 176 177 <dl> 178 <dt><code>/* dir config creater */</code></dt> 179 <dd><code>/* create per-directory config structures */</code></dd> 180 181 <dt><code>/* server config */</code></dt> 182 <dd><code>/* create per-server config structures */</code></dd> 183 184 <dt><code>/* dir merger */</code></dt> 185 <dd><code>/* merge per-directory config structures */</code></dd> 186 187 <dt><code>/* merge server config */</code></dt> 188 <dd><code>/* merge per-server config structures */</code></dd> 189 190 <dt><code>/* command table */</code></dt> 191 <dd><code>/* command apr_table_t */</code></dd> 192 193 <dt><code>/* handlers */</code></dt> 194 <dd><code>/* handlers */</code></dd> 195 </dl> 196 197 <p>The remainder of the old functions should be registered as 198 hooks. There are the following hook stages defined so 199 far...</p> 200 201 <dl> 202 <dt><code>ap_hook_post_config</code></dt> 203 <dd>this is where the old <code>_init</code> routines get 204 registered</dd> 205 206 <dt><code>ap_hook_http_method</code></dt> 207 <dd>retrieve the http method from a request. (legacy)</dd> 208 209 <dt><code>ap_hook_open_logs</code></dt> 210 <dd>open any specified logs</dd> 211 212 <dt><code>ap_hook_auth_checker</code></dt> 213 <dd>check if the resource requires authorization</dd> 214 215 <dt><code>ap_hook_access_checker</code></dt> 216 <dd>check for module-specific restrictions</dd> 217 218 <dt><code>ap_hook_check_user_id</code></dt> 219 <dd>check the user-id and password</dd> 220 221 <dt><code>ap_hook_default_port</code></dt> 222 <dd>retrieve the default port for the server</dd> 223 224 <dt><code>ap_hook_pre_connection</code></dt> 225 <dd>do any setup required just before processing, but after 226 accepting</dd> 227 228 <dt><code>ap_hook_process_connection</code></dt> 229 <dd>run the correct protocol</dd> 230 231 <dt><code>ap_hook_child_init</code></dt> 232 <dd>call as soon as the child is started</dd> 233 234 <dt><code>ap_hook_create_request</code></dt> 235 <dd>??</dd> 236 237 <dt><code>ap_hook_fixups</code></dt> 238 <dd>last chance to modify things before generating content</dd> 239 240 <dt><code>ap_hook_handler</code></dt> 241 <dd>generate the content</dd> 242 243 <dt><code>ap_hook_header_parser</code></dt> 244 <dd>lets modules look at the headers, not used by most modules, because 245 they use <code>post_read_request</code> for this</dd> 246 247 <dt><code>ap_hook_insert_filter</code></dt> 248 <dd>to insert filters into the filter chain</dd> 249 250 <dt><code>ap_hook_log_transaction</code></dt> 251 <dd>log information about the request</dd> 252 253 <dt><code>ap_hook_optional_fn_retrieve</code></dt> 254 <dd>retrieve any functions registered as optional</dd> 255 256 <dt><code>ap_hook_post_read_request</code></dt> 257 <dd>called after reading the request, before any other phase</dd> 258 259 <dt><code>ap_hook_quick_handler</code></dt> 260 <dd>called before any request processing, used by cache modules.</dd> 261 262 <dt><code>ap_hook_translate_name</code></dt> 263 <dd>translate the URI into a filename</dd> 264 265 <dt><code>ap_hook_type_checker</code></dt> 266 <dd>determine and/or set the doc type</dd> 267 </dl> 268 269</div></div> 270<div class="bottomlang"> 271<p><span>Available Languages: </span><a href="/en/developer/modules.html" title="English"> en </a> | 272<a href="/ja/developer/modules.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a></p> 273</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> 274<script type="text/javascript"><!--//--><![CDATA[//><!-- 275var comments_shortname = 'httpd'; 276var comments_identifier = 'http://httpd.apache.org/docs/2.2/developer/modules.html'; 277(function(w, d) { 278 if (w.location.hostname.toLowerCase() == "httpd.apache.org") { 279 d.write('<div id="comments_thread"><\/div>'); 280 var s = d.createElement('script'); 281 s.type = 'text/javascript'; 282 s.async = true; 283 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier; 284 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); 285 } 286 else { 287 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>'); 288 } 289})(window, document); 290//--><!]]></script></div><div id="footer"> 291<p class="apache">Copyright 2013 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> 292<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[//><!-- 293if (typeof(prettyPrint) !== 'undefined') { 294 prettyPrint(); 295} 296//--><!]]></script> 297</body></html>