This is an automated email from the ASF dual-hosted git repository.

dmeden pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new c11869025f geoip - replace pcre with Regex (#12578)
c11869025f is described below

commit c11869025fe55cdd4b8aef5d616507e983435ac3
Author: Damian Meden <[email protected]>
AuthorDate: Tue Oct 21 21:31:26 2025 +0200

    geoip - replace pcre with Regex (#12578)
    
    geoip plugin - replace pcre with Regex
---
 plugins/experimental/geoip_acl/CMakeLists.txt |  1 -
 plugins/experimental/geoip_acl/acl.cc         | 26 ++++++++----------
 plugins/experimental/geoip_acl/acl.h          | 39 +++++++++++++++++++--------
 3 files changed, 39 insertions(+), 27 deletions(-)

diff --git a/plugins/experimental/geoip_acl/CMakeLists.txt 
b/plugins/experimental/geoip_acl/CMakeLists.txt
index 830e03ada2..b9bb30e677 100644
--- a/plugins/experimental/geoip_acl/CMakeLists.txt
+++ b/plugins/experimental/geoip_acl/CMakeLists.txt
@@ -16,5 +16,4 @@
 #######################
 
 add_atsplugin(geoip_acl geoip_acl.cc acl.cc)
-target_link_libraries(geoip_acl PRIVATE PCRE::PCRE)
 verify_remap_plugin(geoip_acl)
diff --git a/plugins/experimental/geoip_acl/acl.cc 
b/plugins/experimental/geoip_acl/acl.cc
index 56c533ad6b..ee775eb695 100644
--- a/plugins/experimental/geoip_acl/acl.cc
+++ b/plugins/experimental/geoip_acl/acl.cc
@@ -28,7 +28,6 @@ namespace geoip_acl_ns
 {
 DbgCtl dbg_ctl{PLUGIN_NAME};
 }
-
 // Implementation of the ACL base class. This wraps the underlying Geo library
 // that we've found and used.
 GeoDBHandle Acl::_geoip;
@@ -175,25 +174,22 @@ RegexAcl::parse_line(const char *filename, const 
std::string &line, int lineno,
 bool
 RegexAcl::compile(const std::string &str, const char *filename, int lineno)
 {
-  const char *error;
+  bool        success{true};
+  std::string error;
   int         erroffset;
 
   _regex_s = str;
-  _rex     = pcre_compile(_regex_s.c_str(), 0, &error, &erroffset, nullptr);
-
-  if (nullptr != _rex) {
-    _extra = pcre_study(_rex, 0, &error);
-    if ((nullptr == _extra) && error && (*error != 0)) {
-      TSError("[%s] Failed to study regular expression in %s:line %d at offset 
%d: %s", PLUGIN_NAME, filename, lineno, erroffset,
-              error);
-      return false;
-    }
-  } else {
-    TSError("[%s] Failed to compile regular expression in %s:line %d: %s", 
PLUGIN_NAME, filename, lineno, error);
-    return false;
+  _regex   = new Regex();
+
+  if (!_regex->compile(_regex_s, error, erroffset, 0)) {
+    TSError("[%s] Regex compilation failed in %s:line %d with error (%s) at 
character %d", PLUGIN_NAME, filename, lineno,
+            error.c_str(), erroffset);
+    delete _regex;
+    _regex  = nullptr;
+    success = false;
   }
 
-  return true;
+  return success;
 }
 
 void
diff --git a/plugins/experimental/geoip_acl/acl.h 
b/plugins/experimental/geoip_acl/acl.h
index 7c089024f7..c07cb170b3 100644
--- a/plugins/experimental/geoip_acl/acl.h
+++ b/plugins/experimental/geoip_acl/acl.h
@@ -24,16 +24,14 @@
 
 #include <ts/ts.h>
 #include <ts/remap.h>
-
+#include <ts/apidefs.h>
+#include "tsutil/DbgCtl.h"
 #include "tscore/ink_defs.h"
 
-#ifdef HAVE_PCRE_PCRE_H
-#include <pcre/pcre.h>
-#else
-#include <pcre.h>
-#endif
+#include "tsutil/Regex.h"
 
 #include <string>
+#include <string_view>
 #include "lulu.h"
 
 namespace geoip_acl_ns
@@ -102,7 +100,16 @@ protected:
 class RegexAcl
 {
 public:
-  RegexAcl(Acl *acl) : _rex(nullptr), _extra(nullptr), _next(nullptr), 
_acl(acl) {}
+  RegexAcl(Acl *acl) : _regex(nullptr), _next(nullptr), _acl(acl) {}
+  ~RegexAcl()
+  {
+    if (_regex) {
+      delete _regex;
+    }
+    if (_acl) {
+      delete _acl;
+    }
+  }
   const std::string &
   get_regex() const
   {
@@ -125,8 +132,10 @@ public:
     if (0 == len) {
       return false;
     }
-
-    return (pcre_exec(_rex, _extra, str, len, 0, PCRE_NOTEMPTY, nullptr, 0) != 
-1);
+    if (_regex) {
+      return _regex->exec(std::string_view(str, len));
+    }
+    return false;
   }
 
   void append(RegexAcl *ra);
@@ -135,8 +144,7 @@ public:
 private:
   bool        compile(const std::string &str, const char *filename, int 
lineno);
   std::string _regex_s;
-  pcre       *_rex;
-  pcre_extra *_extra;
+  Regex      *_regex{nullptr};
   RegexAcl   *_next;
   Acl        *_acl;
 };
@@ -146,6 +154,15 @@ class CountryAcl : public Acl
 {
 public:
   CountryAcl() { memset(_iso_country_codes, 0, sizeof(_iso_country_codes)); }
+  ~CountryAcl()
+  {
+    RegexAcl *cur = _regexes;
+    while (cur) {
+      RegexAcl *next = cur->next();
+      delete cur;
+      cur = next;
+    }
+  }
   void read_regex(const char *fn, int &tokens) override;
   int  process_args(int argc, char *argv[]) override;
   bool eval(TSRemapRequestInfo *rri, TSHttpTxn txnp) const override;

Reply via email to