]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
Final sync of mod_rewrite for Apache 1.2.2:
authorRalf S. Engelschall <rse@apache.org>
Thu, 31 Jul 1997 08:43:16 +0000 (08:43 +0000)
committerRalf S. Engelschall <rse@apache.org>
Thu, 31 Jul 1997 08:43:16 +0000 (08:43 +0000)
  o added diff between mod_rewrite 3.0.6+ and 3.0.9
    minus WIN32/NT stuff, but plus copyright removement.
    In detail:
    - workaround for detecting infinite rewriting loops
    - fixed setting of env vars when "-" is used as subst string
    - fixed forced response code on redirects (PR#777)
    - fixed cases where r->args is ""
    - kludge to disable locking on pipes under braindead SunOS
    - fix for rewritelog in cases where remote hostname is unknown
    - fixed totally damaged request_rec walk-back loop
  o remove static from local data and add static to global ones.
  o replaced ugly proxy finding stuff by simple
    find_linked_module("mod_proxy") call.
  o added missing negation char on rewritelog()
  o fixed a few comment typos

PR: 777
Submitted by:   Ralf S. Engelschall
Reviewed by:    Jim Jagielski, Randy Terbush, Ralf S. Engelschall

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/1.3@78828 13f79535-47bb-0310-9956-ffa450edef68

APACHE_1_2_X/src/CHANGES
APACHE_1_2_X/src/modules/standard/mod_rewrite.c
APACHE_1_2_X/src/modules/standard/mod_rewrite.h

index 474976dfab6bc065d50840d548cc811b7cd6df3d..d98b4dff8233663eb60c54c852bb8cef6c605b6d 100644 (file)
@@ -1,5 +1,24 @@
 Changes with Apache 1.2.2
 
+  *) Last official synchonisation of mod_rewrite with author version (because
+     mod_rewrite is now directly developed by the author at the Apache Group):
+     o added diff between mod_rewrite 3.0.6+ and 3.0.9
+       minus WIN32/NT stuff, but plus copyright removement.
+       In detail:
+       - workaround for detecting infinite rewriting loops
+       - fixed setting of env vars when "-" is used as subst string
+       - fixed forced response code on redirects (PR#777)
+       - fixed cases where r->args is ""
+       - kludge to disable locking on pipes under braindead SunOS
+       - fix for rewritelog in cases where remote hostname is unknown
+       - fixed totally damaged request_rec walk-back loop
+     o remove static from local data and add static to global ones.
+     o replaced ugly proxy finding stuff by simple
+       find_linked_module("mod_proxy") call.
+     o added missing negation char on rewritelog()
+     o fixed a few comment typos
+     [Ralf S. Engelschall]
+
   *) Anonymous_LogEmail was logging on each subrequest.
      [Dean Gaudet] PR#421, PR#868
 
index 471359a07e5ce7cc21e6deeda0873ca1348bbd63..93cfe8244b40b93b551a0c8c979ec3cd1a0792ba 100644 (file)
  */
 
 
-/*
-**  mod_rewrite.c -- The Main Module Code
-**                       _                            _ _ 
+/*                       _                            _ _ 
 **   _ __ ___   ___   __| |    _ __ _____      ___ __(_) |_ ___ 
 **  | '_ ` _ \ / _ \ / _` |   | '__/ _ \ \ /\ / / '__| | __/ _ \
 **  | | | | | | (_) | (_| |   | | |  __/\ V  V /| |  | | ||  __/
 **  |_| |_| |_|\___/ \__,_|___|_|  \___| \_/\_/ |_|  |_|\__\___|
 **                       |_____|
 **
-**  URL Rewriting Module, Version 3.0.6 (15-Jun-1997)
+**  URL Rewriting Module
 **
 **  This module uses a rule-based rewriting engine (based on a
 **  regular-expression parser) to rewrite requested URLs on the fly. 
 **  can lead to internal subprocessing, external request redirection or even
 **  to internal proxy throughput.
 **
-**  The documentation and latest release can be found on
-**  http://www.engelschall.com/sw/mod_rewrite/
-**
-**  Copyright (c) 1996-1997 Ralf S. Engelschall, All rights reserved.
+**  This module was originally written in April 1996 and 
+**  gifted exclusively to the The Apache Group in July 1997 by
 **
-**  Written for The Apache Group by
 **      Ralf S. Engelschall
 **      rse@engelschall.com
 **      www.engelschall.com
@@ -219,7 +214,7 @@ module rewrite_module = {
 };
 
     /* the cache */
