forked from git001/caddyv2-upload
/
upload.go
429 lines (379 loc) · 12.1 KB
/
upload.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package upload
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/dustin/go-humanize"
"go.uber.org/zap"
)
const (
Version = "0.9"
)
func init() {
caddy.RegisterModule(Upload{})
httpcaddyfile.RegisterHandlerDirective("upload", parseCaddyfile)
}
// Middleware implements an HTTP handler that writes the
// uploaded file to a file on the disk.
type Upload struct {
DestDir string `json:"dest_dir,omitempty"`
FileFieldName string `json:"file_field_name,omitempty"`
MaxFilesize int64 `json:"max_filesize_int,omitempty"`
MaxFilesizeH string `json:"max_filesize,omitempty"`
MaxFormBuffer int64 `json:"max_form_buffer_int,omitempty"`
MaxFormBufferH string `json:"max_form_buffer,omitempty"`
ResponseTemplate string `json:"response_template,omitempty"`
NotifyURL string `json:"notify_url,omitempty"`
NotifyMethod string `json:"notify_method,omitempty"`
MyTlsSetting struct {
InsecureSkipVerify bool `json:"insecure,omitempty"`
CAPath string `json:"capath,omitempty"`
}
// TODO: Handle notify Body
ctx caddy.Context
logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (Upload) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.upload",
New: func() caddy.Module { return new(Upload) },
}
}
// Provision implements caddy.Provisioner.
func (u *Upload) Provision(ctx caddy.Context) error {
u.ctx = ctx
u.logger = ctx.Logger(u)
repl := caddy.NewReplacer()
if u.DestDir == "" {
u.logger.Error("Provision",
zap.String("msg", "no Destination Directory specified (dest_dir)"))
return fmt.Errorf("no Destination Directory specified (dest_dir)")
}
mdall_err := os.MkdirAll(u.DestDir, 0755)
if mdall_err != nil {
u.logger.Error("Provision",
zap.String("msg", "MkdirAll: Error creating destination Directory"),
zap.Error(mdall_err))
return mdall_err
}
if u.FileFieldName == "" {
u.logger.Warn("Provision",
zap.String("msg", "no FileFieldName specified (file_field_name), using the default one 'myFile'"),
)
u.FileFieldName = "myFile"
}
if u.ResponseTemplate == "" {
u.logger.Warn("Provision",
zap.String("msg", "no ResponseTemplate specified (response_template), using the default one"),
)
u.ResponseTemplate = "upload-resp-template.txt"
}
if u.NotifyURL != "" && u.NotifyMethod == "" {
u.NotifyMethod = "GET"
}
if u.MaxFilesize == 0 && u.MaxFilesizeH != "" {
MaxFilesizeH := repl.ReplaceAll(u.MaxFilesizeH, "1GB")
u.MaxFilesizeH = MaxFilesizeH
size, err := humanize.ParseBytes(u.MaxFilesizeH)
if err != nil {
u.logger.Error("Provision ReplaceAll",
zap.String("msg", "MaxFilesizeH: Error parsing max_filesize"),
zap.String("MaxFilesizeH", u.MaxFilesizeH),
zap.Error(err))
return err
}
u.MaxFilesize = int64(size)
} else {
if u.MaxFilesize == 0 {
size, err := humanize.ParseBytes("1GB")
if err != nil {
u.logger.Error("Provision int",
zap.String("msg", "MaxFilesize: Error parsing max_filesize_int"),
zap.Int64("MaxFilesize", u.MaxFilesize),
zap.Error(err))
return err
}
u.MaxFilesize = int64(size)
}
}
if u.MaxFormBuffer == 0 && u.MaxFormBufferH != "" {
MaxFormBufferH := repl.ReplaceAll(u.MaxFormBufferH, "1GB")
u.MaxFormBufferH = MaxFormBufferH
size, err := humanize.ParseBytes(u.MaxFormBufferH)
if err != nil {
u.logger.Error("Provision ReplaceAll",
zap.String("msg", "MaxFormBufferH: Error parsing max_form_buffer"),
zap.String("MaxFormBufferH", u.MaxFormBufferH),
zap.Error(err))
return err
}
u.MaxFormBuffer = int64(size)
} else {
if u.MaxFormBuffer == 0 {
size, err := humanize.ParseBytes("1GB")
if err != nil {
u.logger.Error("Provision int",
zap.String("msg", "MaxFormBufferH: Error parsing max_form_buffer_int"),
zap.Int64("MaxFormBuffer", u.MaxFormBuffer),
zap.Error(err))
return err
}
u.MaxFormBuffer = int64(size)
}
}
u.logger.Info("Current Config",
zap.String("Version", Version),
zap.String("dest_dir", u.DestDir),
zap.Int64("max_filesize_int", u.MaxFilesize),
zap.String("max_filesize", u.MaxFilesizeH),
zap.Int64("max_form_buffer_int", u.MaxFormBuffer),
zap.String("max_form_buffer", u.MaxFormBufferH),
zap.String("response_template", u.ResponseTemplate),
zap.String("notify_method", u.NotifyMethod),
zap.String("notify_url", u.NotifyURL),
zap.String("capath", u.MyTlsSetting.CAPath),
zap.Bool("insecure", u.MyTlsSetting.InsecureSkipVerify),
)
return nil
}
// Validate implements caddy.Validator.
func (u *Upload) Validate() error {
// TODO: Do I need this func
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
requuid, requuiderr := repl.GetString("http.request.uuid")
if !requuiderr {
requuid = "0"
u.logger.Error("http.request.uuid",
zap.Bool("requuiderr", requuiderr),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
}
repl.Set("http.upload.max_filesize", u.MaxFilesize)
r.Body = http.MaxBytesReader(w, r.Body, u.MaxFilesize)
if max_size_err := r.ParseMultipartForm(u.MaxFormBuffer); max_size_err != nil {
u.logger.Error("ServeHTTP",
zap.String("requuid", requuid),
zap.String("message", "The uploaded file is too big. Please choose an file that's less than MaxFilesize."),
zap.String("MaxFilesize", humanize.Bytes(uint64(u.MaxFilesize))),
zap.Error(max_size_err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusRequestEntityTooLarge, max_size_err)
}
// FormFile returns the first file for the given file field key
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, ff_err := r.FormFile(u.FileFieldName)
if ff_err != nil {
u.logger.Error("FormFile Error",
zap.String("requuid", requuid),
zap.String("message", "Error Retrieving the File"),
zap.Error(ff_err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, ff_err)
}
defer file.Close()
// Create the file within the DestDir directory
contentDisposition := parseContenDisposition(handler.Header.Get("Content-Disposition"))
rawFileName := contentDisposition["filename"]
validPath, err := validatePath(filepath.Join(u.DestDir, rawFileName), u.DestDir)
if err != nil {
u.logger.Error("Path Error",
zap.String("requuid", requuid),
zap.String("message", "Error in path"),
zap.Error(err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, err)
}
if err := os.MkdirAll(filepath.Dir(validPath), 0755); err != nil {
u.logger.Error("Path Error",
zap.String("requuid", requuid),
zap.String("message", "Failed to create path"),
zap.Error(err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, err)
}
tempFile, tmpf_err := os.OpenFile(validPath, os.O_RDWR|os.O_CREATE, 0755)
if tmpf_err != nil {
u.logger.Error("TempFile Error",
zap.String("requuid", requuid),
zap.String("message", "Error at TempFile"),
zap.Error(tmpf_err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, tmpf_err)
}
defer tempFile.Close()
// read all of the contents of our uploaded file into a
// byte array
//fileBytes, io_err := ioutil.ReadAll(file)
fileBytes, io_err := io.Copy(tempFile, file)
if io_err != nil {
u.logger.Error("Copy Error",
zap.String("requuid", requuid),
zap.String("destFilePath", validPath),
zap.String("message", "Error at io.Copy"),
zap.Error(io_err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, io_err)
}
// write this byte array to our temporary file
//tempFile.Write(fileBytes)
u.logger.Info("Successful Upload Info",
zap.String("requuid", requuid),
zap.String("Uploaded File", validPath),
zap.Int64("File Size", handler.Size),
zap.Int64("written-bytes", fileBytes),
zap.Any("MIME Header", handler.Header),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
repl.Set("http.upload.filename", validPath)
repl.Set("http.upload.filesize", handler.Size)
if u.ResponseTemplate != "" {
r.URL.Path = "/" + u.ResponseTemplate
}
if u.NotifyURL != "" {
errNotify := u.SendNotify(requuid)
if errNotify != nil {
u.logger.Error("Notify Error",
zap.Error(errNotify),
)
}
}
return next.ServeHTTP(w, r)
}
func validatePath(p, basepath string) (string, error) {
rel, err := filepath.Rel(basepath, p)
if err != nil {
return "", fmt.Errorf("invalid path (%s): %w", p, err)
}
if strings.HasPrefix(rel, "..") {
return "", fmt.Errorf("invalid path %s", p)
}
joined := filepath.Join(basepath, rel)
return joined, nil
}
func parseContenDisposition(s string) map[string]string {
parts := strings.Split(s, ";")
kv := map[string]string{}
for _, part := range parts {
kvPair := strings.Split(part, "=")
k := strings.TrimSpace(kvPair[0])
if len(kvPair) < 2 {
kv[k] = ""
continue
}
kv[k] = strings.TrimSuffix(strings.TrimPrefix(kvPair[1], `"`), `"`)
}
return kv
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
for d.NextBlock(0) {
switch d.Val() {
case "dest_dir":
if !d.Args(&u.DestDir) {
return d.ArgErr()
}
case "file_field_name":
if !d.Args(&u.FileFieldName) {
return d.ArgErr()
}
case "max_form_buffer":
var sizeStr string
if !d.AllArgs(&sizeStr) {
return d.ArgErr()
}
size, err := humanize.ParseBytes(sizeStr)
if err != nil {
return d.Errf("parsing max_form_buffer: %v", err)
}
u.MaxFormBuffer = int64(size)
case "max_form_buffer_int":
var sizeStr string
if !d.AllArgs(&sizeStr) {
return d.ArgErr()
}
i, err := strconv.ParseInt(sizeStr, 10, 64)
if err != nil {
return d.Errf("parsing max_form_buffer_int: %v", err)
}
u.MaxFormBuffer = i
case "max_filesize":
var sizeStr string
if !d.AllArgs(&sizeStr) {
return d.ArgErr()
}
size, err := humanize.ParseBytes(sizeStr)
if err != nil {
return d.Errf("parsing max_filesize: %v", err)
}
u.MaxFilesize = int64(size)
case "max_filesize_int":
var sizeStr string
if !d.AllArgs(&sizeStr) {
return d.ArgErr()
}
i, err := strconv.ParseInt(sizeStr, 10, 64)
if err != nil {
return d.Errf("parsing max_filesize_int: %v", err)
}
u.MaxFilesize = i
case "response_template":
if !d.Args(&u.ResponseTemplate) {
return d.ArgErr()
}
case "notify_url":
if !d.Args(&u.NotifyURL) {
return d.ArgErr()
}
case "notify_method":
if !d.Args(&u.NotifyMethod) {
return d.ArgErr()
}
case "insecure":
if !d.NextArg() {
return d.ArgErr()
}
u.MyTlsSetting.InsecureSkipVerify = true
case "capath":
if !d.Args(&u.MyTlsSetting.CAPath) {
return d.ArgErr()
}
default:
return d.Errf("unrecognized servers option '%s'", d.Val())
}
}
}
return nil
}
// parseCaddyfile parses the upload directive. It enables the upload
// of a file:
//
// upload {
// dest_dir <destination directory>
// max_filesize <size>
// response_template [<path to a response template>]
// }
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var u Upload
err := u.UnmarshalCaddyfile(h.Dispenser)
return u, err
}
// Interface guards
var (
_ caddy.Provisioner = (*Upload)(nil)
_ caddy.Validator = (*Upload)(nil)
_ caddyhttp.MiddlewareHandler = (*Upload)(nil)
_ caddyfile.Unmarshaler = (*Upload)(nil)
)