Skip to content

Conversation

adikabintang
Copy link
Contributor

Support for HTTP OPTIONS. Useful for Cross-Origin Resource Sharing (CORS).

@yhirose yhirose mentioned this pull request May 5, 2018
@yhirose
Copy link
Owner

yhirose commented May 5, 2018

@adikabintang, thanks for the PR. Yes, it would be nice to support OPTIONS, so that we could handle the HTTP preflight request.
https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

After I took a look at your code and pondered over how the OPTIONS support could be implemented, I now think it would be better to provide options method on the Server class, so that a user could configure HTTP response headers based on the given path.

svr.options(R"(\*)", [](const auto& req, auto& res) {
    res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
});

svr.options("/resource/foo", [](const auto& req, auto& res) {
    res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin").c_str());
    res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
    res.set_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Origin, Authorization");
    res.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, HEAD");
});

When I have time, I'll try to implement the above idea. Thanks again for your valuable feedback!

@adikabintang
Copy link
Contributor Author

@yhirose This feature would be very useful and I can see that you have a new branch (branch issue-57) to support this. Thank you very much for your work.

yhirose added a commit that referenced this pull request May 9, 2018
yhirose added a commit that referenced this pull request May 9, 2018
@yhirose
Copy link
Owner

yhirose commented May 9, 2018

@adikabintang, I have just merged the branch. I changed HTTP verb method names to be the initial caps such as Get, Options from this version of httplib.h. (This had to be done for #57.)

svr.Options(R"(\*)", [](const auto& req, auto& res) {
    res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
});

svr.Options("/resource/foo", [](const auto& req, auto& res) {
    res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin").c_str());
    res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
    res.set_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Origin, Authorization");
    res.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, HEAD");
});

Hope this fix will solve your problem!

@yhirose yhirose closed this May 9, 2018
@adikabintang
Copy link
Contributor Author

Wow, thank you @yhirose

ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this pull request May 2, 2023
ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this pull request May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants