{ Copyright 1999-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. } { * Apache example module. Provide demonstrations of how modules do things. * It is not meant to be used in a production server. Since it participates * in all of the processing phases, it could conceivable interfere with * the proper operation of other modules -- particularly the ones related * to security. * * In the interest of brevity, all functions and structures internal to * this module, but which may have counterparts in *real* modules, are * prefixed with 'x_' instead of 'example_'. } library mod_example; {$i define.inc} uses Classes, SysUtils, httpd, apr, aprutil; var example_module: module; {$ifdef Unix} public name 'example_module'; {$endif} default_module_ptr: Pmodule; {$ifdef WINDOWS} exports example_module name 'example_module'; {$endif} const MODULE_NAME = 'mod_example.so'; {--------------------------------------------------------------------------} { } { 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 0 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. } const CONFIG_MODE_SERVER = 1; CONFIG_MODE_DIRECTORY = 2; CONFIG_MODE_COMBO = 3; { Shouldn't ever happen. } type x_cfg = record cmode: Integer; { Environment to which record applies * (directory, server, or combination). } local: Integer; { Boolean: "Example" directive declared * here? } congenital: Integer; { Boolean: did we inherit an "Example"? } trace: PChar; { Pointer to trace string. } loc: PChar; { Location to which this record applies. } end; Px_cfg = ^x_cfg; { * 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. } var trace: PChar = nil; static_calls_made: Papr_table_t = nil; { * 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. } x_pool: Papr_pool_t = nil; x_subpool: Papr_pool_t = nil; {--------------------------------------------------------------------------} { } { 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. Declared in the command_rec * list with * AP_INIT_NO_ARGS("directive", function, mconfig, where, help) * * 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. Declared in the * command_rec list with * AP_INIT_RAW_ARGS("directive", function, mconfig, where, help) * * 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. * Declared in the command_rec list with * AP_INIT_FLAG("directive", function, mconfig, where, help) * * 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". Declared in the command_rec list with * AP_INIT_TAKE1("directive", function, mconfig, where, help) * * 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. Declared in the command_rec list with * AP_INIT_TAKE2("directive", function, mconfig, where, help) * * 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. * Declared in the command_rec list with * AP_INIT_TAKE3("directive", function, mconfig, where, help) * * 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. * Declared in the command_rec list with * AP_INIT_TAKE12("directive", function, mconfig, where, help) * * 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. * Declared in the command_rec list with * AP_INIT_TAKE123("directive", function, mconfig, where, help) * * 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. * Declared in the command_rec list with * AP_INIT_TAKE13("directive", function, mconfig, where, help) * * 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. * Declared in the command_rec list with * AP_INIT_TAKE23("directive", function, mconfig, where, help) * * 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. * Declared in the command_rec list with * AP_INIT_ITERATE("directive", function, mconfig, where, help) * * 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. * Declared in the command_rec list with * AP_INIT_ITERATE2("directive", function, mconfig, where, help) * * 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. } function our_dconfig(const r: Prequest_rec): Px_cfg; cdecl; begin Result := Px_cfg(ap_get_module_config(r^.per_dir_config, @example_module)); end; //#if 0 { * Locate our server configuration record for the specified server. } function our_sconfig(const s: Pserver_rec): Px_cfg; cdecl; begin Result := Px_cfg(ap_get_module_config(s^.module_config, @example_module)); end; { * Likewise for our configuration record for the specified request. } function our_rconfig(const r: Prequest_rec): Px_cfg; cdecl; begin Result := Px_cfg(ap_get_module_config(r^.request_config, @example_module)); end; //#endif { * Likewise for our configuration record for a connection. } function our_cconfig(const c: Pconn_rec): Px_cfg; cdecl; begin Result := Px_cfg(ap_get_module_config(c^.conn_config, @example_module)); end; { * This routine sets up some module-wide cells if they haven't been already. } procedure setup_module_cells; cdecl; begin { * If we haven't already allocated our module-private pool, do so now. } if (x_pool = nil) then apr_pool_create(@x_pool, nil); { * Likewise for the table of routine/environment pairs we visit outside of * request context. } if (static_calls_made = nil) then static_calls_made := apr_table_make(x_pool, 16); end; { * 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 x_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 apr_pool_t 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. } const TRACE_NOTE = 'example-trace'; EXAMPLE_LOG_EACH = 0; procedure trace_add(s: Pserver_rec; r: Prequest_rec; mconfig: Px_cfg; const note: PChar); cdecl; var sofar, addon, where, trace_copy, key: PChar; p: Papr_pool_t; begin { * 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 <> nil) then begin p := r^.pool; trace_copy := apr_table_get(r^.notes, TRACE_NOTE); if (trace_copy = nil) then trace_copy := ''; end else begin { * 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. } apr_pool_create(@p, x_pool); if (trace <> nil) then trace := apr_pstrdup(p, trace); { * Now, if we have a sub-pool from before, nuke it and replace with * the one we just allocated. } if (x_subpool <> nil) then apr_pool_destroy(x_subpool); x_subpool := p; trace_copy := trace; end; { * 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. } { Translation note. The part bellow is commented because there is an unidentified problem with it. } {if (mconfig <> nil) then where := mconfig^.loc else} where := 'nowhere'; if (where = nil) then where := ''; { * Now, if we're not in request context, see if we've been called with * this particular combination before. The apr_table_t is allocated in the * module's private pool, which doesn't get destroyed. } if (r = nil) then begin key := apr_pstrcat(p, [note, PChar(':'), where, nil]); if (apr_table_get(static_calls_made, key) <> nil) then { * Been here, done this. } Exit else { * First time for this combination of routine and environment - * log it so we don't do it again. } apr_table_set(static_calls_made, key, 'been here'); end; addon := apr_pstrcat(p, [ PChar('
' + LineEnding, r);
ap_rprintf(r, ' Apache HTTP Server version: "%s"' + LineEnding, [ap_get_server_version()]);
ap_rputs('
' + LineEnding, r);
ap_rprintf(r, ' Server built: "%s"' + LineEnding, [ap_get_server_built()]);
ap_rputs('
' + LineEnding, r); ap_rputs(' The format for the callback trace is:' + LineEnding, r); ap_rputs('
' + LineEnding, r); ap_rputs('' + LineEnding, r); ap_rputs(' The <routine-data> is supplied by' + LineEnding, r); ap_rputs(' the routine when it requests the trace,' + LineEnding, r); ap_rputs(' and the <applies-to> is extracted' + LineEnding, r); ap_rputs(' from the configuration record at the time of the trace.' + LineEnding, r); ap_rputs(' SVR() indicates a server environment' + LineEnding, r); ap_rputs(' (blank means the main or default server, otherwise it''s' + LineEnding, r); ap_rputs(' the name of the VirtualHost); DIR()' + LineEnding, r); ap_rputs(' indicates a location in the URL or filesystem' + LineEnding, r); ap_rputs(' namespace.' + LineEnding, r); ap_rputs('
' + LineEnding, r); ap_rprintf(r, '