You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by so...@apache.org on 2015/06/02 00:15:46 UTC

trafficserver git commit: TS-3653: Add the ability to exclude by regex to url_sig plugin

Repository: trafficserver
Updated Branches:
  refs/heads/master 7cae89187 -> 9cafcee69


TS-3653: Add the ability to exclude by regex to url_sig plugin


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/9cafcee6
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/9cafcee6
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/9cafcee6

Branch: refs/heads/master
Commit: 9cafcee69387692b803ca5be292288ae0dc04639
Parents: 7cae891
Author: Phil Sorber <so...@apache.org>
Authored: Mon Jun 1 16:14:28 2015 -0600
Committer: Phil Sorber <so...@apache.org>
Committed: Mon Jun 1 16:15:29 2015 -0600

----------------------------------------------------------------------
 CHANGES                                |  2 +
 plugins/experimental/url_sig/url_sig.c | 67 +++++++++++++++++++++++++----
 plugins/experimental/url_sig/url_sig.h |  1 -
 3 files changed, 61 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/9cafcee6/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 27a2313..95fa52c 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,8 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache Traffic Server 6.0.0
 
+  *) [TS-3653] Add the ability to exclude by regex to url_sig plugin.
+
   *) [TS-3649] url_sig: fix for crasher related to key index.
    Author: Gancho Tenev <gt...@gmail.com>
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/9cafcee6/plugins/experimental/url_sig/url_sig.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/url_sig/url_sig.c b/plugins/experimental/url_sig/url_sig.c
index 43fdef0..477cbc6 100644
--- a/plugins/experimental/url_sig/url_sig.c
+++ b/plugins/experimental/url_sig/url_sig.c
@@ -31,6 +31,12 @@
 #include <limits.h>
 #include <ctype.h>
 
+#ifdef HAVE_PCRE_PCRE_H
+#include <pcre/pcre.h>
+#else
+#include <pcre.h>
+#endif
+
 #include <ts/ts.h>
 #include <ts/remap.h>
 
@@ -40,13 +46,26 @@ struct config {
   TSHttpStatus err_status;
   char *err_url;
   char keys[MAX_KEY_NUM][MAX_KEY_LEN];
+  pcre *regex;
+  pcre_extra *regex_extra;
 };
 
-void
+static void
 free_cfg(struct config *cfg)
 {
   TSError("Cleaning up...");
   TSfree(cfg->err_url);
+
+  if (cfg->regex_extra)
+#ifndef PCRE_STUDY_JIT_COMPILE
+    pcre_free(cfg->regex_extra);
+#else
+    pcre_free_study(cfg->regex_extra);
+#endif
+
+  if (cfg->regex)
+    pcre_free(cfg->regex);
+
   TSfree(cfg);
 }
 
@@ -149,16 +168,30 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int errbuf_s
       value += 3;
       while (isspace(*value))
         value++;
-      //                      if (strncmp(value, "http://", strlen("http://")) != 0) {
-      //                              snprintf(errbuf, errbuf_size - 1,
-      //                                              "[TSRemapNewInstance] - Invalid config, err_status == 302, but err_url does
-      //                                              not start with \"http://\"");
-      //                              return TS_ERROR;
-      //                      }
       if (cfg->err_status == TS_HTTP_STATUS_MOVED_TEMPORARILY)
         cfg->err_url = TSstrndup(value, strlen(value));
       else
         cfg->err_url = NULL;
+    } else if (strncmp(line, "excl_regex", 10) == 0) {
+      // compile and study regex
+      const char *errptr;
+      int erroffset, options = 0;
+
+      if (cfg->regex) {
+        TSDebug(PLUGIN_NAME, "Skipping duplicate excl_regex");
+        continue;
+      }
+
+      cfg->regex = pcre_compile(value, options, &errptr, &erroffset, NULL);
+      if (cfg->regex == NULL) {
+        TSDebug(PLUGIN_NAME, "Regex compilation failed with error (%s) at character %d.", errptr, erroffset);
+      } else {
+#ifdef PCRE_STUDY_JIT_COMPILE
+        options = PCRE_STUDY_JIT_COMPILE;
+#endif
+        cfg->regex_extra = pcre_study(
+          cfg->regex, options, &errptr); // We do not need to check the error here because we can still run without the studying?
+      }
     } else {
       TSError("Error parsing line %d of file %s (%s).", line_no, config_file, line);
     }
@@ -200,7 +233,7 @@ TSRemapDeleteInstance(void *ih)
   free_cfg((struct config *)ih);
 }
 
-void
+static void
 err_log(char *url, char *msg)
 {
   if (msg && url) {
@@ -257,6 +290,24 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
   TSDebug(PLUGIN_NAME, "%s", url);
 
   query = strstr(url, "?");
+
+  if (cfg->regex) {
+    int offset = 0, options = 0;
+    int ovector[30];
+    int len = url_len;
+    char *anchor = strstr(url, "#");
+    if (query && !anchor) {
+      len -= (query - url);
+    } else if (anchor && !query) {
+      len -= (anchor - url);
+    } else if (anchor && query) {
+      len -= ((query < anchor ? query : anchor) - url);
+    }
+    if (pcre_exec(cfg->regex, cfg->regex_extra, url, len, offset, options, ovector, 30) >= 0) {
+      goto allow;
+    }
+  }
+
   if (query == NULL) {
     err_log(url, "Has no query string.");
     goto deny;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/9cafcee6/plugins/experimental/url_sig/url_sig.h
----------------------------------------------------------------------
diff --git a/plugins/experimental/url_sig/url_sig.h b/plugins/experimental/url_sig/url_sig.h
index 45fe462..d2f4cd0 100644
--- a/plugins/experimental/url_sig/url_sig.h
+++ b/plugins/experimental/url_sig/url_sig.h
@@ -50,5 +50,4 @@
 #define USIG_HMAC_SHA1 1
 #define USIG_HMAC_MD5 2
 
-
 #endif /* URL_SIG_H_ */