Skip to content

Commit a15669b

Browse files
authored
Update header_rewrite to use Regex (#12573)
* Update header_rewrite to use Regex * PR review * cast to int * also move dbg for failed regex compile.
1 parent 11a0bb7 commit a15669b

File tree

7 files changed

+20
-56
lines changed

7 files changed

+20
-56
lines changed

plugins/header_rewrite/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ target_link_libraries(header_rewrite_parser PUBLIC libswoc::libswoc)
3838

3939
target_link_libraries(
4040
header_rewrite
41-
PRIVATE OpenSSL::Crypto PCRE::PCRE
41+
PRIVATE OpenSSL::Crypto
4242
PUBLIC libswoc::libswoc
4343
)
4444

plugins/header_rewrite/conditions.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,11 +1722,8 @@ ConditionLastCapture::set_qualifier(const std::string &q)
17221722
void
17231723
ConditionLastCapture::append_value(std::string &s, const Resources &res)
17241724
{
1725-
if (res.ovector_ptr && res.ovector_count > _ix) {
1726-
int start = res.ovector[_ix * 2];
1727-
int end = res.ovector[_ix * 2 + 1];
1728-
1729-
s.append(std::string_view(res.ovector_ptr).substr(start, (end - start)));
1725+
if (res.matches.size() > _ix) {
1726+
s.append(res.matches[_ix]);
17301727
Dbg(pi_dbg_ctl, "Evaluating LAST-CAPTURE(%d)", _ix);
17311728
}
17321729
}

plugins/header_rewrite/matcher.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "resources.h"
3636
#include "regex_helper.h"
3737
#include "lulu.h"
38+
#include "tsutil/Regex.h"
3839

3940
// Possible operators that we support (at least partially)
4041
enum MatcherOps {
@@ -149,7 +150,6 @@ template <class T> class Matchers : public Matcher
149150
auto &re = std::get<regexHelper>(_data);
150151

151152
if (!re.setRegexMatch(s, has_modifier(mods, CondModifiers::MOD_NOCASE))) {
152-
TSError("[%s] Invalid regex: failed to precompile: %s", PLUGIN_NAME, s.c_str());
153153
Dbg(pi_dbg_ctl, "Invalid regex: failed to precompile: %s", s.c_str());
154154
throw std::runtime_error("Malformed regex");
155155
}
@@ -364,13 +364,10 @@ template <class T> class Matchers : public Matcher
364364
Dbg(pi_dbg_ctl, "Test regular expression against: %s (NOCASE = %s)", t.c_str(),
365365
has_modifier(_mods, CondModifiers::MOD_NOCASE) ? "true" : "false");
366366
const auto &re = std::get<regexHelper>(_data);
367-
int count = re.regexMatch(t.c_str(), t.length(), const_cast<Resources &>(res).ovector);
367+
int count = re.regexMatch(t, const_cast<Resources &>(res).matches);
368368

369369
if (count > 0) {
370370
Dbg(pi_dbg_ctl, "Successfully found regular expression match");
371-
const_cast<Resources &>(res).ovector_ptr = t.c_str();
372-
const_cast<Resources &>(res).ovector_count = count;
373-
374371
return true;
375372
}
376373

plugins/header_rewrite/regex_helper.cc

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,27 @@
1717
*/
1818
#include "regex_helper.h"
1919
#include "lulu.h"
20+
#include "ts/ts.h"
21+
#include "tsutil/Regex.h"
2022

2123
bool
2224
regexHelper::setRegexMatch(const std::string &s, bool nocase)
2325
{
24-
const char *errorComp = nullptr;
25-
const char *errorStudy = nullptr;
26-
int erroffset;
26+
std::string error;
27+
int errorOffset;
2728

2829
regexString = s;
29-
regex = pcre_compile(regexString.c_str(), nocase ? PCRE_CASELESS : 0, &errorComp, &erroffset, nullptr);
3030

31-
if (regex == nullptr) {
32-
return false;
33-
}
34-
regexExtra = pcre_study(regex, 0, &errorStudy);
35-
if ((regexExtra == nullptr) && (errorStudy != nullptr)) {
36-
return false;
37-
}
38-
if (pcre_fullinfo(regex, regexExtra, PCRE_INFO_CAPTURECOUNT, &regexCcount) != 0) {
31+
if (!regex.compile(regexString, error, errorOffset, nocase ? static_cast<int>(RE_CASE_INSENSITIVE) : 0)) {
32+
TSError("[%s] Invalid regex: failed to precompile: %s (%s at %d)", PLUGIN_NAME, s.c_str(), error.c_str(), errorOffset);
33+
Dbg(pi_dbg_ctl, "Invalid regex: failed to precompile: %s (%s at %d)", s.c_str(), error.c_str(), errorOffset);
3934
return false;
4035
}
4136
return true;
4237
}
4338

4439
int
45-
regexHelper::regexMatch(const char *str, int len, int ovector[]) const
40+
regexHelper::regexMatch(std::string_view subject, RegexMatches &matches) const
4641
{
47-
return pcre_exec(regex, // the compiled pattern
48-
regexExtra, // Extra data from study (maybe)
49-
str, // the subject std::string
50-
len, // the length of the subject
51-
0, // start at offset 0 in the subject
52-
0, // default options
53-
ovector, // output vector for substring information
54-
OVECCOUNT); // number of elements in the output vector
42+
return regex.exec(subject, matches);
5543
};

plugins/header_rewrite/regex_helper.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,17 @@
1818
#pragma once
1919

2020
#include "tscore/ink_defs.h"
21-
22-
#ifdef HAVE_PCRE_PCRE_H
23-
#include <pcre/pcre.h>
24-
#else
25-
#include <pcre.h>
26-
#endif
21+
#include "tsutil/Regex.h"
2722

2823
#include <string>
2924

3025
class regexHelper
3126
{
3227
public:
33-
~regexHelper()
34-
{
35-
pcre_free(regex);
36-
pcre_free(regexExtra);
37-
}
38-
3928
bool setRegexMatch(const std::string &s, bool nocase = false);
40-
int regexMatch(const char *, int, int ovector[]) const;
29+
int regexMatch(std::string_view subject, RegexMatches &matches) const;
4130

4231
private:
4332
std::string regexString;
44-
pcre *regex = nullptr;
45-
pcre_extra *regexExtra = nullptr;
46-
int regexCcount = 0;
33+
Regex regex;
4734
};

plugins/header_rewrite/resources.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ Resources::gather(const ResourceIDs ids, TSHttpHookID hook)
3434
{
3535
Dbg(pi_dbg_ctl, "Building resources, hook=%s", TSHttpHookNameLookup(hook));
3636

37-
// Clear the capture groups just in case
38-
ovector_count = 0;
39-
ovector_ptr = nullptr;
40-
4137
Dbg(pi_dbg_ctl, "Gathering resources for hook %s with IDs %d", TSHttpHookNameLookup(hook), ids);
4238

4339
// If we need the client request headers, make sure it's also available in the client vars.

plugins/header_rewrite/resources.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "ts/remap.h"
2828

2929
#include "lulu.h"
30+
#include "tsutil/Regex.h"
3031

3132
#if TS_HAS_CRIPTS
3233
#include "cripts/Certs.hpp"
@@ -93,11 +94,9 @@ class Resources
9394
#else
9495
TransactionState state; // Without cripts, txnp / ssnp goes here
9596
#endif
96-
const char *ovector_ptr = nullptr;
9797
TSHttpStatus resp_status = TS_HTTP_STATUS_NONE;
98-
int ovector[OVECCOUNT];
99-
int ovector_count = 0;
100-
bool changed_url = false;
98+
RegexMatches matches;
99+
bool changed_url = false;
101100

102101
private:
103102
void

0 commit comments

Comments
 (0)