Writing Phttpd modules
Writing a Phttpd module is not difficult, if you remember that
all modules must be reentrant (since Phttpd is a multithreaded program,
it can be executing multiple times in parallell in a module).
You should also avoid to use the normal STDIO routines, since they are
limited to using only the first 64 file descriptors. Use the
fdstdio
interface instead.
Compiling
You must always have the preprocessor symbol "_REENTRANT" defined before
including any system header files. You should also include "phttpd.h" so
that the prototype information about Phttpd interface routines are
included. Also remember to include a flag to the compiler to generate
position independant code (-fpic for GCC, -Kpic for SunSoft C and -pic for
Apogee C). Then link your code with "-G" to produce a shared library.
For example:
gcc -O -fpic -D_REENTRANT -c -o test.o test.c
/usr/ccs/bin/ld -G -o test.so test.o
Module entrypoints
- int pm_init(const char *argv[])
- This function gets called when Phttpd loads a module into
memory. The argv argument contains in [0] the
path to the module, and in the (optional) rest may contain
command line switches that apply to this module only. The function
should return -1 if something goes wrong during the initialization
part, else 0.
- int pm_request(struct connectioninfo *cip)
- This function gets called when Phttpd has received a request
and has determined that the module should be activated
(by looking into the "url-handlers" table).
- void pm_exit(void)
- This function gets called just before Phttpd wants to unload
the module from memory. It is the responsibility of this
function to free and allocated memory, close any opened
file descriptors and such.
Phttpd interfaces
- fdstdio
- Contains a "stdio" workalike, but without some
of the standard stdio library limitations. It is also based on
the normal file descriptors instead of some FILE pointer structure.
- safeio
- Contains cover functions to a set of library/system
calls that aren't as threads-safe as one might have wanted.
- http
- Contains functions help handling the HTTP protocol.
- mime
- Contains functions help handling the HTTP MIME headers.
- config
- Contains functions help handling module configuration files.
- html
- Contains some helper functions to generate HTML output.
- process
- Contains functions to help running subprocesses from a module.
- fscache
- Contains functions to access file system objects in an efficient way.
- fscache
- Contains functions to implement caches.
- hashtable
- Contains functions to manage hashed tables.
- table
- Contains functions to manage linear tables.
Compile and link this module, and install it into /opt/www/modules.
Then modify phttpd.conf so that it can be activated, for example
with a line like this in url-handlers:
/test test.so