⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mod_example.c

📁 apache 安装教程 apache 安装教程
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. * */#include "httpd.h"#include "http_config.h"#include "http_core.h"#include "http_log.h"#include "http_main.h"#include "http_protocol.h"#include "util_script.h"#include <stdio.h>/*--------------------------------------------------------------------------*//*                                                                          *//* 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 *example_pool = NULL;static pool *example_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 example_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, &example_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, &example_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, &example_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 (example_pool == NULL) {        example_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(example_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 "example-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(example_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 (example_subpool != NULL) {            ap_destroy_pool(example_subpool);        }        example_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, "   <LI>\n", "    <DL>\n", "     <DT><SAMP>",                    note, "</SAMP>\n", "     </DT>\n", "     <DD><SAMP>[",                    where, "]</SAMP>\n", "     </DD>\n", "    </DL>\n",                    "   </LI>\n", NULL);    sofar = (trace_copy == NULL) ? "" : trace_copy;    trace_copy = ap_pstrcat(p, sofar, addon, NULL);    if (r != NULL) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -