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

OPTIONS header has to be explicitly handled #67

Closed
edqx opened this issue Aug 31, 2024 · 3 comments
Closed

OPTIONS header has to be explicitly handled #67

edqx opened this issue Aug 31, 2024 · 3 comments

Comments

@edqx
Copy link

edqx commented Aug 31, 2024

I might be missing something, I don't know anything about the HTTP spec.

Using the built-in cors middleware, for each endpoint I have to explicitly declare both an OPTIONS handler as well as the handler for the endpoint:

router.options("/resources", options, .{});
router.post("/resources", createResource, .{});

Otherwise, OPTIONS /resources returns 404 and the browser freaks out.

Am I expected to declare these .options endpoints myself, or should http.zig add them 'implicitly'?

P.S. Great project, been very useful for me.

@karlseguin
Copy link
Owner

I agree this isn't ideal. Before httpz supported middleware, when CORS was just a built-in part of the flow, it behaved like you'd expect.

While I think about how to solve this, one temporary workaround it to define a catch-all route for options:

router.options("/*", noop, .{.middlewares = &.{cors}});

@edqx
Copy link
Author

edqx commented Aug 31, 2024

In terms of solving it, is OPTIONS as a method not a standard way for the client to request information about the route? if so, httpz could handle it implicitly.

karlseguin added a commit that referenced this issue Sep 1, 2024
1 - Global middleware can now be defined. This allows middleware to execute
even when no route is found. This can be useful for things like CORS which
should likely answer an OPTIONS request even on a not found.

2 - When adding middleware to a route, you can now pick between an "append" or
"replace" strategy. This controls how/if the middleware for a route are appended
or replace the middleware for a group and then up the chain to the global.

#67
@karlseguin
Copy link
Owner

When you create the router, you can now pass a list of middlewares. These will be globally applied (including to requests with no matching route):

var router = server.router(.{.middlewares = .{cors});

Any route-specific middleware will be appended to the global list.

Let me know if that solves the issue.

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

No branches or pull requests

2 participants