Skip to content

Commit 447a2a0

Browse files
authored
Changed ControlMatcher to use PCRE2 (#12376)
1 parent 95ac751 commit 447a2a0

File tree

3 files changed

+26
-65
lines changed

3 files changed

+26
-65
lines changed

include/proxy/ControlMatcher.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,8 @@
9797
#include "tsutil/Regex.h"
9898
#include "proxy/hdrs/URL.h"
9999

100-
#if __has_include("pcre/pcre.h")
101-
#include <pcre/pcre.h>
102-
#elif __has_include("pcre.h")
103-
#include <pcre.h>
104-
#else
105-
#error "Unable to locate PCRE heeader"
106-
#endif
100+
#define PCRE2_CODE_UNIT_WIDTH 8
101+
#include "pcre2.h"
107102

108103
#include <swoc/swoc_ip.h>
109104

@@ -159,7 +154,7 @@ class HttpRequestData : public RequestData
159154
};
160155

161156
// Mixin class for shared info across all templates. This just wraps the
162-
// shared members such that we don't have to duplicate all these initialixers
157+
// shared members such that we don't have to duplicate all these initializers
163158
// etc. If someone wants to rewrite all this code to use setters and getters,
164159
// by all means, please do so. The plumbing is in place :).
165160
template <class Data> class BaseMatcher
@@ -209,7 +204,6 @@ template <class Data, class MatchResult> class RegexMatcher : protected BaseMatc
209204

210205
public:
211206
RegexMatcher(const char *name, const char *filename);
212-
~RegexMatcher();
213207

214208
void AllocateSpace(int num_entries);
215209
Result NewEntry(matcher_line *line_info);
@@ -224,8 +218,8 @@ template <class Data, class MatchResult> class RegexMatcher : protected BaseMatc
224218
using super::num_el;
225219

226220
protected:
227-
pcre **re_array = nullptr; // array of compiled regexs
228-
char **re_str = nullptr; // array of uncompiled regex strings
221+
std::vector<Regex> regex_array; // array of regexs
222+
std::vector<std::string> regex_strings; // array of regex strings
229223
};
230224

231225
template <class Data, class MatchResult> class HostRegexMatcher : public RegexMatcher<Data, MatchResult>

src/proxy/ControlMatcher.cc

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,8 @@ HostMatcher<Data, MatchResult>::Match(RequestData *rdata, MatchResult *result) c
152152
Data *data_ptr;
153153
bool r;
154154

