]> granicus.if.org Git - apache/commitdiff
cleanup lookup_map_txtfile function.
authorAndré Malo <nd@apache.org>
Sun, 27 Jul 2003 13:49:15 +0000 (13:49 +0000)
committerAndré Malo <nd@apache.org>
Sun, 27 Jul 2003 13:49:15 +0000 (13:49 +0000)
- improve efficiency
- make it better readable

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

modules/mappers/mod_rewrite.c

index adec8b70f79468cdeb5da949b8eb1588465c3562..6ef3621eb91fb22d560c1a8d4c8aa8d56daf2d64 100644 (file)
 /* for rewrite lock file */
 #define REWRITELOCK_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
 
+/* max line length (incl.\n) in text rewrite maps */
+#define REWRITE_MAX_TXT_MAP_LINE 1024
+
 /*
  * +-------------------------------------------------------+
  * |                                                       |
@@ -1368,50 +1371,56 @@ static apr_status_t run_rewritemap_programs(server_rec *s, apr_pool_t *p)
 static char *lookup_map_txtfile(request_rec *r, const char *file, char *key)
 {
     apr_file_t *fp = NULL;
-    apr_status_t rc;
-    char line[1024];
-    char *value = NULL;
-    char *cpT;
-    apr_size_t skip;
-    char *curkey;
-    char *curval;
+    char line[REWRITE_MAX_TXT_MAP_LINE + 1]; /* +1 for \0 */
+    char *value, *keylast;
 
-    rc = apr_file_open(&fp, file, APR_READ, APR_OS_DEFAULT, r->pool);
-    if (rc != APR_SUCCESS) {
-       return NULL;
+    if (apr_file_open(&fp, file, APR_READ, APR_OS_DEFAULT,
+                      r->pool) != APR_SUCCESS) {
+        return NULL;
     }
 
+    keylast = key + strlen(key);
+    value = NULL;
     while (apr_file_gets(line, sizeof(line), fp) == APR_SUCCESS) {
-        if (line[0] == '#') {
-            continue; /* ignore comments */
-        }
-        cpT = line;
-        curkey = cpT;
-        skip = strcspn(cpT," \t\r\n");
-        if (skip == 0) {
-            continue; /* ignore lines that start with a space, tab, CR, or LF */
-        }
-        cpT += skip;
-        *cpT = '\0';
-        if (strcmp(curkey, key) != 0) {
-            continue; /* key does not match... */
-        }
-
-        /* found a matching key; now extract and return the value */
-        ++cpT;
-        skip = strspn(cpT, " \t\r\n");
-        cpT += skip;
-        curval = cpT;
-        skip = strcspn(cpT, " \t\r\n");
-        if (skip == 0) {
-            continue; /* no value... */
-        }
-        cpT += skip;
-        *cpT = '\0';
-        value = apr_pstrdup(r->pool, curval);
+        char *p, *c;
+
+        /* ignore comments and lines starting with whitespaces */
+        if (*line == '#' || apr_isspace(*line)) {
+            continue;
+        }
+
+        p = line;
+        c = key;
+        while (c < keylast && *p == *c && !apr_isspace(*p)) {
+            ++p;
+            ++c;
+        }
+
+        /* key doesn't match - ignore. */
+        if (c != keylast || !apr_isspace(*p)) {
+            continue;
+        }
+
+        /* jump to the value */
+        while (*p && apr_isspace(*p)) {
+            ++p;
+        }
+
+        /* no value? ignore */
+        if (!*p) {
+            continue;
+        }
+
+        /* extract the value and return. */
+        c = p;
+        while (*p && !apr_isspace(*p)) {
+            ++p;
+        }
+        value = apr_pstrmemdup(r->pool, c, p - c);
         break;
     }
     apr_file_close(fp);
+
     return value;
 }