]> granicus.if.org Git - apache/blob - docs/manual/developer/modguide.xml
Add a small hack to preserve newlines and still retain the links to the doxygen stuff...
[apache] / docs / manual / developer / modguide.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
5 <!-- $LastChangedRevision$ -->
6
7 <!--
8  Licensed to the Apache Software Foundation (ASF) under one or more
9  contributor license agreements.  See the NOTICE file distributed with
10  this work for additional information regarding copyright ownership.
11  The ASF licenses this file to You under the Apache License, Version 2.0
12  (the "License"); you may not use this file except in compliance with
13  the License.  You may obtain a copy of the License at
14
15      http://www.apache.org/licenses/LICENSE-2.0
16
17  Unless required by applicable law or agreed to in writing, software
18  distributed under the License is distributed on an "AS IS" BASIS,
19  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  See the License for the specific language governing permissions and
21  limitations under the License.
22 -->
23
24 <manualpage metafile="modguide.xml.meta">
25 <parentdocument href="./">Developer</parentdocument>
26
27   <title>Developing modules for the Apache HTTP Server 2.4</title>
28
29 <summary>
30 <p>This document explains how you can develop modules for the Apache HTTP 
31 Server 2.4</p>
32 </summary>
33
34 <seealso><a href="request.html">Request Processing in Apache 2.4</a></seealso>
35 <seealso><a href="hooks.html">Apache 2.x Hook Functions</a></seealso>
36
37 <section id="introduction"><title>Introduction</title>
38 <section id="what"><title>What we will be discussing in this document</title>
39 <p>
40 This document will discuss how you can easily create modules for the Apache 
41 HTTP Server 2.4, by exploring an example module called 
42 <code>mod_example</code>. In the first part of this document, the purpose 
43 of this module will be to calculate and print out various digest values for 
44 existing files on your web server, whenever we access the URL <code>
45 http://hostname/filename.sum</code>. For instance, if we want to know the 
46 MD5 digest value of the file located at <code>
47 http://www.example.com/index.html</code>, we would visit <code>
48 http://www.example.com/index.html.sum</code>. 
49 </p>
50
51 <p>
52 In the second part of this document, which deals with configuration 
53 directive and context awareness, we will be looking at a module that simply 
54 write out its own configuration to the client.
55 </p>
56 </section>
57
58 <section id="prerequisites"><title>Prerequisites</title>
59 <p>
60 First and foremost, you are expected to have a basic knowledge of how the C 
61 programming language works. In most cases, we will try to be as pedagogical 
62 as possible and link to documents describing the functions used in the 
63 examples, but there are also many cases where it is necessary to either 
64 just assume that "it works" or do some digging yourself into what the hows 
65 and whys of various function calls. 
66 </p>
67 <p>
68 Lastly, you will need to have a basic understanding of how modules are 
69 loaded and configured in the Apache HTTP Server, as well as how to get the headers for 
70 Apache if you do not have them already, as these are needed for compiling 
71 new modules.
72 </p>
73 </section>
74 <section id="compiling"><title>Compiling your module</title>
75 <p>
76 To compile the source code we are building in this document, we will be 
77 using <a href="../programs/apxs.html">APXS</a>. Assuming your source file 
78 is called mod_example.c, compiling, installing and activating the module is 
79 as simple as: 
80 </p>
81 <example><pre>
82 apxs -i -a -c mod_example.c
83 </pre></example>
84
85 </section>
86 </section>
87
88 <section id="basics"><title>Defining a module</title>
89 <p>
90 <img src="../images/build_a_mod_3.png" alt="Module name tags"/><br/>
91 Every module starts with the same declaration, or name tag if you will, 
92 that defines a module as <em>a separate entity within Apache</em>:</p>
93
94
95 <!-- BEGIN EXAMPLE CODE -->
96 <highlight language="c">
97 module AP_MODULE_DECLARE_DATA   example_module =
98
99     STANDARD20_MODULE_STUFF,
100     create_dir_conf, /* Per-directory configuration handler */
101     merge_dir_conf,  /* Merge handler for per-directory configurations */
102     create_svr_conf, /* Per-server configuration handler */
103     merge_svr_conf,  /* Merge handler for per-server configurations */
104     directives,      /* Any directives we may have for httpd */
105     register_hooks   /* Our hook registering function */
106 };
107 </highlight>
108 <!-- END EXAMPLE CODE -->
109
110 <p>
111 This bit of code lets the server know that we have now registered a new module 
112 in the system, and that its name is <code>example_module</code>. The name 
113 of the module is used primarily for two things:<br/>
114 </p>
115 <ul>
116 <li>Letting the server know how to load the module using the LoadModule</li>
117 <li>Setting up a namespace for the module to use in configurations</li>
118 </ul>
119 <p>
120 For now, we're only concerned with the first purpose of the module name, 
121 which comes into play when we need to load the module:
122 </p>
123 <highlight language="config">
124 LoadModule example_module modules/mod_example.so
125 </highlight>
126 <p>
127 In essence, this tells the server to open up <code>mod_example.so</code> and look for a module 
128 called <code>example_module</code>.
129 </p>
130 <p>
131 Within this name tag of ours is also a bunch of references to how we would 
132 like to handle things: Which directives do we respond to in a configuration 
133 file or .htaccess, how do we operate within specific contexts, and what 
134 handlers are we interested in registering with the Apache HTTP service. We'll 
135 return to all these elements later in this document.
136 </p>
137 </section>
138 <section id="hooking"><title>Getting started: Hooking into the server</title>
139 <section id="hook_intro"><title>An introduction to hooks</title>
140 <p>
141 When handling requests in Apache HTTP Server 2.4, the first thing you will need to do is 
142 create a hook into the request handling process. A hook is essentially a 
143 message telling the server that you are willing to either serve or at least 
144 take a glance at certain requests given by clients. All handlers, whether 
145 it's mod_rewrite, mod_authn_*, mod_proxy and so on, are hooked into 
146 specific parts of the request process. As you are probably aware, modules 
147 serve different purposes; Some are authentication/authorization handlers, 
148 others are file or script handlers while some third modules rewrite URIs or 
149 proxies content. Furthermore, in the end, it is up to the user of the server 
150 how and when each module will come into place. Thus, the server itself does not 
151 presume to know which module is responsible for handling a specific 
152 request, and will ask each module whether they have an interest in a given 
153 request or not. It is then up to each module to either gently decline 
154 serving a request, accept serving it or flat out deny the request from 
155 being served, as authentication/authorization modules do: <br/>
156 <img src="../images/build_a_mod_2.png" alt="Hook handling in httpd"/><br/>
157 To make it a bit easier for handlers such as our mod_example to know 
158 whether the client is requesting content we should handle or not, the server 
159 has directives for hinting to modules whether their assistance is needed or 
160 not. Two of these are <directive module="mod_mime">AddHandler</directive> 
161 and <directive module="core">SetHandler</directive>. Let's take a look at 
162 an example using <directive module="mod_mime">AddHandler</directive>. In 
163 our example case, we want every request ending with .sum to be served by 
164 <code>mod_example</code>, so we'll add a configuration directive that tells 
165 the server to do just that:
166 </p>
167 <highlight language="config">
168 AddHandler example-handler .sum
169 </highlight>
170 <p>
171 What this tells the server is the following: <em>Whenever we receive a request 
172 for a URI ending in .sum, we are to let all modules know that we are 
173 looking for whoever goes by the name of &quot;example-handler&quot; </em>. 
174 Thus, when a request is being served that ends in .sum, the server will let all 
175 modules know, that this request should be served by &quot;example-handler
176 &quot;. As you will see later, when we start building mod_example, we will 
177 check for this handler tag relayed by <code>AddHandler</code> and reply to 
178 the server based on the value of this tag.
179 </p>
180 </section>
181 <section id="hook_declaration"><title>Hooking into httpd</title>
182 <p>
183 To begin with, we only want to create a simple handler, that replies to the 
184 client browser when a specific URL is requested, so we won't bother setting 
185 up configuration handlers and directives just yet. Our initial module 
186 definition will look like this:</p>
187
188
189 <!-- BEGIN EXAMPLE CODE -->
190 <highlight language="c">
191 module AP_MODULE_DECLARE_DATA   example_module =
192 {
193     STANDARD20_MODULE_STUFF,
194     NULL,
195     NULL,
196     NULL,
197     NULL,
198     NULL,
199     register_hooks   /* Our hook registering function */
200 };
201 </highlight>
202 <!-- END EXAMPLE CODE -->
203
204
205 <p>This lets the server know that we are not interesting in anything fancy, we 
206 just want to hook onto the requests and possibly handle some of them. </p> 
207
208 <p> The reference in our example declaration, <code>register_hooks</code> 
209 is the name of a function we will create to manage how we hook onto the 
210 request process. In this example module, the function has just one purpose; 
211 To create a simple hook that gets called after all the rewrites, access 
212 control etc has been handled. Thus, we will let the server know, that we want 
213 to hook into its process as one of the last modules: 
214 </p>
215
216 <!-- BEGIN EXAMPLE CODE -->
217 <highlight language="c">
218 static void register_hooks(apr_pool_t *pool)
219 {
220     /* Create a hook in the request handler, so we get called when a request arrives */
221     ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
222 }
223 </highlight>
224 <!-- END EXAMPLE CODE -->
225
226 <p>
227 The <code>example_handler</code> reference is the function that will handle 
228 the request. We will discuss how to create a handler in the next chapter.
229 </p>
230 </section>
231 <section id="hook_others"><title>Other useful hooks</title>
232 <p>
233 Hooking into the request handling phase is but one of many hooks that you 
234 can create. Some other ways of hooking are:
235 </p>
236 <ul>
237 <li><code>ap_hook_child_init</code>: Place a hook that executes when a child process is spawned (commonly used for initializing modules after the server has forked)</li>
238 <li><code>ap_hook_pre_config</code>: Place a hook that executes before any configuration data has been read (very early hook)</li>
239 <li><code>ap_hook_post_config</code>: Place a hook that executes after configuration has been parsed, but before the server has forked</li>
240 <li><code>ap_hook_translate_name</code>: Place a hook that executes when a URI needs to be translated into a filename on the server (think <code>mod_rewrite</code>)</li>
241 </ul>
242
243 </section>
244 </section>
245
246 <section id="handling"><title>Building a handler</title>
247 <p>
248 A handler is essentially a function that receives a callback when a request 
249 to the server is made. It is passed a record of the current request (how it was 
250 made, which headers and requests were passed along, who's giving the 
251 request and so on), and is put in charge of either telling the server that it's 
252 not interested in the request or handle the request with the tools provided.
253 </p>
254 <section id="simple_handler"><title>A simple &quot;Hello, world!&quot; 
255 handler</title> 
256 <p>Let's start off by making a very simple request handler 
257 that does the following:
258 </p>
259 <ol>
260 <li>Check that this is a request that should be served by &quot;example-handler&quot;</li>
261 <li>Set the content type of our output to <code>text/html</code></li>
262 <li>Write &quot;Hello, world!&quot; back to the client browser</li>
263 <li>Let the server know that we took care of this request and everything went fine</li>
264 </ol>
265 <p>
266 In C code, our example handler will now look like this:
267 </p>
268
269 <!-- BEGIN EXAMPLE CODE -->
270 <highlight language="c">
271 static int example_handler(request_rec *r)
272 {
273     /* First off, we need to check if this is a call for the "example-handler" handler.
274      * If it is, we accept it and do our things, if not, we simply return DECLINED,
275      * and the server will try somewhere else.
276      */
277     if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);
278     
279     /* Now that we are handling this request, we'll write out "Hello, world!" to the client.
280      * To do so, we must first set the appropriate content type, followed by our output.
281      */
282     ap_set_content_type(r, "text/html");
283     ap_rprintf(r, "Hello, world!");
284     
285     /* Lastly, we must tell the server that we took care of this request and everything went fine.
286      * We do so by simply returning the value OK to the server.
287      */
288     return OK;
289 }
290 </highlight>
291 <!-- END EXAMPLE CODE -->
292
293 <p>
294 Now, we put all we have learned together and end up with a program that 
295 looks like 
296 <a href="http://people.apache.org/~humbedooh/mods/examples/mod_example_1.c">mod_example_1.c</a>
297 . The functions used in this example will be explained later in the section 
298 <a href= "#functions">&quot;Some useful functions you should know&quot;</a>. 
299 </p>
300 </section> 
301 <section id="request_rec"><title>The request_rec structure</title> 
302 <p>The most essential part of any request is the <em>request record
303 </em>. In a call to a handler function, this is represented by the <code>
304 request_req* </code> structure passed along with every call that is made. 
305 This struct, typically just refered to as <code>r</code> in modules, 
306 contains all the information you need for your module to fully process any 
307 HTTP request and respond accordingly.</p> <p>Some key elements of the <code>
308 request_req </code> structure are:
309 </p>
310 <ul>
311 <li><code>r-&gt;handler (char*):</code> Contains the name of the handler the server is currently asking to do the handling of this request</li>
312 <li><code>r-&gt;method (char*):</code> Contains the HTTP method being used, f.x. GET or POST</li>
313 <li><code>r-&gt;filename (char*):</code> Contains the translated filename the client is requesting</li>
314 <li><code>r-&gt;args (char*):</code> Contains the query string of the request, if any</li>
315 <li><code>r-&gt;headers_in (apr_table_t*):</code> Contains all the headers sent by the client</li>
316 <li><code>r-&gt;connection (conn_rec*):</code> A record containing information about the current connection</li>
317 <li><code>r-&gt;useragent_ip (char*):</code> The IP address of the client connecting to us</li>
318 <li><code>r-&gt;pool (apr_pool_t*)</code>: The memory pool of this request. We'll discuss this in the &quot;
319 <a href="#memory">Memory management</a>&quot; chapter.</li>
320 </ul>
321 <p>
322 A complete list of all the values contained with in the <code>request_req</code> structure can be found in 
323 the <a href="http://svn.apache.org/repos/asf/httpd/httpd/trunk/include/httpd.h"><code>httpd.h</code></a> header 
324 file or at <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/structrequest__rec.html">http://ci.apache.org/projects/httpd/trunk/doxygen/structrequest__rec.html</a>.
325 </p>
326
327
328 <p>
329 Let's try out some of these variables in another example handler:<br/>
330 </p>
331
332 <!-- BEGIN EXAMPLE CODE -->
333 <highlight language="c">
334 static int example_handler(request_rec *r)
335 {
336     /* Set the appropriate content type */
337     ap_set_content_type(r, &quot;text/html&quot;);
338
339     /* Print out the IP address of the client connecting to us: */
340     ap_rprintf(r, &quot;&lt;h2&gt;Hello, %s!&lt;/h2&gt;&quot;, r-&gt;useragent_ip);
341     
342     /* If we were reached through a GET or a POST request, be happy, else sad. */
343     if ( !strcmp(r-&gt;method, &quot;POST&quot;) || !strcmp(r-&gt;method, &quot;GET&quot;) ) {
344         ap_rputs(&quot;You used a GET or a POST method, that makes us happy!&lt;br/&gt;&quot;, r);
345     }
346     else {
347         ap_rputs(&quot;You did not use POST or GET, that makes us sad :(&lt;br/&gt;&quot;, r);
348     }
349
350     /* Lastly, if there was a query string, let's print that too! */
351     if (r-&gt;args) {
352         ap_rprintf(r, &quot;Your query string was: %s&quot;, r-&gt;args);
353     }
354     return OK;
355 }
356 </highlight>
357 <!-- END EXAMPLE CODE -->
358
359 </section>
360
361 <section id="return_value"><title>Return values</title>
362 <p>
363 Apache relies on return values from handlers to signify whether a request 
364 was handled or not, and if so, whether the request went well or not. If a 
365 module is not interested in handling a specific request, it should always 
366 return the value <code>DECLINED</code>. If it is handling a request, it 
367 should either return the generic value <code>OK</code>, or a specific HTTP 
368 status code, for example:
369 </p>
370
371 <!-- BEGIN EXAMPLE CODE -->
372 <highlight language="c">
373 static int example_handler(request_rec *r)
374 {
375     /* Return 404: Not found */
376     return HTTP_NOT_FOUND;
377 }
378 </highlight>
379 <!-- END EXAMPLE CODE -->
380
381 <p>
382 Returning <code>OK</code> or a HTTP status code does not necessarily mean 
383 that the request will end. The server may still have other handlers that are 
384 interested in this request, for instance the logging modules which, upon a 
385 successful request, will write down a summary of what was requested and how 
386 it went. To do a full stop and prevent any further processing after your 
387 module is done, you can return the value <code>DONE</code> to let the server 
388 know that it should cease all activity on this request and carry on with 
389 the next, without informing other handlers.
390 <br/>
391 <strong>General response codes:</strong>
392 </p>
393 <ul>
394 <li><code>DECLINED</code>: We are not handling this request</li>
395 <li><code>OK</code>: We handled this request and it went well</li>
396 <li><code>DONE</code>: We handled this request and the server should just close this thread without further processing</li>
397 </ul>
398 <p>
399 <strong>HTTP specific return codes (excerpt):</strong>
400 </p>
401 <ul>
402 <li><code>HTTP_OK (200)</code>: Request was okay</li>
403 <li><code>HTTP_MOVED_PERMANENTLY (301)</code>: The resource has moved to a new URL</li>
404 <li><code>HTTP_UNAUTHORIZED (401)</code>: Client is not authorized to visit this page</li>
405 <li><code>HTTP_FORBIDDEN (403)</code>: Permission denied</li>
406 <li><code>HTTP_NOT_FOUND (404)</code>: File not found</li>
407 <li><code>HTTP_INTERNAL_SERVER_ERROR (500)</code>: Internal server error (self explanatory)</li>
408 </ul>
409 </section>
410
411 <section id="functions"><title>Some useful functions you should know</title>
412
413 <ul>
414 <li>
415     <code>ap_rputs(const char *string, request_req *r)</code>: <br/>
416     Sends a string of text to the client. This is a shorthand version of <a 
417     href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#gac827cd0537d2b6213a7c06d7c26cc36e">
418     ap_rwrite</a>.
419     
420
421 <!-- BEGIN EXAMPLE CODE -->
422 <highlight language="c">ap_rputs("Hello, world!", r);</highlight>
423 <!-- END EXAMPLE CODE -->
424
425
426 </li>
427 <li>
428     <code>
429     <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#ga5e91eb6ca777c9a427b2e82bf1eeb81d">ap_rprintf</a></code>: <br/>
430     This function works just like <code>printf</code>, except it sends the result to the client. 
431     
432
433 <!-- BEGIN EXAMPLE CODE -->
434 <highlight language="c">ap_rprintf(r, "Hello, %s!", r->useragent_ip);</highlight>
435 <!-- END EXAMPLE CODE -->
436
437 </li>
438 <li>
439     <code>
440     <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#gaa2f8412c400197338ec509f4a45e4579">ap_set_content_type</a>(request_req *r, const char *type)</code>: <br/>
441     Sets the content type of the output you are sending.
442     
443
444 <!-- BEGIN EXAMPLE CODE -->
445 <highlight language="c">ap_set_content_type(r, "text/plain"); /* force a raw text output */</highlight>
446 <!-- END EXAMPLE CODE -->
447
448 </li>
449
450
451 </ul>
452 </section>
453
454 <section id="memory"><title>Memory management</title>
455 <p>
456 Managing your resources in Apache HTTP Server 2.4 is quite easy, thanks to the memory pool 
457 system. In essence, each server, connection and request have their own 
458 memory pool that gets cleaned up when its scope ends, e.g. when a request 
459 is done or when a server process shuts down. All your module needs to do is 
460 latch onto this memory pool, and you won't have to worry about having to 
461 clean up after yourself - pretty neat, huh?
462 </p>
463
464 <p>
465 In our module, we will primarily be allocating memory for each request, so 
466 it's appropriate to use the <code>r-&gt;pool</code> 
467 reference when creating new objects. A few of the functions for allocating 
468 memory within a pool are:
469 </p>
470 <ul>
471 <li><code>void* <a href="http://apr.apache.org/docs/apr/1.4/group__apr__pools.html#ga85f1e193c31d109affda72f9a92c6915">apr_palloc</a>(
472 apr_pool_t *p, apr_size_t size)</code>: Allocates <code>size</code> number of bytes in the pool for you</li>
473 <li><code>void* <a href="http://apr.apache.org/docs/apr/1.4/group__apr__pools.html#gaf61c098ad258069d64cdf8c0a9369f9e">apr_pcalloc</a>(
474 apr_pool_t *p, apr_size_t size)</code>: Allocates <code>size</code> number of bytes in the pool for you and sets all bytes to 0</li>
475 <li><code>char* <a href="http://apr.apache.org/docs/apr/1.4/group__apr__strings.html#gabc79e99ff19abbd7cfd18308c5f85d47">apr_pstrdup</a>(
476 apr_pool_t *p, const char *s)</code>: Creates a duplicate of the string <code>s</code>. This is useful for copying constant values so you can edit them</li>
477 <li><code>char* <a href="http://apr.apache.org/docs/apr/1.4/group__apr__strings.html#ga3eca76b8d293c5c3f8021e45eda813d8">apr_psprintf</a>(
478 apr_pool_t *p, const char *fmt, ...)</code>: Similar to <code>sprintf</code>, except the server supplies you with an appropriately allocated target variable</li>
479 </ul>
480
481 <p>Let's put these functions into an example handler:</p>
482
483
484 <!-- BEGIN EXAMPLE CODE -->
485 <highlight language="c">
486 static int example_handler(request_rec *r)
487 {
488     const char* original = "You can't edit this!";
489     char* copy;
490     int* integers;
491     
492     /* Allocate space for 10 integer values and set them all to zero. */
493     integers = apr_pcalloc(r->pool, sizeof(int)*10); 
494     
495     /* Create a copy of the 'original' variable that we can edit. */
496     copy = apr_pstrdup(r->pool, original);
497     return OK;
498 }
499 </highlight>
500 <!-- END EXAMPLE CODE -->
501
502 <p>
503 This is all well and good for our module, which won't need any 
504 pre-initialized variables or structures. However, if we wanted to 
505 initialize something early on, before the requests come rolling in, we 
506 could simply add a call to a function in our <code>register_hooks</code> 
507 function to sort it out:
508 </p>
509
510 <!-- BEGIN EXAMPLE CODE -->
511 <highlight language="c">
512 static void register_hooks(apr_pool_t *pool)
513 {
514     /* Call a function that initializes some stuff */
515     example_init_function(pool);
516     /* Create a hook in the request handler, so we get called when a request arrives */
517     ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
518 }
519 </highlight>
520 <!-- END EXAMPLE CODE -->
521
522 <p>
523 In this pre-request initialization function we would not be using the 
524 same pool as we did when allocating resources for request-based functions. 
525 Instead, we would use the pool given to us by the server for allocating memory 
526 on a per-process based level.
527 </p>
528 </section>
529
530 <section id="parsing"><title>Parsing request data</title>
531 <p>
532 In our example module, we would like to add a feature, that checks which 
533 type of digest, MD5 or SHA1 the client would like to see. This could be 
534 solved by adding a query string to the request. A query string is typically 
535 comprised of several keys and values put together in a string, for instance 
536 <code>valueA=yes&amp;valueB=no&amp;valueC=maybe</code>. It is up to the 
537 module itself to parse these and get the data it requires. In our example, 
538 we'll be looking for a key called <code>digest</code>, and if set to <code>
539 md5</code>, we'll produce an MD5 digest, otherwise we'll produce a SHA1 
540 digest.
541 </p>
542 <p>
543 Since the introduction of Apache HTTP Server 2.4, parsing request data from GET and 
544 POST requests have never been easier. All we require to parse both GET and 
545 POST data is four simple lines:
546 </p> 
547
548
549 <!-- BEGIN EXAMPLE CODE -->
550 <highlight language="c">
551 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__apr__tables.html#gad7ea82d6608a4a633fc3775694ab71e4">apr_table_t</a> *GET; <em>
552 </em><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/structapr__array__header__t.html">apr_array_header_t</a>*POST; 
553 <em>
554 </em>
555 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__SCRIPT.html#gaed25877b529623a4d8f99f819ba1b7bd">
556 ap_args_to_table</a>(r, &amp;GET); <em>
557 </em><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__DAEMON.html#ga9d426b6382b49754d4f87c55f65af202">
558 ap_parse_form_data</a>(r, NULL, &amp;POST, -1, 8192); 
559 </highlight>
560 <!-- END EXAMPLE CODE -->
561
562 <p>
563 In our specific example module, we're looking for the <code>digest</code> 
564 value from the query string, which now resides inside a table called <code>
565 GET</code>. To extract this value, we need only perform a simple operation:
566 </p>
567
568
569 <!-- BEGIN EXAMPLE CODE -->
570 <highlight language="c">
571 /* Get the "digest" key from the query string, if any. */
572 const char *digestType = apr_table_get(GET, "digest");
573
574 /* If no key was returned, we will set a default value instead. */
575 if (!digestType) digestType = "sha1";
576 </highlight>
577 <!-- END EXAMPLE CODE -->
578
579 <p>
580 The structures used for the POST and GET data are not exactly the same, so 
581 if we were to fetch a value from POST data instead of the query string, we 
582 would have to resort to a few more lines, as outlined in <a href="#get_post"
583 >this example</a> in the last chapter of this document.
584 </p>
585 </section>
586
587 <section id="advanced_handler"><title>Making an advanced handler</title>
588 <p>
589 Now that we have learned how to parse form data and manage our resources, 
590 we can move on to creating an advanced version of our module, that spits 
591 out the MD5 or SHA1 digest of files:
592 </p>
593
594
595 <!-- BEGIN EXAMPLE CODE -->
596 <highlight language="c">
597 static int example_handler(request_rec *r)
598 {
599     int rc, exists;
600     apr_finfo_t finfo;
601     apr_file_t *file;
602     char *filename;
603     char buffer[256];
604     apr_size_t readBytes;
605     int n;
606     apr_table_t *GET;
607     apr_array_header_t *POST;
608     const char *digestType;
609     
610     
611     /* Check that the &quot;example-handler&quot; handler is being called. */
612     if (!r-&gt;handler || strcmp(r-&gt;handler, &quot;example-handler&quot;)) return (DECLINED);
613     
614     /* Figure out which file is being requested by removing the .sum from it */
615     filename = apr_pstrdup(r-&gt;pool, r-&gt;filename);
616     filename[strlen(filename)-4] = 0; /* Cut off the last 4 characters. */
617     
618     /* Figure out if the file we request a sum on exists and isn't a directory */
619     rc = apr_stat(&amp;finfo, filename, APR_FINFO_MIN, r-&gt;pool);
620     if (rc == APR_SUCCESS) {
621         exists =
622         (
623             (finfo.filetype != APR_NOFILE)
624         &amp;&amp;  !(finfo.filetype &amp; APR_DIR)
625         );
626         if (!exists) return HTTP_NOT_FOUND; /* Return a 404 if not found. */
627     }
628     /* If apr_stat failed, we're probably not allowed to check this file. */
629     else return HTTP_FORBIDDEN;
630     
631     /* Parse the GET and, optionally, the POST data sent to us */
632     
633     ap_args_to_table(r, &amp;GET);
634     ap_parse_form_data(r, NULL, &amp;POST, -1, 8192);
635     
636     /* Set the appropriate content type */
637     ap_set_content_type(r, &quot;text/html&quot;);
638     
639     /* Print a title and some general information */
640     ap_rprintf(r, &quot;&lt;h2&gt;Information on %s:&lt;/h2&gt;&quot;, filename);
641     ap_rprintf(r, &quot;&lt;b&gt;Size:&lt;/b&gt; %u bytes&lt;br/&gt;&quot;, finfo.size);
642     
643     /* Get the digest type the client wants to see */
644     digestType = apr_table_get(GET, &quot;digest&quot;);
645     if (!digestType) digestType = &quot;MD5&quot;;
646     
647     
648     rc = apr_file_open(&amp;file, filename, APR_READ, APR_OS_DEFAULT, r-&gt;pool);
649     if (rc == APR_SUCCESS) {
650         
651         /* Are we trying to calculate the MD5 or the SHA1 digest? */
652         if (!strcasecmp(digestType, &quot;md5&quot;)) {
653             /* Calculate the MD5 sum of the file */
654             union {
655                 char      chr[16];
656                 uint32_t  num[4];
657             } digest;
658             apr_md5_ctx_t md5;
659             apr_md5_init(&amp;md5);
660             readBytes = 256;
661             while ( apr_file_read(file, buffer, &amp;readBytes) == APR_SUCCESS ) {
662                 apr_md5_update(&amp;md5, buffer, readBytes);
663             }
664             apr_md5_final(digest.chr, &amp;md5);
665             
666             /* Print out the MD5 digest */
667             ap_rputs(&quot;&lt;b&gt;MD5: &lt;/b&gt;&lt;code&gt;&quot;, r);
668             for (n = 0; n &lt; APR_MD5_DIGESTSIZE/4; n++) {
669                 ap_rprintf(r, &quot;%08x&quot;, digest.num[n]);
670             }
671             ap_rputs(&quot;&lt;/code&gt;&quot;, r);
672             /* Print a link to the SHA1 version */
673             ap_rputs(&quot;&lt;br/&gt;&lt;a href='?digest=sha1'&gt;View the SHA1 hash instead&lt;/a&gt;&quot;, r);
674         }
675         else {
676             /* Calculate the SHA1 sum of the file */
677             union {
678                 char      chr[20];
679                 uint32_t  num[5];
680             } digest;
681             apr_sha1_ctx_t sha1;
682             apr_sha1_init(&amp;sha1);
683             readBytes = 256;
684             while ( apr_file_read(file, buffer, &amp;readBytes) == APR_SUCCESS ) {
685                 apr_sha1_update(&amp;sha1, buffer, readBytes);
686             }
687             apr_sha1_final(digest.chr, &amp;sha1);
688             
689             /* Print out the SHA1 digest */
690             ap_rputs(&quot;&lt;b&gt;SHA1: &lt;/b&gt;&lt;code&gt;&quot;, r);
691             for (n = 0; n &lt; APR_SHA1_DIGESTSIZE/4; n++) {
692                 ap_rprintf(r, &quot;%08x&quot;, digest.num[n]);
693             }
694             ap_rputs(&quot;&lt;/code&gt;&quot;, r);
695             
696             /* Print a link to the MD5 version */
697             ap_rputs(&quot;&lt;br/&gt;&lt;a href='?digest=md5'&gt;View the MD5 hash instead&lt;/a&gt;&quot;, r);
698         }
699         apr_file_close(file);
700         
701     }    
702     /* Let the server know that we responded to this request. */
703     return OK;
704 }
705 </highlight>
706 <!-- END EXAMPLE CODE -->
707
708 <p>
709 This version in its entirity can be found here: 
710 <a href="http://people.apache.org/~humbedooh/mods/examples/mod_example_2.c">mod_example_2.c</a>.
711 </p>
712 </section>
713
714 </section>
715
716 <section id="configuration"><title>Adding configuration options</title>
717 <p>
718 In this next segment of this document, we will turn our eyes away from the 
719 digest module and create a new example module, whose only function is to 
720 write out its own configuration. The purpose of this is to examine how 
721 the server works with configuration, and what happens when you start writing 
722 advanced configurations 
723 for your modules.
724 </p>
725 <section id="config_intro"><title>An introduction to configuration 
726 directives</title>
727 <p>
728 If you are reading this, then you probably already know 
729 what a configuration directive is. Simply put, a directive is a way of 
730 telling an individual module (or a set of modules) how to behave, such as 
731 these directives control how <code>mod_rewrite</code> works:
732 </p>
733 <highlight language="config">
734 RewriteEngine On
735 RewriteCond %{REQUEST_URI} ^/foo/bar
736 RewriteRule ^/foo/bar/(.*)$ /foobar?page=$1
737 </highlight>
738 <p>
739 Each of these configuration directives are handled by a separate function, 
740 that parses the parameters given and sets up a configuration accordingly.
741 </p>
742 </section>
743 <section id="config_simple"><title>Making an example configuration</title>
744 <p>To begin with, we'll create a basic configuration in C-space:</p>
745
746
747 <!-- BEGIN EXAMPLE CODE -->
748 <highlight language="c">
749 typedef struct {
750     int         enabled;      /* Enable or disable our module */
751     const char *path;         /* Some path to...something */
752     int         typeOfAction; /* 1 means action A, 2 means action B and so on */
753 } example_config;
754 </highlight>
755 <!-- END EXAMPLE CODE -->
756
757 <p>
758 Now, let's put this into perspective by creating a very small module that 
759 just prints out a hard-coded configuration. You'll notice that we use the 
760 <code>register_hooks</code> function for initializing the configuration 
761 values to their defaults:
762 </p>
763
764 <!-- BEGIN EXAMPLE CODE -->
765 <highlight language="c">
766 typedef struct {
767     int         enabled;      /* Enable or disable our module */
768     const char *path;         /* Some path to...something */
769     int         typeOfAction; /* 1 means action A, 2 means action B and so on */
770 } example_config;
771
772 static example_config config;
773
774 static int example_handler(request_rec *r)
775 {
776     if (!r-&gt;handler || strcmp(r-&gt;handler, &quot;example-handler&quot;)) return(DECLINED);
777     ap_set_content_type(r, &quot;text/plain&quot;);
778     ap_rprintf(r, &quot;Enabled: %u\n&quot;, config.enabled);
779     ap_rprintf(r, &quot;Path: %s\n&quot;, config.path);
780     ap_rprintf(r, &quot;TypeOfAction: %x\n&quot;, config.typeOfAction);
781     return OK;
782 }
783
784 static void register_hooks(apr_pool_t *pool) 
785 {
786     config.enabled = 1;
787     config.path = &quot;/foo/bar&quot;;
788     config.typeOfAction = 0x00;
789     ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
790 }
791
792 /* Define our module as an entity and assign a function for registering hooks  */
793
794 module AP_MODULE_DECLARE_DATA   example_module =
795 {
796     STANDARD20_MODULE_STUFF,
797     NULL,            /* Per-directory configuration handler */
798     NULL,            /* Merge handler for per-directory configurations */
799     NULL,            /* Per-server configuration handler */
800     NULL,            /* Merge handler for per-server configurations */
801     NULL,            /* Any directives we may have for httpd */
802     register_hooks   /* Our hook registering function */
803 };
804 </highlight>
805 <!-- END EXAMPLE CODE -->
806
807 <p>
808 So far so good. To access our new handler, we could add the following to 
809 our configuration:
810 </p>
811 <highlight language="config">
812 &lt;Location /example&gt;
813     SetHandler example-handler
814 &lt;/Location&gt;
815 </highlight>
816 <p>
817 When we visit, we'll see our current configuration being spit out by our 
818 module. 
819 </p>
820 </section>
821
822 <section id="register_directive"><title>Registering directives with the server</title>
823 <p>
824 What if we want to change our configuration, not by hard-coding new values 
825 into the module, but by using either the httpd.conf file or possibly a 
826 .htaccess file? It's time to let the server know that we want this to be 
827 possible. To do so, we must first change our <em>name tag</em> to include a 
828 reference to the configuration directives we want to register with the server:
829 </p>
830
831 <!-- BEGIN EXAMPLE CODE -->
832 <highlight language="c">
833 module AP_MODULE_DECLARE_DATA   example_module =
834 {
835     STANDARD20_MODULE_STUFF,
836     NULL,               /* Per-directory configuration handler */
837     NULL,               /* Merge handler for per-directory configurations */
838     NULL,               /* Per-server configuration handler */
839     NULL,               /* Merge handler for per-server configurations */
840     example_directives, /* Any directives we may have for httpd */
841     register_hooks      /* Our hook registering function */
842 };
843 </highlight>
844 <!-- END EXAMPLE CODE -->
845
846 <p>
847 This will tell the server that we are now accepting directives from the 
848 configuration files, and that the structure called <code>example_directives
849 </code> holds information on what our directives are and how they work. 
850 Since we have three different variables in our module configuration, we 
851 will add a structure with three directives and a NULL at the end:
852 </p>
853
854 <!-- BEGIN EXAMPLE CODE -->
855 <highlight language="c">
856 static const command_rec        example_directives[] =
857 {
858     AP_INIT_TAKE1("exampleEnabled", example_set_enabled, NULL, RSRC_CONF, "Enable or disable mod_example"),
859     AP_INIT_TAKE1("examplePath", example_set_path, NULL, RSRC_CONF, "The path to whatever"),
860     AP_INIT_TAKE2("exampleAction", example_set_action, NULL, RSRC_CONF, "Special action value!"),
861     { NULL }
862 };
863 </highlight>
864 <!-- END EXAMPLE CODE -->
865
866 <p>
867 <img src="../images/build_a_mod_4.png" alt="Directives structure"/><br />
868 As you can see, each directive needs at least 5 parameters set:
869 </p>
870 <ol>
871 <li><code><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga07c7d22ae17805e61204463326cf9c34">AP_INIT_TAKE1</a></code>: This is a macro that tells the server that this directive takes one and only one argument. 
872 If we required two arguments, we could use the macro <code><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#gafaec43534fcf200f37d9fecbf9247c21">AP_INIT_TAKE2</a></code> and so on (refer to httpd_conf.h 
873 for more macros).</li>
874 <li><code>exampleEnabled</code>: This is the name of our directive. More precisely, it is what the user must put in his/her 
875 configuration in order to invoke a configuration change in our module.</li>
876 <li><code>example_set_enabled</code>: This is a reference to a C function that parses the directive and sets the configuration 
877 accordingly. We will discuss how to make this in the following paragraph.</li>
878 <li><code>RSRC_CONF</code>: This tells the server where the directive is permitted. We'll go into details on this value in the 
879 later chapters, but for now, <code>RSRC_CONF</code> means that the server will only accept these directives in a server context.</li>
880 <li><code>"Enable or disable...."</code>: This is simply a brief description of what the directive does.</li>
881 </ol>
882 <p>
883 (<em>The "missing" parameter in our definition, which is usually set to 
884 <code>NULL</code>, is an optional function that can be run after the 
885 initial function to parse the arguments have been run. This is usually 
886 omitted, as the function for verifying arguments might as well be used to 
887 set them.</em>)
888 </p>
889 </section>
890 <section id="directive_handler"><title>The directive handler function</title>
891 <p>
892 Now that we've told the server to expect some directives for our module, it's 
893 time to make a few functions for handling these. What the server reads in the 
894 configuration file(s) is text, and so naturally, what it passes along to 
895 our directive handler is one or more strings, that we ourselves need to 
896 recognize and act upon. You'll notice, that since we set our <code>
897 exampleAction</code> directive to accept two arguments, its C function also 
898 has an additional parameter defined:</p> 
899
900 <!-- BEGIN EXAMPLE CODE -->
901 <highlight language="c">
902 /* Handler for the "exambleEnabled" directive */
903 const char *example_set_enabled(cmd_parms *cmd, void *cfg, const char *arg)
904 {
905     if(!strcasecmp(arg, "on")) config.enabled = 1;
906     else config.enabled = 0;
907     return NULL;
908 }
909
910 /* Handler for the "examplePath" directive */
911 const char *example_set_path(cmd_parms *cmd, void *cfg, const char *arg)
912 {
913     config.path = arg;
914     return NULL;
915 }
916
917 /* Handler for the "exampleAction" directive */
918 /* Let's pretend this one takes one argument (file or db), and a second (deny or allow), */
919 /* and we store it in a bit-wise manner. */
920 const char *example_set_action(cmd_parms *cmd, void *cfg, const char *arg1, const char* arg2)
921 {
922     if(!strcasecmp(arg1, "file")) config.typeOfAction = 0x01;
923     else config.typeOfAction = 0x02;
924     
925     if(!strcasecmp(arg2, "deny")) config.typeOfAction += 0x10;
926     else config.typeOfAction += 0x20;
927     return NULL;
928 }
929 </highlight>
930 <!-- END EXAMPLE CODE -->
931
932
933 </section>
934 <section id="directive_complete"><title>Putting it all together</title>
935 <p>
936 Now that we have our directives set up, and handlers configured for them, 
937 we can assemble our module into one big file:
938 </p>
939
940 <!-- BEGIN EXAMPLE CODE -->
941 <highlight language="c">
942 /* mod_example_config_simple.c: */
943 #include &lt;stdio.h&gt;
944 #include "apr_hash.h"
945 #include "ap_config.h"
946 #include "ap_provider.h"
947 #include "httpd.h"
948 #include "http_core.h"
949 #include "http_config.h"
950 #include "http_log.h"
951 #include "http_protocol.h"
952 #include "http_request.h"
953
954 /*
955  ==============================================================================
956  Our configuration prototype and declaration:
957  ==============================================================================
958  */
959 typedef struct {
960     int         enabled;      /* Enable or disable our module */
961     const char *path;         /* Some path to...something */
962     int         typeOfAction; /* 1 means action A, 2 means action B and so on */
963 } example_config;
964
965 static example_config config;
966
967 /*
968  ==============================================================================
969  Our directive handlers:
970  ==============================================================================
971  */
972 /* Handler for the &quot;exambleEnabled&quot; directive */
973 const char *example_set_enabled(cmd_parms *cmd, void *cfg, const char *arg)
974 {
975     if(!strcasecmp(arg, &quot;on&quot;)) config.enabled = 1;
976     else config.enabled = 0;
977     return NULL;
978 }
979
980 /* Handler for the &quot;examplePath&quot; directive */
981 const char *example_set_path(cmd_parms *cmd, void *cfg, const char *arg)
982 {
983     config.path = arg;
984     return NULL;
985 }
986
987 /* Handler for the &quot;exampleAction&quot; directive */
988 /* Let's pretend this one takes one argument (file or db), and a second (deny or allow), */
989 /* and we store it in a bit-wise manner. */
990 const char *example_set_action(cmd_parms *cmd, void *cfg, const char *arg1, const char* arg2)
991 {
992     if(!strcasecmp(arg1, &quot;file&quot;)) config.typeOfAction = 0x01;
993     else config.typeOfAction = 0x02;
994     
995     if(!strcasecmp(arg2, &quot;deny&quot;)) config.typeOfAction += 0x10;
996     else config.typeOfAction += 0x20;
997     return NULL;
998 }
999
1000 /*
1001  ==============================================================================
1002  The directive structure for our name tag:
1003  ==============================================================================
1004  */
1005 static const command_rec        example_directives[] =
1006 {
1007     AP_INIT_TAKE1(&quot;exampleEnabled&quot;, example_set_enabled, NULL, RSRC_CONF, &quot;Enable or disable mod_example&quot;),
1008     AP_INIT_TAKE1(&quot;examplePath&quot;, example_set_path, NULL, RSRC_CONF, &quot;The path to whatever&quot;),
1009     AP_INIT_TAKE2(&quot;exampleAction&quot;, example_set_action, NULL, RSRC_CONF, &quot;Special action value!&quot;),
1010     { NULL }
1011 };
1012 /*
1013  ==============================================================================
1014  Our module handler:
1015  ==============================================================================
1016  */
1017 static int example_handler(request_rec *r)
1018 {
1019     if(!r-&gt;handler || strcmp(r-&gt;handler, &quot;example-handler&quot;)) return(DECLINED);
1020     ap_set_content_type(r, &quot;text/plain&quot;);
1021     ap_rprintf(r, &quot;Enabled: %u\n&quot;, config.enabled);
1022     ap_rprintf(r, &quot;Path: %s\n&quot;, config.path);
1023     ap_rprintf(r, &quot;TypeOfAction: %x\n&quot;, config.typeOfAction);
1024     return OK;
1025 }
1026
1027 /*
1028  ==============================================================================
1029  The hook registration function (also initializes the default config values):
1030  ==============================================================================
1031  */
1032 static void register_hooks(apr_pool_t *pool) 
1033 {
1034     config.enabled = 1;
1035     config.path = &quot;/foo/bar&quot;;
1036     config.typeOfAction = 3;
1037     ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
1038 }
1039 /*
1040  ==============================================================================
1041  Our module name tag:
1042  ==============================================================================
1043  */
1044 module AP_MODULE_DECLARE_DATA   example_module =
1045 {
1046     STANDARD20_MODULE_STUFF,
1047     NULL,               /* Per-directory configuration handler */
1048     NULL,               /* Merge handler for per-directory configurations */
1049     NULL,               /* Per-server configuration handler */
1050     NULL,               /* Merge handler for per-server configurations */
1051     example_directives, /* Any directives we may have for httpd */
1052     register_hooks      /* Our hook registering function */
1053 };
1054 </highlight>
1055 <!-- END EXAMPLE CODE -->
1056
1057
1058 <p>
1059 In our httpd.conf file, we can now change the hard-coded configuration by 
1060 adding a few lines:
1061 </p>
1062 <highlight language="config">
1063 ExampleEnabled On
1064 ExamplePath "/usr/bin/foo"
1065 ExampleAction file allow
1066 </highlight>
1067 <p>
1068 And thus we apply the configuration, visit <code>/example</code> on our 
1069 web site, and we see the configuration has adapted to what we wrote in our 
1070 configuration file.
1071 </p>
1072 </section>
1073
1074
1075 </section>
1076
1077 <section id="context"><title>Context aware configurations</title>
1078 <section id="context_intro"><title>Introduction to context aware configurations</title>
1079 <p>
1080 In Apache HTTP Server 2.4, different URLs, virtual hosts, directories etc can have very 
1081 different meanings to the user of the server, and thus different contexts 
1082 within which modules must operate. For example, let's assume you have this 
1083 configuration set up for mod_rewrite:
1084 </p>
1085 <highlight language="config">
1086 &lt;Directory &quot;/var/www&quot;&gt;
1087     RewriteCond %{HTTP_HOST} ^example.com$
1088     RewriteRule (.*) http://www.example.com/$1
1089 &lt;/Directory&gt;
1090 &lt;Directory &quot;/var/www/sub&quot;&gt;
1091     RewriteRule ^foobar$ index.php?foobar=true
1092 &lt;/Directory&gt;
1093 </highlight>
1094 <p>
1095 In this example, you will have set up two different contexts for 
1096 mod_rewrite:</p>
1097 <ol>
1098 <li>Inside <code>/var/www</code>, all requests for <code>http://example.com</code> must go to <code>http://www.example.com</code></li>
1099 <li>Inside <code>/var/www/sub</code>, all requests for <code>foobar</code> must go to <code>index.php?foobar=true</code></li>
1100 </ol>
1101 <p>
1102 If mod_rewrite (or the entire server for that matter) wasn't context aware, then 
1103 these rewrite rules would just apply to every and any request made, 
1104 regardless of where and how they were made, but since the module can pull 
1105 the context specific configuration straight from the server, it does not need 
1106 to know itself, which of the directives are valid in this context, since 
1107 the server takes care of this.</p>
1108
1109 <p>
1110 So how does a module get the specific configuration for the server, 
1111 directory or location in question? It does so by making one simple call:
1112 </p>
1113
1114 <!-- BEGIN EXAMPLE CODE -->
1115 <highlight language="c">
1116 example_config *config = (example_config*) <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga1093a5908a384eacc929b028c79f2a02">ap_get_module_config</a>(r-&gt;per_dir_config, &amp;example_module);
1117 </highlight>
1118 <!-- END EXAMPLE CODE -->
1119
1120 <p>
1121 That's it! Of course, a whole lot goes on behind the scenes, which we will 
1122 discuss in this chapter, starting with how the server came to know what our 
1123 configuration looks like, and how it came to be set up as it is in the 
1124 specific context.
1125 </p>
1126 </section>
1127
1128 <section id="context_base"><title>Our basic configuration setup</title>
1129 <p>In this chapter, we will be working with a slightly modified version of 
1130 our previous context structure. We will set a <code>context</code> 
1131 variable that we can use to track which context configuration is being 
1132 used by the server in various places:
1133 </p>
1134 <!-- BEGIN EXAMPLE CODE -->
1135 <highlight language="c">
1136 typedef struct {
1137     char        context[256];
1138     char        path[256];
1139     int         typeOfAction;
1140     int         enabled;
1141 } example_config;
1142 </highlight>
1143 <!-- END EXAMPLE CODE -->
1144
1145 <p>Our handler for requests will also be modified, yet still very simple:</p>
1146
1147
1148 <!-- BEGIN EXAMPLE CODE -->
1149 <highlight language="c">
1150 static int example_handler(request_rec *r)
1151 {
1152     if(!r->handler || strcmp(r->handler, "example-handler")) return(DECLINED);
1153     example_config *config = (example_config*) ap_get_module_config(r->per_dir_config, &amp;example_module);
1154     ap_set_content_type(r, "text/plain");
1155     ap_rprintf("Enabled: %u\n", config->enabled);
1156     ap_rprintf("Path: %s\n", config->path);
1157     ap_rprintf("TypeOfAction: %x\n", config->typeOfAction);
1158     ap_rprintf("Context: %s\n", config->context);
1159     return OK;
1160 }
1161 </highlight>
1162 <!-- END EXAMPLE CODE -->
1163
1164 </section>
1165
1166 <section id="context_which"><title>Choosing a context</title>
1167 <p>
1168 Before we can start making our module context aware, we must first define, 
1169 which contexts we will accept. As we saw in the previous chapter, defining 
1170 a directive required five elements be set:</p>
1171
1172
1173 <!-- BEGIN EXAMPLE CODE -->
1174 <highlight language="c">
1175 AP_INIT_TAKE1("exampleEnabled", example_set_enabled, NULL, RSRC_CONF, "Enable or disable mod_example"),
1176 </highlight>
1177 <!-- END EXAMPLE CODE -->
1178
1179
1180 <p>The <code>RSRC_CONF</code> definition told the server that we would only allow 
1181 this directive in a global server context, but since we are now trying out 
1182 a context aware version of our module, we should set this to something 
1183 more lenient, namely the value <code>ACCESS_CONF</code>, which lets us use 
1184 the directive inside &lt;Directory&gt; and &lt;Location&gt; blocks.
1185 </p>
1186 </section>
1187
1188 <section id="context_pool"><title>Using the server to allocate configuration slots</title>
1189 <p> A much smarter way to manage your configurations is by letting the server 
1190 help you create them. To do so, we must first start off by changing our 
1191 <em>name tag</em> to let the server know, that it should assist us in creating 
1192 and managing our configurations. Since we have chosen the per-directory 
1193 (or per-location) context for our module configurations, we'll add a 
1194 per-directory creator and merger function reference in our tag:</p>
1195
1196 <!-- BEGIN EXAMPLE CODE -->
1197 <highlight language="c">
1198 module AP_MODULE_DECLARE_DATA   example_module =
1199 {
1200     STANDARD20_MODULE_STUFF,
1201     create_dir_conf, /* Per-directory configuration handler */
1202     merge_dir_conf,  /* Merge handler for per-directory configurations */
1203     NULL,            /* Per-server configuration handler */
1204     NULL,            /* Merge handler for per-server configurations */
1205     directives,      /* Any directives we may have for httpd */
1206     register_hooks   /* Our hook registering function */
1207 };
1208 </highlight>
1209 <!-- END EXAMPLE CODE -->
1210
1211
1212
1213 </section>
1214
1215 <section id="context_new"><title>Creating new context configurations</title>
1216 <p>
1217 Now that we have told the server to help us create and manage configurations, 
1218 our first step is to make a function for creating new, blank 
1219 configurations. We do so by creating the function we just referenced in 
1220 our name tag as the Per-directory configuration handler:</p>
1221 <!-- BEGIN EXAMPLE CODE -->
1222 <highlight language="c">
1223 void* example_create_dir_conf(apr_pool_t* pool, char* context) {
1224     context = context ? context : "(undefined context)";
1225     example_config *cfg = apr_pcalloc(pool, sizeof(example_config));
1226     if(cfg) {
1227         /* Set some default values */
1228         strcpy(cfg->context, x);
1229         cfg->enabled = 0;
1230         cfg->path = "/foo/bar";
1231         cfg->typeOfAction = 0x11;
1232     }
1233     return cfg;
1234 }
1235 </highlight>
1236 <!-- END EXAMPLE CODE -->
1237
1238
1239 </section>
1240
1241 <section id="context_merge"><title>Merging configurations</title>
1242 <p>
1243 Our next step in creating a context aware configuration is merging 
1244 configurations. This part of the process particularly apply to scenarios 
1245 where you have a parent configuration and a child, such as the following: 
1246 </p>
1247 <highlight language="config">
1248 &lt;Directory &quot;/var/www&quot;&gt;
1249     ExampleEnable On
1250     ExamplePath /foo/bar
1251     ExampleAction file allow
1252 &lt;/Directory&gt;
1253 &lt;Directory &quot;/var/www/subdir&quot;&gt;
1254     ExampleAction file deny
1255 &lt;/Directory&gt;
1256 </highlight>
1257 <p>
1258 In this example, it is natural to assume that the directory <code>
1259 /var/www/subdir</code> should inherit the value set for the <code>/var/www
1260 </code> directory, as we did not specify a <code>ExampleEnable</code> nor 
1261 an <code>ExamplePath</code> for this directory. The server does not presume to 
1262 know if this is true, but cleverly does the following:
1263 </p>
1264 <ol>
1265 <li>Creates a new configuration for <code>/var/www</code></li>
1266 <li>Sets the configuration values according to the directives given for <code>/var/www</code></li>
1267 <li>Creates a new configuration for <code>/var/www/subdir</code></li>
1268 <li>Sets the configuration values according to the directives given for <code>/var/www/subdir</code></li>
1269 <li><strong>Proposes a merge</strong> of the two configurations into a new configuration for <code>/var/www/subdir</code></li>
1270 </ol>
1271 <p>
1272 This proposal is handled by the <code>merge_dir_conf</code> function we 
1273 referenced in our name tag. The purpose of this function is to assess the 
1274 two configurations and decide how they are to be merged:</p>
1275
1276
1277 <!-- BEGIN EXAMPLE CODE -->
1278 <highlight language="c">
1279 void* merge_dir_conf(apr_pool_t* pool, void* BASE, void* ADD) {
1280     example_config* base = (example_config *) BASE ;
1281     example_config* add = (example_config *) ADD ;
1282     example_config* conf = (example_config *) create_dir_conf(pool, "Merged configuration");
1283     
1284     conf->enabled = ( add->enabled == 0 ) ? base->enabled : add->enabled ;
1285     conf->typeOfAction = add->typeOfAction ? add->typeOfAction : base->typeOfAction;
1286     strcpy(conf->path, strlen(add->path) ? add->path : base->path);
1287     
1288     return conf ;
1289 }
1290 </highlight>
1291 <!-- END EXAMPLE CODE -->
1292
1293
1294 </section>
1295
1296 <section id="context_example"><title>Trying out our new context aware configurations</title>
1297 <p>
1298 Now, let's try putting it all together to create a new module that is 
1299 context aware. First off, we'll create a configuration that lets us test 
1300 how the module works:
1301 </p>
1302 <highlight language="config">
1303 &lt;Location &quot;/a&quot;&gt;
1304     SetHandler example-handler
1305     ExampleEnabled on
1306     ExamplePath &quot;/foo/bar&quot;
1307     ExampleAction file allow
1308 &lt;/Location&gt;
1309
1310 &lt;Location &quot;/a/b&quot;&gt;
1311     ExampleAction file deny
1312     ExampleEnabled off
1313 &lt;/Location&gt;
1314
1315 &lt;Location &quot;/a/b/c&quot;&gt;
1316     ExampleAction db deny
1317     ExamplePath &quot;/foo/bar/baz&quot;
1318     ExampleEnabled on
1319 &lt;/Location&gt;
1320 </highlight>
1321 <p>
1322 Then we'll assemble our module code. Note, that since we are now using our 
1323 name tag as reference when fetching configurations in our handler, I have 
1324 added some prototypes to keep the compiler happy:
1325 </p>
1326
1327 <!-- BEGIN EXAMPLE CODE -->
1328 <highlight language="c">
1329 /*$6
1330  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1331  * mod_example_config.c
1332  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1333  */
1334
1335
1336 #include &lt;stdio.h&gt;
1337 #include "apr_hash.h"
1338 #include "ap_config.h"
1339 #include "ap_provider.h"
1340 #include "httpd.h"
1341 #include "http_core.h"
1342 #include "http_config.h"
1343 #include "http_log.h"
1344 #include "http_protocol.h"
1345 #include "http_request.h"
1346
1347 /*$1
1348  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1349     Configuration structure
1350  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1351  */
1352
1353 typedef struct
1354 {
1355     char    context[256];
1356     char    path[256];
1357     int     typeOfAction;
1358     int     enabled;
1359 } example_config;
1360
1361 /*$1
1362  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1363     Prototypes
1364  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1365  */
1366
1367 static int    example_handler(request_rec *r);
1368 const char    *example_set_enabled(cmd_parms *cmd, void *cfg, const char *arg);
1369 const char    *example_set_path(cmd_parms *cmd, void *cfg, const char *arg);
1370 const char    *example_set_action(cmd_parms *cmd, void *cfg, const char *arg1, const char *arg2);
1371 void          *create_dir_conf(apr_pool_t *pool, char *context);
1372 void          *merge_dir_conf(apr_pool_t *pool, void *BASE, void *ADD);
1373 static void   register_hooks(apr_pool_t *pool);
1374
1375 /*$1
1376  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1377     Configuration directives
1378  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1379  */
1380
1381 static const command_rec    directives[] =
1382 {
1383     AP_INIT_TAKE1(&quot;exampleEnabled&quot;, example_set_enabled, NULL, ACCESS_CONF, &quot;Enable or disable mod_example&quot;),
1384     AP_INIT_TAKE1(&quot;examplePath&quot;, example_set_path, NULL, ACCESS_CONF, &quot;The path to whatever&quot;),
1385     AP_INIT_TAKE2(&quot;exampleAction&quot;, example_set_action, NULL, ACCESS_CONF, &quot;Special action value!&quot;),
1386     { NULL }
1387 };
1388
1389 /*$1
1390  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1391     Our name tag
1392  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1393  */
1394
1395 module AP_MODULE_DECLARE_DATA    example_module =
1396 {
1397     STANDARD20_MODULE_STUFF,
1398     create_dir_conf,    /* Per-directory configuration handler */
1399     merge_dir_conf,     /* Merge handler for per-directory configurations */
1400     NULL,               /* Per-server configuration handler */
1401     NULL,               /* Merge handler for per-server configurations */
1402     directives,         /* Any directives we may have for httpd */
1403     register_hooks      /* Our hook registering function */
1404 };
1405
1406 /*
1407  =======================================================================================================================
1408     Hook registration function
1409  =======================================================================================================================
1410  */
1411 static void register_hooks(apr_pool_t *pool)
1412 {
1413     ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
1414 }
1415
1416 /*
1417  =======================================================================================================================
1418     Our example web service handler
1419  =======================================================================================================================
1420  */
1421 static int example_handler(request_rec *r)
1422 {
1423     if(!r-&gt;handler || strcmp(r-&gt;handler, &quot;example-handler&quot;)) return(DECLINED);
1424
1425     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1426     example_config    *config = (example_config *) ap_get_module_config(r-&gt;per_dir_config, &amp;example_module);
1427     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1428
1429     ap_set_content_type(r, &quot;text/plain&quot;);
1430     ap_rprintf(r, &quot;Enabled: %u\n&quot;, config-&gt;enabled);
1431     ap_rprintf(r, &quot;Path: %s\n&quot;, config-&gt;path);
1432     ap_rprintf(r, &quot;TypeOfAction: %x\n&quot;, config-&gt;typeOfAction);
1433     ap_rprintf(r, &quot;Context: %s\n&quot;, config-&gt;context);
1434     return OK;
1435 }
1436
1437 /*
1438  =======================================================================================================================
1439     Handler for the &quot;exambleEnabled&quot; directive
1440  =======================================================================================================================
1441  */
1442 const char *example_set_enabled(cmd_parms *cmd, void *cfg, const char *arg)
1443 {
1444     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1445     example_config    *conf = (example_config *) cfg;
1446     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1447
1448     if(conf)
1449     {
1450         if(!strcasecmp(arg, &quot;on&quot;))
1451             conf-&gt;enabled = 1;
1452         else
1453             conf-&gt;enabled = 0;
1454     }
1455
1456     return NULL;
1457 }
1458
1459 /*
1460  =======================================================================================================================
1461     Handler for the &quot;examplePath&quot; directive
1462  =======================================================================================================================
1463  */
1464 const char *example_set_path(cmd_parms *cmd, void *cfg, const char *arg)
1465 {
1466     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1467     example_config    *conf = (example_config *) cfg;
1468     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1469
1470     if(conf)
1471     {
1472         strcpy(conf-&gt;path, arg);
1473     }
1474
1475     return NULL;
1476 }
1477
1478 /*
1479  =======================================================================================================================
1480     Handler for the &quot;exampleAction&quot; directive ;
1481     Let's pretend this one takes one argument (file or db), and a second (deny or allow), ;
1482     and we store it in a bit-wise manner.
1483  =======================================================================================================================
1484  */
1485 const char *example_set_action(cmd_parms *cmd, void *cfg, const char *arg1, const char *arg2)
1486 {
1487     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1488     example_config    *conf = (example_config *) cfg;
1489     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1490
1491     if(conf)
1492     {
1493         {
1494             if(!strcasecmp(arg1, &quot;file&quot;))
1495                 conf-&gt;typeOfAction = 0x01;
1496             else
1497                 conf-&gt;typeOfAction = 0x02;
1498             if(!strcasecmp(arg2, &quot;deny&quot;))
1499                 conf-&gt;typeOfAction += 0x10;
1500             else
1501                 conf-&gt;typeOfAction += 0x20;
1502         }
1503     }
1504
1505     return NULL;
1506 }
1507
1508 /*
1509  =======================================================================================================================
1510     Function for creating new configurations for per-directory contexts
1511  =======================================================================================================================
1512  */
1513 void *create_dir_conf(apr_pool_t *pool, char *context)
1514 {
1515     context = context ? context : &quot;Newly created configuration&quot;;
1516
1517     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1518     example_config    *cfg = apr_pcalloc(pool, sizeof(example_config));
1519     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1520
1521     if(cfg)
1522     {
1523         {
1524             /* Set some default values */
1525             strcpy(cfg-&gt;context, context);
1526             cfg-&gt;enabled = 0;
1527             memset(cfg-&gt;path, 0, 256);
1528             cfg-&gt;typeOfAction = 0x00;
1529         }
1530     }
1531
1532     return cfg;
1533 }
1534
1535 /*
1536  =======================================================================================================================
1537     Merging function for configurations
1538  =======================================================================================================================
1539  */
1540 void *merge_dir_conf(apr_pool_t *pool, void *BASE, void *ADD)
1541 {
1542     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1543     example_config    *base = (example_config *) BASE;
1544     example_config    *add = (example_config *) ADD;
1545     example_config    *conf = (example_config *) create_dir_conf(pool, &quot;Merged configuration&quot;);
1546     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1547
1548     conf-&gt;enabled = (add-&gt;enabled == 0) ? base-&gt;enabled : add-&gt;enabled;
1549     conf-&gt;typeOfAction = add-&gt;typeOfAction ? add-&gt;typeOfAction : base-&gt;typeOfAction;
1550     strcpy(conf-&gt;path, strlen(add-&gt;path) ? add-&gt;path : base-&gt;path);
1551     return conf;
1552 }
1553 </highlight>
1554 <!-- END EXAMPLE CODE -->
1555
1556
1557 </section>
1558
1559
1560 </section>
1561
1562 <section id="summary"><title>Summing up</title>
1563 <p>
1564 We have now looked at how to create simple modules for Apache HTTP Server 2.4 and 
1565 configuring them. What you do next is entirely up to you, but it is my 
1566 hope that something valuable has come out of reading this documentation. 
1567 If you have questions on how to further develop modules, you are welcome 
1568 to join our <a href="http://httpd.apache.org/lists.html">mailing lists</a> 
1569 or check out the rest of our documentation for further tips.
1570 </p>
1571 </section>
1572
1573 <section id="snippets"><title>Some useful snippets of code</title>
1574
1575 <section id="get_post"><title>Retrieve a variable from POST form data</title>
1576
1577
1578 <!-- BEGIN EXAMPLE CODE -->
1579 <highlight language="c">
1580 const char *read_post_value(const apr_array_header_t *fields, const char *key) 
1581 {
1582     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1583     int                         i;
1584     apr_table_entry_t           *e = 0;
1585     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1586     e = (apr_table_entry_t *) fields-&gt;elts;
1587     for(i = 0; i &lt; fields-&gt;nelts; i++) {
1588         if(!strcmp(e[i].key, key)) return e[i].val;
1589     }
1590     return 0;
1591 }
1592 static int example_handler(request_req *r) 
1593 {
1594     /*~~~~~~~~~~~~~~~~~~~~~~*/
1595     apr_array_header_t *POST;
1596     const char         *value;
1597     /*~~~~~~~~~~~~~~~~~~~~~~*/
1598     ap_parse_form_data(r, NULL, &amp;POST, -1, 8192);
1599     
1600     value = read_post_value(POST, &quot;valueA&quot;);
1601     if (!value) value = &quot;(undefined)&quot;;
1602     ap_rprintf(r, &quot;The value of valueA is: %s&quot;, value);
1603     return OK;
1604 }
1605 </highlight>
1606 <!-- END EXAMPLE CODE -->
1607
1608
1609     </section>
1610     
1611     <section id="headers_out"><title>Printing out every HTTP header received</title>
1612     
1613
1614 <!-- BEGIN EXAMPLE CODE -->
1615 <highlight language="c">
1616 static int example_handler(request_req *r) 
1617 {
1618     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1619     const apr_array_header_t    *fields;
1620     int                         i;
1621     apr_table_entry_t           *e = 0;
1622     /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1623
1624     fields = apr_table_elts(r-&gt;headers_in);
1625     e = (apr_table_entry_t *) fields-&gt;elts;
1626     for(i = 0; i &lt; fields-&gt;nelts; i++) {
1627         ap_rprintf(r, &quot;&lt;b&gt;%s&lt;/b&gt;: %s&lt;br/&gt;&quot;, e[i].key, e[i].val);
1628     }
1629     return OK;
1630 }
1631 </highlight>
1632 <!-- END EXAMPLE CODE -->
1633
1634
1635     </section>
1636     
1637     <section id="request_body"><title>Reading the request body into memory</title>
1638     
1639
1640 <!-- BEGIN EXAMPLE CODE -->
1641 <highlight language="c">
1642 static int util_read(request_rec *r, const char **rbuf, apr_off_t *size)
1643 {
1644     /*~~~~~~~~*/
1645     int rc = OK;
1646     /*~~~~~~~~*/
1647
1648     if((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR))) {
1649         return(rc);
1650     }
1651
1652     if(ap_should_client_block(r)) {
1653
1654         /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1655         char         argsbuffer[HUGE_STRING_LEN];
1656         apr_off_t    rsize, len_read, rpos = 0;
1657         apr_off_t length = r-&gt;remaining;
1658         /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1659
1660         *rbuf = (const char *) apr_pcalloc(r-&gt;pool, (apr_size_t) (length + 1));
1661         *size = length;
1662         while((len_read = ap_get_client_block(r, argsbuffer, sizeof(argsbuffer))) &gt; 0) {
1663             if((rpos + len_read) &gt; length) {
1664                 rsize = length - rpos;
1665             }
1666             else {
1667                 rsize = len_read;
1668             }
1669
1670             memcpy((char *) *rbuf + rpos, argsbuffer, (size_t) rsize);
1671             rpos += rsize;
1672         }
1673     }
1674     return(rc);
1675 }
1676
1677 static int example_handler(request_req* r) 
1678 {
1679     /*~~~~~~~~~~~~~~~~*/
1680     apr_off_t   size;
1681     const char  *buffer;
1682     /*~~~~~~~~~~~~~~~~*/
1683
1684     if(util_read(r, &amp;data, &amp;size) == OK) {
1685         ap_rprintf(&quot;We read a request body that was %u bytes long&quot;, size);
1686     }
1687     return OK;
1688 }
1689     </highlight>
1690
1691 <!-- END EXAMPLE CODE -->
1692
1693
1694     </section>
1695
1696 </section>
1697
1698 </manualpage>