mirror of
https://github.com/caddyserver/caddy.git
synced 2025-07-08 18:55:03 -04:00
Adapted internal middleware
- Detect too many internal redirects - return 500 in this case
This commit is contained in:
parent
a5b565e193
commit
e3d64169ed
@ -16,7 +16,10 @@ type Internal struct {
|
|||||||
Paths []string
|
Paths []string
|
||||||
}
|
}
|
||||||
|
|
||||||
const redirectHeader string = "X-Accel-Redirect"
|
const (
|
||||||
|
redirectHeader string = "X-Accel-Redirect"
|
||||||
|
maxRedirectCount int = 10
|
||||||
|
)
|
||||||
|
|
||||||
func isInternalRedirect(w http.ResponseWriter) bool {
|
func isInternalRedirect(w http.ResponseWriter) bool {
|
||||||
return w.Header().Get(redirectHeader) != ""
|
return w.Header().Get(redirectHeader) != ""
|
||||||
@ -37,7 +40,7 @@ func (i Internal) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
|
|||||||
iw := internalResponseWriter{ResponseWriter: w}
|
iw := internalResponseWriter{ResponseWriter: w}
|
||||||
status, err := i.Next.ServeHTTP(iw, r)
|
status, err := i.Next.ServeHTTP(iw, r)
|
||||||
|
|
||||||
for isInternalRedirect(iw) {
|
for c := 0; c < maxRedirectCount && isInternalRedirect(iw); c++ {
|
||||||
// Redirect - adapt request URL path and send it again
|
// Redirect - adapt request URL path and send it again
|
||||||
// "down the chain"
|
// "down the chain"
|
||||||
r.URL.Path = iw.Header().Get(redirectHeader)
|
r.URL.Path = iw.Header().Get(redirectHeader)
|
||||||
@ -46,6 +49,12 @@ func (i Internal) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
|
|||||||
status, err = i.Next.ServeHTTP(iw, r)
|
status, err = i.Next.ServeHTTP(iw, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isInternalRedirect(iw) {
|
||||||
|
// Too many redirect cycles
|
||||||
|
iw.ClearHeader()
|
||||||
|
return http.StatusInternalServerError, nil
|
||||||
|
}
|
||||||
|
|
||||||
return status, err
|
return status, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user