You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by James Williamson <ja...@nameonthe.net> on 2003/04/09 16:48:08 UTC

mod_webapp patch

Hi,

I wrote this patch for the warp connector a while back which introduces
two new directives

WebAppIgnore [webapp] [pattern]
 and
WebAppMatch [webapp] [pattern]

We've been using it for a few months and it seems pretty stable, it's pretty
useful in that it hands back some of the power back to Apache such as
mod_rewrite
etc which mod_webapp had hitherto eliminated.

Regards,

James Williamson
www.nameonthe.net
Tel: +44 208 7415453
Fax: + 44 208 7411615

Index: apache-1.3/mod_webapp.c
===================================================================
RCS file:
/home/cvspublic/jakarta-tomcat-connectors/webapp/apache-1.3/mod_webapp.c,v
retrieving revision 1.36
diff -u -r1.36 mod_webapp.c
--- apache-1.3/mod_webapp.c 23 Jun 2002 16:50:40 -0000 1.36
+++ apache-1.3/mod_webapp.c 9 Apr 2003 14:36:58 -0000
@@ -63,10 +63,13 @@
 #include <httpd.h>
 #include <http_config.h>
 #include <http_core.h>
+#include <http_request.h>
 #include <http_log.h>
 #include <http_main.h>
+#include <fnmatch.h>
 #include <http_protocol.h>
 #include <util_script.h>
+
 #include <wa.h>

 /*
************************************************************************* */
@@ -82,6 +85,12 @@
 /* The main server using for logging error not related to requests */
 static server_rec *server=NULL;

+extern module dir_module;
+typedef struct dir_config_struct {
+    array_header *index_names;
+} dir_config_rec;
+
+
 /*
************************************************************************* */
 /* MODULE AND LIBRARY INITIALIZATION AND DESTRUCTION
*/
 /*
************************************************************************* */
@@ -232,6 +241,62 @@
     return(NULL);
 }

+/* Allow the option to match certain urls */
+static const char *wam_directive_ignore(cmd_parms *parms, void *mconfig,
char *name, char *w) {
+  wa_virtualhost *host = NULL;
+  wa_application *appl = NULL;
+  wa_chain *elem = NULL;
+  wa_chain *ign = NULL;
+
+  /* Paranoid check */
+  if (!wam_initialized) return(NULL);
+
+  /* Check if this host was recognized */
+  host=ap_get_module_config(parms->server->module_config,&webapp_module);
+  if (host==NULL) return(NULL);
+
+  for(elem = host->apps; elem; elem = elem->next) {
+    appl = (wa_application*) elem->curr;
+    if(strstr(name,appl->name)) {
+      ign = ap_pcalloc(parms->pool,sizeof(ign));
+      ign->next = NULL;
+      ign->curr = (void*)ap_pstrndup(parms->pool,w,strlen(w));
+      ign->next = appl->deny;
+      appl->deny = ign;
+    }
+  }
+  return OK;
+}
+
+
+/* Allow the option to match certain urls */
+static const char *wam_directive_match(cmd_parms *parms, void *mconfig,
char *name, char *w) {
+  wa_virtualhost *host = NULL;
+  wa_application *appl = NULL;
+  wa_chain *elem = NULL;
+  wa_chain *allw = NULL;
+
+  /* Paranoid check */
+  if (!wam_initialized) return(NULL);
+
+  /* Check if this host was recognized */
+  host=ap_get_module_config(parms->server->module_config,&webapp_module);
+  if (host==NULL) return(NULL);
+
+  for(elem = host->apps; elem; elem = elem->next) {
+    appl = (wa_application*) elem->curr;
+    if(strstr(name,appl->name)) {
+      allw = ap_pcalloc(parms->pool,sizeof(allw));
+      allw->next = NULL;
+      allw->curr = (void*)ap_pstrndup(parms->pool,w,(sizeof(w) + 1));
+      allw->next = appl->allw;
+      appl->allw = allw;
+    }
+  }
+  return OK;
+}
+
+
 /* The list of Directives for the WebApp module */
 static const command_rec wam_directives[] = {
     {
@@ -255,8 +320,21 @@
         RSRC_CONF,                  /* where available */
         TAKE3,                      /* arguments */
         "<name> <connection> <uri-path>"
-    }, {NULL}
-
+    }, {
+        "WebAppMatch",
+        wam_directive_match,
+        NULL,
+        OR_OPTIONS,
+        ITERATE2,
+        "<match pattern>"
+    }, {
+        "WebAppIgnore",
+        wam_directive_ignore,
+        NULL,
+        OR_OPTIONS,
+        ITERATE2,
+        "<ignore pattern>"
+    },NULL
 };

 /*
************************************************************************* */
@@ -385,20 +463,30 @@
     wam_handler_write,
 };

