Skip to content

Commit 841a7ac

Browse files
committed
Fix regex URL exact match bug (issue #308)
URLs that exactly match a registered regex pattern string were incorrectly dispatching to that resource instead of returning 404. For example, registering `/foo/{v|[a-z]}/bar` and then requesting the literal URL `/foo/{v|[a-z]}/bar` returned 200 instead of 404. The root cause was that URLs with regex patterns were being added to `registered_resources_str` (fast string lookup map). When a request matched the literal pattern text, it bypassed regex validation. Fix: Only add URLs without regex patterns to the fast string lookup map by checking `idx.get_url_pars().empty()` before insertion.
1 parent cbf6bfd commit 841a7ac

File tree

2 files changed

+1
-5
lines changed

2 files changed

+1
-5
lines changed

src/webserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ bool webserver::register_resource(const std::string& resource, http_resource* hr
204204
std::unique_lock registered_resources_lock(registered_resources_mutex);
205205
pair<map<details::http_endpoint, http_resource*>::iterator, bool> result = registered_resources.insert(map<details::http_endpoint, http_resource*>::value_type(idx, hrm));
206206

207-
if (!family && result.second) {
207+
if (!family && result.second && idx.get_url_pars().empty()) {
208208
registered_resources_str.insert(pair<string, http_resource*>(idx.get_url_complete(), result.first->second));
209209
}
210210

test/integ/basic.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,11 +1595,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_url_exact_match)
15951595

15961596
int64_t http_code = 0;
15971597
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &http_code);
1598-
#if 0 // https://github.com/etr/libhttpserver/issues/308
15991598
LT_ASSERT_EQ(http_code, 404);
1600-
#else
1601-
LT_ASSERT_EQ(http_code, 200);
1602-
#endif
16031599
curl_easy_cleanup(curl);
16041600
}
16051601
LT_END_AUTO_TEST(regex_url_exact_match)

0 commit comments

Comments
 (0)