forked from project-flogo/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
142 lines (121 loc) · 3 KB
/
util.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
package rest
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"strings"
"time"
"github.com/project-flogo/core/data/coerce"
)
const HeaderSetCookie = "Set-Cookie"
func getFileDetails(key string, header *multipart.FileHeader) (map[string]interface{}, error) {
file, err := header.Open()
if err != nil {
return nil, err
}
defer file.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, file); err != nil {
return nil, err
}
fileDetails := map[string]interface{}{
"key": key,
"fileName": header.Filename,
"fileType": header.Header.Get("Content-Type"),
"size": header.Size,
"file": buf.Bytes(),
}
return fileDetails, nil
}
func addCookies(w http.ResponseWriter, cookies []interface{}) error {
w.Header().Del(HeaderSetCookie)
for _, cookie := range cookies {
strCookie := ""
if c, ok := cookie.(*http.Cookie); ok {
strCookie = c.String()
} else if c, ok := cookie.(string); ok {
strCookie = c
} else if c, ok := cookie.(map[string]interface{}); ok {
cookie, err := mapToCookie(c)
if err != nil {
return err
}
strCookie = cookie.String()
} else if params, ok := cookie.(map[string]string); ok {
c, _ := coerce.ToObject(params)
cookie, err := mapToCookie(c)
if err != nil {
return err
}
strCookie = cookie.String()
} else {
return fmt.Errorf("unsupported cookie format: %v", cookie)
}
if strCookie != "" {
w.Header().Add(HeaderSetCookie, strCookie)
}
}
return nil
}
func mapToCookie(values map[string]interface{}) (cookie *http.Cookie, err error) {
cookie = &http.Cookie{}
for key, value := range values {
switch strings.ToLower(key) {
case "name":
cookie.Name = value.(string)
case "value":
cookie.Value, err = coerce.ToString(value)
if err != nil {
return nil, err
}
case "path":
cookie.Path = value.(string)
case "domain":
cookie.Domain = value.(string)
case "expires":
if dt, ok := value.(time.Time); ok {
cookie.Expires = dt.UTC()
} else if dt, ok := value.(string); ok {
exptime, err := time.Parse(time.RFC1123, dt)
if err != nil {
exptime, err = time.Parse("Mon, 02-Jan-2006 15:04:05 MST", dt)
if err != nil {
return nil, fmt.Errorf("invalid expiration: %s", dt)
}
}
cookie.Expires = exptime.UTC()
}
case "maxage":
cookie.MaxAge, err = coerce.ToInt(value)
if err != nil {
return nil, err
}
case "secure":
cookie.Secure, err = coerce.ToBool(value)
if err != nil {
return nil, err
}
case "httponly":
cookie.HttpOnly, err = coerce.ToBool(value)
if err != nil {
return nil, err
}
case "samesite":
if ss, err := coerce.ToInt(value); err == nil {
cookie.SameSite = http.SameSite(ss)
} else if ss, ok := value.(string); ok {
switch strings.ToLower(ss) {
case "lax":
cookie.SameSite = http.SameSiteLaxMode
case "strict":
cookie.SameSite = http.SameSiteStrictMode
default:
cookie.SameSite = http.SameSiteDefaultMode
}
}
}
}
return cookie, nil
}