AdGuardHome/internal/home/middlewares.go
Eugene Burkov b4a35fa887 Pull request: 2343 http server
Merge in DNS/adguard-home from 2343-http-server to master

Closes #2343.

Squashed commit of the following:

commit f4ebfc129484fc3489409069b3580eb70d71cc74
Merge: b13ec7002 36c7735b8
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Nov 25 15:37:27 2020 +0300

    Merge branch 'master' into 2343-http-server

commit b13ec70024f24f6b68b13a1ec6f27c89535feaf8
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Nov 25 15:31:36 2020 +0300

    all: record changes

commit ce44aac9d43e32db3f68746dec7a4f21b0a9dea4
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Nov 25 14:00:45 2020 +0300

    home: set http servers timeouts

commit 7f3e7385d1df39b39713b8ec443da5d9374d0bc8
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Nov 24 19:58:56 2020 +0300

    home: replace default ServeMux with custom one.
2020-11-25 15:50:59 +03:00

42 lines
1,002 B
Go

package home
import (
"net/http"
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
"github.com/AdguardTeam/golibs/log"
)
// middlerware is a wrapper function signature.
type middleware func(http.Handler) http.Handler
// withMiddlewares consequently wraps h with all the middlewares.
func withMiddlewares(h http.Handler, middlewares ...middleware) (wrapped http.Handler) {
wrapped = h
for _, mw := range middlewares {
wrapped = mw(wrapped)
}
return wrapped
}
// RequestBodySizeLimit is maximum request body length in bytes.
const RequestBodySizeLimit = 64 * 1024
// limitRequestBody wraps underlying handler h, making it's request's body Read
// method limited.
func limitRequestBody(h http.Handler) (limited http.Handler) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
r.Body, err = aghio.LimitReadCloser(r.Body, RequestBodySizeLimit)
if err != nil {
log.Error("limitRequestBody: %s", err)
return
}
h.ServeHTTP(w, r)
})
}