-cache *cachep;
+static cache *cachep;
 
     /* whether proxy module is available or not */
 static int proxy_available;
@@ -1114,6 +1109,7 @@ static int hook_fixup(request_rec *r)
     char *prefix;
     int l;
     int n;
+    char *ofilename;
 
     dconf = (rewrite_perdir_conf *)get_module_config(r->per_dir_config, &rewrite_module);
 
@@ -1146,6 +1142,13 @@ static int hook_fixup(request_rec *r)
             return DECLINED;
     }
 
+    /*
+     *  remember the current filename before rewriting for later check
+     *  to prevent deadlooping because of internal redirects
+     *  on final URL/filename which can be equal to the inital one.
+     */
+    ofilename = r->filename;
+
     /*
      *  now apply the rules ... 
      */
@@ -1260,6 +1263,18 @@ static int hook_fixup(request_rec *r)
             if (r->filename[0] != '/')
                 return BAD_REQUEST;
 
+            /* Check for deadlooping:
+             * At this point we KNOW that at least one rewriting
+             * rule was applied, but when the resulting URL is
+             * the same as the initial URL, we are not allowed to
+             * use the following internal redirection stuff because
+             * this would lead to a deadloop.
+             */
+            if (strcmp(r->filename, ofilename) == 0) {
+                rewritelog(r, 1, "[per-dir %s] initial URL equal rewritten URL: %s [IGNORING REWRITE]", dconf->directory, r->filename);
+                return OK;
+            }
+
             /* if there is a valid base-URL then substitute
                the per-dir prefix with this base-URL if the
                current filename still is inside this per-dir 
@@ -1501,8 +1516,17 @@ static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p, char *perdir
             return 0; /* if any condition fails this complete rule fails */
 
         /* if this is a pure matching rule we return immediately */
-        if (strcmp(output, "-") == 0) 
+        if (strcmp(output, "-") == 0) {
+            /* but before we set the env variables... */
+            for (i = 0; p->env[i] != NULL; i++) {
+                strncpy(env2, p->env[i], sizeof(env2)-1);
+                EOS_PARANOIA(env2);
+                strncpy(env, pregsub(r->pool, env2, uri, regexp->re_nsub+1, regmatch), sizeof(env)-1);    /* substitute in output */
+                EOS_PARANOIA(env);
+                add_env_variable(r, env);
+            }
             return 2;
+        }
 
         /* if this is a forced proxy request ... */
         if (p->flags & RULEFLAG_PROXY) {
@@ -1535,7 +1559,7 @@ static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p, char *perdir
             return 1;
         }
 
