diff --git a/src/details/http_endpoint.cpp b/src/details/http_endpoint.cpp index 3bccea6d7..8133f6aac 100644 --- a/src/details/http_endpoint.cpp +++ b/src/details/http_endpoint.cpp @@ -142,6 +142,7 @@ http_endpoint& http_endpoint::operator =(const http_endpoint& h) { } bool http_endpoint::operator <(const http_endpoint& b) const { + if (family_url != b.family_url) return family_url; COMPARATOR(url_normalized, b.url_normalized, std::toupper); } diff --git a/src/webserver.cpp b/src/webserver.cpp index 2e77ac212..f0d458dc7 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -198,7 +198,7 @@ bool webserver::register_resource(const std::string& resource, http_resource* hr pair::iterator, bool> result = registered_resources.insert(map::value_type(idx, hrm)); - if (result.second) { + if (!family && result.second) { registered_resources_str.insert(pair(idx.get_url_complete(), result.first->second)); } diff --git a/test/integ/basic.cpp b/test/integ/basic.cpp index 4746423c4..75fd4d6e3 100644 --- a/test/integ/basic.cpp +++ b/test/integ/basic.cpp @@ -394,7 +394,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, duplicate_endpoints) LT_CHECK_EQ(false, ws->register_resource("OK/", &ok2)); // Check how family interacts. - LT_CHECK_EQ(false, ws->register_resource("OK", &ok2, true)); + LT_CHECK_EQ(true, ws->register_resource("OK", &ok2, true)); // Check that switched case does the right thing, whatever that is here. #ifdef CASE_INSENSITIVE @@ -406,6 +406,100 @@ LT_BEGIN_AUTO_TEST(basic_suite, duplicate_endpoints) #endif LT_END_AUTO_TEST(duplicate_endpoints) +LT_BEGIN_AUTO_TEST(basic_suite, family_endpoints) + static_resource ok1("1"), ok2("2"); + LT_CHECK_EQ(true, ws->register_resource("OK", &ok1)); + LT_CHECK_EQ(true, ws->register_resource("OK", &ok2, true)); + + curl_global_init(CURL_GLOBAL_ALL); + + { + string s; + CURL *curl = curl_easy_init(); + CURLcode res; + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK"); + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); + res = curl_easy_perform(curl); + LT_ASSERT_EQ(res, 0); + LT_CHECK_EQ(s, "1"); + curl_easy_cleanup(curl); + } + + { + string s; + CURL *curl = curl_easy_init(); + CURLcode res; + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/"); + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); + res = curl_easy_perform(curl); + LT_ASSERT_EQ(res, 0); + LT_CHECK_EQ(s, "1"); + curl_easy_cleanup(curl); + } + + { + string s; + CURL *curl = curl_easy_init(); + CURLcode res; + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/go"); + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); + res = curl_easy_perform(curl); + LT_ASSERT_EQ(res, 0); + LT_CHECK_EQ(s, "2"); + curl_easy_cleanup(curl); + } + +#ifdef CASE_INSENSITIVE + { + string s; + CURL *curl = curl_easy_init(); + CURLcode res; + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK"); + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); + res = curl_easy_perform(curl); + LT_ASSERT_EQ(res, 0); + LT_CHECK_EQ(s, "1"); + curl_easy_cleanup(curl); + } + + { + string s; + CURL *curl = curl_easy_init(); + CURLcode res; + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/"); + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); + res = curl_easy_perform(curl); + LT_ASSERT_EQ(res, 0); + LT_CHECK_EQ(s, "1"); + curl_easy_cleanup(curl); + } + + { + string s; + CURL *curl = curl_easy_init(); + CURLcode res; + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/go"); + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); + res = curl_easy_perform(curl); + LT_ASSERT_EQ(res, 0); + LT_CHECK_EQ(s, "2"); + curl_easy_cleanup(curl); + } +#endif +LT_END_AUTO_TEST(family_endpoints) + LT_BEGIN_AUTO_TEST(basic_suite, overlapping_endpoints) // Setup two different resources that can both match the same URL. static_resource ok1("1"), ok2("2"); @@ -418,7 +512,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, overlapping_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/foo/bar/"); + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/bar/"); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -436,7 +530,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, overlapping_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/foo/bar/"); + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/bar/"); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1267,7 +1361,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, non_family_url_with_regex_like_pieces) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/settings/{}"); + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/settings/{}"); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1290,7 +1384,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_url_exact_match) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/foo/a/bar/"); + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/a/bar/"); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1309,7 +1403,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_url_exact_match) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/foo/{v|[a-z]}/bar/"); + curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/{v|[a-z]}/bar/"); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);