This is an automated email from the ASF dual-hosted git repository.
cmcfarlen 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 b55e5051eb Convert slice to use Regex (#12570)
b55e5051eb is described below
commit b55e5051eb2dfabbafd4eaa5c58d13b2c04adef8
Author: Chris McFarlen <[email protected]>
AuthorDate: Sat Oct 18 16:58:05 2025 -0400
Convert slice to use Regex (#12570)
* Convert slice to use Regex
* swap conditions
* add comments for regex execs
---
plugins/slice/Config.cc | 44 +++++++++++++++++++-------------------------
plugins/slice/Config.h | 17 ++++++-----------
2 files changed, 25 insertions(+), 36 deletions(-)
diff --git a/plugins/slice/Config.cc b/plugins/slice/Config.cc
index aa007cccb8..e60d894b7a 100644
--- a/plugins/slice/Config.cc
+++ b/plugins/slice/Config.cc
@@ -33,15 +33,8 @@ constexpr std::string_view DefaultCrrIdentHeader =
{"X-Crr-Ident"};
Config::~Config()
{
- if (nullptr != m_regex_extra) {
-#ifndef PCRE_STUDY_JIT_COMPILE
- pcre_free(m_regex_extra);
-#else
- pcre_free_study(m_regex_extra);
-#endif
- }
if (nullptr != m_regex) {
- pcre_free(m_regex);
+ delete m_regex;
}
}
@@ -159,16 +152,16 @@ Config::fromArgs(int const argc, char const *const argv[])
break;
}
- const char *errptr;
+ std::string err;
int erroffset;
m_regexstr = optarg;
- m_regex = pcre_compile(m_regexstr.c_str(), 0, &errptr, &erroffset,
nullptr);
- if (nullptr == m_regex) {
- ERROR_LOG("Invalid regex: '%s'", m_regexstr.c_str());
- } else {
- m_regex_type = Exclude;
- m_regex_extra = pcre_study(m_regex, 0, &errptr);
+ m_regex = new Regex();
+
+ if (m_regex->compile(m_regexstr, err, erroffset)) {
+ m_regex_type = Exclude;
DEBUG_LOG("Using regex for url exclude: '%s'", m_regexstr.c_str());
+ } else {
+ ERROR_LOG("Invalid regex: '%s' - %s at column %d", m_regexstr.c_str(),
err.c_str(), erroffset);
}
} break;
case 'g': {
@@ -180,17 +173,16 @@ Config::fromArgs(int const argc, char const *const argv[])
ERROR_LOG("Regex already specified!");
break;
}
-
- const char *errptr;
+ std::string err;
int erroffset;
m_regexstr = optarg;
- m_regex = pcre_compile(m_regexstr.c_str(), 0, &errptr, &erroffset,
nullptr);
- if (nullptr == m_regex) {
- ERROR_LOG("Invalid regex: '%s'", m_regexstr.c_str());
- } else {
- m_regex_type = Include;
- m_regex_extra = pcre_study(m_regex, 0, &errptr);
+ m_regex = new Regex();
+
+ if (m_regex->compile(m_regexstr, err, erroffset)) {
+ m_regex_type = Include;
DEBUG_LOG("Using regex for url include: '%s'", m_regexstr.c_str());
+ } else {
+ ERROR_LOG("Invalid regex: '%s' - %s at column %d", m_regexstr.c_str(),
err.c_str(), erroffset);
}
} break;
case 'l': {
@@ -330,12 +322,14 @@ Config::matchesRegex(char const *const url, int const
urllen) const
switch (m_regex_type) {
case Exclude: {
- if (0 <= pcre_exec(m_regex, m_regex_extra, url, urllen, 0, 0, nullptr, 0))
{
+ // Exclude means if the regex matches, it doesn't match
+ if (m_regex->exec({url, static_cast<size_t>(urllen)})) {
matches = false;
}
} break;
case Include: {
- if (pcre_exec(m_regex, m_regex_extra, url, urllen, 0, 0, nullptr, 0) < 0) {
+ // Include means if the regex matches, it matches
+ if (!m_regex->exec({url, static_cast<size_t>(urllen)})) {
matches = false;
}
} break;
diff --git a/plugins/slice/Config.h b/plugins/slice/Config.h
index 27a99ccb74..4d57ea10cc 100644
--- a/plugins/slice/Config.h
+++ b/plugins/slice/Config.h
@@ -18,15 +18,11 @@
#pragma once
+#include <tsutil/Regex.h>
+
#include "slice.h"
#include "ObjectSizeCache.h"
-#ifdef HAVE_PCRE_PCRE_H
-#include <pcre/pcre.h>
-#else
-#include <pcre.h>
-#endif
-
#include <string>
#include <mutex>
@@ -40,11 +36,10 @@ struct Config {
std::string m_remaphost; // remap host to use for loopback slice GET
std::string m_regexstr; // regex string for things to slice (default all)
enum RegexType { None, Include, Exclude };
- RegexType m_regex_type{None};
- pcre *m_regex{nullptr};
- pcre_extra *m_regex_extra{nullptr};
- int m_paceerrsecs{0}; // -1 disable logging, 0 no pacing, max 60s
- int m_prefetchcount{0}; // 0 disables prefetching
+ RegexType m_regex_type{None};
+ Regex *m_regex{nullptr};
+ int m_paceerrsecs{0}; // -1 disable logging, 0 no pacing, max 60s
+ int m_prefetchcount{0}; // 0 disables prefetching
enum RefType { First, Relative };
RefType m_reftype{First}; // reference slice is relative to request
bool m_head_strip_range{false}; // strip range header for head requests