[svn:mod_parrot] r493 - in mod_parrot/trunk: include src

[email protected] Fri, 14 Nov 2008 22:11:15 -0800 (PST)
Newsgroups perl.cvs.mod_parrot
Message-ID <[email protected]>
Author: jhorwitz
Date: Fri Nov 14 22:11:15 2008
New Revision: 493

Modified:
   mod_parrot/trunk/include/modparrot_config.h
   mod_parrot/trunk/src/mod_parrot.c
   mod_parrot/trunk/src/modparrot_config.c
   mod_parrot/trunk/src/module.c

Log:
refactor hook registration so each hook has its own list of modules


Modified: mod_parrot/trunk/include/modparrot_config.h
==============================================================================
--- mod_parrot/trunk/include/modparrot_config.h	(original)
+++ mod_parrot/trunk/include/modparrot_config.h	Fri Nov 14 22:11:15 2008
@@ -119,6 +119,7 @@
     apr_array_header_t *preload;
     apr_array_header_t *module_array;
     apr_hash_t *module_hash;
+    apr_array_header_t *handler_modules[MP_HOOK_LAST];
 };
 typedef struct modparrot_srv_config modparrot_srv_config;
 

Modified: mod_parrot/trunk/src/mod_parrot.c
==============================================================================
--- mod_parrot/trunk/src/mod_parrot.c	(original)
+++ mod_parrot/trunk/src/mod_parrot.c	Fri Nov 14 22:11:15 2008
@@ -38,6 +38,8 @@
 
 #define MODPARROT_VERSION "0.4"
 
+#define NEXT_HANDLER_MODULE(x) (mpcfg->handler_modules[x] ? ((module **)mpcfg->module_array->elts)[mpcfg->handler_modules[x]->elts[++(ctxp->module_index)]] : NULL);
+
 /* declare our module */
 extern module AP_MODULE_DECLARE_DATA parrot_module;
 
@@ -292,7 +294,7 @@
 
     /* get next module in line -- count backwards since cleanups are LIFO */
     m = ctxp->module_index--;
-    if (m < 0) return HTTP_INTERNAL_SERVER_ERROR;
+    assert(m >= 0);
 
     /* decline if mod_parrot isn't enabled  */
     mpcfg = ap_get_module_config(r->server->module_config, &parrot_module);
@@ -301,14 +303,22 @@
     ctxp->r = r;
 
     /* get HLL config */
-    modp = ((module **)mpcfg->module_array->elts)[m];
+    /* can't use NEXT_HANDLER_MODULE here since cleanup handlers are LIFO */
+    modp = ((module **)mpcfg->module_array->elts)[mpcfg->handler_modules[MP_HOOK_CLEANUP]->elts[ctxp->module_index]];
     minfo = (modparrot_module_info *)modp->dynamic_load_handle;
-    /* call meta handler */
-    if (!modparrot_call_meta_handler(ctxp->interp, minfo->namespace,
-       "cleanup_handler", &status)) {
-        MPLOG_ERRORF(r->server, "no cleanup metahandler found for module '%s'",
-            modp->name);
-        status = HTTP_INTERNAL_SERVER_ERROR;
+    /* check for an actual cleanup metahandler sub */
+    /* XXX we shouldn't really be using get_sub_pmc here... */
+    if (get_sub_pmc(ctxp->interp, minfo->namespace, "cleanup_handler")) {
+        /* call meta handler */
+        if (!modparrot_call_meta_handler(ctxp->interp, minfo->namespace,
+           "cleanup_handler", &status)) {
+            MPLOG_ERRORF(
+                r->server,
+                "no cleanup metahandler found for module '%s'",
+                modp->name
+            );
+            status = HTTP_INTERNAL_SERVER_ERROR;
+        }
     }
 
     /* clean up */
@@ -325,7 +335,7 @@
     return APR_SUCCESS;
 }
 
-#define MP_REQUEST_METAHANDLER(hname, register_cleanup) \
+#define MP_REQUEST_METAHANDLER(hname, henum, register_cleanup) \
     int modparrot_meta_##hname(request_rec *r) \
     { \
         modparrot_context *ctxp; \
@@ -340,12 +350,12 @@
             MPLOG_ERROR(r->server, "context initialization failed"); \
             return HTTP_INTERNAL_SERVER_ERROR; \
         } \
-        /* get next module in line */ \
-        ctxp->module_index++; \
         /* decline if mod_parrot isn't enabled */ \
         mpcfg = ap_get_module_config(r->server->module_config, \
             &parrot_module); \
+        /* get next module in line */ \
         if (!(mpcfg->option_flags & MP_OPT_ENABLE)) return DECLINED; \
