21.6. Porting to Apache 2.0In addition to the earlier discussion on how to write a module from scratch for Apache 2.0, which is broadly the same as for 1.x, we'll show how to port one. First of all, it is probably easiest to compile the module using apxs (although we are not keen on this approach, it is definitely the easiest, sadly). You'll need to have configured Apache like this: ./configure --enable-so Then compiling mod_reveal is easy: apxs -c mod_reveal.c This will, once its working, yield .libs/mod_reveal.so (use the -i option, and apxs will obligingly install it in /usr/local/apache2/lib). However, compiling the Apache 1.x version of mod_reveal produces a large number of errors (note that you might save yourself some agony by adding -Wc,-Wall and -Wc,-Werror to the command line). The first problem is that some headers have been split up and moved around. So, we had to add: #include "http_request.h" to get the definition for server_rec. Also, many data structures and functions in Apache 1.3 had names that could cause conflict with other libraries. So, they have all been prefixed in an attempt to make them unique. The prefixes are ap_, apr_, and apu_ depending on whether they belong to Apache, APR, or APR-util. If they are data structures, they typically have also had _t appended. So, pool has become apr_pool_t. Many functions have also moved from ap_ to apr_; for example, ap_pstrcat( ) has become apr_pstrcat( ) and now needs the header apr_strings.h. Functions that didn't take pool arguments now do. For example:
becomes: ap_add_version_component(pPool,"Reveal/0.0"); The command structure is now typesafe and uses special macros for each type of command, depending on the number of parameters it takes. For example:
becomes:
As a consequence of the type-safety, some fast and loose trickery we played is no longer acceptable. For example:
becomes:
Handlers have changed completely and are now done via hooks. So, instead of:
we now have:
and an ap_hook_handler( ) entry in the RegisterHooks( ) function mentioned later in this section. Obviously, we haven't covered all the API changes. But Apache 2.0 API, unlike the 1.x API, is thoroughly documented, both in the headers and, using the doxygen documentation tool, on the Web (and, of course, in the distribution). The web-based documentation for APR and APR-util can be found here: http://apr.apache.org/. Documentation for everything that's documented can also be generated by typing: make dox at the top of the httpd-2.0 tree, though at the time of writing you do have to tweak docs/doxygen.conf slightly by hand. Sadly, there is no better way, at the moment, to figure out API changes than to dredge through these. The grep utility is extremely useful. Once the API changes have been dealt with, the next problem is to switch to the new hooking scheme. In 1.3, we had this:
In 2.0, this gets a lot shorter, as all the hooks are now initialized in a single function. All this is explained in more detail in the previous chapter, but here's what this becomes:
One minor glitch this revealed was that: static void RevealChildInit(server_rec *pServer,apr_pool_t *pPool) should now be: static void RevealChildInit(apr_pool_t *pPool,server_rec *pServer) And rather more frighteningly: static void RevealInit(server_rec *pServer,apr_pool_t *pPool) becomes:
returning a value of OK, which is fine in our case. Also note that we no longer have a child_exit hook — that can be done with a pool-cleanup function. For this module at least, that's it! All that has to be done now is to load it with an appropriate AddModule: LoadModule reveal_module .../mod_reveal.so and it behaves just like the Apache 1.3 version.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|