Skip to content

Commit 7cce8e3

Browse files
authored
fix: use same regex code at ZTSClient (#11323)
### Motivation #9533 causes some degraded behaviors at [ZTSClient](https://github.com/apache/pulsar/pull/9533/files#diff-41a90a5ec7937b5e2151752843621d6acfe8963077dc04c285842a09a4e45ecc). 1. when using boost::regex - can't set `path` at file scheme 2. when using std::regex - can't set relative path like `file:./path/to/private.key` I'd like to fix this issue. ### Modifications * Use same regex code between boost and std like [this](https://github.com/apache/pulsar/pull/9533/files#diff-a67e917f5f42f3337507a69a4e0f13e286238dc759b4048cb5141290cf445b38) * Fix regex to be able to capture relative path like `file:./`
1 parent 3adc475 commit 7cce8e3

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc

+6-16
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ namespace ptree = boost::property_tree;
4646

4747
#ifdef PULSAR_USE_BOOST_REGEX
4848
#include <boost/regex.hpp>
49+
#define PULSAR_REGEX_NAMESPACE boost
4950
#else
5051
#include <regex>
52+
#define PULSAR_REGEX_NAMESPACE std
5153
#endif
5254

5355
DECLARE_LOG_OBJECT()
@@ -365,27 +367,15 @@ const std::string ZTSClient::getHeader() const { return roleHeader_; }
365367
PrivateKeyUri ZTSClient::parseUri(const char *uri) {
366368
PrivateKeyUri uriSt;
367369
// scheme mediatype[;base64] path file
368-
369-
#ifdef PULSAR_USE_BOOST_REGEX
370-
static const boost::regex expression(
371-
"^(\?:([^:/\?#]+):)(\?:([;/\\-\\w]*),)\?(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)");
372-
boost::cmatch groups;
373-
if (boost::regex_match(uri, groups, expression)) {
374-
uriSt.scheme = groups.str(1);
375-
uriSt.mediaTypeAndEncodingType = groups.str(2);
376-
uriSt.data = groups.str(4);
377-
}
378-
#else // !PULSAR_USE_BOOST_REGEX
379-
static const std::regex expression(
380-
R"(^(?:([A-Za-z]+):)(?:([/\w\-]+;\w+),([=\w]+))?(?:\/\/)?(\/[^?#]+)?)");
381-
std::cmatch groups;
382-
if (std::regex_match(uri, groups, expression)) {
370+
static const PULSAR_REGEX_NAMESPACE::regex expression(
371+
R"(^(?:([A-Za-z]+):)(?:([/\w\-]+;\w+),([=\w]+))?(?:\/\/)?([^?#]+)?)");
372+
PULSAR_REGEX_NAMESPACE::cmatch groups;
373+
if (PULSAR_REGEX_NAMESPACE::regex_match(uri, groups, expression)) {
383374
uriSt.scheme = groups.str(1);
384375
uriSt.mediaTypeAndEncodingType = groups.str(2);
385376
uriSt.data = groups.str(3);
386377
uriSt.path = groups.str(4);
387378
}
388-
#endif // PULSAR_USE_BOOST_REGEX
389379
return uriSt;
390380
}
391381
} // namespace pulsar

pulsar-client-cpp/tests/ZTSClientTest.cc

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ TEST(ZTSClientTest, testZTSClient) {
4242
ASSERT_EQ("/path/to/private.key", uri.path);
4343
}
4444

45+
{
46+
PrivateKeyUri uri = ZTSClientWrapper::parseUri("file:./path/to/private.key");
47+
ASSERT_EQ("file", uri.scheme);
48+
ASSERT_EQ("./path/to/private.key", uri.path);
49+
}
50+
51+
{
52+
PrivateKeyUri uri = ZTSClientWrapper::parseUri("file://./path/to/private.key");
53+
ASSERT_EQ("file", uri.scheme);
54+
ASSERT_EQ("./path/to/private.key", uri.path);
55+
}
56+
4557
{
4658
PrivateKeyUri uri = ZTSClientWrapper::parseUri("data:application/x-pem-file;base64,SGVsbG8gV29ybGQK");
4759
ASSERT_EQ("data", uri.scheme);

0 commit comments

Comments
 (0)