155-
// Check to see if there is any work to do before making
156-
// the string copy
157-
if (num_el <= 0) {
155+
// Check to see if there is any work to do before making the string copy
156+
if (this->num_el <= 0) {
158157
return;
159158
}
160159

@@ -335,8 +334,7 @@ UrlMatcher<Data, MatchResult>::Match(RequestData *rdata, MatchResult *result) co
335334
{
336335
char *url_str;
337336

338-
// Check to see there is any work to before we copy the
339-
// URL
337+
// Check to see there is any work to before we copy the URL
340338
if (num_el <= 0) {
341339
return;
342340
}
@@ -365,19 +363,6 @@ RegexMatcher<Data, MatchResult>::RegexMatcher(const char *name, const char *file
365363
{
366364
}
367365

368-
//
369-
// RegexMatcher<Data,MatchResult>::~RegexMatcher()
370-
//
371-
template <class Data, class MatchResult> RegexMatcher<Data, MatchResult>::~RegexMatcher()
372-
{
373-
for (int i = 0; i < num_el; i++) {
374-
pcre_free(re_array[i]);
375-
ats_free(re_str[i]);
376-
}
377-
delete[] re_str;
378-
ats_free(re_array);
379-
}
380-
381366
//
382367
// void RegexMatcher<Data,MatchResult>::Print() const
383368
//
@@ -389,7 +374,7 @@ RegexMatcher<Data, MatchResult>::Print() const
389374
{
390375
printf("\tRegex Matcher with %d elements\n", num_el);
391376
for (int i = 0; i < num_el; i++) {
392-
printf("\t\tRegex: %s\n", re_str[i]);
377+
printf("\t\tRegex: %s\n", regex_strings[i].c_str());
393378
data_array[i].Print();
394379
}
395380
}
@@ -404,14 +389,11 @@ RegexMatcher<Data, MatchResult>::AllocateSpace(int num_entries)
404389
// Should not have been allocated before
405390
ink_assert(array_len == -1);
406391

407-
re_array = static_cast<pcre **>(ats_malloc(sizeof(pcre *) * num_entries));
408-
memset(re_array, 0, sizeof(pcre *) * num_entries);
392+
regex_array.resize(num_entries);
393+
regex_strings.resize(num_entries);
409394

410395
data_array = new Data[num_entries];
411396

412-
re_str = new char *[num_entries];
413-
memset(re_str, 0, sizeof(char *) * num_entries);
414-
415397
array_len = num_entries;
416398
num_el = 0;
417399
}
@@ -425,7 +407,7 @@ RegexMatcher<Data, MatchResult>::NewEntry(matcher_line *line_info)
425407
{
426408
Data *cur_d;
427409
char *pattern;
428-
const char *errptr;
410+
std::string error_msg;
429411
int erroffset;
430412
Result error = Result::ok();
431413

@@ -442,12 +424,12 @@ RegexMatcher<Data, MatchResult>::NewEntry(matcher_line *line_info)
442424
ink_assert(pattern != nullptr);
443425

444426
// Create the compiled regular expression
445-
re_array[num_el] = pcre_compile(pattern, 0, &errptr, &erroffset, nullptr);
446-
if (!re_array[num_el]) {
427+
regex_array[num_el].compile(pattern, error_msg, erroffset);
428+
if (regex_array[num_el].empty()) {
447429
return Result::failure("%s regular expression error at line %d position %d : %s", matcher_name, line_info->line_num, erroffset,
448-
errptr);
430+
error_msg.c_str());
449431
}
450-
re_str[num_el] = ats_strdup(pattern);
432+
regex_strings[num_el] = pattern;
451433

452434
// Remove our consumed label from the parsed line
453435
line_info->line[0][line_info->dest_entry] = nullptr;
@@ -459,10 +441,7 @@ RegexMatcher<Data, MatchResult>::NewEntry(matcher_line *line_info)
459441

460442
if (error.failed()) {
461443
// There was a problem so undo the effects this function
462-
ats_free(re_str[num_el]);
463-
re_str[num_el] = nullptr;
464-
pcre_free(re_array[num_el]);
465-
re_array[num_el] = nullptr;
444+
regex_strings[num_el] = ""; // reset the string
466445
} else {
467446
num_el++;
468447
}
@@ -473,23 +452,19 @@ RegexMatcher<Data, MatchResult>::NewEntry(matcher_line *line_info)
473452
//
474453
// void RegexMatcher<Data,MatchResult>::Match(RequestData* rdata, MatchResult* result)
475454
//
476-
// Coduncts a linear search through the regex array and
455+
// Conducts a linear search through the regex array and
477456
// updates arg result for each regex that matches arg URL
478457
//
479458
template <class Data, class MatchResult>
480459
void
481460
RegexMatcher<Data, MatchResult>::Match(RequestData *rdata, MatchResult *result) const
482461
{
483-
char *url_str;
484-
int r;
485-
486-
// Check to see there is any work to before we copy the
487-
// URL
462+
// Check to see there is any work to before we copy the URL
488463
if (num_el <= 0) {
489464
return;
490465
}
491466

492-
url_str = rdata->get_string();
467+
char *url_str = rdata->get_string();
493468

494469
// Can't do a regex match with a NULL string so
495470
// use an empty one instead
@@ -500,15 +475,13 @@ RegexMatcher<Data, MatchResult>::Match(RequestData *rdata, MatchResult *result)
500475
// INKqa12980
501476
// The function unescapifyStr() is already called in
502477
// HttpRequestData::get_string(); therefore, no need to call again here.
503-
504478
for (int i = 0; i < num_el; i++) {
505-
r = pcre_exec(re_array[i], nullptr, url_str, strlen(url_str), 0, 0, nullptr, 0);
506-
if (r > -1) {
479+
if (regex_array[i].exec(url_str) == true) {
507480
Dbg(dbg_ctl_matcher, "%s Matched %s with regex at line %d", matcher_name, url_str, data_array[i].line_num);
508481
data_array[i].UpdateMatch(result, rdata);
509-
} else if (r < -1) {
482+
} else {
510483
// An error has occurred
511-
Warning("Error [%d] matching regex at line %d.", r, data_array[i].line_num);
484+
Warning("Error matching regex at line %d.", data_array[i].line_num);
512485
} // else it's -1 which means no match was found.
513486
}
514487
ats_free(url_str);
@@ -534,10 +507,8 @@ void
534507
HostRegexMatcher<Data, MatchResult>::Match(RequestData *rdata, MatchResult *result) const
535508
{
536509
const char *url_str;
537-
int r;
538510

539-
// Check to see there is any work to before we copy the
540-
// URL
511+
// Check to see there is any work to before we copy the URL
541512
if (this->num_el <= 0) {
542513
return;
543514
}
@@ -549,9 +520,8 @@ HostRegexMatcher<Data, MatchResult>::Match(RequestData *rdata, MatchResult *resu
549520
if (url_str == nullptr) {
550521
url_str = "";
551522
}
552-
for (int i = 0; i < this->num_el; i++) {
553-
r = pcre_exec(this->re_array[i], nullptr, url_str, strlen(url_str), 0, 0, nullptr, 0);
554-
if (r != -1) {
523+
for (int i = 0; i < num_el; i++) {
524+
if (this->regex_array[i].exec(url_str) == true) {
555525
Dbg(dbg_ctl_matcher, "%s Matched %s with regex at line %d", const_cast<char *>(this->matcher_name), url_str,
556526
this->data_array[i].line_num);
557527
this->data_array[i].UpdateMatch(result, rdata);

src/traffic_server/traffic_server.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,9 +1786,6 @@ main(int /* argc ATS_UNUSED */, const char **argv)
17861786
// Override default swoc::Errata settings.
17871787
Initialize_Errata_Settings();
17881788

1789-
pcre_malloc = ats_malloc;
1790-
pcre_free = ats_free;
1791-
17921789
// Define the version info
17931790
auto &version = AppVersionInfo::setup_version("traffic_server");
17941791

0 commit comments

Comments
 (0)