Skip to content

Commit

Permalink
Only match path params that span full path segment
Browse files Browse the repository at this point in the history
  • Loading branch information
bgs99 committed Sep 6, 2024
1 parent cb74e41 commit 74643c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 6 additions & 5 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ class PathParamsMatcher final : public MatcherBase {
bool match(Request &request) const override;

private:
static constexpr char marker = ':';
static constexpr char marker[] = "/:";
// Treat segment separators as the end of path parameter capture
// Does not need to handle query parameters as they are parsed before path
// matching
Expand Down Expand Up @@ -5899,13 +5899,14 @@ inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) {
#endif

while (true) {
const auto marker_pos = pattern.find(marker, last_param_end);
const auto marker_pos = pattern.find(
marker, last_param_end == 0 ? last_param_end : last_param_end - 1);
if (marker_pos == std::string::npos) { break; }

static_fragments_.push_back(
pattern.substr(last_param_end, marker_pos - last_param_end));
pattern.substr(last_param_end, marker_pos - last_param_end + 1));

const auto param_name_start = marker_pos + 1;
const auto param_name_start = marker_pos + 2;

auto sep_pos = pattern.find(separator, param_name_start);
if (sep_pos == std::string::npos) { sep_pos = pattern.length(); }
Expand Down Expand Up @@ -5967,7 +5968,7 @@ inline bool PathParamsMatcher::match(Request &request) const {
request.path_params.emplace(
param_name, request.path.substr(starting_pos, sep_pos - starting_pos));

// Mark everythin up to '/' as matched
// Mark everything up to '/' as matched
starting_pos = sep_pos + 1;
}
// Returns false if the path is longer than the pattern
Expand Down
12 changes: 12 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7599,6 +7599,18 @@ TEST(PathParamsTest, SequenceOfParams) {
EXPECT_EQ(request.path_params, expected_params);
}

TEST(PathParamsTest, SemicolonInTheMiddleIsNotAParam) {
const auto pattern = "/prefix:suffix";
detail::PathParamsMatcher matcher(pattern);

Request request;
request.path = "/prefix:suffix";
ASSERT_TRUE(matcher.match(request));

const std::unordered_map<std::string, std::string> expected_params = {};
EXPECT_EQ(request.path_params, expected_params);
}

TEST(UniversalClientImplTest, Ipv6LiteralAddress) {
// If ipv6 regex working, regex match codepath is taken.
// else port will default to 80 in Client impl
Expand Down

0 comments on commit 74643c8

Please sign in to comment.