Allow custom responses in onRequest #274
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Custom responses in
onRequestCurrent state of the art
The
onRequesthandler introduced by @jchip offers a way to access therequest andresponse objects before proxying is being handled¹. This allows modification of the request that's being sent to the eventual target. The target can also be changed entirely.The problem
Sending a custom response instead of just modifying the request, at the moment, is not quite possible without redbird still attempting to proxy the request somewhere, which results in it either overwriting your response with a
404 Not Foundresponse or it throwing an exception because it attempted to send a header after the response has already been (partially) sent.What this PR intends to do
With this PR draft I propose changes that make sending custom responses and proper handling for them in the
onRequestpossible.Upon returning the (mutated)
response object in theonRequesthandler, redbird will know that the request should skip proxying and instead will be replied to with what the handler fed to theresponse object.The logic for that, a test for it and a brief mention in the README have already been implemented in commit
5395e51.What's still missing / has to be done
The current logic still requires resolvers to supply a URL to proxy the request to [see ¹], because that's how the internals determine a target to process and attempt to send the request to.
The test (and eventual use of this feature) would, for now, just submit
'0.0.0.0'as the proxy target, which allows the logic to reach the code that handles custom responses in the first place.Example code:
I am aware that that's not ideal and we should find a better and more user friendly way to handle that. That would require bigger a few more changes though.
New possibilities with these changes:
notFoundhandler3XX)Tasks
onRequesthandlers that send a custom response0.0.0.0as proxy targetI hope handling redirects in a reverse proxy module does not seem out-of-scope to you folks. Personally I think it makes sense for stuff like old domains or deprecated routes that, using this and some sort of user-specific config, can just be redirected instead of needing to proxy it to some kind of separate bouncer server instance or wrapping more logic around redbird.
Since we already have
onRequestlogic, making these changes seems like a reasonable addition to me.If something is missing or feels off, don't hesitate to discuss it!