yhirose/cpp-httplib

Incorrect usage of urls ending without '/' in static file server (Windows only)

IBEX-BrianSmith opened this issue · 0 comments

Found in cpp-httplib v0.15.2 running on Windows 10 with Visual Studio 2022 (v143). Note: this behaviour has not been observed on Linux.

If a root mount point '/' is set before other mount points, then this prevents urls addressing the other mount points without a '/' at the end. For example:

#include <iostream>
#include "cpp-httplib/httplib.h"

using namespace httplib;

int main()
{
    Server svr;

    svr.set_mount_point("/", "C:\\dev");
    svr.set_mount_point("/test", "C:\\dev\\test");

    std::jthread lThread([&]() {
        svr.listen("0.0.0.0", 80); });
    std::cin.get();
    svr.stop();
}

curl -v 127.0.0.1/test results in a 404 Not Found.
curl -v 127.0.0.1/test/ results in a 200 OK.

The workaround is to set the root mount point as the last entry:

...
    svr.set_mount_point("/test", "C:\\dev\\test");
    svr.set_mount_point("/", "C:\\dev");
...

curl -v 127.0.0.1/test results in a 200 OK.
curl -v 127.0.0.1/test/ results in a 200 OK.

The problem lies in Server::handle_file_request where detail::is_file(path) is used. This uses _access_s for Windows which is for files or directories and is incorrect for this use(detail::is_dir() does not use this approach). The directory is then opened as a file which fails and hence the 404 response.

This may be linked to #1389.