📄 mod_example.c
字号:
/* 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_'. */#include "httpd.h"#include "http_config.h"#include "http_core.h"#include "http_log.h"#include "http_main.h"#include "http_protocol.h"#include "http_request.h"#include "util_script.h"#include "http_connection.h"#include "apr_strings.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 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. */typedef struct x_cfg { 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. */} 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. */static const char *trace = NULL;static apr_table_t *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 apr_pool_t *x_pool = NULL;static apr_pool_t *x_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 AP_MODULE_DECLARE_DATA 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. 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. */static x_cfg *our_dconfig(const request_rec *r){ return (x_cfg *) ap_get_module_config(r->per_dir_config, &example_module);}#if 0/* * Locate our server configuration record for the specified server. */static x_cfg *our_sconfig(const server_rec *s){ return (x_cfg *) ap_get_module_config(s->module_config, &example_module);}/* * Likewise for our configuration record for the specified request. */static x_cfg *our_rconfig(const request_rec *r){ return (x_cfg *) ap_get_module_config(r->request_config, &example_module);}#endif/* * Likewise for our configuration record for a connection. */static x_cfg *our_cconfig(const conn_rec *c){ return (x_cfg *) ap_get_module_config(c->conn_config, &example_module);}/* * This routine sets up some module-wide cells if they haven't been already. */static void setup_module_cells(void){ /* * If we haven't already allocated our module-private pool, do so now. */ if (x_pool == NULL) { apr_pool_create(&x_pool, NULL); }; /* * Likewise for the table of routine/environment pairs we visit outside of * request context. */ if (static_calls_made == NULL) { static_calls_made = apr_table_make(x_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 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. */#define TRACE_NOTE "example-trace"static void trace_add(server_rec *s, request_rec *r, x_cfg *mconfig, const char *note){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -