Skip to content

Commit df18b5e

Browse files
committed
Add paramter "max_form_buffer"
The paramter "max_form_buffer" is for limiting the memory at form parsing time. close #2
1 parent 16d7491 commit df18b5e

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ In this handler are the following modules in use.
2727
|**response_template**
2828
|The response template after a upload was successfully
2929

30+
|**max_form_buffer**
31+
|The maximum buffer size for https://pkg.go.dev/net/http#Request.ParseMultipartForm[ParseMultipartForm]. The default size is **1G**
32+
3033
|**max_size**
3134
|is the maximum size in bytes allowed for the upload.
3235
It accepts all size values supported by https://pkg.go.dev/github.com/dustin/go-humanize#pkg-constants[go-humanize]. Reads of
@@ -78,6 +81,9 @@ Here a example Caddyfile which expects that the environment variable
7881
----
7982
{
8083
order upload before file_server
84+
log {
85+
level DEBUG
86+
}
8187
}
8288
8389
{$APPPORT} {
@@ -89,6 +95,7 @@ Here a example Caddyfile which expects that the environment variable
8995
@mypost method POST
9096
upload @mypost {
9197
dest_dir tmp-upl
98+
max_form_buffer 1G
9299
max_filesize 4MB
93100
response_template templates/upload-resp-template.txt
94101
}

docker-files/opt/webroot/config/Caddyfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
order upload before file_server
3+
log {
4+
level DEBUG
5+
}
6+
37
}
48

59
{$APPPORT} {
@@ -11,6 +15,7 @@
1115
@mypost method POST
1216
upload @mypost {
1317
dest_dir tmp-upl
18+
max_form_buffer 1G
1419
max_filesize 4MB
1520
response_template templates/upload-resp-template.txt
1621
}

upload.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
Version = "0.3"
18+
Version = "0.4"
1919
)
2020

2121
func init() {
@@ -28,6 +28,7 @@ func init() {
2828
type Upload struct {
2929
DestDir string `json:"dest_dir,omitempty"`
3030
MaxFilesize int64 `json:"max_filesize,omitempty"`
31+
MaxFormBuffer int64 `json:"max_form_buffer,omitempty"`
3132
ResponseTemplate string `json:"response_template,omitempty"`
3233
NotifyURL string `json:"notify_url,omitempty"`
3334
NotifyMethod string `json:"notify_method,omitempty"`
@@ -81,9 +82,14 @@ func (u *Upload) Provision(ctx caddy.Context) error {
8182
u.NotifyMethod = "GET"
8283
}
8384

85+
if u.MaxFormBuffer == 0 {
86+
u.MaxFormBuffer = 1000000000
87+
}
88+
8489
u.logger.Info("Current Config",
8590
zap.String("dest_dir", u.DestDir),
8691
zap.Int64("max_filesize", u.MaxFilesize),
92+
zap.Int64("max_form_buffer", u.MaxFormBuffer),
8793
zap.String("response_template", u.ResponseTemplate),
8894
zap.String("notify_method", u.NotifyMethod),
8995
zap.String("notify_url", u.NotifyURL),
@@ -116,7 +122,7 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
116122
repl.Set("http.upload.max_filesize", u.MaxFilesize)
117123

118124
r.Body = http.MaxBytesReader(w, r.Body, u.MaxFilesize)
119-
if max_size_err := r.ParseMultipartForm(u.MaxFilesize); max_size_err != nil {
125+
if max_size_err := r.ParseMultipartForm(u.MaxFormBuffer); max_size_err != nil {
120126
u.logger.Error("ServeHTTP",
121127
zap.String("requuid", requuid),
122128
zap.String("message", "The uploaded file is too big. Please choose an file that's less than MaxFilesize."),
@@ -209,6 +215,16 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
209215
if !d.Args(&u.DestDir) {
210216
return d.ArgErr()
211217
}
218+
case "max_form_buffer":
219+
var sizeStr string
220+
if !d.AllArgs(&sizeStr) {
221+
return d.ArgErr()
222+
}
223+
size, err := humanize.ParseBytes(sizeStr)
224+
if err != nil {
225+
return d.Errf("parsing max_size: %v", err)
226+
}
227+
u.MaxFormBuffer = int64(size)
212228
case "max_filesize":
213229
var sizeStr string
214230
if !d.AllArgs(&sizeStr) {

0 commit comments

Comments
 (0)