-/*
************************************************************************* */
-/* REQUEST HANDLING
*/
-/*
************************************************************************* */
+/* Do the directory merge */
+static void *wam_dir_merge(pool *p, void *base_conf, void *new_conf) {
+  wa_virtualhost *merged_config = (wa_virtualhost *) ap_pcalloc(p,
sizeof(wa_virtualhost));
+  wa_virtualhost *pconf = (wa_virtualhost *) base_conf;
+  wa_virtualhost *nconf = (wa_virtualhost *) new_conf;
+  /* return (void *) merged_config; */
+  fprintf(stdout,"merging");
+  return (void *)base_conf;
+}

-/* Match an Apache request */
-static int wam_match(request_rec *r) {
+/* Do the fixups */
+static int wam_fixups(request_rec *r) {
+    int num_names = 0;
+    request_rec *req = NULL;
+    char **redirs = NULL;
     wa_virtualhost *host=NULL;
     wa_application *appl=NULL;
     wa_chain *elem=NULL;
+    wa_chain *allw = NULL;
+    wa_chain *deny = NULL;

     /* Paranoid check */
     if (!wam_initialized) return(DECLINED);

-    /* Check if this host was recognized */
     host=ap_get_module_config(r->server->module_config,&webapp_module);
     if (host==NULL) return(DECLINED);

@@ -406,19 +494,30 @@
     elem=host->apps;
     while(elem!=NULL) {
         appl=(wa_application *)elem->curr;
-        if (strncmp(appl->rpth,r->uri,strlen(appl->rpth))==0) break;
-
+        if (strncmp(appl->rpth,r->filename,strlen(appl->rpth))==0) { /* got
a match */
+          for(deny = appl->deny; deny; deny = deny->next) {
+
ap_log_error(APLOG_MARK,APLOG_NOERRNO|APLOG_ERR,r->server,"trying %s against
%s",deny->curr,r->uri);
+            if(ap_fnmatch(deny->curr,r->uri,FNM_CASE_BLIND) == 0) {
+
ap_log_error(APLOG_MARK,APLOG_NOERRNO|APLOG_ERR,r->server,"denied %s against
%s",deny->curr,r->uri);
+              return DECLINED;
+            }
+          }
+          for(allw = appl->allw; allw; allw = allw->next) {
+            if(ap_fnmatch(allw->curr,r->uri,FNM_CASE_BLIND) == 0) {
+              r->handler=ap_pstrdup(r->pool,"webapp-handler");
+              ap_set_module_config(r->request_config, &webapp_module,
appl);
+              break;
+            }
+          }
+        }
         appl=NULL;
         elem=elem->next;
     }
-    if (appl==NULL) return(DECLINED);

-    /* The uri path is matched: set the handler and return */
-    r->handler=ap_pstrdup(r->pool,"webapp-handler");
+    if(appl == NULL) return DECLINED;

-    /* Set the webapp request structure into Apache's request structure */
-    ap_set_module_config(r->request_config, &webapp_module, appl);
-    return(OK);
+    /* Check if this host was recognized */
+    return DECLINED;
 }

 /* Handle the current request */
@@ -542,17 +641,17 @@
     STANDARD_MODULE_STUFF,
     wam_module_init,                    /* module initializer */
     NULL,                               /* per-directory config creator */
-    NULL,                               /* dir config merger */
+    wam_dir_merge,                      /* dir config merger */
     NULL,                               /* server config creator */
     NULL,                               /* server config merger */
     wam_directives,                     /* command table */
     wam_handlers,                       /* [9] list of handlers */
-    wam_match,                          /* [2] filename-to-URI translation
*/
+    NULL,                          /* [2] filename-to-URI translation */
     NULL,                               /* [5] check/validate user_id */
     NULL,                               /* [6] check user_id is valid
*here* */
     NULL,                               /* [4] check access by host address
*/
     NULL,                               /* [7] MIME type checker/setter */
-    NULL,                               /* [8] fixups */
+    wam_fixups,                               /* [8] fixups */
     NULL,                               /* [10] logger */
     NULL,                               /* [3] header parser */
     wam_child_init,                     /* child initializer */






---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org