0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2025-01-20 22:52:58 -05:00
caddy/modules/caddyhttp
Francis Lavoie c7d6c4cbb9
reverseproxy: copy_response and copy_response_headers for handle_response routes (#4391)
* reverseproxy: New `copy_response` handler for `handle_response` routes

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.

* Delete the "Content-Length" header if we aren't copying

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
}
```

* Implement `copy_response_headers`, with include/exclude list support

* Apply suggestions from code review

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
2022-03-09 11:00:51 -07:00
..
caddyauth go.mod: Migrate to golang.org/x/term (#4073) 2021-03-29 12:39:08 -06:00
encode encode: ignore flushing until after first write (#4318) 2021-08-31 13:36:36 -06:00
fileserver fileserver: Add pass_thru Caddyfile option (#4613) 2022-03-04 20:50:05 -07:00
headers headers: Fix + in Caddyfile to properly append rather than set (#4506) 2022-01-04 10:10:11 -07:00
map map: Fix 95c03506 (avoid repeated expansions) 2021-10-19 12:25:36 -06:00
push caddyhttp: Make logging of credential headers opt-in (#4438) 2021-12-02 13:26:24 -07:00
requestbody Revert "requestbody: Allow overwriting remote address" 2021-01-19 18:43:01 -07:00
reverseproxy reverseproxy: copy_response and copy_response_headers for handle_response routes (#4391) 2022-03-09 11:00:51 -07:00
rewrite rewrite: Add method Caddyfile directive (#4528) 2022-01-18 12:17:35 -07:00
standard tracing: New OpenTelemetry module (#4361) 2022-03-08 12:18:32 -07:00
templates templates: Fix docs for .Args 2022-03-03 11:12:37 -07:00
tracing tracing: New OpenTelemetry module (#4361) 2022-03-08 12:18:32 -07:00
app.go caddyhttp: Move HTTP redirect listener to an optional module (#4585) 2022-02-19 15:36:36 -07:00
autohttps.go caddyhttp: Don't attempt to manage Tailscale certs 2022-03-02 13:42:38 -07:00
caddyhttp.go fileserver: do not double-escape paths (#4447) 2021-12-11 09:26:21 -05:00
caddyhttp_test.go caddyhttp: Refactor and export SanitizedPathJoin for use in fastcgi (#4207) 2021-06-17 09:59:08 -06:00
celmatcher.go go.mod: Update direct dependencies 2021-06-03 12:18:25 -06:00
celmatcher_test.go
errors.go
httpredirectlistener.go caddyhttp: Move HTTP redirect listener to an optional module (#4585) 2022-02-19 15:36:36 -07:00
marshalers.go caddyhttp: Make logging of credential headers opt-in (#4438) 2021-12-02 13:26:24 -07:00
matchers.go caddyhttp: Support zone identifiers in remote_ip matcher (#4597) 2022-03-01 15:50:12 -07:00
matchers_test.go caddyhttp: Sanitize the path before evaluating path matchers (#4407) 2021-11-08 13:45:03 -07:00
metrics.go move common metrics-related funcs to internal package 2022-01-25 15:07:17 -05:00
metrics_test.go move common metrics-related funcs to internal package 2022-01-25 15:07:17 -05:00
replacer.go caddyhttp: Implement http.request.uuid placeholder (#4285) 2021-12-15 00:17:53 -07:00
replacer_test.go caddyfile: Fix caddy fmt nesting not decrementing (#4157) 2021-05-10 12:01:27 -06:00
responsematchers.go General minor improvements to docs 2021-09-24 18:31:01 -06:00
responsematchers_test.go reverseproxy: Add handle_response blocks to reverse_proxy (#3710) (#4021) 2021-05-02 12:39:06 -06:00
responsewriter.go
routes.go caddyhttp: Add support for triggering errors from try_files (#4346) 2021-09-17 00:52:32 -06:00
server.go caddyhttp: Honor wildcard hosts in log SkipHosts (#4606) 2022-03-04 13:44:59 -07:00
staticerror.go httpcaddyfile: Add error directive for the existing handler (#4034) 2021-03-12 13:25:49 -07:00
staticresp.go caddyhttp: Check for invalid subdirectives of static_response 2021-02-02 16:19:58 -07:00
staticresp_test.go Move from deprecated ioutil to os and io packages (#4364) 2021-09-29 11:17:48 -06:00
subroute.go
vars.go caddyhttp: Enhance vars matcher (#4433) 2021-12-13 13:59:58 -07:00