ninenines / cowboy

Small, fast, modern HTTP server for Erlang/OTP.
https://ninenines.eu
ISC License
7.27k stars 1.16k forks source link

Apply routing constraint to […] bind? #1636

Open richard-giraud opened 7 months ago

richard-giraud commented 7 months ago

Hello,

Is it possible to apply a constraint to the binding? I’m trying to use the :cowboy_static handler while keeping certain files from being served (e.g., *.php). I can apply a constraint to any of the regular binds and not that one; it doesn’t seem to come up in the list of binds.

If it’s not available, a syntax like [:foo…] may be a good approach. If that is of interest, I can try to implement it.

Thank you in advane.

essen commented 7 months ago

You can't because constraints are applied to bindings and '_' doesn't bind anything.

The best approach for your case is probably to write a middleware that you put between cowboy_router and cowboy_handler that would check if it's a PHP file and change the handler and handler_opts value based on that, or simply stop the request there and return a 4xx error.

richard-giraud commented 7 months ago

@essen Thank you for response I appreciate you clarifying what is possible at this time and I will investigate the approach you recommended.

Is the omission of this feature a design choice? Or is it desirable and simply not implemented yet?

If this is a feature that is not wanted, I think there is value in updating the documentation to explicitly mention that using constraints with […] is not possible. It may save the next person some of the hours that I spent trying to figure it out.

I’m happy to help with this as best as I can. Please let me know if either of the above appeals to you and I will do my best to craft a suitable PR.

Thanks again

essen commented 7 months ago

Documentation updates are always welcome, regardless of why things are as they are.

In this case, the code is a few years old so I'm not sure why it's only applying to explicitly bound values. I likely have not thought about using them with '_' or '...' yet. Experimenting with this would be welcome but note that I don't know what it should look like at this point.