-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
reverseproxy: New copy_response
handler for handle_response
routes
#4391
Conversation
5fe45c7
to
274ba70
Compare
What do you think about the idea of copying only incoming headers? Right now, we have to specify each header on (transparent) {
header Content-Disposition {http.reverse_proxy.header.Content-Disposition}
header Content-Transfer-Encoding {http.reverse_proxy.header.Content-Transfer-Encoding}
header Content-Type {http.reverse_proxy.header.Content-Type}
header Referrer-Policy {http.reverse_proxy.header.Referrer-Policy}
header Set-Cookie {http.reverse_proxy.header.Set-Cookie}
header X-Content-Type-Options {http.reverse_proxy.header.X-Content-Type-Options}
header X-Download-Options {http.reverse_proxy.header.X-Download-Options}
header X-Frame-Options {http.reverse_proxy.header.X-Frame-Options}
header X-Permitted-Cross-Domain-Policies {http.reverse_proxy.header.X-Permitted-Cross-Domain-Policies}
header X-Request-Id {http.reverse_proxy.header.X-Request-Id}
header X-Runtime {http.reverse_proxy.header.X-Runtime}
}
reverse_proxy web:3000 {
@accel header X-Accel-Redirect *
handle_response @accel {
import transparent
rewrite {http.reverse_proxy.header.X-Accel-Redirect}
file_server
}
} It would be nice if we have something like |
That's actually how it behaves with this PR @rainerborene
|
Oh, sorry, nevermind. It does copy the headers with |
@francislavoie got it. I suggest splitting it in two directives then: Edit: |
I prefer copy (because that's what the code does). And I was thinking |
33c590f
to
1a77bd3
Compare
Alright - I've implemented
The above will copy just the I ordered the |
2201ad3
to
166d1fc
Compare
@francislavoie Is it possible to use |
Yes, it supports either Feel free to build from this branch to try it out. |
166d1fc
to
10000cf
Compare
10000cf
to
93f5cd8
Compare
93f5cd8
to
00766a9
Compare
Followup to #4298 and #4388. This adds a new `copy_response` handler which may only be used in `reverse_proxy`'s `handle_response` routes, which can be used to actually copy the proxy response downstream. Previously, if `handle_response` was used (with routes, not the status code mode), it was impossible to use the upstream's response body at all, because we would always close the body, expecting the routes to write a new body from scratch. To implement this, I had to refactor `h.reverseProxy()` to move all the code that came after the `HandleResponse` loop into a new function. This new function `h.finalizeResponse()` takes care of preparing the response by removing extra headers, dealing with trailers, then copying the headers and body downstream. Since basically what we want `copy_response` to do is invoke `h.finalizeResponse()` at a configurable point in time, we need to pass down the proxy handler, the response, and some other state via a new `req.WithContext(ctx)`. Wrapping a new context is pretty much the only way we have to jump a few layers in the HTTP middleware chain and let a handler pick up this information. Feels a bit dirty, but it works. Also fixed a bug with the `http.reverse_proxy.upstream.duration` placeholder, it always had the same duration as `http.reverse_proxy.upstream.latency`, but the former was meant to be the time taken for the roundtrip _plus_ copying/writing the response.
Fixes a bug where the Content-Length will mismatch the actual bytes written if we skipped copying the response, so we get a message like this when using curl: ``` curl: (18) transfer closed with 18 bytes remaining to read ``` To replicate: ``` { admin off debug } :8881 { reverse_proxy 127.0.0.1:8882 { @200 status 200 handle_response @200 { header Foo bar } } } :8882 { header Content-Type application/json respond `{"hello": "world"}` 200 } ```
00766a9
to
19a9115
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, finally looked it over, and honestly, it's not as scary as I thought it would be, ha. Good job.
What if we mark these features as EXPERIMENTAL so we can possibly change/tweak them later if needed?
Experimental sounds fine to me |
Followup to #4298 and #4388.
This adds a new
copy_response
handler which may only be used inreverse_proxy
'shandle_response
routes, which can be used to actually copy the proxy response downstream.Previously, if
handle_response
was used (with routes, not the status code mode), it was impossible to use the upstream's response body at all, because we would always close the body, expecting the routes to write a new body from scratch.To implement this, I had to refactor
h.reverseProxy()
to move all the code that came after theHandleResponse
loop into a new function. This new functionh.finalizeResponse()
takes care of preparing the response by removing extra headers, dealing with trailers, then copying the headers and body downstream.Since basically what we want
copy_response
to do is invokeh.finalizeResponse()
at a configurable point in time, we need to pass down the proxy handler, the response, and some other state via a newreq.WithContext(ctx)
. Wrapping a new context is pretty much the only way we have to jump a few layers in the HTTP middleware chain and let a handler pick up this information. Feels a bit dirty, but it works.Also fixed a bug with the
http.reverse_proxy.upstream.duration
placeholder, it always had the same duration ashttp.reverse_proxy.upstream.latency
, but the former was meant to be the time taken for the roundtrip plus copying/writing the response.Example config,
copy_response
here will make sure the upstream's response body of{"hello": "world"}
gets copied to the client. Previously, you had to write a new response withrespond
orfile_server
etc:TODO: I just noticed that if no handler that writes a body is used, the
Content-Length
header from the upstream is still copied duringh.finalizeResponse()
, so we end up with a mismatch between that header and the actual body in the response (being empty). Maybe we should remove theContent-Length
header (or set it to zero? idk) specifically ifbodyClosed
is true. Not sure yet.Edit: Fixed the
Content-Length
issue.