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

Feature request: Customize OPTIONS return code #939

Open
avaliente-evs opened this issue Nov 7, 2024 · 6 comments · May be fixed by #951
Open

Feature request: Customize OPTIONS return code #939

avaliente-evs opened this issue Nov 7, 2024 · 6 comments · May be fixed by #951
Labels
feature Code based project improvement

Comments

@avaliente-evs
Copy link

Hello,

I'm working on a server application whose existing (external) clients expects to have 200 on OPTIONS, and I cannot do that currently with crow.
Would it be possible to customize the return code for OPTIONS requests?

I can write the feature and send a PR, but I would like to know if this behavior is OK for you?

@The-EDev The-EDev added the feature Code based project improvement label Nov 8, 2024
@gittiver
Copy link
Member

gittiver commented Nov 8, 2024

Does something like:

CROW_ROUTE(app, "/add_json")
.methods(crow::HTTPMethod::Option)([](const crow::request& req) {
// here you can set the response code
});

not work?

@gittiver
Copy link
Member

will be closed as no answer to comment.

@gittiver gittiver added the further information required existing information is lacking, or feedback is required label Nov 25, 2024
@avaliente-evs
Copy link
Author

Sorry did not have time to investigate more.
On the website: https://crowcpp.org/master/guides/routes/ it says

Crow handles OPTIONS method automatically. The HEAD method is handled automatically unless defined in a route. Adding OPTIONS to a route's methods has no effect.

I did not try exactly the example you posted here, but I did try in a dynamic route and the handler was not called

@gittiver gittiver removed the further information required existing information is lacking, or feedback is required label Nov 25, 2024
@gittiver
Copy link
Member

It seems that the http Standard prefers 200 but also allows 204. (see https://www.rfc-editor.org/rfc/rfc9110.html#name-200-ok)

Currently 204 is hardcoded in routing.h.

As a workaround you could patch this.

As it seems that some browsers depend on it (see https://stackoverflow.com/questions/14675330/what-http-response-code-should-be-used-for-an-option-request) I would propose to add an Cmake Option "HTTP_OPTION_RESPONSE_RETURNS_OK" which defaults to off and to switch the return code based on that option.

Alternatively we could switch in general to 200 but may run into issues maybe with older browsers or clients which make special assumptions.

@MoxueKulin
Copy link

Can the routing. h file be modified to return the Access Control Allow Origin request header for OPTIONS pre check requests?

@gittiver
Copy link
Member

Yes, that modification could be done. But normally it should be supported by CORS Middleware.
see https://github.com/CrowCpp/Crow/blob/master/examples/middlewares/example_cors.cpp and https://crowcpp.org/master/guides/included-middleware/.
If that setup does not work, feel free to come back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Code based project improvement
Projects
None yet
4 participants