+        modp = NEXT_HANDLER_MODULE(henum); \
         /* take this opportunity to register the mod_parrot cleanup handler \
          * here, as we may not handle any other part of the request until
          * then. \
@@ -361,7 +371,6 @@
             return OK; \
         } \
         /* get HLL config */ \
-        modp = ((module **)mpcfg->module_array->elts)[ctxp->module_index]; \
         minfo = (modparrot_module_info *)modp->dynamic_load_handle; \
         dircfg = (modparrot_module_config *)ap_get_module_config( \
             r->per_dir_config, modp); \
@@ -379,17 +388,17 @@
         return status; \
     }
 
-MP_REQUEST_METAHANDLER(post_read_request_handler, 1)
-MP_REQUEST_METAHANDLER(map_to_storage_handler, 0)
-MP_REQUEST_METAHANDLER(trans_handler, 0)
-MP_REQUEST_METAHANDLER(header_parser_handler, 0)
-MP_REQUEST_METAHANDLER(access_handler, 0)
-MP_REQUEST_METAHANDLER(authen_handler, 0)
-MP_REQUEST_METAHANDLER(authz_handler, 0)
-MP_REQUEST_METAHANDLER(response_handler, 0)
-MP_REQUEST_METAHANDLER(type_handler, 0)
-MP_REQUEST_METAHANDLER(fixup_handler, 0)
-MP_REQUEST_METAHANDLER(log_handler, 0)
+MP_REQUEST_METAHANDLER(post_read_request_handler, MP_HOOK_POST_READ_REQUEST, 1)
+MP_REQUEST_METAHANDLER(map_to_storage_handler, MP_HOOK_MAP_TO_STORAGE, 0)
+MP_REQUEST_METAHANDLER(trans_handler, MP_HOOK_TRANS, 0)
+MP_REQUEST_METAHANDLER(header_parser_handler, MP_HOOK_HEADER_PARSER, 0)
+MP_REQUEST_METAHANDLER(access_handler, MP_HOOK_ACCESS, 0)
+MP_REQUEST_METAHANDLER(authen_handler, MP_HOOK_AUTHEN, 0)
+MP_REQUEST_METAHANDLER(authz_handler, MP_HOOK_AUTHZ, 0)
+MP_REQUEST_METAHANDLER(response_handler, MP_HOOK_RESPONSE, 0)
+MP_REQUEST_METAHANDLER(type_handler, MP_HOOK_TYPE, 0)
+MP_REQUEST_METAHANDLER(fixup_handler, MP_HOOK_FIXUP, 0)
+MP_REQUEST_METAHANDLER(log_handler, MP_HOOK_LOG, 0)
 
 int modparrot_meta_pre_connection_handler(conn_rec *c, void *csd)
 {
@@ -406,18 +415,17 @@
         return HTTP_INTERNAL_SERVER_ERROR;
     }
 
-    /* get next module in line */
-    ctxp->module_index++;
-
     /* decline if mod_parrot isn't enabled */
     mpcfg = ap_get_module_config(c->base_server->module_config, &parrot_module);
     if (!(mpcfg->option_flags & MP_OPT_ENABLE)) return DECLINED;
 
+    /* get next module in line */
+    modp = NEXT_HANDLER_MODULE(MP_HOOK_PRE_CONNECTION);
+
     ctxp->c = c;
     ctxp->csd = csd;
 
     /* get HLL config */
