You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Alexei Kosut <ak...@nueva.pvt.k12.ca.us> on 1997/06/29 08:22:33 UTC

*Match directives

As was discussed, and generally agreed to be advisable, I've rewritten
my patch to regex-enable mod_alias to use seperate directives for
the regular expression versions of Alias, ScriptAlias and Redirect. As
suggested, I've used the Match suffix, which matches (sic)
BrowserMatch.

The following patch also creates <DirectoryMatch>, <LocationMatch> and
<FilesMatch> sections, which do the same thing as <Directory ~>,
etc...

Index: http_core.c
===================================================================
RCS file: /export/home/cvs/apache/src/http_core.c,v
retrieving revision 1.87
diff -c -r1.87 http_core.c
*** http_core.c	1997/06/28 23:57:55	1.87
--- http_core.c	1997/06/29 06:16:15
***************
*** 637,643 ****
  #endif    
      cmd->override = OR_ALL|ACCESS_CONF;
  
!     if (!strcmp(cmd->path, "~")) {
  	cmd->path = getword_conf (cmd->pool, &arg);
  	r = pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
      }
--- 637,646 ----
  #endif    
      cmd->override = OR_ALL|ACCESS_CONF;
  
!     if (cmd->info) { /* <DirectoryMatch> */
! 	r = pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
!     }
!     else if (!strcmp(cmd->path, "~")) {
  	cmd->path = getword_conf (cmd->pool, &arg);
  	r = pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
      }
***************
*** 681,687 ****
      cmd->path = getword_conf (cmd->pool, &arg);
      cmd->override = OR_ALL|ACCESS_CONF;
  
!     if (!strcmp(cmd->path, "~")) {
  	cmd->path = getword_conf (cmd->pool, &arg);
  	r = pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
      }
--- 684,693 ----
      cmd->path = getword_conf (cmd->pool, &arg);
      cmd->override = OR_ALL|ACCESS_CONF;
  
!     if (cmd->info) { /* <LocationMatch> */
! 	r = pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
!     }
!     else if (!strcmp(cmd->path, "~")) {
  	cmd->path = getword_conf (cmd->pool, &arg);
  	r = pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
      }
***************
*** 728,734 ****
      if (cmd->path)
  	cmd->override = OR_ALL|ACCESS_CONF;
  
