commit ff6527c6b71fd1d33c39b8dca2a89170de5c2263
Author: Miro Stauder <miro@proxysql.com>
Date:   Mon Jan 9 12:05:55 2023 +0000

    re2_regex.patch

diff --git a/examples/Makefile.am b/examples/Makefile.am
index 318a7a8..0da1879 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -16,8 +16,12 @@
 #     License along with this library; if not, write to the Free Software
 #     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-LDADD = $(top_builddir)/src/libhttpserver.la
-AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
+DEPS_PATH=$(top_srcdir)/../../
+RE2_PATH=$(DEPS_PATH)/re2/re2
+RE2_IDIR=$(RE2_PATH)
+
+LDADD = $(top_builddir)/src/libhttpserver.la -L$(RE2_PATH)/obj -lre2
+AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/ -I$(RE2_IDIR)
 METASOURCES = AUTO
 noinst_PROGRAMS = hello_world service minimal_hello_world custom_error allowing_disallowing_methods handlers hello_with_get_arg setting_headers custom_access_log basic_authentication digest_authentication minimal_https minimal_file_response minimal_deferred url_registration minimal_ip_ban benchmark_select benchmark_threads benchmark_nodelay deferred_with_accumulator
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 5e549bb..1fcc59e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,7 +16,11 @@
 #     License along with this library; if not, write to the Free Software
 #     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-AM_CPPFLAGS = -I../ -I$(srcdir)/httpserver/
+DEPS_PATH=$(top_srcdir)/../../
+RE2_PATH=$(DEPS_PATH)/re2/re2
+RE2_IDIR=$(RE2_PATH)
+
+AM_CPPFLAGS = -I../ -I$(srcdir)/httpserver/ -I$(RE2_IDIR)
 METASOURCES = AUTO
 lib_LTLIBRARIES = libhttpserver.la
 libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp http_request.cpp http_response.cpp string_response.cpp basic_auth_fail_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp details/http_endpoint.cpp
@@ -37,7 +41,7 @@ endif
 
 libhttpserver_la_CFLAGS = $(AM_CFLAGS)
 libhttpserver_la_CXXFLAGS = $(AM_CXXFLAGS)
-libhttpserver_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined
+libhttpserver_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined -L$(RE2_PATH)/obj -lre2
 
 install-data-hook:
 	(mkdir -p $(DESTDIR)$(includedir) && cd $(DESTDIR)$(includedir) && $(LN_S) -f httpserver.hpp httpserverpp)
diff --git a/src/details/http_endpoint.cpp b/src/details/http_endpoint.cpp
index 584fb50..01292d5 100644
--- a/src/details/http_endpoint.cpp
+++ b/src/details/http_endpoint.cpp
@@ -52,6 +52,7 @@ http_endpoint::http_endpoint
     bool registration,
     bool use_regex
 ):
+    re_url_normalized(new re2::RE2("")),
     family_url(family),
     reg_compiled(false)
 {
@@ -130,9 +131,19 @@ http_endpoint::http_endpoint
         url_normalized += "$";
         try
         {
-            re_url_normalized = std::regex(url_normalized, std::regex::extended | std::regex::icase | std::regex::nosubs);
+            re2::RE2::Options opts {};
+            opts.set_case_sensitive(true);
+            opts.set_never_capture(true);
+
+            re_url_normalized.reset(
+                new re2::RE2(url_normalized, opts)
+            );
+
+            if (re_url_normalized->ok() != true) {
+                throw std::invalid_argument("Invalid regex supplied to re2");
+            }
         }
-        catch (std::regex_error& e)
+        catch (std::invalid_argument& e)
         {
             throw std::invalid_argument("Not a valid regex in URL: " + url_normalized);
         }
@@ -146,7 +157,7 @@ http_endpoint::http_endpoint(const http_endpoint& h):
     url_pars(h.url_pars),
     url_pieces(h.url_pieces),
     chunk_positions(h.chunk_positions),
-    re_url_normalized(h.re_url_normalized),
+    re_url_normalized(new re2::RE2(h.re_url_normalized->pattern())),
     family_url(h.family_url),
     reg_compiled(h.reg_compiled)
 {
@@ -158,7 +169,7 @@ http_endpoint& http_endpoint::operator =(const http_endpoint& h)
     url_normalized = h.url_normalized;
     family_url = h.family_url;
     reg_compiled = h.reg_compiled;
-    re_url_normalized = h.re_url_normalized;
+    re_url_normalized.reset(new re2::RE2(h.re_url_normalized->pattern()));
     url_pars = h.url_pars;
     url_pieces = h.url_pieces;
     chunk_positions = h.chunk_positions;
@@ -176,7 +187,7 @@ bool http_endpoint::match(const http_endpoint& url) const
 
     if(!family_url || url.url_pieces.size() < url_pieces.size())
     {
-        return regex_match(url.url_complete, re_url_normalized);
+        return RE2::FullMatch(url.url_complete, *re_url_normalized);
     }
 
     string nn = "/";
@@ -186,7 +197,7 @@ bool http_endpoint::match(const http_endpoint& url) const
         nn += (first ? "" : "/") + url.url_pieces[i];
         first = false;
     }
-    return regex_match(nn, re_url_normalized);
+    return RE2::FullMatch(nn, *re_url_normalized);
 }
 
 };
diff --git a/src/httpserver/details/http_endpoint.hpp b/src/httpserver/details/http_endpoint.hpp
index 37fd0d8..ae92782 100644
--- a/src/httpserver/details/http_endpoint.hpp
+++ b/src/httpserver/details/http_endpoint.hpp
@@ -25,11 +25,12 @@
 #ifndef _HTTP_ENDPOINT_HPP_
 #define _HTTP_ENDPOINT_HPP_
 
-#include <regex>
+#include "re2/re2.h"
 #include <stdexcept>
 #include <string>
 #include <utility>
 #include <vector>
+#include <memory>
 
 namespace httpserver
 {
@@ -136,7 +137,7 @@ class http_endpoint
         http_endpoint():
             url_complete("/"),
             url_normalized("/"),
-            re_url_normalized(std::regex("")), // initialize empty
+            re_url_normalized(new re2::RE2("")), // initialize empty
             family_url(false),
             reg_compiled(false)
         {
@@ -187,7 +188,7 @@ class http_endpoint
         /**
          * Regex used in comparisons
         **/
-        std::regex re_url_normalized;
+        std::unique_ptr<re2::RE2> re_url_normalized;
 
         /**
          * Boolean indicating wheter the endpoint represents a family
