httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dean Gaudet <dgau...@arctic.org>
Subject [PATCH] make mod_rewrite use ap_cpystrn
Date Thu, 08 Jan 1998 02:32:17 GMT
Well I haven't tested this because I realised that Ralf is still in the
middle of a mod_rewrite restructuring... is that true?  If so do you have
a patch we can commit so I can continue performance tuning? 

At any rate, you'll note some speedups in this because of the useful
return value from ap_cpystrn.  In particular variable expansion is now
O(n) instead of O(n**2).  There's a bunch of cases where mod_rewrite uses
strlen() which could be sped up.

But I'm not committing it without Ralf's ok.

Dean

Index: modules/standard/mod_rewrite.c
===================================================================
RCS file: /export/home/cvs/apachen/src/modules/standard/mod_rewrite.c,v
retrieving revision 1.58
diff -u -r1.58 mod_rewrite.c
--- mod_rewrite.c	1998/01/07 16:46:55	1.58
+++ mod_rewrite.c	1998/01/08 02:26:46
@@ -1092,11 +1092,9 @@
             n = prefix_stat(r->filename, &finfo);
             if (n == 0) {
                 if ((cp = document_root(r)) != NULL) {
-                    strncpy(docroot, cp, sizeof(docroot)-1);
-                    EOS_PARANOIA(docroot);
+                    l = ap_cpystrn(docroot, cp, sizeof(docroot)) - docroot;
 
                     /* always NOT have a trailing slash */
-                    l = strlen(docroot);
                     if (docroot[l-1] == '/') {
                         docroot[l-1] = '\0';
                     }
@@ -1716,8 +1714,7 @@
      */
     if (strcmp(output, "-") == 0) {
         for (i = 0; p->env[i] != NULL; i++) {
-            strncpy(env, p->env[i], sizeof(env)-1);
-            EOS_PARANOIA(env);
+            ap_cpystrn(env, p->env[i], sizeof(env));
             expand_backref_inbuffer(r->pool, env, sizeof(env), briRR, '$');
             expand_backref_inbuffer(r->pool, env, sizeof(env), briRC, '%');
             add_env_variable(r, env);
@@ -1731,8 +1728,7 @@
      *  substitution URL string in `newuri'.
      */
     /*  1. take the output string  */
-    strncpy(newuri, output, sizeof(newuri)-1);
-    EOS_PARANOIA(newuri);
+    ap_cpystrn(newuri, output, sizeof(newuri));
     /*  2. expand $N (i.e. backrefs to RewriteRule pattern)  */
     expand_backref_inbuffer(r->pool, newuri, sizeof(newuri), briRR, '$');
     /*  3. expand %N (i.e. backrefs to latest RewriteCond pattern)  */
@@ -1753,8 +1749,7 @@
      */
     for (i = 0; p->env[i] != NULL; i++) {
         /*  1. take the string  */
-        strncpy(env, p->env[i], sizeof(env)-1);
-        EOS_PARANOIA(env);
+        ap_cpystrn(env, p->env[i], sizeof(env));
         /*  2. expand $N (i.e. backrefs to RewriteRule pattern)  */
         expand_backref_inbuffer(r->pool, env, sizeof(env), briRR, '$');
         /*  3. expand %N (i.e. backrefs to latest RewriteCond pattern)  */
@@ -1915,8 +1910,7 @@
     /* expand the regex backreferences from the RewriteRule ($0-$9), 
        then from the last RewriteCond (%0-%9) and then expand the 
        variables (%{....}) */
-    strncpy(input, p->input, sizeof(input)-1);
-    EOS_PARANOIA(input);
+    ap_cpystrn(input, p->input, sizeof(input));
     expand_backref_inbuffer(r->pool, input, sizeof(input), briRR, '$');
     expand_backref_inbuffer(r->pool, input, sizeof(input), briRC, '%');
     expand_variables_inbuffer(r, input, sizeof(input));
@@ -2117,19 +2111,17 @@
 
         /* cut the hostname and port out of the URI */
 #ifdef APACHE_SSL
-        strncpy(buf, r->filename+strlen(http_method(r))+3, sizeof(buf)-1);
+        ap_cpystrn(buf, r->filename+strlen(http_method(r))+3, sizeof(buf));
 #else
-        strncpy(buf, r->filename+7, sizeof(buf)-1);
+        ap_cpystrn(buf, r->filename+7, sizeof(buf));
 #endif
-        EOS_PARANOIA(buf);
         hostp = buf;
         for (cp = hostp; *cp != '\0' && *cp != '/' && *cp != ':'; cp++)
             ;
         if (*cp == ':') {
             /* set host */
             *cp++ = '\0';
-            strncpy(host, hostp, sizeof(host)-1);
-            EOS_PARANOIA(host);
+            ap_cpystrn(host, hostp, sizeof(host));
             /* set port */
             portp = cp;
             for (; *cp != '\0' && *cp != '/'; cp++)
@@ -2144,8 +2136,7 @@
         else if (*cp == '/') {
             /* set host */
             *cp = '\0';
-            strncpy(host, hostp, sizeof(host)-1);
-            EOS_PARANOIA(host);
+            ap_cpystrn(host, hostp, sizeof(host));
             *cp = '/';
             /* set port */
             port = DEFAULT_PORT;
@@ -2154,8 +2145,7 @@
         }
         else {
             /* set host */
-            strncpy(host, hostp, sizeof(host)-1);
-            EOS_PARANOIA(host);
+            ap_cpystrn(host, hostp, sizeof(host));
             /* set port */
             port = DEFAULT_PORT;
             /* set remaining url */
@@ -2248,9 +2238,8 @@
     }
 
     /* now apply the pregsub() function */
-    strncpy(buf, pregsub(p, buf, bri->source, 
-                         bri->nsub+1, bri->regmatch), nbuf-1);
-    EOS_PARANOIA_SIZE(buf, nbuf);
+    ap_cpystrn(buf, pregsub(p, buf, bri->source, 
+                         bri->nsub+1, bri->regmatch), nbuf);
 
     if (c != '$') {
         /* restore the original $N backrefs */
@@ -2411,8 +2400,7 @@
         }
     }
     *cpO = '\0';
-    strncpy(uri, newuri, uri_len-1);
-    uri[uri_len-1] = '\0';
+    ap_cpystrn(uri, newuri, uri_len);
     return;
 }
 
@@ -2553,19 +2541,17 @@
     if ((fp = pfopen(r->pool, file, "r")) == NULL)
         return NULL;
 
-    strncpy(output, MAPFILE_OUTPUT, sizeof(output)-1);
-    EOS_PARANOIA(output);
+    ap_cpystrn(output, MAPFILE_OUTPUT, sizeof(output));
     while (fgets(line, sizeof(line), fp) != NULL) {
         if (line[strlen(line)-1] == '\n')
             line[strlen(line)-1] = '\0';
         if (regexec(lookup_map_txtfile_regexp, line, 
                     lookup_map_txtfile_regexp->re_nsub+1, 
                     lookup_map_txtfile_regmatch, 0) == 0) {
-            strncpy(result, pregsub(r->pool, output, line, 
+            ap_cpystrn(result, pregsub(r->pool, output, line, 
                     lookup_map_txtfile_regexp->re_nsub+1, 
                     lookup_map_txtfile_regmatch), 
-                    sizeof(result)-1); /* substitute in output */
-            EOS_PARANOIA(result);
+                    sizeof(result)); /* substitute in output */
             cpT = strchr(result, ',');
             *cpT = '\0';
             curkey = result;
@@ -2912,8 +2898,7 @@
     char *newbuf;
     newbuf = expand_variables(r, buf);
     if (strcmp(newbuf, buf) != 0) {
-        strncpy(buf, newbuf, buf_len-1);
-        buf[buf_len-1] = '\0';
+        ap_cpystrn(buf, newbuf, buf_len);
     }
     return;
 }
@@ -2926,31 +2911,30 @@
     char *cp2;
     char *cp3;
     int expanded;
+    char *outp;
+    char *endp;
 
-    strncpy(input, str, sizeof(input)-1);
-    EOS_PARANOIA(input);
+    ap_cpystrn(input, str, sizeof(input));
     output[0] = '\0';
+    outp = output;
+    endp = output + sizeof(output);
     expanded = 0;
     for (cp = input; cp < input+MAX_STRING_LEN; ) {
         if ((cp2 = strstr(cp, "%{")) != NULL) {
             if ((cp3 = strstr(cp2, "}")) != NULL) {
                 *cp2 = '\0';
-                strncpy(&output[strlen(output)], cp, 
-                        sizeof(output)-strlen(output)-1);
+                outp = ap_cpystrn(outp, cp, endp - outp);
 
                 cp2 += 2;
                 *cp3 = '\0';
-                strncpy(&output[strlen(output)], lookup_variable(r, cp2),
-                        sizeof(output)-strlen(output)-1);
+                outp = ap_cpystrn(outp, lookup_variable(r, cp2), endp - outp);
 
                 cp = cp3+1;
                 expanded = 1;
                 continue;
             }
         }
-        strncpy(&output[strlen(output)], cp, 
-                sizeof(output)-strlen(output)-1);
-        EOS_PARANOIA(output);
+	outp = ap_cpystrn(outp, cp, endp - outp);
         break;
     }
     return expanded ? pstrdup(r->pool, output) : str;
@@ -3357,9 +3341,7 @@
     output = input;
 
     /* first create a match string which always has a trailing slash */
-    strncpy(matchbuf, match, sizeof(matchbuf)-1);
-    EOS_PARANOIA(matchbuf);
-    l = strlen(matchbuf);
+    l = ap_cpystrn(matchbuf, match, sizeof(matchbuf)) - matchbuf;
     if (matchbuf[l-1] != '/') {
        matchbuf[l] = '/';
        matchbuf[l+1] = '\0';
@@ -3371,9 +3353,7 @@
         output = pstrdup(r->pool, output+l); 
 
         /* and now add the base-URL as replacement prefix */
-        strncpy(substbuf, subst, sizeof(substbuf)-1);
-        EOS_PARANOIA(substbuf);
-        l = strlen(substbuf);
+        l = ap_cpystrn(substbuf, subst, sizeof(substbuf)) - substbuf;
         if (substbuf[l-1] != '/') {
            substbuf[l] = '/';
            substbuf[l+1] = '\0';
@@ -3482,8 +3462,7 @@
         n = ((cp-s) > MAX_STRING_LEN-1 ? MAX_STRING_LEN-1 : (cp-s));
         memcpy(var, s, n);
         var[n] = '\0';
-        strncpy(val, cp+1, sizeof(val)-1);
-        EOS_PARANOIA(val);
+        ap_cpystrn(val, cp+1, sizeof(val));
         table_set(r->subprocess_env, pstrdup(r->pool, var), 
                   pstrdup(r->pool, val));
         rewritelog(r, 5, "setting env variable '%s' to '%s'", var, val);
@@ -3503,8 +3482,7 @@
     char curpath[LONG_STRING_LEN];
     char *cp;
 
-    strncpy(curpath, path, sizeof(curpath)-1);
-    EOS_PARANOIA(curpath);
+    ap_cpystrn(curpath, path, sizeof(curpath));
     if (curpath[0] != '/') 
         return 0;
     if ((cp = strchr(curpath+1, '/')) != NULL)



Mime
View raw message