-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
123 lines (96 loc) · 3.79 KB
/
main.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
package main
import (
"crypto/md5"
"fmt"
"io"
"path/filepath"
"strconv"
"time"
"github.com/kataras/iris/v12"
)
const maxSize = 5 << 20 // 5MB
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./templates", ".html"))
// Serve the upload_form.html to the client.
app.Get("/upload", func(ctx iris.Context) {
// create a token (optionally).
now := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(now, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
// render the form with the token for any use you'd like.
// ctx.ViewData("", token)
// or add second argument to the `View` method.
// Token will be passed as {{.}} in the template.
if err := ctx.View("upload_form.html", token); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
/* Read before continue.
0. The default post max size is 32MB,
you can extend it to read more data using the `iris.WithPostMaxMemory(maxSize)` configurator at `app.Run`,
note that this will not be enough for your needs, read below.
1. The faster way to check the size is using the `ctx.GetContentLength()` which returns the whole request's size
(plus a logical number like 2MB or even 10MB for the rest of the size like headers). You can create a
middleware to adapt this to any necessary handler.
myLimiter := func(ctx iris.Context) {
if ctx.GetContentLength() > maxSize { // + 2 << 20 {
ctx.StatusCode(iris.StatusRequestEntityTooLarge)
return
}
ctx.Next()
}
app.Post("/upload", myLimiter, myUploadHandler)
Most clients will set the "Content-Length" header (like browsers) but it's always better to make sure that any client
can't send data that your server can't or doesn't want to handle. This can be happen using
the `app.Use(LimitRequestBodySize(maxSize))` (as app or route middleware)
or the `ctx.SetMaxRequestBodySize(maxSize)` to limit the request based on a customized logic inside a particular handler, they're the same,
read below.
2. You can force-limit the request body size inside a handler using the `ctx.SetMaxRequestBodySize(maxSize)`,
this will force the connection to close if the incoming data are larger (most clients will receive it as "connection reset"),
use that to make sure that the client will not send data that your server can't or doesn't want to accept, as a fallback.
app.Post("/upload", iris.LimitRequestBodySize(maxSize), myUploadHandler)
OR
app.Post("/upload", func(ctx iris.Context){
ctx.SetMaxRequestBodySize(maxSize)
// [...]
})
3. Another way is to receive the data and check the second return value's `Size` value of the `ctx.FormFile`, i.e `info.Size`, this will give you
the exact file size, not the whole incoming request data length.
app.Post("/", func(ctx iris.Context){
file, info, err := ctx.FormFile("uploadfile")
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
return
}
defer file.Close()
if info.Size > maxSize {
ctx.StatusCode(iris.StatusRequestEntityTooLarge)
return
}
// [...]
})
*/
// Handle the post request from the upload_form.html to the server
app.Post("/upload", iris.LimitRequestBodySize(maxSize+1<<20), func(ctx iris.Context) {
// Get the file from the request.
f, fh, err := ctx.FormFile("uploadfile")
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
return
}
defer f.Close()
_, err = ctx.SaveFormFile(fh, filepath.Join("./uploads", fh.Filename))
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
return
}
})
// start the server at http://localhost:8080 with post limit at 5 MB.
app.Listen(":8080" /* 0.*/, iris.WithPostMaxMemory(maxSize))
}