/* * Apache example module. Provide demonstrations of how modules do things. * */ #include "mod_ptt.h" extern int numboards; extern boardheader_t *bcache; /*--------------------------------------------------------------------------*/ /* */ /* Data declarations. */ /* */ /* Here are the static cells and structure declarations private to our */ /* module. */ /* */ /*--------------------------------------------------------------------------*/ /* * Sample configuration record. Used for both per-directory and per-server * configuration data. * * It's perfectly reasonable to have two different structures for the two * different environments. The same command handlers will be called for * both, though, so the handlers need to be able to tell them apart. One * possibility is for both structures to start with an int which is zero for * one and 1 for the other. * * Note that while the per-directory and per-server configuration records are * available to most of the module handlers, they should be treated as * READ-ONLY by all except the command and merge handlers. Sometimes handlers * are handed a record that applies to the current location by implication or * inheritance, and modifying it will change the rules for other locations. */ typedef struct excfg { int cmode; /* Environment to which record applies (directory, * server, or combination). */ #define CONFIG_MODE_SERVER 1 #define CONFIG_MODE_DIRECTORY 2 #define CONFIG_MODE_COMBO 3 /* Shouldn't ever happen. */ int local; /* Boolean: "Example" directive declared here? */ int congenital; /* Boolean: did we inherit an "Example"? */ char *trace; /* Pointer to trace string. */ char *loc; /* Location to which this record applies. */ } excfg; /* * Let's set up a module-local static cell to point to the accreting callback * trace. As each API callback is made to us, we'll tack on the particulars * to whatever we've already recorded. To avoid massive memory bloat as * directories are walked again and again, we record the routine/environment * the first time (non-request context only), and ignore subsequent calls for * the same routine/environment. */ static const char *trace = NULL; static table *static_calls_made = NULL; /* * To avoid leaking memory from pools other than the per-request one, we * allocate a module-private pool, and then use a sub-pool of that which gets * freed each time we modify the trace. That way previous layers of trace * data don't get lost. */ static pool *ptt_pool = NULL; static pool *ptt_subpool = NULL; /* * Declare ourselves so the configuration routines can find and know us. * We'll fill it in at the end of the module. */ module MODULE_VAR_EXPORT ptt_module; /*--------------------------------------------------------------------------*/ /* */ /* The following pseudo-prototype declarations illustrate the parameters */ /* passed to command handlers for the different types of directive */ /* syntax. If an argument was specified in the directive definition */ /* (look for "command_rec" below), it's available to the command handler */ /* via the (void *) info field in the cmd_parms argument passed to the */ /* handler (cmd->info for the examples below). */ /* */ /*--------------------------------------------------------------------------*/ /* * Command handler for a NO_ARGS directive. * * static const char *handle_NO_ARGS(cmd_parms *cmd, void *mconfig); */ /* * Command handler for a RAW_ARGS directive. The "args" argument is the text * of the commandline following the directive itself. * * static const char *handle_RAW_ARGS(cmd_parms *cmd, void *mconfig, * const char *args); */ /* * Command handler for a FLAG directive. The single parameter is passed in * "bool", which is either zero or not for Off or On respectively. * * static const char *handle_FLAG(cmd_parms *cmd, void *mconfig, int bool); */ /* * Command handler for a TAKE1 directive. The single parameter is passed in * "word1". * * static const char *handle_TAKE1(cmd_parms *cmd, void *mconfig, * char *word1); */ /* * Command handler for a TAKE2 directive. TAKE2 commands must always have * exactly two arguments. * * static const char *handle_TAKE2(cmd_parms *cmd, void *mconfig, * char *word1, char *word2); */ /* * Command handler for a TAKE3 directive. Like TAKE2, these must have exactly * three arguments, or the parser complains and doesn't bother calling us. * * static const char *handle_TAKE3(cmd_parms *cmd, void *mconfig, * char *word1, char *word2, char *word3); */ /* * Command handler for a TAKE12 directive. These can take either one or two * arguments. * - word2 is a NULL pointer if no second argument was specified. * * static const char *handle_TAKE12(cmd_parms *cmd, void *mconfig, * char *word1, char *word2); */ /* * Command handler for a TAKE123 directive. A TAKE123 directive can be given, * as might be expected, one, two, or three arguments. * - word2 is a NULL pointer if no second argument was specified. * - word3 is a NULL pointer if no third argument was specified. * * static const char *handle_TAKE123(cmd_parms *cmd, void *mconfig, * char *word1, char *word2, char *word3); */ /* * Command handler for a TAKE13 directive. Either one or three arguments are * permitted - no two-parameters-only syntax is allowed. * - word2 and word3 are NULL pointers if only one argument was specified. * * static const char *handle_TAKE13(cmd_parms *cmd, void *mconfig, * char *word1, char *word2, char *word3); */ /* * Command handler for a TAKE23 directive. At least two and as many as three * arguments must be specified. * - word3 is a NULL pointer if no third argument was specified. * * static const char *handle_TAKE23(cmd_parms *cmd, void *mconfig, * char *word1, char *word2, char *word3); */ /* * Command handler for a ITERATE directive. * - Handler is called once for each of n arguments given to the directive. * - word1 points to each argument in turn. * * static const char *handle_ITERATE(cmd_parms *cmd, void *mconfig, * char *word1); */ /* * Command handler for a ITERATE2 directive. * - Handler is called once for each of the second and subsequent arguments * given to the directive. * - word1 is the same for each call for a particular directive instance (the * first argument). * - word2 points to each of the second and subsequent arguments in turn. * * static const char *handle_ITERATE2(cmd_parms *cmd, void *mconfig, * char *word1, char *word2); */ /*--------------------------------------------------------------------------*/ /* */ /* These routines are strictly internal to this module, and support its */ /* operation. They are not referenced by any external portion of the */ /* server. */ /* */ /*--------------------------------------------------------------------------*/ /* * Locate our directory configuration record for the current request. */ static excfg *our_dconfig(request_rec *r) { return (excfg *) ap_get_module_config(r->per_dir_config, &ptt_module); } #if 0 /* * Locate our server configuration record for the specified server. */ static excfg *our_sconfig(server_rec *s) { return (excfg *) ap_get_module_config(s->module_config, &ptt_module); } /* * Likewise for our configuration record for the specified request. */ static excfg *our_rconfig(request_rec *r) { return (excfg *) ap_get_module_config(r->request_config, &ptt_module); } #endif /* * This routine sets up some module-wide cells if they haven't been already. */ static void setup_module_cells() { /* * If we haven't already allocated our module-private pool, do so now. */ if (ptt_pool == NULL) { ptt_pool = ap_make_sub_pool(NULL); }; /* * Likewise for the table of routine/environment pairs we visit outside of * request context. */ if (static_calls_made == NULL) { static_calls_made = ap_make_table(ptt_pool, 16); }; } /* * This routine is used to add a trace of a callback to the list. We're * passed the server record (if available), the request record (if available), * a pointer to our private configuration record (if available) for the * environment to which the callback is supposed to apply, and some text. We * turn this into a textual representation and add it to the tail of the list. * The list can be displayed by the example_handler() routine. * * If the call occurs within a request context (i.e., we're passed a request * record), we put the trace into the request pool and attach it to the * request via the notes mechanism. Otherwise, the trace gets added * to the static (non-request-specific) list. * * Note that the r->notes table is only for storing strings; if you need to * maintain per-request data of any other type, you need to use another * mechanism. */ #define TRACE_NOTE "ptt-trace" static void trace_add(server_rec *s, request_rec *r, excfg *mconfig, const char *note) { const char *sofar; char *addon; char *where; pool *p; const char *trace_copy; /* * Make sure our pools and tables are set up - we need 'em. */ setup_module_cells(); /* * Now, if we're in request-context, we use the request pool. */ if (r != NULL) { p = r->pool; if ((trace_copy = ap_table_get(r->notes, TRACE_NOTE)) == NULL) { trace_copy = ""; } } else { /* * We're not in request context, so the trace gets attached to our * module-wide pool. We do the create/destroy every time we're called * in non-request context; this avoids leaking memory in some of * the subsequent calls that allocate memory only once (such as the * key formation below). * * Make a new sub-pool and copy any existing trace to it. Point the * trace cell at the copied value. */ p = ap_make_sub_pool(ptt_pool); if (trace != NULL) { trace = ap_pstrdup(p, trace); } /* * Now, if we have a sub-pool from before, nuke it and replace with * the one we just allocated. */ if (ptt_subpool != NULL) { ap_destroy_pool(ptt_subpool); } ptt_subpool = p; trace_copy = trace; } /* * If we weren't passed a configuration record, we can't figure out to * what location this call applies. This only happens for co-routines * that don't operate in a particular directory or server context. If we * got a valid record, extract the location (directory or server) to which * it applies. */ where = (mconfig != NULL) ? mconfig->loc : "nowhere"; where = (where != NULL) ? where : ""; /* * Now, if we're not in request context, see if we've been called with * this particular combination before. The table is allocated in the * module's private pool, which doesn't get destroyed. */ if (r == NULL) { char *key; key = ap_pstrcat(p, note, ":", where, NULL); if (ap_table_get(static_calls_made, key) != NULL) { /* * Been here, done this. */ return; } else { /* * First time for this combination of routine and environment - * log it so we don't do it again. */ ap_table_set(static_calls_made, key, "been here"); } } addon = ap_pstrcat(p, "
\n", r);
ap_rprintf(r, " Apache HTTP Server version: \"%s\"\n",
ap_get_server_version());
ap_rprintf(r,"r->filename : %s
",r->filename);
ap_rprintf(r,"r->request_time : %s
",ctime(&r->request_time));
ap_rprintf(r,"r->method : %s
",r->method);
ap_rprintf(r,"r->method_number : %d
",r->method_number);
ap_rprintf(r,"r->path_info : %s
",r->path_info);
ap_rprintf(r,"r->args : %s
",r->args);
ap_rprintf(r,"r->unparsed_uri : %s
",r->unparsed_uri);
ap_rprintf(r,"r->handler : %s
",r->handler);
ap_rprintf(r,"r->content_type : %s
",r->content_type);
ap_rprintf(r, " Server built: \"%s\"\n", ap_get_server_built());
for(i = 0; i++ < numboards; i++)
ap_rprintf(r,"%s %s
",bcache[i].brdname,bcache[i].title);
/*
* We're all done, so cancel the timeout we set. Since this is probably
* the end of the request we *could* assume this would be done during
* post-processing - but it's possible that another handler might be
* called and inherit our outstanding timer. Not good; to each its own.
*/
ap_kill_timeout(r);
/*
* We did what we wanted to do, so tell the rest of the server we
* succeeded.
*/
return OK;
}
/*--------------------------------------------------------------------------*/
/* */
/* Now let's declare routines for each of the callback phase in order. */
/* (That's the order in which they're listed in the callback list, *not */
/* the order in which the server calls them! See the command_rec */
/* declaration near the bottom of this file.) Note that these may be */
/* called for situations that don't relate primarily to our function - in */
/* other words, the fixup handler shouldn't assume that the request has */
/* to do with "example" stuff. */
/* */
/* With the exception of the content handler, all of our routines will be */
/* called for each request, unless an earlier handler from another module */
/* aborted the sequence. */
/* */
/* Handlers that are declared as "int" can return the following: */
/* */
/* OK Handler accepted the request and did its thing with it. */
/* DECLINED Handler took no action. */
/* HTTP_mumble Handler looked at request and found it wanting. */
/* */
/* What the server does after calling a module handler depends upon the */
/* handler's return value. In all cases, if the handler returns */
/* DECLINED, the server will continue to the next module with an handler */
/* for the current phase. However, if the handler return a non-OK, */
/* non-DECLINED status, the server aborts the request right there. If */
/* the handler returns OK, the server's next action is phase-specific; */
/* see the individual handler comments below for details. */
/* */
/*--------------------------------------------------------------------------*/
/*
* This function is called during server initialisation. Any information
* that needs to be recorded must be in static cells, since there's no
* configuration record.
*
* There is no return value.
*/
/*
* All our module-initialiser does is add its trace to the log.
*/
static void ptt_init(server_rec *s, pool *p)
{
char *note;
char *sname = s->server_hostname;
/*
* Set up any module cells that ought to be initialised.
*/
setup_module_cells();
/*
* The arbitrary text we add to our trace entry indicates for which server
* we're being called.
*/
sname = (sname != NULL) ? sname : "";
note = ap_pstrcat(p, "ptt_init(", sname, ")", NULL);
trace_add(s, NULL, NULL, note);
}
/*
* This function is called during server initialisation when an heavy-weight
* process (such as a child) is being initialised. As with the
* module-initialisation function, any information that needs to be recorded
* must be in static cells, since there's no configuration record.
*
* There is no return value.
*/
/*
* All our process-initialiser does is add its trace to the log.
*/
static void ptt_child_init(server_rec *s, pool *p)
{
char *note;
char *sname = s->server_hostname;
resolve_utmp();
resolve_boards();
resolve_garbage();
resolve_fcache();
/*
* Set up any module cells that ought to be initialised.
*/
setup_module_cells();
/*
* The arbitrary text we add to our trace entry indicates for which server
* we're being called.
*/
sname = (sname != NULL) ? sname : "";
note = ap_pstrcat(p, "ptt_child_init(", sname, ")", NULL);
trace_add(s, NULL, NULL, note);
}
/*
* This function is called when an heavy-weight process (such as a child) is
* being run down or destroyed. As with the child-initialisation function,
* any information that needs to be recorded must be in static cells, since
* there's no configuration record.
*
* There is no return value.
*/
/*
* All our process-death routine does is add its trace to the log.
*/
static void ptt_child_exit(server_rec *s, pool *p)
{
char *note;
char *sname = s->server_hostname;
/*
* The arbitrary text we add to our trace entry indicates for which server
* we're being called.
*/
sname = (sname != NULL) ? sname : "";
note = ap_pstrcat(p, "ptt_child_exit(", sname, ")", NULL);
trace_add(s, NULL, NULL, note);
}
/*
* This function gets called to create a per-directory configuration
* record. This will be called for the "default" server environment, and for
* each directory for which the parser finds any of our directives applicable.
* If a directory doesn't have any of our directives involved (i.e., they
* aren't in the .htaccess file, or a