Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
IBEX-BrianSmith opened this issue Jun 7, 2024 · 1 comment
Labels

Comments

@IBEX-BrianSmith
Copy link

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.

@yhirose
Copy link
Owner

yhirose commented Aug 30, 2024

@IBEX-BrianSmith thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants