@@ -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 (" \t Regex Matcher with %d elements\n " , num_el);
391376 for (int i = 0 ; i < num_el; i++) {
392- printf (" \t\t Regex: %s\n " , re_str [i]);
377+ printf (" \t\t Regex: %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//
479458template <class Data , class MatchResult >
480459void
481460RegexMatcher<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
534507HostRegexMatcher<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);
0 commit comments