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

Nice way to call "handle_file_request" from user code #1906

Closed
paulharris opened this issue Sep 2, 2024 · 4 comments
Closed

Nice way to call "handle_file_request" from user code #1906

paulharris opened this issue Sep 2, 2024 · 4 comments

Comments

@paulharris
Copy link
Contributor

Hi, this was partially addressed in #235 ,
I think it would be nice if we could write code along the lines of:

srv.Get("/favicon.ico", [=](const httplib::Request &, httplib::Response &res) {
    srv.send_file( location_of_favicon, res );
}

or something similar.
The point is Server::handle_file_request calls find_content_type() and does the neat mmap async method now, it would be nice to simply be able to ask httplib to send a specific file when desired.

favicon.ico is a simplistic example, but there are other cases where you compute a filename and want to send that.

@yhirose
Copy link
Owner

yhirose commented Sep 3, 2024

@paulharris thanks for the idea. There is no need for it in any my projects for now. But a pull request is always welcome!

@sonavalepratham
Copy link

@paulharris I am beginner in open source & would like to work on this. can you please explain me what needs to be done here?

@yhirose yhirose closed this as completed in 8415bf0 Sep 7, 2024
@yhirose
Copy link
Owner

yhirose commented Sep 7, 2024

@paulharris I implemented it. (https://github.com/yhirose/cpp-httplib?tab=readme-ov-file#send-file-content)

svr.Get("/content", [&](const Request &req, Response &res) {
  res.set_file_content("./path/to/conent.html");
});

svr.Get("/content", [&](const Request &req, Response &res) {
  res.set_file_content("./path/to/conent", "text/html");
});

@paulharris
Copy link
Contributor Author

Thanks @yhirose !
See #1927 for what I had implemented last week and was about to push when I saw these messages.

I find res.set_file_content() good, but a bit low level.
I was hoping for something half way between the mountpoint approach and this raw approach.
The problem with mountpoint is it will respond first, before any other GET handler,
and you don't get a chance to tweak the response if you need to (which I don't).

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

No branches or pull requests

3 participants