Skip to content

Commit b55e505

Browse files
authored
Convert slice to use Regex (#12570)
* Convert slice to use Regex * swap conditions * add comments for regex execs
1 parent a15669b commit b55e505

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

plugins/slice/Config.cc

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,8 @@ constexpr std::string_view DefaultCrrIdentHeader = {"X-Crr-Ident"};
3333

3434
Config::~Config()
3535
{
36-
if (nullptr != m_regex_extra) {
37-
#ifndef PCRE_STUDY_JIT_COMPILE
38-
pcre_free(m_regex_extra);
39-
#else
40-
pcre_free_study(m_regex_extra);
41-
#endif
42-
}
4336
if (nullptr != m_regex) {
44-
pcre_free(m_regex);
37+
delete m_regex;
4538
}
4639
}
4740

@@ -159,16 +152,16 @@ Config::fromArgs(int const argc, char const *const argv[])
159152
break;
160153
}
161154

162-
const char *errptr;
155+
std::string err;
163156
int erroffset;
164157
m_regexstr = optarg;
165-
m_regex = pcre_compile(m_regexstr.c_str(), 0, &errptr, &erroffset, nullptr);
166-
if (nullptr == m_regex) {
167-
ERROR_LOG("Invalid regex: '%s'", m_regexstr.c_str());
168-
} else {
169-
m_regex_type = Exclude;
170-
m_regex_extra = pcre_study(m_regex, 0, &errptr);
158+
m_regex = new Regex();
159+
160+
if (m_regex->compile(m_regexstr, err, erroffset)) {
161+
m_regex_type = Exclude;
171162
DEBUG_LOG("Using regex for url exclude: '%s'", m_regexstr.c_str());
163+
} else {
164+
ERROR_LOG("Invalid regex: '%s' - %s at column %d", m_regexstr.c_str(), err.c_str(), erroffset);
172165
}
173166
} break;
174167
case 'g': {
@@ -180,17 +173,16 @@ Config::fromArgs(int const argc, char const *const argv[])
180173
ERROR_LOG("Regex already specified!");
181174
break;
182175
}
183-
184-
const char *errptr;
176+
std::string err;
185177
int erroffset;
186178
m_regexstr = optarg;
187-
m_regex = pcre_compile(m_regexstr.c_str(), 0, &errptr, &erroffset, nullptr);
188-
if (nullptr == m_regex) {
189-
ERROR_LOG("Invalid regex: '%s'", m_regexstr.c_str());
190-
} else {
191-
m_regex_type = Include;
192-
m_regex_extra = pcre_study(m_regex, 0, &errptr);
179+
m_regex = new Regex();
180+
181+
if (m_regex->compile(m_regexstr, err, erroffset)) {
182+
m_regex_type = Include;
193183
DEBUG_LOG("Using regex for url include: '%s'", m_regexstr.c_str());
184+
} else {
185+
ERROR_LOG("Invalid regex: '%s' - %s at column %d", m_regexstr.c_str(), err.c_str(), erroffset);
194186
}
195187
} break;
196188
case 'l': {
@@ -330,12 +322,14 @@ Config::matchesRegex(char const *const url, int const urllen) const
330322

331323
switch (m_regex_type) {
332324
case Exclude: {
333-
if (0 <= pcre_exec(m_regex, m_regex_extra, url, urllen, 0, 0, nullptr, 0)) {
325+
// Exclude means if the regex matches, it doesn't match
326+
if (m_regex->exec({url, static_cast<size_t>(urllen)})) {
334327
matches = false;
335328
}
336329
} break;
337330
case Include: {
338-
if (pcre_exec(m_regex, m_regex_extra, url, urllen, 0, 0, nullptr, 0) < 0) {
331+
// Include means if the regex matches, it matches
332+
if (!m_regex->exec({url, static_cast<size_t>(urllen)})) {
339333
matches = false;
340334
}
341335
} break;

plugins/slice/Config.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818

1919
#pragma once
2020

21+
#include <tsutil/Regex.h>
22+
2123
#include "slice.h"
2224
#include "ObjectSizeCache.h"
2325

24-
#ifdef HAVE_PCRE_PCRE_H
25-
#include <pcre/pcre.h>
26-
#else
27-
#include <pcre.h>
28-
#endif
29-
3026
#include <string>
3127
#include <mutex>
3228

@@ -40,11 +36,10 @@ struct Config {
4036
std::string m_remaphost; // remap host to use for loopback slice GET
4137
std::string m_regexstr; // regex string for things to slice (default all)
4238
enum RegexType { None, Include, Exclude };
43-
RegexType m_regex_type{None};
44-
pcre *m_regex{nullptr};
45-
pcre_extra *m_regex_extra{nullptr};
46-
int m_paceerrsecs{0}; // -1 disable logging, 0 no pacing, max 60s
47-
int m_prefetchcount{0}; // 0 disables prefetching
39+
RegexType m_regex_type{None};
40+
Regex *m_regex{nullptr};
41+
int m_paceerrsecs{0}; // -1 disable logging, 0 no pacing, max 60s
42+
int m_prefetchcount{0}; // 0 disables prefetching
4843
enum RefType { First, Relative };
4944
RefType m_reftype{First}; // reference slice is relative to request
5045
bool m_head_strip_range{false}; // strip range header for head requests

0 commit comments

Comments
 (0)