caddy icon indicating copy to clipboard operation
caddy copied to clipboard

feat: add generic response interceptors

Open dunglas opened this issue 1 year ago • 3 comments

In proxy mode, Caddy allows intercepting and modifying responses coming from upstream servers. This is especially useful for implementing X-Sendfile/X-Accel-Redirect-like features.

However, the current implementation of this feature is only available in proxy mode and doesn't allow to intercept and modify responses created by modules.

This makes it impossible to use features like X-Sendfile with FrankenPHP, https://github.com/mliezun/caddy-snake, and other similar potential modules (LUA module, etc).

This patch implements a generic implementation of response interceptors that behaves similarly to the reverse_proxy handle_response subdirective.

It currently does not support the copy_response and copy_response_headers subdirective, but it should be easy to add support for these features in future Pull Requests. It should also be possible, in the future, to replace the current reverse_proxy specific implementation with this new more generic one, but it is out of scope for this PR.

Closes https://github.com/dunglas/frankenphp/issues/365. Closes https://github.com/dunglas/frankenphp/pull/396.

dunglas avatar Apr 10 '24 10:04 dunglas

Nice feature! Thanks @dunglas ! I posted asking for help configuring similar capability in this post on caddy forum few days ago.

elghailani avatar Apr 10 '24 13:04 elghailani

Looking good here. @francislavoie any thoughts on the Caddyfile implementation?

mholt avatar Apr 12 '24 13:04 mholt

@dunglas I'll probably tag 2.8 beta 1 this week; it'd be nice to get this in 2.8 beta 1, but if not, maybe we can get it in beta 2 or the RC.

mholt avatar Apr 23 '24 21:04 mholt

@dunglas Before we merge this, can we mark the exported types/methods as "EXPERIMENTAL: Subject to change or removal" (or something like that)? It sounds like there are still decisions to be made to finalize APIs to accommodate expanded functionality later, and by marking it as experimental we can at least merge it in sooner and then change it, as opposed to making painful changes later on.

mholt avatar May 13 '24 15:05 mholt

Sounds good to me to mark the public API as experimental. Should I update the code?

dunglas avatar May 13 '24 15:05 dunglas

Yeah, if you have a chance to do that could you push a commit? I'm wrapping up a few things here.

mholt avatar May 13 '24 15:05 mholt

@mholt done (the public type was already marked as experimental).

dunglas avatar May 13 '24 17:05 dunglas