!     if (!strcmp(cmd->path, "~")) {
  	cmd->path = getword_conf (cmd->pool, &arg);
  	if (old_path && cmd->path[0] != '/' && cmd->path[0] != '^')
  	    cmd->path = pstrcat(cmd->pool, "^", old_path, cmd->path, NULL);
--- 734,745 ----
      if (cmd->path)
  	cmd->override = OR_ALL|ACCESS_CONF;
  
!     if (cmd->info) { /* <FilesMatch> */
! 	if (old_path && cmd->path[0] != '/' && cmd->path[0] != '^')
!             cmd->path = pstrcat(cmd->pool, "^", old_path, cmd->path, NULL);
!         r = pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
!     }
!     else if (!strcmp(cmd->path, "~")) {
  	cmd->path = getword_conf (cmd->pool, &arg);
  	if (old_path && cmd->path[0] != '/' && cmd->path[0] != '^')
  	    cmd->path = pstrcat(cmd->pool, "^", old_path, cmd->path, NULL);
***************
*** 1206,1211 ****
--- 1217,1225 ----
  { "</Limit>", endlimit, NULL, OR_ALL, RAW_ARGS, "Marks end of <Limit>" },
  { "<IfModule", start_ifmod, NULL, OR_ALL, RAW_ARGS, "Container for directives based on existance of specified modules" },
  { "</IfModule>", end_ifmod, NULL, OR_ALL, NO_ARGS, "Marks end of <IfModule>" },
+ { "<DirectoryMatch", dirsection, (void*)1, RSRC_CONF, RAW_ARGS, "Container for directives affecting resources located in the specified directories" },
+ { "<LocationMatch", urlsection, (void*)1, RSRC_CONF, RAW_ARGS, "Container for directives affecting resources accessed through the specified URL paths" },
+ { "<FilesMatch", filesection, (void*)1, OR_ALL, RAW_ARGS, "Container for directives affecting files matching specified patterns" },
  { "AuthType", set_string_slot, (void*)XtOffsetOf(core_dir_config, auth_type),
      OR_AUTHCFG, TAKE1, "An HTTP authorization type (e.g., \"Basic\")" },
  { "AuthName", set_string_slot, (void*)XtOffsetOf(core_dir_config, auth_name),
Index: mod_alias.c
===================================================================
RCS file: /export/home/cvs/apache/src/mod_alias.c,v
retrieving revision 1.16
diff -c -r1.16 mod_alias.c
*** mod_alias.c	1997/06/15 19:22:28	1.16
--- mod_alias.c	1997/06/29 06:16:16
***************
*** 65,70 ****
--- 65,71 ----
      char *real;
      char *fake;
      char *handler;
+     regex_t *regexp;
      int redir_status;		/* 301, 302, 303, 410, etc */
  } alias_entry;
  
***************
*** 117,123 ****
      return a;
  }
  
! const char *add_alias(cmd_parms *cmd, void *dummy, char *f, char *r)
  {
      server_rec *s = cmd->server;
      alias_server_conf *conf =
--- 118,125 ----
      return a;
  }
  
! const char *add_alias_internal(cmd_parms *cmd, void *dummy, char *f, char *r,
! 			       int use_regex)
  {
      server_rec *s = cmd->server;
      alias_server_conf *conf =
***************
*** 126,143 ****
  
      /* XX r can NOT be relative to DocumentRoot here... compat bug. */
      
      new->fake = f; new->real = r; new->handler = cmd->info;
      return NULL;
  }
  
! const char *add_redirect(cmd_parms *cmd, alias_dir_conf *dirconf, char *arg1,
! 			 char *arg2, char *arg3)
  {
      alias_entry *new;
      server_rec *s = cmd->server;
      alias_server_conf *serverconf =
          (alias_server_conf *)get_module_config(s->module_config,&alias_module);
      int status = (int)cmd->info;
      char *f = arg2;
      char *url = arg3;
  
--- 128,162 ----
  
      /* XX r can NOT be relative to DocumentRoot here... compat bug. */
      
+     if (use_regex) {
+ 	new->regexp = pregcomp(cmd->pool, f, REG_EXTENDED);
+ 	if (new->regexp == NULL)
+ 	    return "Regular expression could not be compiled.";
+     }
+ 
      new->fake = f; new->real = r; new->handler = cmd->info;
+ 
      return NULL;
  }
  
! const char *add_alias(cmd_parms *cmd, void *dummy, char *f, char *r) {
!     return add_alias_internal(cmd, dummy, f, r, 0);
! }
! 
! const char *add_alias_regex(cmd_parms *cmd, void *dummy, char *f, char *r) {
!     return add_alias_internal(cmd, dummy, f, r, 1);
! }
! 
! const char *add_redirect_internal(cmd_parms *cmd, alias_dir_conf *dirconf, 
! 				  char *arg1, char *arg2, char *arg3,
! 				  int use_regex)
  {
      alias_entry *new;
      server_rec *s = cmd->server;
      alias_server_conf *serverconf =
          (alias_server_conf *)get_module_config(s->module_config,&alias_module);
      int status = (int)cmd->info;
+     regex_t *r = NULL;
      char *f = arg2;
      char *url = arg3;
  
***************
*** 156,161 ****
--- 175,186 ----
  	url = arg2;
      }
  
+     if (use_regex) {
+         r = pregcomp(cmd->pool, f, REG_EXTENDED);
+         if (r == NULL)
+             return "Regular expression could not be compiled.";
+     }
+ 
      if (is_HTTP_REDIRECT(status)) {
  	if (!url) return "URL to redirect to is missing";
  	if (!is_url (url)) return "Redirect to non-URL";
***************
*** 169,179 ****
      else
          new = push_array (serverconf->redirects);
  
!     new->fake = f; new->real = url;
      new->redir_status = status;
      return NULL;
  }
  
  command_rec alias_cmds[] = {
  { "Alias", add_alias, NULL, RSRC_CONF, TAKE2, 
      "a fakename and a realname"},
--- 194,214 ----
      else
          new = push_array (serverconf->redirects);
  
!     new->fake = f; new->real = url; new->regexp = r;
      new->redir_status = status;
      return NULL;
  }
  
+ const char *add_redirect(cmd_parms *cmd, alias_dir_conf *dirconf, char *arg1,
+ 			 char *arg2, char *arg3) {
+     return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 0);
+ }
+ 
+ const char *add_redirect_regex(cmd_parms *cmd, alias_dir_conf *dirconf,
+ 			       char *arg1, char *arg2, char *arg3) {
+     return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 1);
+ }
+ 
  command_rec alias_cmds[] = {
  { "Alias", add_alias, NULL, RSRC_CONF, TAKE2, 
      "a fakename and a realname"},
***************
*** 182,187 ****
--- 217,229 ----
  { "Redirect", add_redirect, (void*)HTTP_MOVED_TEMPORARILY, 
      OR_FILEINFO, TAKE23, 
      "an optional status, then document to be redirected and destination URL" },
+ { "AliasMatch", add_alias_regex, NULL, RSRC_CONF, TAKE2, 
+     "a regular expression and a filename"},
+ { "ScriptAliasMatch", add_alias_regex, "cgi-script", RSRC_CONF, TAKE2, 
+     "a regular expression and a filename"},
+ { "RedirectMatch", add_redirect_regex, (void*)HTTP_MOVED_TEMPORARILY, 
+     OR_FILEINFO, TAKE23, 
+     "an optional status, then a regular expression and destination URL" },
  { "RedirectTemp", add_redirect, (void*)HTTP_MOVED_TEMPORARILY, 
      OR_FILEINFO, TAKE2, 
      "a document to be redirected, then the destination URL" },
***************
*** 228,255 ****
  char *try_alias_list (request_rec *r, array_header *aliases, int doesc, int *status)
  {
      alias_entry *entries = (alias_entry *)aliases->elts;
      int i;
      
      for (i = 0; i < aliases->nelts; ++i) {
          alias_entry *p = &entries[i];
!         int l = alias_matches (r->uri, p->fake);
  
!         if (l > 0) {
  	    if (p->handler) { /* Set handler, and leave a note for mod_cgi */
! 	        r->handler = pstrdup(r->pool, p->handler);
  		table_set (r->notes, "alias-forced-type", p->handler);
  	    }
! 
  	    *status = p->redir_status;
  
! 	    if (doesc) {
! 		char *escurl;
! 		escurl = os_escape_path(r->pool, r->uri + l, 1);
! 
! 		return pstrcat(r->pool, p->real, escurl, NULL);
! 	    } else
! 		return pstrcat(r->pool, p->real, r->uri + l, NULL);
!         }
      }
  
      return NULL;
--- 270,313 ----
  char *try_alias_list (request_rec *r, array_header *aliases, int doesc, int *status)
  {
      alias_entry *entries = (alias_entry *)aliases->elts;
+     regmatch_t regm[10];
+     char *found = NULL;
      int i;
      
      for (i = 0; i < aliases->nelts; ++i) {
          alias_entry *p = &entries[i];
! 	int l;
! 
! 	if (p->regexp) {
! 	    if (!regexec(p->regexp, r->uri, p->regexp->re_nsub+1, regm, 0))
! 		found = pregsub(r->pool, p->real, r->uri,
! 				p->regexp->re_nsub+1, regm);
! 	}
! 	else {
! 	    l = alias_matches (r->uri, p->fake);
  
! 	    if (l > 0) {
! 		if (doesc) {
! 		    char *escurl;
! 		    escurl = os_escape_path(r->pool, r->uri + l, 1);
! 		    
! 		    found = pstrcat(r->pool, p->real, escurl, NULL);
! 		} else
! 		    found = pstrcat(r->pool, p->real, r->uri + l, NULL);
! 	    }
! 	}
! 
! 	if (found) {
  	    if (p->handler) { /* Set handler, and leave a note for mod_cgi */
! 		r->handler = pstrdup(r->pool, p->handler);
  		table_set (r->notes, "alias-forced-type", p->handler);
  	    }
! 	    
  	    *status = p->redir_status;
  
! 	    return found;
! 	}
! 	
      }
  
      return NULL;


-- 
________________________________________________________________________
Alexei Kosut <ak...@nueva.pvt.k12.ca.us>      The Apache HTTP Server
URL: http://www.nueva.pvt.k12.ca.us/~akosut/   http://www.apache.org/