-
Notifications
You must be signed in to change notification settings - Fork 6
/
meta.go
113 lines (89 loc) · 1.89 KB
/
meta.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
package coquelicot
import (
"errors"
"fmt"
"mime"
"net/http"
)
// Info about request headers
type meta struct {
MediaType string
Boundary string
Range *dataRange
Filename string
UploadSid string
}
type dataRange struct {
Start int64
End int64
Size int64
}
// Parse request headers and make Meta.
func parseMeta(req *http.Request) (*meta, error) {
m := &meta{}
if err := m.parseContentType(req.Header.Get("Content-Type")); err != nil {
return nil, err
}
if err := m.parseContentRange(req.Header.Get("Content-Range")); err != nil {
return nil, err
}
if err := m.parseContentDisposition(req.Header.Get("Content-Disposition")); err != nil {
return nil, err
}
cookie, err := req.Cookie("coquelicot")
if err != nil {
return nil, err
}
if cookie != nil {
m.UploadSid = cookie.Value
}
return m, nil
}
func (m *meta) parseContentType(ct string) error {
if ct == "" {
m.MediaType = "application/octet-stream"
return nil
}
mediatype, params, err := mime.ParseMediaType(ct)
if err != nil {
return err
}
if mediatype == "multipart/form-data" {
boundary, ok := params["boundary"]
if !ok {
return errors.New("meta: boundary not defined")
}
m.MediaType = mediatype
m.Boundary = boundary
} else {
m.MediaType = "application/octet-stream"
}
return nil
}
func (m *meta) parseContentRange(cr string) error {
if cr == "" {
return nil
}
var start, end, size int64
_, err := fmt.Sscanf(cr, "bytes %d-%d/%d", &start, &end, &size)
if err != nil {
return err
}
m.Range = &dataRange{Start: start, End: end, Size: size}
return nil
}
func (m *meta) parseContentDisposition(cd string) error {
if cd == "" {
return nil
}
_, params, err := mime.ParseMediaType(cd)
if err != nil {
return err
}
filename, ok := params["filename"]
if !ok {
return errors.New("meta: filename in Content-Disposition not defined")
}
m.Filename = filename
return nil
}