1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="modguide.xml.meta">
24 <parentdocument href="./">Developer</parentdocument>
26 <title>Developing modules for the Apache HTTP Server 2.4</title>
29 <p>This document explains how you can develop modules for the Apache HTTP Server 2.4</p>
32 <seealso><a href="request.html">Request Processing in Apache 2.4</a></seealso>
33 <seealso><a href="hooks.html">Apache 2.x Hook Functions</a></seealso>
35 <section id="introduction"><title>Introduction</title>
36 <section id="what"><title>What we will be discussing in this document</title>
38 This document will discuss how you can easily create modules for the Apache HTTP Server 2.4 ("Apache"),
39 by exploring an example module called <code>mod_example</code>. In the first part of this document, the purpose of this
40 module will be to calculate and print out various digest values for existing files on your web server, whenever we
41 access the URL <code>http://hostname/filename.sum</code>. For instance, if we want to know the MD5 digest value of the file
42 located at <code>http://www.example.com/index.html</code>, we would visit <code>http://www.example.com/index.html.sum</code>.
45 In the second part of this document, which deals with configuration directive and context awareness, we will be looking at
46 a module that simply write out its own configuration to the client.
49 <section id="prerequisites"><title>Prerequisites</title>
51 First and foremost, you are expected to have a basic knowledge of how the C programming language works. In most cases,
52 we will try to be as pedagogical as possible and link to documents describing the functions used in the examples,
53 but there are also many cases where it is necessary to either just assume that "it works" or do some digging youself
54 into what the hows and whys of various function calls.
57 Lastly, you will need to have a basic understanding of how modules are loaded and configured in Apache, as well as
58 how to get the headers for Apache if you do not have them already, as these are needed for compiling new modules.
61 <section id="compiling"><title>Compiling your module</title>
63 To compile the source code we are building in this document, we will be using <a href="../programs/apxs.html">APXS</a>.
64 Assuming your source file is called mod_example.c, compiling, installing and activating the module is as simple as:
66 apxs -i -a -c mod_example.c
72 <section id="basics"><title>Defining a module</title>
73 <img src="../images/build_a_mod_3.png"/><br/>
74 <p>Every module starts with the same declaration, or name tag if you will, that defines a module as <em>a separate entity within Apache</em>:
77 <!-- BEGIN EXAMPLE CODE -->
78 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
79 module AP_MODULE_DECLARE_DATA example_module <code style='color:#806030; '>=</code>
80 <code style='color:#806030; '>{</code>
81 STANDARD20_MODULE_STUFF<code style='color:#806030; '>,</code>
82 create_dir_conf<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-directory configuration handler */</code>
83 merge_dir_conf<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-directory configurations */</code>
84 create_svr_conf<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-server configuration handler */</code>
85 merge_svr_conf<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-server configurations */</code>
86 directives<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Any directives we may have for httpd */</code>
87 register_hooks <code style='color:#c34e00; '>/* Our hook registering function */</code>
88 <code style='color:#806030; '>}</code><code style='color:#806030; '>;</code>
90 <!-- END EXAMPLE CODE -->
93 This bit of code lets Apache know that we have now registered a new module in the system,
94 and that its name is <code>example_module</code>. The name of the module is used primarilly
97 <li>Letting Apache know how to load the module using the LoadModule</li>
98 <li>Setting up a namespace for the module to use in configurations</li>
100 For now, we're only concerned with the first purpose of the module name,
101 which comes into play when we need to load the module:<br/>
102 <example><pre><a href="../mod/mod_so.html#LoadModule">LoadModule</a> example_module modules/mod_example.so</pre></example>
103 In essence, this tells Apache to open up <code>mod_example.so</code> and look for a module
104 called <code>example_module</code>.
107 Within this name tag of ours is also a bunch of references to how we would like to handle things:
108 Which directives do we respond to in a configuration file or .htaccess, how do we operate
109 within specific contexts, and what handlers are we interested in registering with the Apache
110 service. We'll return to all these elements later in this document.
113 <section id="hooking"><title>Getting started: Hooking into Apache</title>
114 <section id="hook_intro"><title>An introduction to hooks</title>
116 When handling requests in Apache, the first thing you will need to do is create a hook into
117 the request handling process. A hook is essentially a message telling Apache that you are
118 willing to either serve or at least take a glance at certain requests given by clients. All
119 handlers, whether it's mod_rewrite, mod_authn_*, mod_proxy and so on, are hooked into specific
120 parts of the request process. As you are probably aware, modules serve different purposes; Some
121 are authentication/authorization handlers, others are file or script handlers while some third
122 modules rewrite URIs or proxies content. Furthermore, in the end, it is up to the user of Apache
123 how and when each module will come into place. Thus, Apache itself does not presume to know
124 which module is responsible for handling a specific request, and will ask each module whether
125 they have an interest in a given request or not. It is then up to each module to either gently
126 decline serving a request, accept serving it or flat out deny the request from being served,
127 as authentication/authorization modules do: <br/>
128 <img src="../images/build_a_mod_2.png" /><br/>
129 To make it a bit easier for handlers such as our mod_example to know whether the client is
130 requesting content we should handle or not, Apache has directives for hinting to modules whether
131 their assistance is needed or not. Two of these are <directive
132 module="mod_mime">AddHandler</directive> and <directive module="core">SetHandler</directive>.
133 Let's take a look at an example using <directive module="mod_mime">AddHandler</directive>.
134 In our example case, we want every request ending with .sum to be served by <code>mod_example</code>,
135 so we'll add a configuration directive that tells Apache to do just that:
137 AddHandler example-handler .sum
139 What this tells Apache is the following: <em>Whenever we receive a request for a URI ending in .sum,
140 we are to let all modules know that we are looking for whoever goes by the name of "example-handler"
141 </em>. Thus, when a request is being served that ends in .sum, Apache will let all modules know, that
142 this request should be served by "example-handler". As you will see later, when we start
143 building mod_example, we will check for this handler tag relayed by <code>AddHandler</code> and
144 reply to Apache based on the value of this tag.
147 <section id="hook_declaration"><title>Hooking into httpd</title>
149 To begin with, we only want to create a simple handler, that replies to the client browser
150 when a specific URL is requested, so we won't bother setting up configuration handlers and
151 directives just yet. Our initial module definition will look like this:<br/>
154 <!-- BEGIN EXAMPLE CODE -->
155 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
156 module AP_MODULE_DECLARE_DATA example_module =
158 STANDARD20_MODULE_STUFF,
159 <code style='color:#7f0055; font-weight:bold; '>NULL</code>,
160 <code style='color:#7f0055; font-weight:bold; '>NULL</code>,
161 <code style='color:#7f0055; font-weight:bold; '>NULL</code>,
162 <code style='color:#7f0055; font-weight:bold; '>NULL</code>,
163 <code style='color:#7f0055; font-weight:bold; '>NULL</code>,
164 <code style='color:#800040; '>register_hooks</code> <code style='color:#c34e00; '><code style='color:#c34e00; '>/* Our hook registering function */</code></code>
167 <!-- END EXAMPLE CODE -->
170 This lets Apache know that we are not interesting in anything fancy, we just want to hook onto
171 the requests and possibly handle some of them.
174 The reference in our example declaration, <code>register_hooks</code> is the name of a function
175 we will create to manage how we hook onto the request process. In this example module, the function
176 has just one purpose; To create a simple hook that gets called after all the rewrites, access
177 control etc has been handled. Thus, we will let Apache know, that we want to hook into its process
178 as one of the last modules:
181 <!-- BEGIN EXAMPLE CODE -->
182 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
183 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>void</code> register_hooks<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>)</code>
184 <code style='color:#806030; '>{</code>
185 <code style='color:#c34e00; '>/* Create a hook in the request handler, so we get called when a request arrives */</code>
186 <code style='color:#800040; '>ap_hook_handler</code><code style='color:#806030; '>(</code>example_handler<code style='color:#806030; '>,</code> <code style='color:#7f0055; font-weight:bold; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#7f0055; font-weight:bold; '>NULL</code><code style='color:#806030; '>,</code> APR_HOOK_LAST<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
187 <code style='color:#806030; '>}</code>
189 <!-- END EXAMPLE CODE -->
192 The <code>example_handler</code> reference is the function that will handle the request. We will
193 discuss how to create a handler in the next chapter.
196 <section id="hook_others"><title>Other useful hooks</title>
198 Hooking into the request handling phase is but one of many hooks that you can create. Some other ways of hooking are:
200 <li><code>ap_hook_child_init</code>: Place a hook that executes when a child process is spawned (commonly used for initializing modules after Apache has forked)</li>
201 <li><code>ap_hook_pre_config</code>: Place a hook that executes before any configuration data has been read (very early hook)</li>
202 <li><code>ap_hook_post_config</code>: Place a hook that executes after configuration has been parsed, but before Apache has forked</li>
203 <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>
209 <section id="handling"><title>Building a handler</title>
211 A handler is essentially a function that receives a callback when a request to Apache is made.
212 It is passed a record of the current request (how it was made, which headers and requests were
213 passed along, who's giving the request and so on), and is put in charge of either telling
214 Apache that it's not interested in the request or handle the request with the tools provided.
216 <section id="simple_handler"><title>A simple "Hello, world!" handler</title>
217 Let's start off by making a very simple request handler that does the following: <br/>
219 <li>Check that this is a request that should be served by "example-handler"</li>
220 <li>Set the content type of our output to <code>text/html</code></li>
221 <li>Write "Hello, world!" back to the client browser</li>
222 <li>Let Apache know that we took care of this request and everything went fine</li>
224 In C code, our example handler will now look like this:<br/>
227 <!-- BEGIN EXAMPLE CODE -->
228 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
229 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
230 <code style='color:#806030; '>{</code>
231 <code style='color:#c34e00; '>/* First off, we need to check if this is a call for the "example-handler" handler.</code>
232 <code style='color:#c34e00; '>     * If it is, we accept it and do our things, it not, we simply return DECLINED,</code>
233 <code style='color:#c34e00; '>     * and Apache will try somewhere else.</code>
234 <code style='color:#c34e00; '>     */</code>
235 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code> <code style='color:#806030; '>|</code><code style='color:#806030; '>|</code> <code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code><code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>example-handler</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#806030; '>(</code>DECLINED<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
237 <code style='color:#c34e00; '>/* Now that we are handling this request, we'll write out "Hello, world!" to the client.</code>
238 <code style='color:#c34e00; '>     * To do so, we must first set the appropriate content type, followed by our output.</code>
239 <code style='color:#c34e00; '>     */</code>
240 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#gaa2f8412c400197338ec509f4a45e4579">ap_set_content_type</a><code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>text/html</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
241 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#ga5e91eb6ca777c9a427b2e82bf1eeb81d">ap_rprintf</a><code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Hello, world!</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
243 <code style='color:#c34e00; '>/* Lastly, we must tell Apache that we took care of this request and everything went fine.</code>
244 <code style='color:#c34e00; '>     * We do so by simply returning the value OK to Apache.</code>
245 <code style='color:#c34e00; '>     */</code>
246 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
247 <code style='color:#806030; '>}</code>
249 <!-- END EXAMPLE CODE -->
252 Now, we put all we have learned together and end up with a program that looks like <a href="mod_example_1.c">
253 mod_example_1.c</a>. The functions used in this example will be explained later in the section
254 <a href="#functions">"Some useful functions you should know"</a>.
256 <section id="request_rec"><title>The request_rec structure</title>
257 <p>The most essential part of any request is the <em>request record</em>. In a call to a handler function, this
258 is represented by the <code>request_req*</code> structure passed along with every call that is made. This
259 struct, typically just refered to as <code>r</code> in modules, contains all the information you need for
260 your module to fully process any HTTP request and respond accordingly.</p>
261 <p>Some key elements of the <code>request_req</code> structure are:
263 <li><code><code style='color:#008833'>r->handler</code> (char*)</code>: Contains the name of the handler, Apache is currently asking to do the handling of this request</li>
264 <li><code><code style='color:#008833'>r->method</code> (char*)</code>: Contains the HTTP method being used, f.x. GET or POST</li>
265 <li><code><code style='color:#008833'>r->filename</code> (char*)</code>: Contains the translated filename the client is requesting</li>
266 <li><code><code style='color:#008833'>r->args</code> (char*)</code>: Contains the query string of the request, if any</li>
267 <li><code><code style='color:#008833'>r->headers_in</code> (apr_table_t*)</code>: Contains all the headers sent by the client</li>
268 <li><code><code style='color:#008833'>r->connection</code> (conn_rec*)</code>: A record containing information about the current connection</li>
269 <li><code><code style='color:#008833'>r->useragent_ip</code> (char*)</code>: The IP address of the client connecting to us</li>
270 <li><code><code style='color:#008833'>r->pool</code> (apr_pool_t*)</code>: The memory pool of this request. We'll discuss this in the "
271 <a href="#memory">Memory management</a>" chapter.</li>
273 A complete list of all the values contained with in the <code>request_req</code> structure can be found in
274 the <a href="http://svn.apache.org/repos/asf/httpd/httpd/trunk/include/httpd.h"><code>httpd.h</code></a> header
275 file or at [insert link here].
278 Let's try out some of these variables in another example handler:<br/>
281 <!-- BEGIN EXAMPLE CODE -->
282 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
283 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
284 <code style='color:#806030; '>{</code>
285 <code style='color:#c34e00; '>/* Set the appropriate content type */</code>
286 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#gaa2f8412c400197338ec509f4a45e4579">ap_set_content_type</a><code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>text/html</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
288 <code style='color:#c34e00; '>/* Print out the IP address of the client connecting to us: */</code>
289 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#ga5e91eb6ca777c9a427b2e82bf1eeb81d">ap_rprintf</a><code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '><h2>Hello, </code><code style='color:#0f6900; '>%s</code><code style='color:#e60000; '>!</h2></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>useragent_ip</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
291 <code style='color:#c34e00; '>/* If we were reached through a GET or a POST request, be happy, else sad. */</code>
292 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code> <code style='color:#806030; '>!</code><code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>method</code><code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>POST</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>|</code><code style='color:#806030; '>|</code> <code style='color:#806030; '>!</code><code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>method</code><code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>GET</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
293 ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>You used a GET or a POST method, that makes us happy!<br></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
294 <code style='color:#806030; '>}</code>
295 <code style='color:#400000; font-weight:bold; '>else</code> <code style='color:#806030; '>{</code>
296 ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>You did not use POST or GET, that makes us sad :(<br></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
297 <code style='color:#806030; '>}</code>
299 <code style='color:#c34e00; '>/* Lastly, if there was a query string, let's print that too! */</code>
300 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>args</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
301 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#ga5e91eb6ca777c9a427b2e82bf1eeb81d">ap_rprintf</a><code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Your query string was: </code><code style='color:#0f6900; '>%s</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>args</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
302 <code style='color:#806030; '>}</code>
303 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
304 <code style='color:#806030; '>}</code>
306 <!-- END EXAMPLE CODE -->
312 <section id="return_value"><title>Return values</title>
314 Apache relies on return values from handlers to signify whether a request was handled or not,
315 and if so, whether the request went well or not. If a module is not interested in handling
316 a specific request, it should always return the value <code>DECLINED</code>. If it is handling
317 a request, it should either return the generic value <code>OK</code>, or a specific HTTP status
321 <!-- BEGIN EXAMPLE CODE -->
322 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
323 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
324 <code style='color:#806030; '>{</code>
325 <code style='color:#c34e00; '>/* Return 404: Not found */</code>
326 <code style='color:#400000; font-weight:bold; '>return</code> HTTP_NOT_FOUND<code style='color:#806030; '>;</code>
327 <code style='color:#806030; '>}</code>
329 <!-- END EXAMPLE CODE -->
332 Returning <code>OK</code> or a HTTP status code does not necessarilly mean that the request will end. Apache
333 may still have other handlers that are interested in this request, for instance the logging modules
334 which, upon a successful request, will write down a summary of what was requested and how it went.
335 To do a full stop and prevent any further processing after your module is done, you can return the
336 value <code>DONE</code> to let Apache know that it should cease all activity on this request and
337 carry on with the next, without informing other handlers.
339 <strong>General response codes:</strong>
341 <li><code>DECLINED</code>: We are not handling this request</li>
342 <li><code>OK</code>: We handled this request and it went well</li>
343 <li><code>DONE</code>: We handled this request and Apache should just close this thread without further processing</li>
345 <strong>HTTP specific return codes (excerpt):</strong>
347 <li><code>HTTP_OK (200)</code>: Request was okay</li>
348 <li><code>HTTP_MOVED_PERMANENTLY (301)</code>: The resource has moved to a new URL</li>
349 <li><code>HTTP_UNAUTHORIZED (401)</code>: Client is not authorized to visit this page</li>
350 <li><code>HTTP_FORBIDDEN (403)</code>: Permission denied</li>
351 <li><code>HTTP_NOT_FOUND (404)</code>: File not found</li>
352 <li><code>HTTP_INTERNAL_SERVER_ERROR (500)</code>: Internal server error (self explanatory)</li>
357 <section id="functions"><title>Some useful functions you should know</title>
361 <code>ap_rputs(const char *string, request_req *r)</code>: <br/>
362 Sends a string of text to the client. This is a shorthand version of <a
363 href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#gac827cd0537d2b6213a7c06d7c26cc36e">
367 <!-- BEGIN EXAMPLE CODE -->
368 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>Hello, world!</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code></pre>
369 <!-- END EXAMPLE CODE -->
375 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#ga5e91eb6ca777c9a427b2e82bf1eeb81d">ap_rprintf</a></code>: <br/>
376 This function works just like <code>printf</code>, except it sends the result to the client.
379 <!-- BEGIN EXAMPLE CODE -->
380 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'><code style='color:#2e8800; '><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#ga5e91eb6ca777c9a427b2e82bf1eeb81d">ap_rprintf</a></code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Hello, </code><code style='color:#0f6900; '>%s</code><code style='color:#e60000; '>!</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>useragent_ip</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code></pre>
381 <!-- END EXAMPLE CODE -->
386 <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/>
387 Sets the content type of the output you are sending.
390 <!-- BEGIN EXAMPLE CODE -->
391 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'><code style='color:#2e8800; '><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__PROTO.html#gaa2f8412c400197338ec509f4a45e4579">ap_set_content_type</a></code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>text/plain</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* force a raw text output */</code></pre>
392 <!-- END EXAMPLE CODE -->
400 <section id="memory"><title>Memory management</title>
402 Managing your resources in Apache is quite easy, thanks to the memory pool system. In essence,
403 each server, connection and request have their own memory pool that gets cleaned up when its
404 scope ends, e.g. when a request is done or when a server process shuts down. All your module
405 needs to do is latch onto this memory pool, and you won't have to worry about having to clean
406 up after yourself - pretty neat, huh?</p>
409 In our module, we will primarilly be allocating memory for each request, so it's appropriate to
410 use the <code><code style='color:#008833'>r->pool</code></code> reference when creating new objects. A few of the functions for
411 allocating memory within a pool are:
413 <li><code>void* <a href="http://apr.apache.org/docs/apr/0.9/group__apr__pools.html#g85f1e193c31d109affda72f9a92c6915">apr_palloc</a>(
414 apr_pool_t *p, apr_size_t size)</code>: Allocates <code>size</code> number of bytes in the pool for you</li>
415 <li><code>void* <a href="http://apr.apache.org/docs/apr/0.9/group__apr__pools.html#gf61c098ad258069d64cdf8c0a9369f9e">apr_pcalloc</a>(
416 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>
417 <li><code>char* <a href="http://apr.apache.org/docs/apr/0.9/group__apr__strings.html#gbc79e99ff19abbd7cfd18308c5f85d47">apr_pstrdup</a>(
418 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>
420 Let's put these functions into an example handler:<br/>
423 <!-- BEGIN EXAMPLE CODE -->
424 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
425 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
426 <code style='color:#806030; '>{</code>
427 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '>*</code> original <code style='color:#806030; '>=</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>You can't edit this!</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
428 <code style='color:#c34e00; '>/* Allocate space for 10 integer values and set them all to zero. */</code>
429 <code style='color:#400000; font-weight:bold; '>int</code><code style='color:#806030; '>*</code> integers <code style='color:#806030; '>=</code> apr_pcalloc<code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>pool</code><code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>sizeof</code><code style='color:#806030; '>(</code><code style='color:#400000; font-weight:bold; '>int</code><code style='color:#806030; '>)</code><code style='color:#806030; '>*</code><code style='color:#c00000; '>10</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
431 <code style='color:#c34e00; '>/* Create a copy of the 'original' variable that we can edit. */</code>
432 <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '>*</code> copy <code style='color:#806030; '>=</code> apr_pstrdup<code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>pool</code><code style='color:#806030; '>,</code> original<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
433 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
434 <code style='color:#806030; '>}</code>
436 <!-- END EXAMPLE CODE -->
438 This is all well and good for our module, which won't need any pre-initialized variables or structures.
439 However, if we wanted to initialize something early on, before the requests come rolling in, we could
440 simply add a call to a function in our <code>register_hooks</code> function to sort it out:
442 <!-- BEGIN EXAMPLE CODE -->
443 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
444 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>void</code> register_hooks<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>)</code>
445 <code style='color:#806030; '>{</code>
446 <code style='color:#c34e00; '>/* Call a function that initializes some stuff */</code>
447 <code style='color:#800040; '>example_init_function</code><code style='color:#806030; '>(</code>pool<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
448 <code style='color:#c34e00; '>/* Create a hook in the request handler, so we get called when a request arrives */</code>
449 <code style='color:#800040; '>ap_hook_handler</code><code style='color:#806030; '>(</code>example_handler<code style='color:#806030; '>,</code> <code style='color:#7f0055; font-weight:bold; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#7f0055; font-weight:bold; '>NULL</code><code style='color:#806030; '>,</code> APR_HOOK_LAST<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
450 <code style='color:#806030; '>}</code>
452 <!-- END EXAMPLE CODE -->
454 In this, pre-request initialization function, we would not be using the same pool as we did when
455 allocating resources for request-based functions. Instead, we would use the pool given to us by
456 Apache for allocating memory on a per-process based level.
461 <section id="parsing"><title>Parsing request data</title>
463 In our example module, we would like to add a feature, that checks which type of digest, MD5 or SHA1
464 the client would like to see. This could be solved by adding a query string to the request. A query
465 string is typically comprised of several keys and values put together in a string, for instance
466 <code>valueA=yes&valueB=no&valueC=maybe</code>. It is up to the module itself to parse these
467 and get the data it requires. In our example, we'll be looking for a key called <code>digest</code>,
468 and if set to <code>md5</code>, we'll produce an MD5 digest, otherwise we'll produce a SHA1 digest.
471 Since the introduction of Apache 2.4, parsing request data from GET and POST requests have never
472 been easier. All we require to parse both GET and POST data is four simple lines:
475 <!-- BEGIN EXAMPLE CODE -->
476 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
477 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__apr__tables.html#gad7ea82d6608a4a633fc3775694ab71e4">apr_table_t</a> *GET;
478 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/structapr__array__header__t.html">apr_array_header_t</a> *POST;
480 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__SCRIPT.html#gaed25877b529623a4d8f99f819ba1b7bd">ap_args_to_table</a>(r, &GET);
481 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__DAEMON.html#ga9d426b6382b49754d4f87c55f65af202">ap_parse_form_data</a>(r, <code style='color:#7f0055; font-weight:bold; '>NULL</code>, &POST, -1, 8192);
483 <!-- END EXAMPLE CODE -->
486 In our specific example module, we're looking for the <code>digest</code> value from the query string,
487 which now resides inside a table called <code>GET</code>. To extract this value, we need only perform
488 a simple operation:<br/>
491 <!-- BEGIN EXAMPLE CODE -->
492 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
493 <code style='color:#c34e00; '>/* Get the "digest" key from the query string, if any. */</code>
494 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '> *</code>digestType <code style='color:#806030; '>=</code> <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__apr__tables.html#ga4db13e3915c6b9a3142b175d4c15d915">apr_table_get</a><code style='color:#806030; '>(</code>GET<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>digest</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
496 <code style='color:#c34e00; '>/* If no key was returned, we will set a default value instead. */</code>
497 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>digestType<code style='color:#806030; '>)</code> digestType <code style='color:#806030; '>=</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>sha1</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
500 <!-- END EXAMPLE CODE -->
503 The structures used for the POST and GET data are not exactly the same, so if we were to fetch a value from
504 POST data instead of the query string, we would have to resort to a few more lines, as outlined in
505 <a href="#get_post">this example</a> in the last chapter of this document.
509 <section id="advanced_handler"><title>Making an advanced handler</title>
510 Now that we have learned how to parse form data and manage our resources, we can move on to creating an advanced
511 version of our module, that spits out the MD5 or SHA1 digest of files:<br/>
514 <!-- BEGIN EXAMPLE CODE -->
515 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
516 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
517 <code style='color:#806030; '>{</code>
518 <code style='color:#400000; font-weight:bold; '>int</code> rc<code style='color:#806030; '>,</code> exists<code style='color:#806030; '>;</code>
519 apr_finfo_t finfo<code style='color:#806030; '>;</code>
520 apr_file_t<code style='color:#806030; '> *</code>file<code style='color:#806030; '>;</code>
521 <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>filename<code style='color:#806030; '>;</code>
522 <code style='color:#400000; font-weight:bold; '>char</code> buffer<code style='color:#806030; '>[</code><code style='color:#c00000; '>256</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
523 apr_size_t readBytes<code style='color:#806030; '>;</code>
524 <code style='color:#400000; font-weight:bold; '>int</code> n<code style='color:#806030; '>;</code>
525 apr_table_t<code style='color:#806030; '> *</code>GET<code style='color:#806030; '>;</code>
526 apr_array_header_t<code style='color:#806030; '> *</code>POST<code style='color:#806030; '>;</code>
527 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '> *</code>digestType<code style='color:#806030; '>;</code>
530 <code style='color:#c34e00; '>/* Check that the "example-handler" handler is being called. */</code>
531 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code> <code style='color:#806030; '>|</code><code style='color:#806030; '>|</code> <code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code><code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>example-handler</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#806030; '>(</code>DECLINED<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
533 <code style='color:#c34e00; '>/* Figure out which file is being requested by removing the .sum from it */</code>
534 filename <code style='color:#806030; '>=</code> apr_pstrdup<code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>pool</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>filename</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
535 filename<code style='color:#806030; '>[</code><code style='color:#800040; '>strlen</code><code style='color:#806030; '>(</code>filename<code style='color:#806030; '>)</code><code style='color:#806030; '>-</code><code style='color:#c00000; '>4</code><code style='color:#806030; '>]</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* Cut off the last 4 characters. */</code>
537 <code style='color:#c34e00; '>/* Figure out if the file we request a sum on exists and isn't a directory */</code>
538 rc <code style='color:#806030; '>=</code> apr_stat<code style='color:#806030; '>(</code><code style='color:#806030; '>&</code>finfo<code style='color:#806030; '>,</code> filename<code style='color:#806030; '>,</code> APR_FINFO_MIN<code style='color:#806030; '>,</code> r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>pool</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
539 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code>rc <code style='color:#806030; '>=</code><code style='color:#806030; '>=</code> APR_SUCCESS<code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
540 exists <code style='color:#806030; '>=</code>
541 <code style='color:#806030; '>(</code>
542 <code style='color:#806030; '>(</code>finfo<code style='color:#806030; '>.</code>filetype <code style='color:#806030; '>!</code><code style='color:#806030; '>=</code> APR_NOFILE<code style='color:#806030; '>)</code>
543 <code style='color:#806030; '>&</code><code style='color:#806030; '>&</code> <code style='color:#806030; '>!</code><code style='color:#806030; '>(</code>finfo<code style='color:#806030; '>.</code>filetype <code style='color:#806030; '>&</code> APR_DIR<code style='color:#806030; '>)</code>
544 <code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
545 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>exists<code style='color:#806030; '>)</code> <code style='color:#400000; font-weight:bold; '>return</code> HTTP_NOT_FOUND<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* Return a 404 if not found. */</code>
546 <code style='color:#806030; '>}</code>
547 <code style='color:#c34e00; '>/* If apr_stat failed, we're probably not allowed to check this file. */</code>
548 <code style='color:#400000; font-weight:bold; '>else</code> <code style='color:#400000; font-weight:bold; '>return</code> HTTP_FORBIDDEN<code style='color:#806030; '>;</code>
550 <code style='color:#c34e00; '>/* Parse the GET and, optionally, the POST data sent to us */</code>
552 ap_args_to_table<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>GET<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
553 ap_parse_form_data<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>POST<code style='color:#806030; '>,</code> <code style='color:#806030; '>-</code><code style='color:#c00000; '>1</code><code style='color:#806030; '>,</code> <code style='color:#c00000; '>8192</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
555 <code style='color:#c34e00; '>/* Set the appropriate content type */</code>
556 ap_set_content_type<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>text/html</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
558 <code style='color:#c34e00; '>/* Print a title and some general information */</code>
559 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '><h2>Information on </code><code style='color:#0f6900; '>%s</code><code style='color:#e60000; '>:</h2></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> filename<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
560 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '><b>Size:</b> </code><code style='color:#0f6900; '>%u</code><code style='color:#e60000; '> bytes<br/></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> finfo<code style='color:#806030; '>.</code>size<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
562 <code style='color:#c34e00; '>/* Get the digest type the client wants to see */</code>
563 digestType <code style='color:#806030; '>=</code> apr_table_get<code style='color:#806030; '>(</code>GET<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>digest</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
564 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>digestType<code style='color:#806030; '>)</code> digestType <code style='color:#806030; '>=</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>MD5</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
567 rc <code style='color:#806030; '>=</code> apr_file_open<code style='color:#806030; '>(</code><code style='color:#806030; '>&</code>file<code style='color:#806030; '>,</code> filename<code style='color:#806030; '>,</code> APR_READ<code style='color:#806030; '>,</code> APR_OS_DEFAULT<code style='color:#806030; '>,</code> r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>pool</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
568 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code>rc <code style='color:#806030; '>=</code><code style='color:#806030; '>=</code> APR_SUCCESS<code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
570 <code style='color:#c34e00; '>/* Are we trying to calculate the MD5 or the SHA1 digest? */</code>
571 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>digestType<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>md5</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
572 <code style='color:#c34e00; '>/* Calculate the MD5 sum of the file */</code>
573 <code style='color:#400000; font-weight:bold; '>union</code> <code style='color:#806030; '>{</code>
574 <code style='color:#400000; font-weight:bold; '>char</code> chr<code style='color:#806030; '>[</code><code style='color:#c00000; '>16</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
575 uint32_t num<code style='color:#806030; '>[</code><code style='color:#c00000; '>4</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
576 <code style='color:#806030; '>}</code> digest<code style='color:#806030; '>;</code>
577 apr_md5_ctx_t md5<code style='color:#806030; '>;</code>
578 apr_md5_init<code style='color:#806030; '>(</code><code style='color:#806030; '>&</code>md5<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
579 readBytes <code style='color:#806030; '>=</code> <code style='color:#c00000; '>256</code><code style='color:#806030; '>;</code>
580 <code style='color:#400000; font-weight:bold; '>while</code> <code style='color:#806030; '>(</code> apr_file_read<code style='color:#806030; '>(</code>file<code style='color:#806030; '>,</code> buffer<code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>readBytes<code style='color:#806030; '>)</code> <code style='color:#806030; '>=</code><code style='color:#806030; '>=</code> APR_SUCCESS <code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
581 apr_md5_update<code style='color:#806030; '>(</code><code style='color:#806030; '>&</code>md5<code style='color:#806030; '>,</code> buffer<code style='color:#806030; '>,</code> readBytes<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
582 <code style='color:#806030; '>}</code>
583 apr_md5_final<code style='color:#806030; '>(</code>digest<code style='color:#806030; '>.</code>chr<code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>md5<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
585 <code style='color:#c34e00; '>/* Print out the MD5 digest */</code>
586 ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '><b>MD5: </b><code></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
587 <code style='color:#400000; font-weight:bold; '>for</code> <code style='color:#806030; '>(</code>n <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code> n <code style='color:#806030; '><</code> APR_MD5_DIGESTSIZE<code style='color:#806030; '>/</code><code style='color:#c00000; '>4</code><code style='color:#806030; '>;</code> n<code style='color:#806030; '>+</code><code style='color:#806030; '>+</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
588 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#0f6900; '>%08x</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> digest<code style='color:#806030; '>.</code>num<code style='color:#806030; '>[</code>n<code style='color:#806030; '>]</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
589 <code style='color:#806030; '>}</code>
590 ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '></code></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
591 <code style='color:#c34e00; '>/* Print a link to the SHA1 version */</code>
592 ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '><br/><a href='?digest=sha1'>View the SHA1 hash instead</a></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
593 <code style='color:#806030; '>}</code>
594 <code style='color:#400000; font-weight:bold; '>else</code> <code style='color:#806030; '>{</code>
595 <code style='color:#c34e00; '>/* Calculate the SHA1 sum of the file */</code>
596 <code style='color:#400000; font-weight:bold; '>union</code> <code style='color:#806030; '>{</code>
597 <code style='color:#400000; font-weight:bold; '>char</code> chr<code style='color:#806030; '>[</code><code style='color:#c00000; '>20</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
598 uint32_t num<code style='color:#806030; '>[</code><code style='color:#c00000; '>5</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
599 <code style='color:#806030; '>}</code> digest<code style='color:#806030; '>;</code>
600 apr_sha1_ctx_t sha1<code style='color:#806030; '>;</code>
601 apr_sha1_init<code style='color:#806030; '>(</code><code style='color:#806030; '>&</code>sha1<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
602 readBytes <code style='color:#806030; '>=</code> <code style='color:#c00000; '>256</code><code style='color:#806030; '>;</code>
603 <code style='color:#400000; font-weight:bold; '>while</code> <code style='color:#806030; '>(</code> apr_file_read<code style='color:#806030; '>(</code>file<code style='color:#806030; '>,</code> buffer<code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>readBytes<code style='color:#806030; '>)</code> <code style='color:#806030; '>=</code><code style='color:#806030; '>=</code> APR_SUCCESS <code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
604 apr_sha1_update<code style='color:#806030; '>(</code><code style='color:#806030; '>&</code>sha1<code style='color:#806030; '>,</code> buffer<code style='color:#806030; '>,</code> readBytes<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
605 <code style='color:#806030; '>}</code>
606 apr_sha1_final<code style='color:#806030; '>(</code>digest<code style='color:#806030; '>.</code>chr<code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>sha1<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
608 <code style='color:#c34e00; '>/* Print out the SHA1 digest */</code>
609 ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '><b>SHA1: </b><code></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
610 <code style='color:#400000; font-weight:bold; '>for</code> <code style='color:#806030; '>(</code>n <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code> n <code style='color:#806030; '><</code> APR_SHA1_DIGESTSIZE<code style='color:#806030; '>/</code><code style='color:#c00000; '>4</code><code style='color:#806030; '>;</code> n<code style='color:#806030; '>+</code><code style='color:#806030; '>+</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
611 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#0f6900; '>%08x</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> digest<code style='color:#806030; '>.</code>num<code style='color:#806030; '>[</code>n<code style='color:#806030; '>]</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
612 <code style='color:#806030; '>}</code>
613 ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '></code></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
615 <code style='color:#c34e00; '>/* Print a link to the MD5 version */</code>
616 ap_rputs<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '><br/><a href='?digest=md5'>View the MD5 hash instead</a></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
617 <code style='color:#806030; '>}</code>
618 apr_file_close<code style='color:#806030; '>(</code>file<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
620 <code style='color:#806030; '>}</code>
624 <code style='color:#c34e00; '>/* Let Apache know that we responded to this request. */</code>
625 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
626 <code style='color:#806030; '>}</code>
628 <!-- END EXAMPLE CODE -->
631 This version in its entirity can be found here: <a href="mod_example_2.c">mod_example_2.c</a>.
636 <section id="configuration"><title>Adding configuration options</title>
638 In this next segment of this document, we will turn our eyes away from the digest module and create a new
639 example module, whose only function is to write out its own configuration. The purpose of this is to
640 examine how Apache works with configuration, and what happens when you start writing advanced configurations
643 <section id="config_intro"><title>An introduction to configuration directives</title>
644 If you are reading this, then you probably already know what a configuration directive is. Simply put,
645 a directive is a way of telling an individual module (or a set of modules) how to behave, such as these
646 directives control how <code>mod_rewrite</code> works:
649 RewriteCond %{REQUEST_URI} ^/foo/bar
650 RewriteRule ^/foo/bar/(.*)$ /foobar?page=$1
652 Each of these configuration directives are handled by a separate function, that parses the parameters given
653 and sets up a configuration accordingly.
655 <section id="config_simple"><title>Making an example configuration</title>
656 To begin with, we'll create a basic configuration in C-space:
659 <!-- BEGIN EXAMPLE CODE -->
660 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
661 <code style='color:#400000; font-weight:bold; '>typedef</code> <code style='color:#400000; font-weight:bold; '>struct</code> <code style='color:#806030; '>{</code>
662 <code style='color:#400000; font-weight:bold; '>int</code> enabled<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* Enable or disable our module */</code>
663 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>path<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* Some path to...something */</code>
664 <code style='color:#400000; font-weight:bold; '>int</code> typeOfAction<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* 1 means action A, 2 means action B and so on */</code>
665 <code style='color:#806030; '>}</code> example_config<code style='color:#806030; '>;</code>
667 <!-- END EXAMPLE CODE -->
670 Now, let's put this into perspective by creating a very small module that just prints out a hard-coded
671 configuration. You'll notice that we use the <code>register_hooks</code> function for initializing the
672 configuration values to their defaults:
675 <!-- BEGIN EXAMPLE CODE -->
676 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
677 <code style='color:#400000; font-weight:bold; '>typedef</code> <code style='color:#400000; font-weight:bold; '>struct</code> <code style='color:#806030; '>{</code>
678 <code style='color:#400000; font-weight:bold; '>int</code> enabled<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* Enable or disable our module */</code>
679 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>path<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* Some path to...something */</code>
680 <code style='color:#400000; font-weight:bold; '>int</code> typeOfAction<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* 1 means action A, 2 means action B and so on */</code>
681 <code style='color:#806030; '>}</code> example_config<code style='color:#806030; '>;</code>
683 <code style='color:#400000; font-weight:bold; '>static</code> example_config config<code style='color:#806030; '>;</code>
685 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
686 <code style='color:#806030; '>{</code>
687 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code> <code style='color:#806030; '>|</code><code style='color:#806030; '>|</code> <code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code><code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>example-handler</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#400000; font-weight:bold; '>return</code><code style='color:#806030; '>(</code>DECLINED<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
688 ap_set_content_type<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>text/plain</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
689 ap_rprintf<code style='color:#806030; '>(r, </code><code style='color:#800000; '>"</code><code style='color:#e60000; '>Enabled: </code><code style='color:#0f6900; '>%u</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>.</code>enabled<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
690 ap_rprintf<code style='color:#806030; '>(r, </code><code style='color:#800000; '>"</code><code style='color:#e60000; '>Path: </code><code style='color:#0f6900; '>%s</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>.</code>path<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
691 ap_rprintf<code style='color:#806030; '>(r, </code><code style='color:#800000; '>"</code><code style='color:#e60000; '>TypeOfAction: </code><code style='color:#0f6900; '>%x</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>.</code>typeOfAction<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
692 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
693 <code style='color:#806030; '>}</code>
695 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>void</code> register_hooks<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>)</code>
696 <code style='color:#806030; '>{</code>
697 config<code style='color:#806030; '>.</code>enabled <code style='color:#806030; '>=</code> <code style='color:#c00000; '>1</code><code style='color:#806030; '>;</code>
698 config<code style='color:#806030; '>.</code>path <code style='color:#806030; '>=</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>/foo/bar</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
699 config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x00</code><code style='color:#806030; '>;</code>
700 ap_hook_handler<code style='color:#806030; '>(</code>example_handler<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> APR_HOOK_LAST<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
701 <code style='color:#806030; '>}</code>
703 <code style='color:#c34e00; '>/* Define our module as an entity and assign a function for registering hooks */</code>
705 module AP_MODULE_DECLARE_DATA example_module <code style='color:#806030; '>=</code>
706 <code style='color:#806030; '>{</code>
707 STANDARD20_MODULE_STUFF<code style='color:#806030; '>,</code>
708 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-directory configuration handler */</code>
709 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-directory configurations */</code>
710 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-server configuration handler */</code>
711 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-server configurations */</code>
712 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Any directives we may have for httpd */</code>
713 register_hooks <code style='color:#c34e00; '>/* Our hook registering function */</code>
714 <code style='color:#806030; '>}</code><code style='color:#806030; '>;</code>
716 <!-- END EXAMPLE CODE -->
719 So far so good. To access our new handler, we could add the following to our configuration:
721 <Location /example>
722 SetHandler example-handler
725 When we visit, we'll see our current configuration being spit out by our module.
728 <section id="register_directive"><title>Registering directives with Apache</title>
729 What if we want to change our configuration, not by hard-coding new values into the module,
730 but by using either the httpd.conf file or possibly a .htaccess file? It's time to let Apache
731 know that we want this to be possible. To do so, we must first change our <em>name tag</em>
732 to include a reference to the configuration directives we want to register with Apache:
735 <!-- BEGIN EXAMPLE CODE -->
736 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
737 module AP_MODULE_DECLARE_DATA example_module =
739 STANDARD20_MODULE_STUFF,
740 <code style='color:#7f0055; font-weight:bold; '>NULL</code>, <code style='color:#c34e00; '>/* Per-directory configuration handler */</code>
741 <code style='color:#7f0055; font-weight:bold; '>NULL</code>, <code style='color:#c34e00; '>/* Merge handler for per-directory configurations */</code>
742 <code style='color:#7f0055; font-weight:bold; '>NULL</code>, <code style='color:#c34e00; '>/* Per-server configuration handler */</code>
743 <code style='color:#7f0055; font-weight:bold; '>NULL</code>, <code style='color:#c34e00; '>/* Merge handler for per-server configurations */</code>
744 example_directives, <code style='color:#c34e00; '>/* Any directives we may have for httpd */</code>
745 register_hooks <code style='color:#c34e00; '>/* Our hook registering function */</code>
748 <!-- END EXAMPLE CODE -->
751 This will tell Apache that we are now accepting directives from the configuration files, and that the
752 structure called <code>example_directives</code> holds information on what our directives are and how
753 they work. Since we have three different variables in our module configuration, we will add a structure
754 with three directives and a NULL at the end:
757 <!-- BEGIN EXAMPLE CODE -->
758 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
759 static const command_rec example_directives[] =
761 <code style='color:#2e8800; '><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga07c7d22ae17805e61204463326cf9c34">AP_INIT_TAKE1</a></code>("exampleEnabled", example_set_enabled, <code style='color:#7f0055; font-weight:bold; '>NULL</code>, RSRC_CONF, "Enable or disable mod_example"),
762 <code style='color:#2e8800; '><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga07c7d22ae17805e61204463326cf9c34">AP_INIT_TAKE1</a></code>("examplePath", example_set_path, <code style='color:#7f0055; font-weight:bold; '>NULL</code>, RSRC_CONF, "The path to whatever"),
763 <code style='color:#2e8800; '><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#gafaec43534fcf200f37d9fecbf9247c21">AP_INIT_TAKE2</a></code>("exampleAction", example_set_action, <code style='color:#7f0055; font-weight:bold; '>NULL</code>, RSRC_CONF, "Special action value!"),
764 { <code style='color:#7f0055; font-weight:bold; '>NULL</code> }
767 <!-- END EXAMPLE CODE -->
770 <img src="../images/build_a_mod_4.png" border="1"/><br/>
772 As you can see, each directive needs at least 5 parameters set:
774 <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 Apache that this directive takes one and only one argument.
775 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
776 for more macros).</li>
777 <li><code>exampleEnabled</code>: This is the name of our directive. More precisely, it is what the user must put in his/her
778 configuration in order to invoke a configuration change in our module.</li>
779 <li><code>example_set_enabled</code>: This is a reference to a C function that parses the directive and sets the configuration
780 accordingly. We will discuss how to make this in the following paragraph.</li>
781 <li><code>RSRC_CONF</code>: This tells Apache where the directive is permissable. We'll go into details on this value in the
782 later chapters, but for now, <code>RSRC_CONF</code> means that Apache will only accept these directives in a server context.</li>
783 <li><code>"Enable or disable...."</code>: This is simply a brief description of what the directive does.</li>
785 (<em>The "missing" parameter in our definition, which is usually set to <code>NULL</code>, is an optional function that can be
786 run after the initial function to parse the arguments have been run. This is usually omitted, as the function for verifying
787 arguments might as well be used to set them.</em>)
790 <section id="directive_handler"><title>The directive handler function</title>
792 Now that we've told Apache to expect some directives for our module, it's time to make a few functions for handling these. What
793 Apache reads in the configuration file(s) is text, and so naturally, what it passes along to our directive handler is one or
794 more strings, that we ourselves need to recognize and act upon. You'll notice, that since we set our <code>exampleAction</code>
795 directive to accept two arguments, its C function also has an additional parameter defined:<br/>
798 <!-- BEGIN EXAMPLE CODE -->
799 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
800 <code style='color:#c34e00; '>/* Handler for the "exambleEnabled" directive */</code>
801 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '> *</code>example_set_enabled<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg<code style='color:#806030; '>)</code>
802 <code style='color:#806030; '>{</code>
803 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>on</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> config<code style='color:#806030; '>.</code>enabled <code style='color:#806030; '>=</code> <code style='color:#c00000; '>1</code><code style='color:#806030; '>;</code>
804 <code style='color:#400000; font-weight:bold; '>else</code> config<code style='color:#806030; '>.</code>enabled <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
805 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
806 <code style='color:#806030; '>}</code>
808 <code style='color:#c34e00; '>/* Handler for the "examplePath" directive */</code>
809 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '> *</code>example_set_path<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg<code style='color:#806030; '>)</code>
810 <code style='color:#806030; '>{</code>
811 config<code style='color:#806030; '>.</code>path <code style='color:#806030; '>=</code> arg<code style='color:#806030; '>;</code>
812 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
813 <code style='color:#806030; '>}</code>
815 <code style='color:#c34e00; '>/* Handler for the "exampleAction" directive */</code>
816 <code style='color:#c34e00; '>/* Let's pretend this one takes one argument (file or db), and a second (deny or allow), */</code>
817 <code style='color:#c34e00; '>/* and we store it in a bit-wise manner. */</code>
818 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '> *</code>example_set_action<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg1<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '>*</code> arg2<code style='color:#806030; '>)</code>
819 <code style='color:#806030; '>{</code>
820 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg1<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>file</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x01</code><code style='color:#806030; '>;</code>
821 <code style='color:#400000; font-weight:bold; '>else</code> config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x02</code><code style='color:#806030; '>;</code>
823 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg2<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>deny</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>+</code><code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x10</code><code style='color:#806030; '>;</code>
824 <code style='color:#400000; font-weight:bold; '>else</code> config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>+</code><code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x20</code><code style='color:#806030; '>;</code>
825 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
826 <code style='color:#806030; '>}</code>
828 <!-- END EXAMPLE CODE -->
833 <section id="directive_complete"><title>Putting it all together</title>
835 Now that we have our directives set up, and handlers configured for them, we can assemble our module
839 <!-- BEGIN EXAMPLE CODE -->
840 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
841 <code style='color:#c34e00; '>/* mod_example_config_simple.c: */</code>
842 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '><</code><code style='color:#40015a; '>stdio.h</code><code style='color:#800000; '>></code>
843 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>apr_hash.h</code><code style='color:#800000; '>"</code>
844 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>ap_config.h</code><code style='color:#800000; '>"</code>
845 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>ap_provider.h</code><code style='color:#800000; '>"</code>
846 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>httpd.h</code><code style='color:#800000; '>"</code>
847 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_core.h</code><code style='color:#800000; '>"</code>
848 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_config.h</code><code style='color:#800000; '>"</code>
849 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_log.h</code><code style='color:#800000; '>"</code>
850 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_protocol.h</code><code style='color:#800000; '>"</code>
851 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_request.h</code><code style='color:#800000; '>"</code>
853 <code style='color:#c34e00; '>/*</code>
854 <code style='color:#c34e00; '> ==============================================================================</code>
855 <code style='color:#c34e00; '> Our configuration prototype and declaration:</code>
856 <code style='color:#c34e00; '> ==============================================================================</code>
857 <code style='color:#c34e00; '> */</code>
858 <code style='color:#400000; font-weight:bold; '>typedef</code> <code style='color:#400000; font-weight:bold; '>struct</code> <code style='color:#806030; '>{</code>
859 <code style='color:#400000; font-weight:bold; '>int</code> enabled<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* Enable or disable our module */</code>
860 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>path<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* Some path to...something */</code>
861 <code style='color:#400000; font-weight:bold; '>int</code> typeOfAction<code style='color:#806030; '>;</code> <code style='color:#c34e00; '>/* 1 means action A, 2 means action B and so on */</code>
862 <code style='color:#806030; '>}</code> example_config<code style='color:#806030; '>;</code>
864 <code style='color:#400000; font-weight:bold; '>static</code> example_config config<code style='color:#806030; '>;</code>
866 <code style='color:#c34e00; '>/*</code>
867 <code style='color:#c34e00; '> ==============================================================================</code>
868 <code style='color:#c34e00; '> Our directive handlers:</code>
869 <code style='color:#c34e00; '> ==============================================================================</code>
870 <code style='color:#c34e00; '> */</code>
871 <code style='color:#c34e00; '>/* Handler for the "exambleEnabled" directive */</code>
872 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '> *</code>example_set_enabled<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg<code style='color:#806030; '>)</code>
873 <code style='color:#806030; '>{</code>
874 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>on</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> config<code style='color:#806030; '>.</code>enabled <code style='color:#806030; '>=</code> <code style='color:#c00000; '>1</code><code style='color:#806030; '>;</code>
875 <code style='color:#400000; font-weight:bold; '>else</code> config<code style='color:#806030; '>.</code>enabled <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
876 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
877 <code style='color:#806030; '>}</code>
879 <code style='color:#c34e00; '>/* Handler for the "examplePath" directive */</code>
880 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '> *</code>example_set_path<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg<code style='color:#806030; '>)</code>
881 <code style='color:#806030; '>{</code>
882 config<code style='color:#806030; '>.</code>path <code style='color:#806030; '>=</code> arg<code style='color:#806030; '>;</code>
883 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
884 <code style='color:#806030; '>}</code>
886 <code style='color:#c34e00; '>/* Handler for the "exampleAction" directive */</code>
887 <code style='color:#c34e00; '>/* Let's pretend this one takes one argument (file or db), and a second (deny or allow), */</code>
888 <code style='color:#c34e00; '>/* and we store it in a bit-wise manner. */</code>
889 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '> *</code>example_set_action<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg1<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '>*</code> arg2<code style='color:#806030; '>)</code>
890 <code style='color:#806030; '>{</code>
891 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg1<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>file</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x01</code><code style='color:#806030; '>;</code>
892 <code style='color:#400000; font-weight:bold; '>else</code> config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x02</code><code style='color:#806030; '>;</code>
894 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg2<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>deny</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>+</code><code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x10</code><code style='color:#806030; '>;</code>
895 <code style='color:#400000; font-weight:bold; '>else</code> config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>+</code><code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x20</code><code style='color:#806030; '>;</code>
896 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
897 <code style='color:#806030; '>}</code>
899 <code style='color:#c34e00; '>/*</code>
900 <code style='color:#c34e00; '> ==============================================================================</code>
901 <code style='color:#c34e00; '> The directive structure for our name tag:</code>
902 <code style='color:#c34e00; '> ==============================================================================</code>
903 <code style='color:#c34e00; '> */</code>
904 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>const</code> command_rec example_directives<code style='color:#806030; '>[</code><code style='color:#806030; '>]</code> <code style='color:#806030; '>=</code>
905 <code style='color:#806030; '>{</code>
906 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga07c7d22ae17805e61204463326cf9c34">AP_INIT_TAKE1</a><code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>exampleEnabled</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> example_set_enabled<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> RSRC_CONF<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Enable or disable mod_example</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>,</code>
907 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga07c7d22ae17805e61204463326cf9c34">AP_INIT_TAKE1</a><code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>examplePath</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> example_set_path<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> RSRC_CONF<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>The path to whatever</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>,</code>
908 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#gafaec43534fcf200f37d9fecbf9247c21">AP_INIT_TAKE2</a><code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>exampleAction</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> example_set_action<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> RSRC_CONF<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Special action value!</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>,</code>
909 <code style='color:#806030; '>{</code> <code style='color:#007d45; '>NULL</code> <code style='color:#806030; '>}</code>
910 <code style='color:#806030; '>}</code><code style='color:#806030; '>;</code>
911 <code style='color:#c34e00; '>/*</code>
912 <code style='color:#c34e00; '> ==============================================================================</code>
913 <code style='color:#c34e00; '> Our module handler:</code>
914 <code style='color:#c34e00; '> ==============================================================================</code>
915 <code style='color:#c34e00; '> */</code>
916 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
917 <code style='color:#806030; '>{</code>
918 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code> <code style='color:#806030; '>|</code><code style='color:#806030; '>|</code> <code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code><code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>example-handler</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#400000; font-weight:bold; '>return</code><code style='color:#806030; '>(</code>DECLINED<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
919 ap_set_content_type<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>text/plain</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
920 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Enabled: </code><code style='color:#0f6900; '>%u</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>.</code>enabled<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
921 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Path: </code><code style='color:#0f6900; '>%s</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>.</code>path<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
922 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>TypeOfAction: </code><code style='color:#0f6900; '>%x</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>.</code>typeOfAction<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
923 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
924 <code style='color:#806030; '>}</code>
926 <code style='color:#c34e00; '>/*</code>
927 <code style='color:#c34e00; '> ==============================================================================</code>
928 <code style='color:#c34e00; '> The hook registration function (also initializes the default config values):</code>
929 <code style='color:#c34e00; '> ==============================================================================</code>
930 <code style='color:#c34e00; '> */</code>
931 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>void</code> register_hooks<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>)</code>
932 <code style='color:#806030; '>{</code>
933 config<code style='color:#806030; '>.</code>enabled <code style='color:#806030; '>=</code> <code style='color:#c00000; '>1</code><code style='color:#806030; '>;</code>
934 config<code style='color:#806030; '>.</code>path <code style='color:#806030; '>=</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>/foo/bar</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
935 config<code style='color:#806030; '>.</code>typeOfAction <code style='color:#806030; '>=</code> <code style='color:#c00000; '>3</code><code style='color:#806030; '>;</code>
936 ap_hook_handler<code style='color:#806030; '>(</code>example_handler<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> APR_HOOK_LAST<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
937 <code style='color:#806030; '>}</code>
938 <code style='color:#c34e00; '>/*</code>
939 <code style='color:#c34e00; '> ==============================================================================</code>
940 <code style='color:#c34e00; '> Our module name tag:</code>
941 <code style='color:#c34e00; '> ==============================================================================</code>
942 <code style='color:#c34e00; '> */</code>
943 module AP_MODULE_DECLARE_DATA example_module <code style='color:#806030; '>=</code>
944 <code style='color:#806030; '>{</code>
945 STANDARD20_MODULE_STUFF<code style='color:#806030; '>,</code>
946 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-directory configuration handler */</code>
947 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-directory configurations */</code>
948 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-server configuration handler */</code>
949 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-server configurations */</code>
950 example_directives<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Any directives we may have for httpd */</code>
951 register_hooks <code style='color:#c34e00; '>/* Our hook registering function */</code>
952 <code style='color:#806030; '>}</code><code style='color:#806030; '>;</code>
954 <!-- END EXAMPLE CODE -->
959 In our httpd.conf file, we can now change the hard-coded configuration by adding a few lines:
962 ExamplePath "/usr/bin/foo"
963 ExampleAction file allow
965 And thus we apply the configuration, visit <code>/example</code> on our web site, and we see the configuration has
966 adapted to what we wrote in our configuration file.
973 <section id="context"><title>Context aware configurations</title>
974 <section id="context_intro"><title>Introduction to context aware configurations</title>
976 In Apache, different URLs, virtual hosts, directories etc can have very different meanings
977 to the user of Apache, and thus different contexts within which modules must operate. For example,
978 let's assume you have this configuration set up for mod_rewrite:
980 <Directory "/var/www">
981 RewriteCond %{HTTP_HOST} ^example.com$
982 RewriteRule (.*) http://www.example.com/$1
984 <Directory "/var/www/sub">
985 RewriteRule ^foobar$ index.php?foobar=true
988 In this example, you will have set up two different contexts for mod_rewrite:
990 <li>Inside <code>/var/www</code>, all requests for <code>http://example.com</code> must go to <code>http://www.example.com</code></li>
991 <li>Inside <code>/var/www/sub</code>, all requests for <code>foobar</code> must go to <code>index.php?foobar=true</code></li>
993 If mod_rewrite (or Apache for that matter) wasn't context aware, then these rewrite rules would just apply to every and any request made,
994 regardless of where and how they were made, but since the module can pull the context specific configuration straight from Apache, it
995 does not need to know itself, which of the directives are valid in this context, since Apache takes care of this.</p>
998 So how does a module get the specific configuration for the server, directory or location in question? It does so by making one simple call:
1001 <!-- BEGIN EXAMPLE CODE -->
1002 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1003 <!-- END EXAMPLE CODE -->
1006 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>(<code style='color:#008833'>r->per_dir_config</code>, &example_module);
1008 That's it! Of course, a whole lot goes on behind the scenes, which we will discuss in this chapter, starting with how
1009 Apache came to know what our configuration looks like, and how it came to be set up as it is in the specific context.
1013 <section id="context_base"><title>Our basic configuration setup</title>
1014 <p>In this chapter, we will be working with a slightly modified version of our previous
1015 context structure. We will set a <code>context</code> variable that we can use to track
1016 which context configuration is being used by Apache in various places:
1019 <!-- BEGIN EXAMPLE CODE -->
1020 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1021 <code style='color:#400000; font-weight:bold; '>typedef</code> <code style='color:#400000; font-weight:bold; '>struct</code> <code style='color:#806030; '>{</code>
1022 <code style='color:#400000; font-weight:bold; '>char</code> context<code style='color:#806030; '>[</code><code style='color:#c00000; '>256</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
1023 <code style='color:#400000; font-weight:bold; '>char</code> path<code style='color:#806030; '>[</code><code style='color:#c00000; '>256</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
1024 <code style='color:#400000; font-weight:bold; '>int</code> typeOfAction<code style='color:#806030; '>;</code>
1025 <code style='color:#400000; font-weight:bold; '>int</code> enabled<code style='color:#806030; '>;</code>
1026 <code style='color:#806030; '>}</code> example_config<code style='color:#806030; '>;</code>
1028 <!-- END EXAMPLE CODE -->
1033 <p>Our handler for requests will also be modified, yet still very simple:
1036 <!-- BEGIN EXAMPLE CODE -->
1037 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1038 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
1039 <code style='color:#806030; '>{</code>
1040 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code> <code style='color:#806030; '>|</code><code style='color:#806030; '>|</code> <code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code><code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>example-handler</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#400000; font-weight:bold; '>return</code><code style='color:#806030; '>(</code>DECLINED<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1041 example_config<code style='color:#806030; '> *</code>config <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code>example_config<code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> ap_get_module_config<code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>per_dir_config</code><code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>example_module<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1042 ap_set_content_type<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>text/plain</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1043 ap_rprintf<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>Enabled: </code><code style='color:#0f6900; '>%u</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1044 ap_rprintf<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>Path: </code><code style='color:#0f6900; '>%s</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1045 ap_rprintf<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>TypeOfAction: </code><code style='color:#0f6900; '>%x</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1046 ap_rprintf<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>Context: </code><code style='color:#0f6900; '>%s</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>context</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1047 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
1048 <code style='color:#806030; '>}</code>
1050 <!-- END EXAMPLE CODE -->
1057 <section id="context_which"><title>Choosing a context</title>
1059 Before we can start making our module context aware, we must first define, which contexts we will accept.
1060 As we saw in the previous chapter, defining a directive required five elements be set:
1063 <!-- BEGIN EXAMPLE CODE -->
1064 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1065 <code style='color:#2e8800; '><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga07c7d22ae17805e61204463326cf9c34">AP_INIT_TAKE1</a></code>("exampleEnabled", example_set_enabled, <code style='color:#7f0055; font-weight:bold; '>NULL</code>, RSRC_CONF, "Enable or disable mod_example"),
1067 <!-- END EXAMPLE CODE -->
1070 The <code>RSRC_CONF</code> definition told Apache that we would only allow this directive in a global server context, but
1071 since we are now trying out a context aware version of our module, we should set this to something more lenient, namely
1072 the value <code>ACCESS_CONF</code>, which lets us use the directive inside <Directory> and <Location> blocks.
1076 <section id="context_pool"><title>Using Apache to allocate configuration slots</title>
1077 <p> A much smarter way to manage your configurations is by letting Apache help you create them.
1078 To do so, we must first start off by chancing our <em>name tag</em> to let Apache know, that
1079 it should assist us in creating and managing our configurations. Since we have chosen the per-directory
1080 (or per-location) context for our module configurations, we'll add a per-directory creator and merger
1081 function reference in our tag:
1083 <!-- BEGIN EXAMPLE CODE -->
1084 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1085 module AP_MODULE_DECLARE_DATA example_module <code style='color:#806030; '>=</code>
1086 <code style='color:#806030; '>{</code>
1087 STANDARD20_MODULE_STUFF<code style='color:#806030; '>,</code>
1088 create_dir_conf<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-directory configuration handler */</code>
1089 merge_dir_conf<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-directory configurations */</code>
1090 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-server configuration handler */</code>
1091 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-server configurations */</code>
1092 directives<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Any directives we may have for httpd */</code>
1093 register_hooks <code style='color:#c34e00; '>/* Our hook registering function */</code>
1094 <code style='color:#806030; '>}</code><code style='color:#806030; '>;</code>
1096 <!-- END EXAMPLE CODE -->
1105 <section id="context_which"><title>Creating new context configurations</title>
1107 Now that we have told Apache to help us create and manage configurations, our first step is to
1108 make a function for creating new, blank configurations. We do so by creating the function we just
1109 referenced in our name tag as the Per-directory configuration handler:
1110 <!-- BEGIN EXAMPLE CODE -->
1111 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1112 <code style='color:#400000; font-weight:bold; '>void</code><code style='color:#806030; '>*</code> example_create_dir_conf<code style='color:#806030; '>(</code>apr_pool_t<code style='color:#806030; '>*</code> pool<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>char</code><code style='color:#806030; '>*</code> context<code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1113 context <code style='color:#806030; '>=</code> context <code style='color:#806030; '>?</code> context <code style='color:#806030; '>:</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>(undefined context)</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
1114 example_config<code style='color:#806030; '> *</code>cfg <code style='color:#806030; '>=</code> apr_pcalloc<code style='color:#806030; '>(</code>pool<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>sizeof</code><code style='color:#806030; '>(</code>example_config<code style='color:#806030; '>)</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1115 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code>cfg<code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1116 <code style='color:#c34e00; '>/* Set some default values */</code>
1117 <code style='color:#800040; '>strcpy</code><code style='color:#806030; '>(</code>cfg<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>context</code><code style='color:#806030; '>,</code> x<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1118 cfg<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
1119 cfg<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code> <code style='color:#806030; '>=</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>/foo/bar</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
1120 cfg<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x11</code><code style='color:#806030; '>;</code>
1121 <code style='color:#806030; '>}</code>
1122 <code style='color:#400000; font-weight:bold; '>return</code> dir<code style='color:#806030; '>;</code>
1123 <code style='color:#806030; '>}</code>
1125 <!-- END EXAMPLE CODE -->
1131 <section id="context_which"><title>Merging configurations</title>
1133 Our next step in creating a context aware configuration is merging configurations. This part of the process
1134 particularly apply to scenarios where you have a parent configuration and a child, such as the following:
1136 <Directory "/var/www">
1138 ExamplePath /foo/bar
1139 ExampleAction file allow
1141 <Directory "/var/www/subdir">
1142 ExampleAction file deny
1145 In this example, it is natural to assume that the directory <code>/var/www/subdir</code> should inherit the
1146 value set for the <code>/var/www</code> directory, as we did not specify a <code>ExampleEnable</code> nor an
1147 <code>ExamplePath</code> for this directory. Apache does not presume to know if this is true, but cleverly
1150 <li>Creates a new configuration for <code>/var/www</code></li>
1151 <li>Sets the configuration values according to the directives given for <code>/var/www</code></li>
1152 <li>Creates a new configuration for <code>/var/www/subdir</code></li>
1153 <li>Sets the configuration values according to the directives given for <code>/var/www/subdir</code></li>
1154 <li><strong>Proposes a merge</strong> of the two configurations into a new configuration for <code>/var/www/subdir</code></li>
1156 This proposal is handled by the <code>merge_dir_conf</code> function we referenced in our name tag. The purpose of
1157 this function is to assess the two configurations and decide how they are to be merged:
1160 <!-- BEGIN EXAMPLE CODE -->
1161 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1162 <code style='color:#400000; font-weight:bold; '>void</code><code style='color:#806030; '>*</code> merge_dir_conf<code style='color:#806030; '>(</code>apr_pool_t<code style='color:#806030; '>*</code> pool<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code><code style='color:#806030; '>*</code> BASE<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code><code style='color:#806030; '>*</code> ADD<code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1163 example_config<code style='color:#806030; '>*</code> base <code style='color:#806030; '>=</code> BASE <code style='color:#806030; '>;</code>
1164 example_config<code style='color:#806030; '>*</code> add <code style='color:#806030; '>=</code> ADD <code style='color:#806030; '>;</code>
1165 example_config<code style='color:#806030; '>*</code> conf <code style='color:#806030; '>=</code> create_dir_conf<code style='color:#806030; '>(</code>pool<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Merged configuration</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1167 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>=</code><code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code> <code style='color:#806030; '>)</code> <code style='color:#806030; '>?</code> base<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>:</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>;</code>
1168 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>=</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>?</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>:</code> base<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code><code style='color:#806030; '>;</code>
1169 <code style='color:#800040; '>strcpy</code><code style='color:#806030; '>(</code>conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>,</code> <code style='color:#800040; '>strlen</code><code style='color:#806030; '>(</code>add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>?</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code> <code style='color:#806030; '>:</code> base<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1171 <code style='color:#400000; font-weight:bold; '>return</code> conf <code style='color:#806030; '>;</code>
1172 <code style='color:#806030; '>}</code>
1174 <!-- END EXAMPLE CODE -->
1179 <section id="context_which"><title>Trying out our new context aware configurations</title>
1181 Now, let's try putting it all together to create a new module that it context aware. First off, we'll
1182 create a configuration that lets us test how the module works:
1184 <Location "/a">
1185 SetHandler example-handler
1187 ExamplePath "/foo/bar"
1188 ExampleAction file allow
1191 <Location "/a/b">
1192 ExampleAction file deny
1196 <Location "/a/b/c">
1197 ExampleAction db deny
1198 ExamplePath "/foo/bar/baz"
1202 Then we'll assemble our module code. Note, that since we are now using our name tag as reference when fetching
1203 configurations in our handler, I have added some prototypes to keep the compiler happy:
1206 <!-- BEGIN EXAMPLE CODE -->
1207 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1208 <code style='color:#c34e00; '>/*$6</code>
1209 <code style='color:#c34e00; '> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</code>
1210 <code style='color:#c34e00; '> * mod_example_config.c</code>
1211 <code style='color:#c34e00; '> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</code>
1212 <code style='color:#c34e00; '> */</code>
1215 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '><</code><code style='color:#40015a; '>stdio.h</code><code style='color:#800000; '>></code>
1216 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>apr_hash.h</code><code style='color:#800000; '>"</code>
1217 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>ap_config.h</code><code style='color:#800000; '>"</code>
1218 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>ap_provider.h</code><code style='color:#800000; '>"</code>
1219 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>httpd.h</code><code style='color:#800000; '>"</code>
1220 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_core.h</code><code style='color:#800000; '>"</code>
1221 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_config.h</code><code style='color:#800000; '>"</code>
1222 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_log.h</code><code style='color:#800000; '>"</code>
1223 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_protocol.h</code><code style='color:#800000; '>"</code>
1224 <code style='color:#004a43; '>#</code><code style='color:#004a43; '>include </code><code style='color:#800000; '>"</code><code style='color:#40015a; '>http_request.h</code><code style='color:#800000; '>"</code>
1226 <code style='color:#c34e00; '>/*$1</code>
1227 <code style='color:#c34e00; '> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</code>
1228 <code style='color:#c34e00; '>    Configuration structure</code>
1229 <code style='color:#c34e00; '> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</code>
1230 <code style='color:#c34e00; '> */</code>
1232 <code style='color:#400000; font-weight:bold; '>typedef</code> <code style='color:#400000; font-weight:bold; '>struct</code>
1233 <code style='color:#806030; '>{</code>
1234 <code style='color:#400000; font-weight:bold; '>char</code> context<code style='color:#806030; '>[</code><code style='color:#c00000; '>256</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
1235 <code style='color:#400000; font-weight:bold; '>char</code> path<code style='color:#806030; '>[</code><code style='color:#c00000; '>256</code><code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
1236 <code style='color:#400000; font-weight:bold; '>int</code> typeOfAction<code style='color:#806030; '>;</code>
1237 <code style='color:#400000; font-weight:bold; '>int</code> enabled<code style='color:#806030; '>;</code>
1238 <code style='color:#806030; '>}</code> example_config<code style='color:#806030; '>;</code>
1240 <code style='color:#c34e00; '>/*$1</code>
1241 <code style='color:#c34e00; '> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</code>
1242 <code style='color:#c34e00; '>    Prototypes</code>
1243 <code style='color:#c34e00; '> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</code>
1244 <code style='color:#c34e00; '> */</code>
1246 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1247 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>example_set_enabled<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1248 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>example_set_path<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1249 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>example_set_action<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg1<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg2<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1250 <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>create_dir_conf<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>context<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1251 <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>merge_dir_conf<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>BASE<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>ADD<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1252 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>void</code> register_hooks<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1254 <code style='color:#c34e00; '>/*$1</code>
1255 <code style='color:#c34e00; '> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</code>
1256 <code style='color:#c34e00; '>    Configuration directives</code>
1257 <code style='color:#c34e00; '> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</code>
1258 <code style='color:#c34e00; '> */</code>
1260 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>const</code> command_rec directives<code style='color:#806030; '>[</code><code style='color:#806030; '>]</code> <code style='color:#806030; '>=</code>
1261 <code style='color:#806030; '>{</code>
1262 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga07c7d22ae17805e61204463326cf9c34">AP_INIT_TAKE1</a><code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>exampleEnabled</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> example_set_enabled<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> ACCESS_CONF<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Enable or disable mod_example</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>,</code>
1263 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#ga07c7d22ae17805e61204463326cf9c34">AP_INIT_TAKE1</a><code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>examplePath</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> example_set_path<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> ACCESS_CONF<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>The path to whatever</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>,</code>
1264 <a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__CONFIG.html#gafaec43534fcf200f37d9fecbf9247c21">AP_INIT_TAKE2</a><code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>exampleAction</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> example_set_action<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> ACCESS_CONF<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Special action value!</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>,</code>
1265 <code style='color:#806030; '>{</code> <code style='color:#007d45; '>NULL</code> <code style='color:#806030; '>}</code>
1266 <code style='color:#806030; '>}</code><code style='color:#806030; '>;</code>
1268 <code style='color:#c34e00; '>/*$1</code>
1269 <code style='color:#c34e00; '> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</code>
1270 <code style='color:#c34e00; '>    Our name tag</code>
1271 <code style='color:#c34e00; '> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</code>
1272 <code style='color:#c34e00; '> */</code>
1274 module AP_MODULE_DECLARE_DATA example_module <code style='color:#806030; '>=</code>
1275 <code style='color:#806030; '>{</code>
1276 STANDARD20_MODULE_STUFF<code style='color:#806030; '>,</code>
1277 create_dir_conf<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-directory configuration handler */</code>
1278 merge_dir_conf<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-directory configurations */</code>
1279 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Per-server configuration handler */</code>
1280 <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Merge handler for per-server configurations */</code>
1281 directives<code style='color:#806030; '>,</code> <code style='color:#c34e00; '>/* Any directives we may have for httpd */</code>
1282 register_hooks <code style='color:#c34e00; '>/* Our hook registering function */</code>
1283 <code style='color:#806030; '>}</code><code style='color:#806030; '>;</code>
1285 <code style='color:#c34e00; '>/*</code>
1286 <code style='color:#c34e00; '> =======================================================================================================================</code>
1287 <code style='color:#c34e00; '>    Hook registration function</code>
1288 <code style='color:#c34e00; '> =======================================================================================================================</code>
1289 <code style='color:#c34e00; '> */</code>
1290 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>void</code> register_hooks<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>)</code>
1291 <code style='color:#806030; '>{</code>
1292 ap_hook_handler<code style='color:#806030; '>(</code>example_handler<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> APR_HOOK_LAST<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1293 <code style='color:#806030; '>}</code>
1295 <code style='color:#c34e00; '>/*</code>
1296 <code style='color:#c34e00; '> =======================================================================================================================</code>
1297 <code style='color:#c34e00; '>    Our example web service handler</code>
1298 <code style='color:#c34e00; '> =======================================================================================================================</code>
1299 <code style='color:#c34e00; '> */</code>
1300 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
1301 <code style='color:#806030; '>{</code>
1302 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code> <code style='color:#806030; '>|</code><code style='color:#806030; '>|</code> <code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>handler</code><code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>example-handler</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#400000; font-weight:bold; '>return</code><code style='color:#806030; '>(</code>DECLINED<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1304 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1305 example_config <code style='color:#806030; '>*</code>config <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code>example_config <code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> ap_get_module_config<code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>per_dir_config</code><code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>example_module<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1306 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1308 ap_set_content_type<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>text/plain</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1309 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Enabled: </code><code style='color:#0f6900; '>%u</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1310 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Path: </code><code style='color:#0f6900; '>%s</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1311 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>TypeOfAction: </code><code style='color:#0f6900; '>%x</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1312 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Context: </code><code style='color:#0f6900; '>%s</code><code style='color:#0f6900; '>\n</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> config<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>context</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1313 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
1314 <code style='color:#806030; '>}</code>
1316 <code style='color:#c34e00; '>/*</code>
1317 <code style='color:#c34e00; '> =======================================================================================================================</code>
1318 <code style='color:#c34e00; '>    Handler for the "exambleEnabled" directive</code>
1319 <code style='color:#c34e00; '> =======================================================================================================================</code>
1320 <code style='color:#c34e00; '> */</code>
1321 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>example_set_enabled<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg<code style='color:#806030; '>)</code>
1322 <code style='color:#806030; '>{</code>
1323 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1324 example_config <code style='color:#806030; '>*</code>conf <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code>example_config <code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> cfg<code style='color:#806030; '>;</code>
1325 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1327 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code>conf<code style='color:#806030; '>)</code>
1328 <code style='color:#806030; '>{</code>
1329 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>on</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code>
1330 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>1</code><code style='color:#806030; '>;</code>
1331 <code style='color:#400000; font-weight:bold; '>else</code>
1332 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
1333 <code style='color:#806030; '>}</code>
1335 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
1336 <code style='color:#806030; '>}</code>
1338 <code style='color:#c34e00; '>/*</code>
1339 <code style='color:#c34e00; '> =======================================================================================================================</code>
1340 <code style='color:#c34e00; '>    Handler for the "examplePath" directive</code>
1341 <code style='color:#c34e00; '> =======================================================================================================================</code>
1342 <code style='color:#c34e00; '> */</code>
1343 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>example_set_path<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg<code style='color:#806030; '>)</code>
1344 <code style='color:#806030; '>{</code>
1345 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1346 example_config <code style='color:#806030; '>*</code>conf <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code>example_config <code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> cfg<code style='color:#806030; '>;</code>
1347 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1349 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code>conf<code style='color:#806030; '>)</code>
1350 <code style='color:#806030; '>{</code>
1351 <code style='color:#800040; '>strcpy</code><code style='color:#806030; '>(</code>conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>,</code> arg<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1352 <code style='color:#806030; '>}</code>
1354 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
1355 <code style='color:#806030; '>}</code>
1357 <code style='color:#c34e00; '>/*</code>
1358 <code style='color:#c34e00; '> =======================================================================================================================</code>
1359 <code style='color:#c34e00; '>    Handler for the "exampleAction" directive ;</code>
1360 <code style='color:#c34e00; '>    Let's pretend this one takes one argument (file or db), and a second (deny or allow), ;</code>
1361 <code style='color:#c34e00; '>    and we store it in a bit-wise manner.</code>
1362 <code style='color:#c34e00; '> =======================================================================================================================</code>
1363 <code style='color:#c34e00; '> */</code>
1364 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>example_set_action<code style='color:#806030; '>(</code>cmd_parms <code style='color:#806030; '>*</code>cmd<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>cfg<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg1<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>arg2<code style='color:#806030; '>)</code>
1365 <code style='color:#806030; '>{</code>
1366 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1367 example_config <code style='color:#806030; '>*</code>conf <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code>example_config <code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> cfg<code style='color:#806030; '>;</code>
1368 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1370 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code>conf<code style='color:#806030; '>)</code>
1371 <code style='color:#806030; '>{</code>
1372 <code style='color:#806030; '>{</code>
1373 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg1<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>file</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code>
1374 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x01</code><code style='color:#806030; '>;</code>
1375 <code style='color:#400000; font-weight:bold; '>else</code>
1376 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x02</code><code style='color:#806030; '>;</code>
1377 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>strcasecmp<code style='color:#806030; '>(</code>arg2<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>deny</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code>
1378 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>+</code><code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x10</code><code style='color:#806030; '>;</code>
1379 <code style='color:#400000; font-weight:bold; '>else</code>
1380 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>+</code><code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x20</code><code style='color:#806030; '>;</code>
1381 <code style='color:#806030; '>}</code>
1382 <code style='color:#806030; '>}</code>
1384 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>;</code>
1385 <code style='color:#806030; '>}</code>
1387 <code style='color:#c34e00; '>/*</code>
1388 <code style='color:#c34e00; '> =======================================================================================================================</code>
1389 <code style='color:#c34e00; '>    Function for creating new configurations for per-directory contexts</code>
1390 <code style='color:#c34e00; '> =======================================================================================================================</code>
1391 <code style='color:#c34e00; '> */</code>
1392 <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>create_dir_conf<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>context<code style='color:#806030; '>)</code>
1393 <code style='color:#806030; '>{</code>
1394 context <code style='color:#806030; '>=</code> context <code style='color:#806030; '>?</code> context <code style='color:#806030; '>:</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Newly created configuration</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
1396 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1397 example_config <code style='color:#806030; '>*</code>cfg <code style='color:#806030; '>=</code> apr_pcalloc<code style='color:#806030; '>(</code>pool<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>sizeof</code><code style='color:#806030; '>(</code>example_config<code style='color:#806030; '>)</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1398 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1400 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code>cfg<code style='color:#806030; '>)</code>
1401 <code style='color:#806030; '>{</code>
1402 <code style='color:#806030; '>{</code>
1403 <code style='color:#c34e00; '>/* Set some default values */</code>
1404 <code style='color:#800040; '>strcpy</code><code style='color:#806030; '>(</code>cfg<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>context</code><code style='color:#806030; '>,</code> context<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1405 cfg<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
1406 <code style='color:#800040; '>memset</code><code style='color:#806030; '>(</code>cfg<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>,</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>,</code> <code style='color:#c00000; '>256</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1407 cfg<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0x00</code><code style='color:#806030; '>;</code>
1408 <code style='color:#806030; '>}</code>
1409 <code style='color:#806030; '>}</code>
1411 <code style='color:#400000; font-weight:bold; '>return</code> cfg<code style='color:#806030; '>;</code>
1412 <code style='color:#806030; '>}</code>
1414 <code style='color:#c34e00; '>/*</code>
1415 <code style='color:#c34e00; '> =======================================================================================================================</code>
1416 <code style='color:#c34e00; '>    Merging function for configurations</code>
1417 <code style='color:#c34e00; '> =======================================================================================================================</code>
1418 <code style='color:#c34e00; '> */</code>
1419 <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>merge_dir_conf<code style='color:#806030; '>(</code>apr_pool_t <code style='color:#806030; '>*</code>pool<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>BASE<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>void</code> <code style='color:#806030; '>*</code>ADD<code style='color:#806030; '>)</code>
1420 <code style='color:#806030; '>{</code>
1421 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1422 example_config <code style='color:#806030; '>*</code>base <code style='color:#806030; '>=</code> BASE<code style='color:#806030; '>;</code>
1423 example_config <code style='color:#806030; '>*</code>add <code style='color:#806030; '>=</code> ADD<code style='color:#806030; '>;</code>
1424 example_config <code style='color:#806030; '>*</code>conf <code style='color:#806030; '>=</code> create_dir_conf<code style='color:#806030; '>(</code>pool<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>Merged configuration</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1425 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1427 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code>add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>=</code><code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>?</code> base<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code> <code style='color:#806030; '>:</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>enabled</code><code style='color:#806030; '>;</code>
1428 conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>=</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>?</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code> <code style='color:#806030; '>:</code> base<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>typeOfAction</code><code style='color:#806030; '>;</code>
1429 <code style='color:#800040; '>strcpy</code><code style='color:#806030; '>(</code>conf<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>,</code> <code style='color:#800040; '>strlen</code><code style='color:#806030; '>(</code>add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>?</code> add<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code> <code style='color:#806030; '>:</code> base<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>path</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1430 <code style='color:#400000; font-weight:bold; '>return</code> conf<code style='color:#806030; '>;</code>
1431 <code style='color:#806030; '>}</code>
1433 <!-- END EXAMPLE CODE -->
1441 <section id="summary"><title>Summing up</title>
1443 We have now looked at how to create simple modules for Apache and configuring them. What you do next is entirely up
1444 to you, but it is my hope that something valuable has come out of reading this documentation. If you have questions
1445 on how to further develop modules, you are welcome to join our <a href="http://httpd.apache.org/lists.html">mailing lists</a>
1446 or check out the rest of our documentation for further tips.
1450 <section id="snippets"><title>Some useful snippets of code</title>
1452 <section id="get_post"><title>Retrieve a variable from POST form data</title>
1455 <!-- BEGIN EXAMPLE CODE -->
1456 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1457 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>read_post_value<code style='color:#806030; '>(</code><code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>key<code style='color:#806030; '>)</code>
1458 <code style='color:#806030; '>{</code>
1459 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1460 <code style='color:#400000; font-weight:bold; '>const</code> apr_array_header_t <code style='color:#806030; '>*</code>fields<code style='color:#806030; '>;</code>
1461 <code style='color:#400000; font-weight:bold; '>int</code> i<code style='color:#806030; '>;</code>
1462 apr_table_entry_t <code style='color:#806030; '>*</code>e <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
1463 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1464 e <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code>apr_table_entry_t <code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> fields<code style='color:#806030; '>-</code><code style='color:#806030; '>></code>elts<code style='color:#806030; '>;</code>
1465 <code style='color:#400000; font-weight:bold; '>for</code><code style='color:#806030; '>(</code>i <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code> i <code style='color:#806030; '><</code> fields<code style='color:#806030; '>-</code><code style='color:#806030; '>></code>nelts<code style='color:#806030; '>;</code> i<code style='color:#806030; '>+</code><code style='color:#806030; '>+</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1466 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>!</code><code style='color:#800040; '>strcmp</code><code style='color:#806030; '>(</code>e<code style='color:#806030; '>[</code>i<code style='color:#806030; '>]</code><code style='color:#806030; '>.</code>key<code style='color:#806030; '>,</code> key<code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#400000; font-weight:bold; '>return</code> e<code style='color:#806030; '>[</code>i<code style='color:#806030; '>]</code><code style='color:#806030; '>.</code>val<code style='color:#806030; '>;</code>
1467 <code style='color:#806030; '>}</code>
1468 <code style='color:#400000; font-weight:bold; '>return</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
1469 <code style='color:#806030; '>}</code>
1470 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_req <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
1471 <code style='color:#806030; '>{</code>
1472 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~*/</code>
1473 apr_array_header_t <code style='color:#806030; '>*</code>POST<code style='color:#806030; '>;</code>
1474 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>value<code style='color:#806030; '>;</code>
1475 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~*/</code>
1476 ap_parse_form_data<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#007d45; '>NULL</code><code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>POST<code style='color:#806030; '>,</code> <code style='color:#806030; '>-</code><code style='color:#c00000; '>1</code><code style='color:#806030; '>,</code> <code style='color:#c00000; '>8192</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1478 value <code style='color:#806030; '>=</code> read_post_value<code style='color:#806030; '>(</code>POST<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>valueA</code><code style='color:#800000; '>"</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1479 <code style='color:#400000; font-weight:bold; '>if</code> <code style='color:#806030; '>(</code><code style='color:#806030; '>!</code>value<code style='color:#806030; '>)</code> value <code style='color:#806030; '>=</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>(undefined)</code><code style='color:#800000; '>"</code><code style='color:#806030; '>;</code>
1480 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '>The value of valueA is: </code><code style='color:#0f6900; '>%s</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> value<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1481 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
1482 <code style='color:#806030; '>}</code>
1484 <!-- END EXAMPLE CODE -->
1489 <section id="headers_out"><title>Printing out every HTTP header received</title>
1492 <!-- BEGIN EXAMPLE CODE -->
1493 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1494 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_req <code style='color:#806030; '>*</code>r<code style='color:#806030; '>)</code>
1495 <code style='color:#806030; '>{</code>
1496 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1497 <code style='color:#400000; font-weight:bold; '>const</code> apr_array_header_t <code style='color:#806030; '>*</code>fields<code style='color:#806030; '>;</code>
1498 <code style='color:#400000; font-weight:bold; '>int</code> i<code style='color:#806030; '>;</code>
1499 apr_table_entry_t <code style='color:#806030; '>*</code>e <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
1500 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1502 fields <code style='color:#806030; '>=</code> <a href="http://apr.apache.org/docs/apr/1.4/group__apr__tables.html#gaea3005541cce67481f48ab201b5c0cf3">apr_table_elts</a><code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code>headers_in<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1503 e <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code>apr_table_entry_t <code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> fields<code style='color:#806030; '>-</code><code style='color:#806030; '>></code>elts<code style='color:#806030; '>;</code>
1504 <code style='color:#400000; font-weight:bold; '>for</code><code style='color:#806030; '>(</code>i <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code> i <code style='color:#806030; '><</code> fields<code style='color:#806030; '>-</code><code style='color:#806030; '>></code>nelts<code style='color:#806030; '>;</code> i<code style='color:#806030; '>+</code><code style='color:#806030; '>+</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1505 ap_rprintf<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#800000; '>"</code><code style='color:#e60000; '><b></code><code style='color:#0f6900; '>%s</code><code style='color:#e60000; '></b>: </code><code style='color:#0f6900; '>%s</code><code style='color:#e60000; '><br/></code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> e<code style='color:#806030; '>[</code>i<code style='color:#806030; '>]</code><code style='color:#806030; '>.</code>key<code style='color:#806030; '>,</code> e<code style='color:#806030; '>[</code>i<code style='color:#806030; '>]</code><code style='color:#806030; '>.</code>val<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1506 <code style='color:#806030; '>}</code>
1507 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
1508 <code style='color:#806030; '>}</code>
1510 <!-- END EXAMPLE CODE -->
1515 <section id="request_body"><title>Reading the request body into memory</title>
1518 <!-- BEGIN EXAMPLE CODE -->
1519 <pre style='color:#000000;background:#ffffef;border: 1px dashed #333; padding: 0.5em; margin: 1em 2em 1em 1em;'>
1520 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> util_read<code style='color:#806030; '>(</code>request_rec <code style='color:#806030; '>*</code>r<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code><code style='color:#806030; '>*</code>rbuf<code style='color:#806030; '>,</code> apr_off_t <code style='color:#806030; '>*</code>size<code style='color:#806030; '>)</code>
1521 <code style='color:#806030; '>{</code>
1522 <code style='color:#c34e00; '>/*~~~~~~~~*/</code>
1523 <code style='color:#400000; font-weight:bold; '>int</code> rc <code style='color:#806030; '>=</code> OK<code style='color:#806030; '>;</code>
1524 <code style='color:#c34e00; '>/*~~~~~~~~*/</code>
1526 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>(</code>rc <code style='color:#806030; '>=</code> ap_setup_client_block<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> REQUEST_CHUNKED_ERROR<code style='color:#806030; '>)</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1527 <code style='color:#400000; font-weight:bold; '>return</code><code style='color:#806030; '>(</code>rc<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1528 <code style='color:#806030; '>}</code>
1530 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code>ap_should_client_block<code style='color:#806030; '>(</code>r<code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1532 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1533 <code style='color:#400000; font-weight:bold; '>char</code> argsbuffer<code style='color:#806030; '>[</code>HUGE_STRING_LEN<code style='color:#806030; '>]</code><code style='color:#806030; '>;</code>
1534 apr_off_t rsize<code style='color:#806030; '>,</code> len_read<code style='color:#806030; '>,</code> rpos <code style='color:#806030; '>=</code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>;</code>
1535 apr_off_t length <code style='color:#806030; '>=</code> r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>remaining</code><code style='color:#806030; '>;</code>
1536 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</code>
1538 <code style='color:#806030; '>*</code>rbuf <code style='color:#806030; '>=</code> <code style='color:#806030; '>(</code><code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> apr_pcalloc<code style='color:#806030; '>(</code>r<code style='color:#806030; '>-</code><code style='color:#806030; '>></code><code style='color:#008833'>pool</code><code style='color:#806030; '>,</code> <code style='color:#806030; '>(</code>apr_size_t<code style='color:#806030; '>)</code> <code style='color:#806030; '>(</code>length <code style='color:#806030; '>+</code> <code style='color:#c00000; '>1</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1539 <code style='color:#806030; '>*</code>size <code style='color:#806030; '>=</code> length<code style='color:#806030; '>;</code>
1540 <code style='color:#400000; font-weight:bold; '>while</code><code style='color:#806030; '>(</code><code style='color:#806030; '>(</code>len_read <code style='color:#806030; '>=</code> ap_get_client_block<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> argsbuffer<code style='color:#806030; '>,</code> <code style='color:#400000; font-weight:bold; '>sizeof</code><code style='color:#806030; '>(</code>argsbuffer<code style='color:#806030; '>)</code><code style='color:#806030; '>)</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>></code> <code style='color:#c00000; '>0</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1541 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code><code style='color:#806030; '>(</code>rpos <code style='color:#806030; '>+</code> len_read<code style='color:#806030; '>)</code> <code style='color:#806030; '>></code> length<code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1542 rsize <code style='color:#806030; '>=</code> length <code style='color:#806030; '>-</code> rpos<code style='color:#806030; '>;</code>
1543 <code style='color:#806030; '>}</code>
1544 <code style='color:#400000; font-weight:bold; '>else</code> <code style='color:#806030; '>{</code>
1545 rsize <code style='color:#806030; '>=</code> len_read<code style='color:#806030; '>;</code>
1546 <code style='color:#806030; '>}</code>
1548 <code style='color:#800040; '>memcpy</code><code style='color:#806030; '>(</code><code style='color:#806030; '>(</code><code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code><code style='color:#806030; '>)</code> <code style='color:#806030; '>*</code>rbuf <code style='color:#806030; '>+</code> rpos<code style='color:#806030; '>,</code> argsbuffer<code style='color:#806030; '>,</code> <code style='color:#806030; '>(</code><code style='color:#800040; '>size_t</code><code style='color:#806030; '>)</code> rsize<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1549 rpos <code style='color:#806030; '>+</code><code style='color:#806030; '>=</code> rsize<code style='color:#806030; '>;</code>
1550 <code style='color:#806030; '>}</code>
1551 <code style='color:#806030; '>}</code>
1552 <code style='color:#400000; font-weight:bold; '>return</code><code style='color:#806030; '>(</code>rc<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1553 <code style='color:#806030; '>}</code>
1555 <code style='color:#400000; font-weight:bold; '>static</code> <code style='color:#400000; font-weight:bold; '>int</code> example_handler<code style='color:#806030; '>(</code>request_req<code style='color:#806030; '>*</code> r<code style='color:#806030; '>)</code>
1556 <code style='color:#806030; '>{</code>
1557 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~*/</code>
1558 apr_off_t size<code style='color:#806030; '>;</code>
1559 <code style='color:#400000; font-weight:bold; '>const</code> <code style='color:#400000; font-weight:bold; '>char</code> <code style='color:#806030; '>*</code>buffer<code style='color:#806030; '>;</code>
1560 <code style='color:#c34e00; '>/*~~~~~~~~~~~~~~~~*/</code>
1562 <code style='color:#400000; font-weight:bold; '>if</code><code style='color:#806030; '>(</code>util_read<code style='color:#806030; '>(</code>r<code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>data<code style='color:#806030; '>,</code> <code style='color:#806030; '>&</code>size<code style='color:#806030; '>)</code> <code style='color:#806030; '>=</code><code style='color:#806030; '>=</code> OK<code style='color:#806030; '>)</code> <code style='color:#806030; '>{</code>
1563 ap_rprintf<code style='color:#806030; '>(</code><code style='color:#800000; '>"</code><code style='color:#e60000; '>We read a request body that was </code><code style='color:#0f6900; '>%u</code><code style='color:#e60000; '> bytes long</code><code style='color:#800000; '>"</code><code style='color:#806030; '>,</code> size<code style='color:#806030; '>)</code><code style='color:#806030; '>;</code>
1564 <code style='color:#806030; '>}</code>
1565 <code style='color:#400000; font-weight:bold; '>return</code> OK<code style='color:#806030; '>;</code>
1566 <code style='color:#806030; '>}</code>
1568 <!-- END EXAMPLE CODE -->