CrowCpp / Crow

A Fast and Easy to use microframework for the web.
https://crowcpp.org
Other
3k stars 335 forks source link

When configuring `CROW_STATIC_ENDPOINT="/<path>"`, the `CROW_CATCHALL_ROUTE` becomes ineffective. #845

Open lujiex opened 3 weeks ago

lujiex commented 3 weeks ago

test.cpp: int main() { crow::SimpleApp app; logging::Zlog _zlog = logging::Zlog::GetInstance(); crow::logger::setHandler(&_zlog);

CROW_ROUTE(app, "/")
(
    [](const crow::request &, crow::response &res)
    {
        res.set_static_file_info("dist/index.html");
        res.end();
    });

CROW_CATCHALL_ROUTE(app)
([](crow::response& res) {
    std::cout<<"Catchall route called"<<std::endl;
    if (res.code == 404)
    {
        res.body = "The URL does not seem to be correct.";
    }
    else if (res.code == 405)
    {
        res.body = "The HTTP method does not seem to be correct.";
    }
    res.end();
});

app.port(443).ssl_file("ssl.pem", "ssl.pem").multithreaded().loglevel(crow::LogLevel::Debug);
auto future = app.run_async();
future.wait();

}

log: ./restful Crow/master server is running at https://0.0.0.0:443 using 2 threads Call app.loglevel(crow::LogLevel::Warning) to hide Info level logs. 0x76000b40 {0} queue length: 1 0x76000b40 {0} queue length: 2 0x76000b40 {0} queue length: 3 Could not start adaptor: sslv3 alert certificate unknown (SSL routines, ssl3_read_bytes) Could not start adaptor: sslv3 alert certificate unknown (SSL routines, ssl3_read_bytes) 0x76000b40 {0} queue length: 4 0x14fd3b8 timer cancelled: 0x75ffec50 0 task_timer cancelled: 0x75ffec50 0 task_timer scheduled: 0x75ffec50 1 0x14fd3b8 timer added: 0x75ffec50 1 0x14fd3b8 timer cancelled: 0x75ffec50 1 task_timer cancelled: 0x75ffec50 1 Request: 192.168.8.6:2704 0x14fd3b8 HTTP/1.1 GET /xxxxx Matched rule '/' 1 / 2 Response: 0x14fd3b8 /xxxxx 404 0 0x14fd3b8 timer cancelled: 0x75ffec50 1 task_timer cancelled: 0x75ffec50 1 task_timer scheduled: 0x75ffec50 2 0x14fd3b8 timer added: 0x75ffec50 2 task_timer called: 0x75ffec50 2 0x14fd3b8 timer cancelled: 0x75ffec50 2 task_timer cancelled: 0x75ffec50 2 0x14fd3b8 from read(1) with description: "success"

I am using a version dated June 27, 2024, and the version information included in the version.h file is as follows:

pragma once

namespace crow { constexpr const char VERSION[] = "master"; }

When attempting to utilize CROW_CATCHALL_ROUTE as demonstrated in the example_catchall.cpp file to capture all erroneous routes, I encounter no noticeable effect. The custom handler function is not invoked, as evidenced by the absence of the message "Catchall route called" being printed. This issue seemingly arises from my configuration of CROW_STATIC_ENDPOINT="/<path>". It appears this setup may be hindering the functionality of CROW_CATCHALL_ROUTE, possibly because both my static content is served from the root (/), while API endpoints commence at /v1/, with the API version subject to change over time.

Despite this configuration, my aim is to still direct routing failures into the CROW_CATCHALL_ROUTE, ensuring comprehensive handling regardless of whether the request pertains to static resources or varying API versions.

lujiex commented 3 weeks ago

I have validated my hypothesis; during compilation, upon removing CROW_STATIC_ENDPOINT="/<path>", the CROW_CATCHALL_ROUTE functions as intended.