badguardhome/internal/home/middlewares.go
Ainar Garipov f016ae172c Pull request: home: inc req size for some apis
Merge in DNS/adguard-home from 2666-req-body-lim to master

Updates #2666.

Squashed commit of the following:

commit a525974aee54831963e3f95c8186d44f1752e9c7
Merge: 947703f3 44168292
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Feb 11 13:48:06 2021 +0300

    Merge branch 'master' into 2666-req-body-lim

commit 947703f36e1ee0ab08f938850f76824b7899d7e1
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Feb 11 13:28:54 2021 +0300

    home: inc req size for some apis
2021-02-11 14:10:42 +03:00

68 lines
1.7 KiB
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
var bodySizeLimit int64 = RequestBodySizeLimit
if u := r.URL; u.Path == "/control/access/set" {
// An exception for a poorly designed API. Remove once
// the new, better API is up.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2666.
bodySizeLimit *= 4
}
r.Body, err = aghio.LimitReadCloser(r.Body, bodySizeLimit)
if err != nil {
log.Error("limitRequestBody: %s", err)
return
}
h.ServeHTTP(w, r)
})
}
// wrapIndexBeta returns handler that deals with new client.
func (web *Web) wrapIndexBeta(http.Handler) (wrapped http.Handler) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h, pattern := Context.mux.Handler(r)
switch pattern {
case "/":
web.handlerBeta.ServeHTTP(w, r)
case "/install.html":
web.installerBeta.ServeHTTP(w, r)
default:
h.ServeHTTP(w, r)
}
})
}