-    modp = ((module **)mpcfg->module_array->elts)[ctxp->module_index];
     minfo = (modparrot_module_info *)modp->dynamic_load_handle;
     /* call meta handler */
     if (!modparrot_call_meta_handler(ctxp->interp, minfo->namespace,
@@ -469,17 +477,16 @@
         return HTTP_INTERNAL_SERVER_ERROR;
     }
 
-    /* get next module in line */
-    ctxp->module_index++;
-
     /* decline if mod_parrot isn't enabled */
     mpcfg = ap_get_module_config(c->base_server->module_config, &parrot_module);
     if (!(mpcfg->option_flags & MP_OPT_ENABLE)) return DECLINED;
 
+    /* get next module in line */
+    modp = NEXT_HANDLER_MODULE(MP_HOOK_PROCESS_CONNECTION);
+
     ctxp->c = c;
 
     /* get HLL config */
-    modp = ((module **)mpcfg->module_array->elts)[ctxp->module_index];
     minfo = (modparrot_module_info *)modp->dynamic_load_handle;
     /* call meta handler */
     if (!modparrot_call_meta_handler(ctxp->interp, minfo->namespace,
@@ -530,18 +537,17 @@
         return;
     }
 
-    /* get next module in line */
-    ctxp->module_index++;
-
     /* decline if mod_parrot isn't enabled */
     mpcfg = ap_get_module_config(s->module_config, &parrot_module);
     if (!(mpcfg->option_flags & MP_OPT_ENABLE)) return;
 
+    /* get next module in line */
+    modp = NEXT_HANDLER_MODULE(MP_HOOK_CHILD_INIT);
+
     ctxp->pchild = p;
     ctxp->s = s;
 
     /* get HLL config */
-    modp = ((module **)mpcfg->module_array->elts)[ctxp->module_index];
     minfo = (modparrot_module_info *)modp->dynamic_load_handle;
     /* call meta handler */
     if (!modparrot_call_meta_handler(ctxp->interp, minfo->namespace,
@@ -592,20 +598,19 @@
         return HTTP_INTERNAL_SERVER_ERROR;
     }
 
-    /* get next module in line */
-    ctxp->module_index++;
-
     /* decline if mod_parrot isn't enabled */
     mpcfg = ap_get_module_config(s->module_config, &parrot_module);
     if (!(mpcfg->option_flags & MP_OPT_ENABLE)) return DECLINED;
 
+    /* get next module in line */
+    modp = NEXT_HANDLER_MODULE(MP_HOOK_POST_CONFIG);
+
     ctxp->pconf = pconf;
     ctxp->plog = plog;
     ctxp->ptemp = ptemp;
     ctxp->s = s;
 
     /* get HLL config */
-    modp = ((module **)mpcfg->module_array->elts)[ctxp->module_index];
     minfo = (modparrot_module_info *)modp->dynamic_load_handle;
     /* call meta handler */
     if (!modparrot_call_meta_handler(ctxp->interp, minfo->namespace,
@@ -707,20 +712,19 @@
         return HTTP_INTERNAL_SERVER_ERROR;
     }
 
-    /* get next module in line */
-    ctxp->module_index++;
-
     /* decline if mod_parrot isn't enabled -- but open_logs must return OK */
     mpcfg = ap_get_module_config(s->module_config, &parrot_module);
     if (!(mpcfg->option_flags & MP_OPT_ENABLE)) return OK;
 
+    /* get next module in line */
+    modp = NEXT_HANDLER_MODULE(MP_HOOK_OPEN_LOGS);
+
     ctxp->pconf = pconf;
     ctxp->plog = plog;
     ctxp->ptemp = ptemp;
     ctxp->s = s;
 
     /* get HLL config */
-    modp = ((module **)mpcfg->module_array->elts)[ctxp->module_index];
     minfo = (modparrot_module_info *)modp->dynamic_load_handle;
     /* call meta handler */
     if (!modparrot_call_meta_handler(ctxp->interp, minfo->namespace,

Modified: mod_parrot/trunk/src/modparrot_config.c
==============================================================================
--- mod_parrot/trunk/src/modparrot_config.c	(original)
+++ mod_parrot/trunk/src/modparrot_config.c	Fri Nov 14 22:11:15 2008
@@ -63,6 +63,7 @@
 void *create_modparrot_srv_config(apr_pool_t *p, server_rec *s)
 {
     modparrot_srv_config *cfg;
+    int i;
 
     cfg = (modparrot_srv_config *)apr_pcalloc(p, sizeof(modparrot_srv_config));
     cfg->pool = p;
@@ -75,6 +76,9 @@
     cfg->include_path = NULL;
     cfg->module_array = apr_array_make(p, 2, sizeof(module *));
     cfg->module_hash = apr_hash_make(p);
+    for (i = 0; i < MP_HOOK_LAST; i++) {
+        cfg->handler_modules[i] = NULL;
+    }
 
     /* destroy context pool on cleanup */
     apr_pool_cleanup_register(p, s, modparrot_cleanup, apr_pool_cleanup_null);

Modified: mod_parrot/trunk/src/module.c
==============================================================================
--- mod_parrot/trunk/src/module.c	(original)
+++ mod_parrot/trunk/src/module.c	Fri Nov 14 22:11:15 2008
@@ -231,6 +231,15 @@
 
     for (i = 0; i < MP_HOOK_LAST; i++) {
         if (minfo->hooks[i]) {
+            int *pidx;
+            /* add module to handler index so meta handlers know who we are */
+            if (!mpcfg->handler_modules[i]) {
+                mpcfg->handler_modules[i] = apr_array_make(p, 1, sizeof(int));
+            }
+            pidx = (int *)apr_array_push(mpcfg->handler_modules[i]);
+            *pidx = module_index;
+
+            /* register this hook with apache */
             switch(i) {
                 case MP_HOOK_OPEN_LOGS:
                     ap_hook_open_logs(modparrot_meta_open_logs_handler, NULL,