]> granicus.if.org Git - apache/commitdiff
optimization:
authorAndré Malo <nd@apache.org>
Wed, 16 Jul 2003 17:27:26 +0000 (17:27 +0000)
committerAndré Malo <nd@apache.org>
Wed, 16 Jul 2003 17:27:26 +0000 (17:27 +0000)
- add comment about what subst_prefix_path function does
- reduce the use of fixed buffers
- get a rid of unnecessary memory operations

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

modules/mappers/mod_rewrite.c

index 9b69df6c9f34d207a902b085cfa5031a6c5059bc..68c71cb140efbf734d22d093fbd1ebaa5c9f362e 100644 (file)
@@ -4240,47 +4240,49 @@ static cacheentry *retrieve_cache_string(cache *c, const char *res, char *key)
 ** +-------------------------------------------------------+
 */
 
+/*
+ * substitute the prefix path 'match' in 'input' with 'subst'
+ * (think of RewriteBase which substitutes the physical path with
+ *  the virtual path)
+ */
+
 static char *subst_prefix_path(request_rec *r, char *input, char *match,
                                const char *subst)
 {
-    char matchbuf[LONG_STRING_LEN];
-    char substbuf[LONG_STRING_LEN];
-    char *output;
-    int l;
-
-    output = input;
+    apr_size_t len = strlen(match);
 
-    /* first create a match string which always has a trailing slash */
-    l = apr_cpystrn(matchbuf, match, sizeof(matchbuf) - 1) - matchbuf;
-    if (!l || matchbuf[l-1] != '/') {
-       matchbuf[l] = '/';
-       matchbuf[l+1] = '\0';
-       l++;
+    if (len && match[len - 1] == '/') {
+        --len;
     }
-    /* now compare the prefix */
-    if (strncmp(input, matchbuf, l) == 0) {
-        rewritelog(r, 5, "strip matching prefix: %s -> %s", output, output+l);
-        output = apr_pstrdup(r->pool, output+l);
 
-        /* and now add the base-URL as replacement prefix */
-        l = apr_cpystrn(substbuf, subst, sizeof(substbuf) - 1) - substbuf;
-        if (!l || substbuf[l-1] != '/') {
-           substbuf[l] = '/';
-           substbuf[l+1] = '\0';
-           l++;
-        }
-        if (output[0] == '/') {
-            rewritelog(r, 4, "add subst prefix: %s -> %s%s",
-                       output, substbuf, output+1);
-            output = apr_pstrcat(r->pool, substbuf, output+1, NULL);
+    if (!strncmp(input, match, len) && input[len++] == '/') {
+        apr_size_t slen, outlen;
+        char *output;
+
+        rewritelog(r, 5, "strip matching prefix: %s -> %s", input, input+len);
+
+        slen = strlen(subst);
+        if (slen && subst[slen - 1] != '/') {
+            ++slen;
         }
-        else {
-            rewritelog(r, 4, "add subst prefix: %s -> %s%s",
-                       output, substbuf, output);
-            output = apr_pstrcat(r->pool, substbuf, output, NULL);
+
+        outlen = strlen(input) + slen - len;
+        output = apr_palloc(r->pool, outlen + 1); /* don't forget the \0 */
+
+        memcpy(output, subst, slen);
+        if (slen && !output[slen-1]) {
+            output[slen-1] = '/';
         }
+        memcpy(output+slen, input+len, outlen - slen);
+        output[outlen] = '\0';
+
+        rewritelog(r, 4, "add subst prefix: %s -> %s", input+len, output);
+
+        return output;
     }
-    return output;
+
+    /* prefix didn't match */
+    return input;
 }