-        /* if this is a implicit redirect in a per-dir rule */
+        /* if this is an implicit redirect in a per-dir rule */
         i = strlen(output);
         if (perdir != NULL
             && (   (i > 7 && strncmp(output, "http://", 7) == 0)
@@ -1567,10 +1591,9 @@ static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p, char *perdir
             return 1;
         }
 
-        /* add the previously stripped perdir prefix 
-           if the new URI is not a new one (i.e.
-           prefixed by a slash which means that is 
-           no for this per-dir context) */
+        /* add again the previously stripped perdir prefix if the new 
+           URI is not a new one (i.e. prefixed by a slash which means 
+           that it is not for this per-dir context)                    */
         if (prefixstrip && output[0] != '/') {
             rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s", perdir, output, perdir, output);
             output = pstrcat(r->pool, perdir, output, NULL);
@@ -1622,6 +1645,7 @@ static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p, char *perdir
            and the current URL still is not a fully qualified one we
            finally prefix it with http[s]://<ourname> explicitly */
         if (flags & RULEFLAG_FORCEREDIRECT) {
+            r->status = p->forced_responsecode;
             if (  !(strlen(r->filename) > 7 &&
                     strncmp(r->filename, "http://", 7) == 0)
                && !(strlen(r->filename) > 8 &&
@@ -1657,7 +1681,6 @@ static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p, char *perdir
                 else
                     rewritelog(r, 2, "[per-dir %s] prepare forced redirect %s -> %s", perdir, r->filename, newuri);
                 r->filename = pstrdup(r->pool, newuri);
-                r->status = p->forced_responsecode;
                 return 1;
             }
         }
@@ -1768,7 +1791,9 @@ static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p, char *perdir
     if (p->flags & CONDFLAG_NOTMATCH) 
         rc = !rc;
 
-    rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s' => %s", input, p->pattern, rc ? "matched" : "not-matched");
+    rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s%s' => %s", 
+               input, (p->flags & CONDFLAG_NOTMATCH ? "!" : ""), 
+               p->pattern, rc ? "matched" : "not-matched");
 
     /* end just return the result */
     return rc;
@@ -1806,9 +1831,15 @@ static void splitout_queryargs(request_rec *r, int qsappend)
             r->args = pstrcat(r->pool, q, "&", r->args, NULL);
         else
             r->args = pstrdup(r->pool, q);
-        if (r->args[strlen(r->args)-1] == '&')
-            r->args[strlen(r->args)-1] = '\0';
-        rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri, r->filename, r->args);
+        if (strlen(r->args) == 0) {
+            r->args = NULL;
+            rewritelog(r, 3, "split uri=%s -> uri=%s, args=<none>", olduri, r->filename);
+        }
+        else {
+            if (r->args[strlen(r->args)-1] == '&')
+                r->args[strlen(r->args)-1] = '\0';
+            rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri, r->filename, r->args);
+        }
     }
     return;            
 }
@@ -2211,7 +2242,9 @@ static char *lookup_map_program(request_rec *r, int fpin, int fpout, char *key)
     int i;
 
     /* lock the channel */
+#ifdef USE_PIPE_LOCKING
     fd_lock(fpin);
+#endif
 
     /* write out the request key */
     write(fpin, key, strlen(key));
@@ -2227,7 +2260,9 @@ static char *lookup_map_program(request_rec *r, int fpin, int fpout, char *key)
     buf[i] = '\0';
 
     /* unlock the channel */
+#ifdef USE_PIPE_LOCKING
     fd_unlock(fpin);
+#endif
 
     if (strcasecmp(buf, "NULL") == 0)
         return NULL;
@@ -2252,8 +2287,8 @@ static void open_rewritelog(server_rec *s, pool *p)
     rewrite_server_conf *conf;
     char *fname;
     FILE *fp;
-    static int    rewritelog_flags = ( O_WRONLY|O_APPEND|O_CREAT );
-    static mode_t rewritelog_mode  = ( S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH );
+    int    rewritelog_flags = ( O_WRONLY|O_APPEND|O_CREAT );
+    mode_t rewritelog_mode  = ( S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH );
   
     conf = get_module_config(s->module_config, &rewrite_module);
     
@@ -2304,14 +2339,15 @@ static void rewritelog(request_rec *r, int level, const char *text, ...)
     rewrite_server_conf *conf;
     conn_rec *connect;
     char *str1;
-    static char str2[HUGE_STRING_LEN];
-    static char str3[HUGE_STRING_LEN];
-    static char type[20];
-    static char redir[20];
+    char str2[512];
+    char str3[1024];
+    char type[20];
+    char redir[20];
     va_list ap;
     int i;
     request_rec *req;
     char *ruser;
+    const char *rhost;
     
     va_start(ap, text);
     conf = get_module_config(r->server->module_config, &rewrite_module);
@@ -2337,7 +2373,11 @@ static void rewritelog(request_rec *r, int level, const char *text, ...)
         ruser = "\"\"";
     }
 
