summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/requestbody/requestbody.go
blob: 8aba14ebfe6f2b04ac90f3c0deddf060dae1d3df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package requestbody

import (
	"net/http"

	"bitbucket.org/lightcodelabs/caddy2"
	"bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp"
)

func init() {
	caddy2.RegisterModule(caddy2.Module{
		Name: "http.middleware.request_body",
		New:  func() interface{} { return new(RequestBody) },
	})
}

// RequestBody is a middleware for manipulating the request body.
type RequestBody struct {
	MaxSize int64 `json:"max_size,omitempty"`
}

func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
	if r.Body == nil {
		return next.ServeHTTP(w, r)
	}
	if rb.MaxSize > 0 {
		r.Body = http.MaxBytesReader(w, r.Body, rb.MaxSize)
	}
	return next.ServeHTTP(w, r)
}

// Interface guard
var _ caddyhttp.MiddlewareHandler = (*RequestBody)(nil)