Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
add std::regex/posix-regex wrapper
Browse files Browse the repository at this point in the history
* only use posix-re on gcc-4.x, not clang
  • Loading branch information
weigon committed Jun 14, 2018
1 parent 83f309d commit 13d75a6
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/http/src/http_server_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

#include <atomic>
#include <chrono>
#include <regex>
#include <thread>

#include <sys/types.h>
Expand All @@ -46,6 +45,7 @@
#include "mysqlrouter/plugin_config.h"
#include "mysqlrouter/http_server_component.h"
#include "http_server_plugin.h"
#include "posix_re.h"

IMPORT_LOG_FUNCTIONS()

Expand All @@ -66,7 +66,7 @@ std::atomic<int> g_shutdown_pending { 0 };
* if no handler is found, reply with 404 not found
*/
void HttpRequestRouter::append(const std::string &url_regex_str, std::unique_ptr<BaseRequestHandler> cb) {
request_handlers_.emplace_back(RouterData { url_regex_str, std::regex {url_regex_str}, std::move(cb) });
request_handlers_.emplace_back(RouterData { url_regex_str, PosixRE {url_regex_str}, std::move(cb) });
}

void HttpRequestRouter::remove(const std::string &url_regex_str) {
Expand Down Expand Up @@ -101,7 +101,7 @@ void HttpRequestRouter::route(HttpRequest req) const {
auto uri = req.get_uri();

for (auto &request_handler: request_handlers_) {
if (std::regex_search(uri, request_handler.url_regex)) {
if (request_handler.url_regex.search(uri)) {
request_handler.handler->handle_request(req);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/http/src/http_server_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <string>
#include <vector>
#include <thread>
#include <regex>

#include <event2/buffer.h>
#include <event2/event.h>
Expand All @@ -37,6 +36,7 @@
#include <event2/util.h>

#include "mysqlrouter/http_server_component.h"
#include "posix_re.h"

using harness_socket_t = evutil_socket_t;

Expand All @@ -46,7 +46,7 @@ class HttpRequestRouter
{
struct RouterData {
std::string url_regex_str;
std::regex url_regex;
PosixRE url_regex;
std::unique_ptr<BaseRequestHandler> handler;
};
std::vector<RouterData> request_handlers_;
Expand Down
184 changes: 184 additions & 0 deletions src/http/src/posix_re.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef MYSQLROUTER_POSIX_RE_INCLUDED
#define MYSQLROUTER_POSIX_RE_INCLUDED

// Posix (Extended) Regular Expression
//
// C++11 has std::regex, by gcc-4.x throws exceptions when it
// it used. Instead we build on a subset of std::regex
//
//

#if ((defined(__GNUC__) && __GNUC__ < 5) && !defined(__clang__))
// detect broken gcc 4.x, but ignore clang which announces itself as GCC 4.2.1
#if !defined(_WIN32)
#define USE_POSIX_RE_IMPL
#else
#error "GCC 4.x or older, on Windows isn't supported"
#endif
#endif

#ifdef USE_POSIX_RE_IMPL
#include <regex.h>
#else
#include <regex>
#endif

#include <memory>

class PosixRE_constants {
public:
#ifdef USE_POSIX_RE_IMPL
using syntax_option_type = int;
using match_flag_type = int;
using error_type = int;
static constexpr auto match_default = 0;
static constexpr auto basic = 0;
static constexpr auto extended = REG_EXTENDED;
static constexpr auto icase = REG_ICASE;
static constexpr auto nosubs = REG_NOSUB;

static constexpr auto match_not_bol = REG_NOTBOL;
static constexpr auto match_not_eol = REG_NOTEOL;
#else
using syntax_option_type = std::regex_constants::syntax_option_type;
using error_type = std::regex_constants::error_type;
using match_flag_type = std::regex_constants::match_flag_type;
static constexpr auto match_default = std::regex_constants::match_default;
static constexpr auto basic = std::regex_constants::basic;
static constexpr auto extended = std::regex_constants::extended;
static constexpr auto icase = std::regex_constants::icase;
static constexpr auto nosubs = std::regex_constants::nosubs;

static constexpr auto match_not_bol = std::regex_constants::match_not_bol;
static constexpr auto match_not_eol = std::regex_constants::match_not_eol;
#endif
};

class PosixREError: public std::runtime_error {
PosixRE_constants::error_type code_;
public:
PosixREError(PosixRE_constants::error_type c, const char *w):
std::runtime_error{w},
code_{c}
{
}

PosixRE_constants::error_type code() const noexcept {
return code_;
}
};



class PosixRE {
#ifdef USE_POSIX_RE_IMPL
std::unique_ptr<regex_t> reg_;
#else
std::regex reg_;
#endif

public:
using flag_type = PosixRE_constants::syntax_option_type;
using match_flag_type = PosixRE_constants::match_flag_type;
static constexpr auto match_default = PosixRE_constants::match_default;

static constexpr auto basic = PosixRE_constants::basic;
static constexpr auto extended = PosixRE_constants::extended;
static constexpr auto icase = PosixRE_constants::icase;
static constexpr auto nosubs = PosixRE_constants::nosubs;

static constexpr auto match_not_eol = PosixRE_constants::match_not_eol;
static constexpr auto match_not_bol = PosixRE_constants::match_not_bol;

PosixRE(const std::string &regex_str, flag_type syntax_options = extended):
#ifdef USE_POSIX_RE_IMPL
reg_{new regex_t}
#else
reg_{regex_str, syntax_options}
#endif
{
#ifdef USE_POSIX_RE_IMPL
int error_code;
if (0 != (error_code = regcomp(reg_.get(), regex_str.c_str(), syntax_options))) {
char errbuf[256];
regerror(error_code, reg_.get(), errbuf, sizeof(errbuf));

throw PosixREError(error_code, errbuf);
}
#endif
}

PosixRE(const char *regex_str, flag_type syntax_options = extended):
#ifdef USE_POSIX_RE_IMPL
reg_{new regex_t}
#else
reg_{regex_str, syntax_options}
#endif
{
#ifdef USE_POSIX_RE_IMPL
int error_code;
if (0 != (error_code = regcomp(reg_.get(), regex_str, syntax_options))) {
char errbuf[256];
regerror(error_code, reg_.get(), errbuf, sizeof(errbuf));

throw PosixREError(error_code, errbuf);
}
#endif
}

// no copy, move only
PosixRE(const PosixRE &) = default;
PosixRE &operator=(const PosixRE&) = default;

PosixRE(PosixRE &&) = default;
PosixRE &operator=(PosixRE &&) = default;

~PosixRE() {
#ifdef USE_POSIX_RE_IMPL
auto r = reg_.get();
if (r) regfree(r);
#endif
}

/**
* search entire line for match.
*/
bool search(const std::string &line, match_flag_type match_flags = match_default) const {
#ifdef USE_POSIX_RE_IMPL
if (0 != regexec(reg_.get(), line.c_str(), 0, NULL, match_flags)) {
return false;
}

return true;
#else
return std::regex_search(line, reg_, match_flags);
#endif
}
};


#endif
5 changes: 5 additions & 0 deletions src/http/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ add_test_file(test_time.cc
LIB_DEPENDS http_client
INCLUDE_DIRS ${GTEST_INCLUDE_DIRS}
)

add_test_file(test_posix_re.cc
MODULE http
INCLUDE_DIRS ${GTEST_INCLUDE_DIRS}
)
57 changes: 57 additions & 0 deletions src/http/tests/test_posix_re.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "gmock/gmock.h"


#include "posix_re.h"

class PosixReTest : public ::testing::Test {
protected:
virtual void SetUp() {
}
};

TEST_F(PosixReTest, search) {
SCOPED_TRACE("// parse");

EXPECT_TRUE(PosixRE("bcd").search("abcde"));
EXPECT_FALSE(PosixRE("^bcd").search("abcde"));
EXPECT_FALSE(PosixRE("bcd$").search("abcde"));
EXPECT_TRUE(PosixRE("^bcd").search("bcde"));
}

TEST_F(PosixReTest, construct) {
SCOPED_TRACE("// parse");

// either a PosixREError or a std::regex_error
EXPECT_THROW(PosixRE("[a-z]["), std::runtime_error);
}


int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 13d75a6

Please sign in to comment.