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

Static File Server Endpoint #497

Closed
AndruePeters opened this issue Jul 11, 2022 · 6 comments
Closed

Static File Server Endpoint #497

AndruePeters opened this issue Jul 11, 2022 · 6 comments
Labels
question Issue can be closed by providing information

Comments

@AndruePeters
Copy link

Hey,

I've read documentation and looked at some issues, but I'm not sure how to fix this.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.css" rel="stylesheet" >  
</head>
<body></body>
</html

I don't want to have to prefix all links with "static".
I thought if I changed the endpoint prefix, then this would work.

#define CROW_STATIC_ENDPOINT "/<path>"

Shouldn't this redirect the endpoint /path to /static/path?

This still presents me with 404 errors, though. Is there a way to get the desired behavior? I'm testing multiple servers and really don't want to have to do static/css/bootstrap.css.

@AndruePeters
Copy link
Author

I've been thinking about how to implement this, but it seems quite difficult.

Is there anyway that a special rule can be made, so all files in static can use relative paths?

@AndruePeters
Copy link
Author

This is a current workaround I have, but it's not great:

    CROW_ROUTE(app, "/css/<string>")
    ([&app](const std::string& path) {
        crow::response res;
        res.body = getFileContents(getStaticPath("css", path));
        res.add_header("Content-Type", "stylesheet");
        return res;
    });

    CROW_ROUTE(app, "/js/<string>")
    ([](const std::string& path) {
        crow::response res;
        res.body = getFileContents(getStaticPath("js", path));
        res.add_header("Content-Type", "text/javascript");
        return res;
    });

@The-EDev The-EDev added the question Issue can be closed by providing information label Jul 11, 2022
@The-EDev
Copy link
Member

Changing CROW_STATIC_ENDPOINT only changes the endpoint that needs to be requested, if you want to change where your files are on the server, you need to change CROW_STATIC_DIRECTORY.


Is there anyway that a special rule can be made, so all files in static can use relative paths?

This is mostly up to the browser to do that, so a URL like https://example.com/index.html with a reference to css/stylesheet.css. Your browser would request https://example.com/css/stylesheet.css, which you can get by setting CROW_STATIC_DIRECTORY to "".

For subdirectories like https://example.com/category/index.html you can set up a blueprint with a custom static dir (though I might have noticed a bug while writing this comment).


This is a current workaround I have, but it's not great

I can see a few issues with this implementation. For starters, if you're going to use a response it would be best to use ([](crow::response& res) { and res.end() rather than defining it later and then using return res.

Another thing is using <string> instead of <path> disqualifies any / characters, making any files inside subdirectories invalid.

Lastly is that you can use Crow's response::set_static_file_info(path) which handles the Content-Type, sanitization (to prevent unauthorized navigation), and returning a 404 if the file doesn't exist.

@AndruePeters
Copy link
Author

Thanks for you response. I'll take a look at more of this tomorrow, but using the route with response::set_static_file_info(path) didn't work. The page hung for a while. This was essentially what I had, but the page refused to finish loading.

I didn't see blueprints when I went through the website docs. I'll take a look at that. Also, thanks for CROW_STATIC_DIRECTORY. I'll take a look at that.

    CROW_ROUTE(app, "/css/<string>")
    ([&app](const std::string& path) {
        crow::response res;
        res.response::set_static_file_info("/static/" + path;)
        return res;
    });
    ```

@The-EDev
Copy link
Member

The page hung for a while. This was essentially what I had, but the page refused to finish loading.

I'm guessing this is due to #469. It's been fixed in master but v1.0 still has it. I hope this helps.

@The-EDev
Copy link
Member

I'll close this for the time being. @AndruePeters let me know if you're facing issues still.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue can be closed by providing information
Projects
None yet
Development

No branches or pull requests

2 participants