-    str1 = pstrcat(r->pool, get_remote_host(connect, r->server->module_config, REMOTE_NAME), " ",
+    rhost = get_remote_host(connect, r->server->module_config, REMOTE_NAME);
+    if (rhost == NULL)
+        rhost = "UNKNOWN-HOST";
+
+    str1 = pstrcat(r->pool, rhost, " ",
                             (connect->remote_logname != NULL ? connect->remote_logname : "-"), " ",
                             ruser, NULL);
     ap_vsnprintf(str2, sizeof(str2), text, ap);
@@ -2347,8 +2387,8 @@ static void rewritelog(request_rec *r, int level, const char *text, ...)
     else
         strcpy(type, "subreq");
 
-    for (i = 0, req = r->prev; req != NULL; req = req->prev) 
-        ;
+    for (i = 0, req = r; req->prev != NULL; req = req->prev) 
+        i++;
     if (i == 0)
         redir[0] = '\0';
     else
@@ -3185,18 +3225,7 @@ static char **resolv_ipaddr_list(request_rec *r, char *name)
 
 static int is_proxy_available(server_rec *s)
 {
-    extern module *preloaded_modules[];
-    command_rec *c;
-    int n;
-    
-    for (n = 0; preloaded_modules[n] != NULL; n++) {
-        for (c = preloaded_modules[n]->cmds; c && c->name; ++c) {
-            if (strcmp(c->name, "ProxyRequests") == 0) {
-                return 1;
-            }
-        }
-    }
-    return 0;
+    return (find_linked_module("mod_proxy") != NULL);
 }
 
 
@@ -3278,7 +3307,7 @@ static void fd_unlock(int fd)
 **
 */
 
-int compare_lexicography(char *cpNum1, char *cpNum2)
+static int compare_lexicography(char *cpNum1, char *cpNum2)
 {
     int i;
     int n1, n2;
index b1a90a385734d40b1f596816e8d27fc6fb313c84..10f91070a8522d68e20d22289b14884cb378ce62 100644 (file)
@@ -56,7 +56,6 @@
 #define _MOD_REWRITE_H 1
 
 /*
-**  mod_rewrite.h -- Common Header File
 **                       _                            _ _ 
 **   _ __ ___   ___   __| |    _ __ _____      ___ __(_) |_ ___ 
 **  | '_ ` _ \ / _ \ / _` |   | '__/ _ \ \ /\ / / '__| | __/ _ \
@@ -64,7 +63,7 @@
 **  |_| |_| |_|\___/ \__,_|___|_|  \___| \_/\_/ |_|  |_|\__\___|
 **                       |_____|
 **
-**  URL Rewriting Module, Version 3.0.6 (15-Jun-1997)
+**  URL Rewriting Module
 **
 **  This module uses a rule-based rewriting engine (based on a
 **  regular-expression parser) to rewrite requested URLs on the fly. 
 **  can lead to internal subprocessing, external request redirection or even
 **  to internal proxy throughput.
 **
-**  The documentation and latest release can be found on
-**  http://www.engelschall.com/sw/mod_rewrite/
+**  This module was originally written in April 1996 and 
+**  gifted exclusively to the The Apache Group in July 1997 by
 **
-**  Copyright (c) 1996-1997 Ralf S. Engelschall, All rights reserved.
-**
-**  Written for The Apache Group by
 **      Ralf S. Engelschall
 **      rse@engelschall.com
 **      www.engelschall.com
 #include <fcntl.h>
 #endif
 
-
+    /* The locking support for the RewriteMap programs:
+       Locking a pipe to the child works fine under most
+       Unix derivates, but braindead SunOS 4.1.x has 
+       problems with this approach... */
+#define USE_PIPE_LOCKING 1
+#ifdef SUNOS4
+#undef USE_PIPE_LOCKING
+#endif
 
 
 /*
@@ -390,7 +393,7 @@ static void fd_lock(int fd);
 static void fd_unlock(int fd);
 
     /* Lexicographic Comparison */
-int compare_lexicography(char *cpNum1, char *cpNum2);
+static int compare_lexicography(char *cpNum1, char *cpNum2);
 
 #endif /* _MOD_REWRITE_H */