@marcel91200 / caddy Mirror · read-only
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 11, 01:00 UTC

master
go 96 lines 3.03 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package requestbody
16
17 import (
18 "errors"
19 "io"
20 "net/http"
21 "strings"
22
23 "github.com/caddyserver/caddy/v2"
24 "github.com/caddyserver/caddy/v2/modules/caddyhttp"
25 )
26
27 func init() {
28 caddy.RegisterModule(RequestBody{})
29 }
30
31 // RequestBody is a middleware for manipulating the request body.
32 type RequestBody struct {
33 // The maximum number of bytes to allow reading from the body by a later handler.
34 // If more bytes are read, an error with HTTP status 413 is returned.
35 // Requests declaring a larger Content-Length get the same error
36 // before any later handler runs.
37 MaxSize int64 `json:"max_size,omitempty"`
38
39 // This field permit to replace body on the fly
40 // EXPERIMENTAL. Subject to change/removal.
41 Set string `json:"set,omitempty"`
42 }
43
44 // CaddyModule returns the Caddy module information.
45 func (RequestBody) CaddyModule() caddy.ModuleInfo {
46 return caddy.ModuleInfo{
47 ID: "http.handlers.request_body",
48 New: func() caddy.Module { return new(RequestBody) },
49 }
50 }
51
52 func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
53 if rb.Set != "" {
54 if r.Body != nil {
55 err := r.Body.Close()
56 if err != nil {
57 return err
58 }
59 }
60 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
61 // Only known placeholders are replaced: the body is user-supplied and
62 // often JSON, whose braces would otherwise be read as placeholders and
63 // blanked, sending an empty body instead. This matches the respond body.
64 replacedBody := repl.ReplaceKnown(rb.Set, "")
65 r.Body = io.NopCloser(strings.NewReader(replacedBody))
66 r.ContentLength = int64(len(replacedBody))
67 }
68 if r.Body == nil {
69 return next.ServeHTTP(w, r)
70 }
71 if rb.MaxSize > 0 {
72 // wrap before rejecting: error routes get this request and may read the body
73 r.Body = errorWrapper{http.MaxBytesReader(w, r.Body, rb.MaxSize)}
74 if r.ContentLength > rb.MaxSize {
75 return caddyhttp.Error(http.StatusRequestEntityTooLarge, &http.MaxBytesError{Limit: rb.MaxSize})
76 }
77 }
78 return next.ServeHTTP(w, r)
79 }
80
81 // errorWrapper wraps errors that are returned from Read()
82 // so that they can be associated with a proper status code.
83 type errorWrapper struct {
84 io.ReadCloser
85 }
86
87 func (ew errorWrapper) Read(p []byte) (n int, err error) {
88 n, err = ew.ReadCloser.Read(p)
89 if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
90 err = caddyhttp.Error(http.StatusRequestEntityTooLarge, err)
91 }
92 return n, err
93 }
94
95 // Interface guard
96 var _ caddyhttp.MiddlewareHandler = (*RequestBody)(nil)