📄 mod_example.c
字号:
static int x_pre_connection(conn_rec *c, void *csd){ x_cfg *cfg; cfg = our_cconfig(c);#if 0 /* * Log the call and exit. */ trace_add(r->server, NULL, cfg, "x_post_config()");#endif return OK;}/* This routine is used to actually process the connection that was received. * Only protocol modules should implement this hook, as it gives them an * opportunity to replace the standard HTTP processing with processing for * some other protocol. Both echo and POP3 modules are available as * examples. * * The return VALUE is OK, DECLINED, or HTTP_mumble. If we return OK, no * further modules are called for this phase. */static int x_process_connection(conn_rec *c){ return DECLINED;}/* * This routine is called after the request has been read but before any other * phases have been processed. This allows us to make decisions based upon * the input header fields. * * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no * further modules are called for this phase. */static int x_post_read_request(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); /* * We don't actually *do* anything here, except note the fact that we were * called. */ trace_add(r->server, r, cfg, "x_post_read_request()"); return DECLINED;}/* * This routine gives our module an opportunity to translate the URI into an * actual filename. If we don't do anything special, the server's default * rules (Alias directives and the like) will continue to be followed. * * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no * further modules are called for this phase. */static int x_translate_handler(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); /* * We don't actually *do* anything here, except note the fact that we were * called. */ trace_add(r->server, r, cfg, "x_translate_handler()"); return DECLINED;}/* * this routine gives our module another chance to examine the request * headers and to take special action. This is the first phase whose * hooks' configuration directives can appear inside the <Directory> * and similar sections, because at this stage the URI has been mapped * to the filename. For example this phase can be used to block evil * clients, while little resources were wasted on these. * * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, * the server will still call any remaining modules with an handler * for this phase. */static int x_header_parser_handler(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); /* * We don't actually *do* anything here, except note the fact that we were * called. */ trace_add(r->server, r, cfg, "header_parser_handler()"); return DECLINED;}/* * This routine is called to check the authentication information sent with * the request (such as looking up the user in a database and verifying that * the [encrypted] password sent matches the one in the database). * * The return value is OK, DECLINED, or some HTTP_mumble error (typically * HTTP_UNAUTHORIZED). If we return OK, no other modules are given a chance * at the request during this phase. */static int x_check_user_id(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); /* * Don't do anything except log the call. */ trace_add(r->server, r, cfg, "x_check_user_id()"); return DECLINED;}/* * This routine is called to check to see if the resource being requested * requires authorisation. * * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no * other modules are called during this phase. * * If *all* modules return DECLINED, the request is aborted with a server * error. */static int x_auth_checker(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); /* * Log the call and return OK, or access will be denied (even though we * didn't actually do anything). */ trace_add(r->server, r, cfg, "x_auth_checker()"); return DECLINED;}/* * This routine is called to check for any module-specific restrictions placed * upon the requested resource. (See the mod_access module for an example.) * * The return value is OK, DECLINED, or HTTP_mumble. All modules with an * handler for this phase are called regardless of whether their predecessors * return OK or DECLINED. The first one to return any other status, however, * will abort the sequence (and the request) as usual. */static int x_access_checker(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); trace_add(r->server, r, cfg, "x_access_checker()"); return DECLINED;}/* * This routine is called to determine and/or set the various document type * information bits, like Content-type (via r->content_type), language, et * cetera. * * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no * further modules are given a chance at the request for this phase. */static int x_type_checker(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); /* * Log the call, but don't do anything else - and report truthfully that * we didn't do anything. */ trace_add(r->server, r, cfg, "x_type_checker()"); return DECLINED;}/* * This routine is called to perform any module-specific fixing of header * fields, et cetera. It is invoked just before any content-handler. * * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the * server will still call any remaining modules with an handler for this * phase. */static int x_fixer_upper(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); /* * Log the call and exit. */ trace_add(r->server, r, cfg, "x_fixer_upper()"); return OK;}/* * This routine is called to perform any module-specific logging activities * over and above the normal server things. * * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, any * remaining modules with an handler for this phase will still be called. */static int x_logger(request_rec *r){ x_cfg *cfg; cfg = our_dconfig(r); trace_add(r->server, r, cfg, "x_logger()"); return DECLINED;}/*--------------------------------------------------------------------------*//* *//* Which functions are responsible for which hooks in the server. *//* *//*--------------------------------------------------------------------------*//* * Each function our module provides to handle a particular hook is * specified here. The functions are registered using * ap_hook_foo(name, predecessors, successors, position) * where foo is the name of the hook. * * The args are as follows: * name -> the name of the function to call. * predecessors -> a list of modules whose calls to this hook must be * invoked before this module. * successors -> a list of modules whose calls to this hook must be * invoked after this module. * position -> The relative position of this module. One of * APR_HOOK_FIRST, APR_HOOK_MIDDLE, or APR_HOOK_LAST. * Most modules will use APR_HOOK_MIDDLE. If multiple * modules use the same relative position, Apache will * determine which to call first. * If your module relies on another module to run first, * or another module running after yours, use the * predecessors and/or successors. * * The number in brackets indicates the order in which the routine is called * during request processing. Note that not all routines are necessarily * called (such as if a resource doesn't have access restrictions). * The actual delivery of content to the browser [9] is not handled by * a hook; see the handler declarations below. */static void x_register_hooks(apr_pool_t *p){ ap_hook_pre_config(x_pre_config, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_post_config(x_post_config, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_open_logs(x_open_logs, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_child_init(x_child_init, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_handler(x_handler, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_quick_handler(x_quick_handler, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_pre_connection(x_pre_connection, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_process_connection(x_process_connection, NULL, NULL, APR_HOOK_MIDDLE); /* [1] post read_request handling */ ap_hook_post_read_request(x_post_read_request, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_log_transaction(x_logger, NULL, NULL, APR_HOOK_MIDDLE);#if 0 ap_hook_http_method(x_http_method, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_default_port(x_default_port, NULL, NULL, APR_HOOK_MIDDLE);#endif ap_hook_translate_name(x_translate_handler, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_header_parser(x_header_parser_handler, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_check_user_id(x_check_user_id, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_fixups(x_fixer_upper, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_type_checker(x_type_checker, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_access_checker(x_access_checker, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_auth_checker(x_auth_checker, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_insert_filter(x_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);}/*--------------------------------------------------------------------------*//* *//* All of the routines have been declared now. Here's the list of *//* directives specific to our module, and information about where they *//* may appear and how the command parser should pass them to us for *//* processing. Note that care must be taken to ensure that there are NO *//* collisions of directive names between modules. *//* *//*--------------------------------------------------------------------------*//* * List of directives specific to our module. */static const command_rec x_cmds[] ={ AP_INIT_NO_ARGS( "Example", /* directive name */ cmd_example, /* config action routine */ NULL, /* argument to include in call */ OR_OPTIONS, /* where available */ "Example directive - no arguments" /* directive description */ ), {NULL}};/*--------------------------------------------------------------------------*//* *//* Finally, the list of callback routines and data structures that provide *//* the static hooks into our module from the other parts of the server. *//* *//*--------------------------------------------------------------------------*//* * Module definition for configuration. If a particular callback is not * needed, replace its routine name below with the word NULL. */module AP_MODULE_DECLARE_DATA example_module ={ STANDARD20_MODULE_STUFF, x_create_dir_config, /* per-directory config creator */ x_merge_dir_config, /* dir config merger */ x_create_server_config, /* server config creator */ x_merge_server_config, /* server config merger */ x_cmds, /* command table */ x_register_hooks, /* set up other request processing hooks */};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -