caddy/modules
Francis Lavoie 95b6ac44a6
caddyhttp: Fix fallback for the error handler chain
This is mainly a problem with the default behaviour of the Caddyfile's `handle_errors` routes, but it's kinda tricky to solve well. I went with an approach that involves a smidge of magic which might not really be desirable.

See https://caddy.community/t/problem-with-basicauth-handle-errors/12243/9 for context.

So we do already have a default fallback for error routes, i.e. `errorEmptyHandler` in `caddyhttp.go` which is always configured as the last handler in the chain (implicitly, not visible in the config).

The problem is that when subroutes come into play with `"terminal": true`, then this fallback handler will never be reached. This is the case when the Caddyfile generates a config which has a host matcher from a site block (which is most of the time) when the user configured `handle_errors` to handle specific errors (such as 502s or 404s to serve HTML pages for those, etc). If other errors, like `basicauth`'s 401s are emitted in that case, then the result is that the default of HTTP status 200 will be served instead of the 401, which breaks `basicauth` completely.

The fix I went with is to make the Caddyfile adapter append special `error` handlers inside of the `handle_errors` subroutes which throw error `-1`, which `server.go` then picks up, and seeing `-1` responds with the original error code of `401` instead. The `-1` thing is the aforementioned magic.

At first, I had this implemented with `static_response` setting the StatusCode to `{http.error.status_code}`, but it didn't feel right to use a placeholder because it's inherently slightly less efficient, and it wasn't 100% correct because non-handler errors wouldn't be handled as 500s properly I think (because if it's not a `HandlerError`, then `http.error.status_code` doesn't exist, so it would maybe try to write an the placeholder replacement result of an empty string as `0` for the status code).
2021-05-05 07:27:33 -04:00
..
caddyhttp caddyhttp: Fix fallback for the error handler chain 2021-05-05 07:27:33 -04:00
caddypki Minor tweaks 2021-04-01 12:49:51 -06:00
caddytls caddytls: Implement remote IP connection matcher (#4123) 2021-04-30 10:14:52 -06:00
filestorage
logging logging: add replace filter for static value replacement (#4029) 2021-03-12 13:01:34 -07:00
metrics metrics: allow disabling OpenMetrics negotiation (#3944) 2020-12-30 11:44:02 -07:00
standard metrics: Initial integration of Prometheus metrics (#3709) 2020-09-17 12:01:20 -06:00