etr / libhttpserver

C++ library for creating an embedded Rest HTTP server (and more)
GNU Lesser General Public License v2.1
882 stars 184 forks source link

[BUG] URL's that exactly match a registried regex pattern dispatch to that registered resource #308

Open bcsgh opened 1 year ago

bcsgh commented 1 year ago

Prerequisites

Description

This test here is in error because that map is populated with the URL including the regexp pattern.

This results in a url that includes the same text as the regex pattern matching, even if that text isn't matched by the regex.

Steps to Reproduce

Add the following to test/integ/basic.cpp (https://github.com/etr/libhttpserver/pull/309/commits/2ea297f680f63649715eababf2badb824e467638):

LT_BEGIN_AUTO_TEST(basic_suite, regex_url_exact_match)
    ok_resource resource;
    ws->register_resource("/foo/{v|[a-z]}/bar", &resource);
    curl_global_init(CURL_GLOBAL_ALL);

    string s;
    CURL *curl = curl_easy_init();
    CURLcode res;

    // Exact string match for the registered pattern 
    curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/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);
    res = curl_easy_perform(curl);
    LT_ASSERT_EQ(res, 0);

    int64_t http_code = 0;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &http_code);
    LT_ASSERT_EQ(http_code, 404);    /// <<<<<<<<<<<<< This fails: 200!=404
    curl_easy_cleanup(curl);
LT_END_AUTO_TEST(regex_url_exact_match)

Expected behavior: HTTP 404 (assuming the RE pattern doesn't match itself).

Actual behavior: HTTP 200

Reproduces how often: 100%

Versions

Current master branch

etr commented 1 year ago

@bcsgh I think your change fixes this issue, right?

bcsgh commented 1 year ago

I fixed some other issues. This one is still there because I gave up before firing out how to solve it (and because I'm not using that type of pattern):

https://github.com/etr/libhttpserver/blob/master/test/integ/basic.cpp#L1429-L1433 The #if 0 at the above should be switched to test for the correct behavior rather than demoing the wrong one.