@marcel91200 / caddy Mirror · read-only / commits / e3d04ff8
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 11:45 UTC

caddyhttp: Skip inserting HTTP->HTTPS redir if catch-all for both exist (#5051)

Francis Lavoie committed Sep 20, 2022 at 00:11 UTC e3d04ff86b5bf886bc4c6cf7806987eeda96d163
1 file changed +20 -10
modules/caddyhttp/autohttps.go
+20 -10
@@ -378,19 +378,29 @@ redirServersLoop:
378 // we'll create a new server for all the listener addresses
379 // that are unused and serve the remaining redirects from it
380 for _, srv := range app.Servers {
381 - if srv.hasListenerAddress(redirServerAddr) {
382 - // find the index of the route after the last route with a host
383 - // matcher, then insert the redirects there, but before any
384 - // user-defined catch-all routes
385 - // see https://github.com/caddyserver/caddy/issues/3212
386 - insertIndex := srv.findLastRouteWithHostMatcher()
387 - srv.Routes = append(srv.Routes[:insertIndex], append(routes, srv.Routes[insertIndex:]...)...)
381 + // only look at servers which listen on an address which
382 + // we want to add redirects to
383 + if !srv.hasListenerAddress(redirServerAddr) {
384 + continue
385 + }
386
389 - // append our catch-all route in case the user didn't define their own
390 - srv.Routes = appendCatchAll(srv.Routes)
387 + // find the index of the route after the last route with a host
388 + // matcher, then insert the redirects there, but before any
389 + // user-defined catch-all routes
390 + // see https://github.com/caddyserver/caddy/issues/3212
391 + insertIndex := srv.findLastRouteWithHostMatcher()
392
392 - continue redirServersLoop
393 + // add the redirects at the insert index, except for when
394 + // we have a catch-all for HTTPS, in which case the user's
395 + // defined catch-all should take precedence. See #4829
396 + if len(uniqueDomainsForCerts) != 0 {
397 + srv.Routes = append(srv.Routes[:insertIndex], append(routes, srv.Routes[insertIndex:]...)...)
398 }
399 +
400 + // append our catch-all route in case the user didn't define their own
401 + srv.Routes = appendCatchAll(srv.Routes)
402 +
403 + continue redirServersLoop
404 }
405
406 // no server with this listener address exists;