December 24th Monday (十二月 二十四日 月曜日)

本文介绍了Apache服务器中模块钩子机制的应用,包括如何使用APR_HOOK_MIDDLE注册钩子,以及如何实现可选钩子。通过示例展示了如何为auth_checker和client_login注册钩子,并在特定条件下运行这些钩子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_type_checker(choices_select, NULL, NULL, APR_HOOK_FIRST);

  These modules can use APR_HOOK_FIRST or APR_HOOK_LAST.  The values APR_HOOK_REALLY_FIRST and APR_HOOK_REALLY_LAST
are also available, although they are seldom appropriate and may give unexpected behavior.  For example, a REALLY_LAST
function may never run if it follows an Apache standard function that never returns DECLINED.

  The two NULL values are the predecessors and successors of our function, and they offer fine control.  They offer
the opportunity to name other modules explicitly.  They are appropriate for closely related modules whose functions,
if both modules are present, must run in a particular order.  These arguments take the form of a
NULL-terminated list of modules that must run before or after ours.

static void register_hooks(apr_pool_t *p)
{
  static const char * const aszPre[]={ "mod_authz_owner.c", NULL };
  ap_hook_auth_checker(check_user_access, aszPre,
    NULL, APR_HOOK_MIDDLE);
}

Exporting an Optional Hook

1. APR_DECLARE_EXTERNAL_HOOK(authz_dbd, AP, int, client_login,
      (request_rec *r, int code, const char *action))

2. APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(authz_dbd, AP, int, client_login,
      (request_rec *r, int code, const char *action),
      (r, code, action), OK, DECLINED)

3.there is a call to run the functions registered for the hook in the function implementing.
static int authz_dbd_login(request_rec *r, authz_dbd_cfg *cfg, const char *action)
{
  /* code omitted -- perform database login/logout */
  if (successful) {
    authz_dbd_run_client_login(r, code, action);
  }
  return code ;
}

Now a module implementing this hook a function straight.

static int client_cookie(request_rec *r, int code, const char *action) {
  if (strcmp(action, "login") == 0) {
    /* Set authentication token in client session cookie */
  }
  else if (strcmp(action, "logout") == 0) {
    /* Clear session cookie */
  }
  return OK;
}

static void register_hooks(apr_pool_t *pool) {
  APR_OPTIONAL_HOOK(authz_dbd, client_login, client_cookie,
  NULL, NULL, APR_HOOK_MIDDLE);
}

We could have implemented this function as a non-optional hook, by replacing the macro
APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL with APR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL from apr_hooks.h.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值