1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00
caddy/caddyconfig/httpcaddyfile
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
..
addresses.go httpcaddyfile: Add default_bind global option (#4531) 2022-01-18 11:29:07 -07:00
addresses_fuzz.go ci: fuzz: switch engine from libfuzzer to native go-fuzz (#3194) 2020-03-26 18:20:34 -06:00
addresses_test.go httpcaddyfile: Preserve IPv6 addresses through normalization (fix #4381) 2021-10-20 10:27:59 -06:00
builtins.go caddytls: dns_challenge_override_domain for challenge delegation (#4596) 2022-03-08 12:03:43 -07:00
builtins_test.go caddyhttp: Split up logged remote address into IP and port (#4403) 2021-11-29 01:18:35 -05:00
directives.go reverseproxy: copy_response and copy_response_headers for handle_response routes (#4391) 2022-03-09 11:00:51 -07:00
directives_test.go tests: Clean up redundant type declarations 2020-04-10 08:48:21 -06:00
httptype.go caddytls: Support external certificate Managers (like Tailscale) (#4541) 2022-02-17 15:40:34 -07:00
httptype_test.go caddyfile: Introduce basic linting and fmt check (#3923) 2021-01-04 11:11:36 -07:00
options.go caddytls: Support external certificate Managers (like Tailscale) (#4541) 2022-02-17 15:40:34 -07:00
options_test.go caddyconfig: add global option for configuring loggers (#4028) 2021-03-12 13:00:02 -07:00
pkiapp.go httpcaddyfile: Add pki app root and intermediate cert/key config (#4514) 2022-01-18 12:18:31 -07:00
serveroptions.go httpcaddyfile: Support explicitly turning off strict_sni_host (#4592) 2022-03-01 20:02:39 -05:00
tlsapp.go httpcaddyfile: Disabling OCSP stapling for both managed and unmanaged (#4589) 2022-02-19 14:20:38 -07:00
tlsapp_test.go httpcaddyfile: Revise automation policy generation (#3824) 2020-10-28 20:36:00 -06:00