-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
325 lines (236 loc) · 6.03 KB
/
context.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
package fox
import (
"crypto/sha256"
"database/sql"
"errors"
"fmt"
"net"
"os"
"strings"
"time"
"encoding/base64"
_ "github.com/mattn/go-sqlite3"
"github.com/robin-andreasson/fox/parser"
)
type Context struct {
Url string
Method string
Headers map[string]string
setHeaders map[string][]string
Body any // Body from the http request
Session any // Session payload received from the Session middleware
Refresh any // Refresh payload received from the Refresh middleware
Error []error
Params map[string]string
Query map[string]string
Cookies map[string]string
Raw []byte
_next bool
_conn net.Conn
}
type CookieAttributes struct {
HttpOnly bool
BASE64 bool
Secure bool
Partitioned bool
Path string
Domain string
SameSite string //strict, lax or none
ExpiresIn int
MaxAge int
}
func (c *Context) Next() error {
c._next = true
return nil
}
/*
returns set response headers
*/
func (c *Context) ResHeaders() map[string][]string {
return c.setHeaders
}
/*
Set a header by passing a name and value
*/
func (c *Context) SetHeader(name string, value string) error {
name = strings.Title(strings.ToLower(name))
if name == "Set-Cookie" {
if len([]byte(value)) > 4093 {
return errors.New("set-cookie value exceeded the size limit of 4093")
}
c.setHeaders[name] = append(c.setHeaders[name], value)
} else {
c.setHeaders[name] = []string{value}
}
return nil
}
/*
Send text to the request endpoint
content type is set to text/html; charset=utf-8
*/
func (c *Context) Text(code int, body string) error {
c.SetHeader("Content-Type", "text/html; charset=utf-8")
return c.response(code, []byte(body))
}
/*
Send file data to the request endpoint
mime types like images, zips, fonts, audio, pdf and mp4 files are calculated.
mime types from e.g. script files that is in need for a sniffing technique is found through file extension
*/
func (c *Context) File(code int, path string) error {
bytes, err := os.ReadFile(path)
if err != nil {
return err
}
mime := parser.Mime(path, bytes)
c.SetHeader("Content-Type", mime)
return c.response(code, bytes)
}
/*
Send json data to the request endpoint
*/
func (c *Context) JSON(code int, body any) error {
if !parser.IsMap(body) && !parser.IsArray(body) {
return errors.New("invalid type for body, expected map or array/slice")
}
s, err := parser.JSONMarshal(body)
if err != nil {
return err
}
c.SetHeader("Content-Type", "application/json")
return c.response(code, []byte(s))
}
/*
send empty body to the request endpoint
*/
func (c *Context) Status(code int) error {
return c.response(code, []byte{})
}
/*
redirect the client to the specified url path
*/
func (c *Context) Redirect(path string) error {
c.SetHeader("Location", path)
return c.Status(Status.SeeOther)
}
/*
create a session
*/
func (c *Context) SetSession(payload any) error {
if !sessionOpt.init {
return errors.New("session options are nil")
}
if !parser.IsMap(payload) && !parser.IsArray(payload) {
return errors.New("invalid type for payload, expected map or array/slice")
}
payload, err := parser.JSONMarshal(payload)
if err != nil {
return err
}
hash := sha256.New()
data := []byte(fmt.Sprint(sessionOpt.Secret, payload, sessionOpt.Secret))
hash.Write(data)
sessID := fmt.Sprintf("%x", hash.Sum(nil))
db, err := sql.Open("sqlite3", sessionOpt.Path)
if err != nil {
return err
}
defer db.Close()
stmt, err := db.Prepare("INSERT OR REPLACE INTO sessions VALUES (?, ?, ?)")
if err != nil {
return err
}
_, err = stmt.Exec(sessID, payload, time.Now().UnixMilli()+int64(sessionOpt.TimeOut))
if err != nil {
return err
}
c.Cookie("FOXSESSID", sessID, sessionOpt.Cookie)
return nil
}
/*
set a refresh session
returns access token
*/
func (c *Context) SetRefresh(accesstoken_payload any, refreshtoken_payload any) error {
if !refreshOpt.init {
return errors.New("refresh options are nil")
}
refreshtoken, err := generateToken(refreshtoken_payload, refreshOpt.RefreshToken)
if err != nil {
return err
}
accesstoken, err := generateToken(accesstoken_payload, refreshOpt.AccessToken)
if err != nil {
return err
}
c.Cookie("FOXREFRESH", refreshtoken, refreshOpt.Cookie)
c.SetHeader("X-Fox-Access-Token", accesstoken)
return nil
}
/*
Set a cookie
*/
func (c *Context) Cookie(name string, value string, attributes CookieAttributes) error {
cookie := name + "="
if attributes.BASE64 {
cookie += base64.StdEncoding.EncodeToString([]byte(value))
} else {
cookie += value
}
if attributes.MaxAge != 0 {
cookie += "; Max-Age=" + fmt.Sprint(attributes.MaxAge)
} else if attributes.ExpiresIn != 0 {
cookie += "; Expires=" + formatTime(attributes.ExpiresIn)
}
if attributes.Partitioned {
cookie += "; Partitioned"
}
if attributes.HttpOnly {
cookie += "; HttpOnly"
}
if attributes.Secure {
cookie += "; Secure"
}
if attributes.Domain != "" {
cookie += "; Domain=" + attributes.Domain
}
if attributes.Path != "" {
cookie += "; Path=" + attributes.Path
}
switch strings.ToLower(attributes.SameSite) {
case "strict":
cookie += "; SameSite=Strict"
case "lax":
cookie += "; SameSite=Lax"
case "none":
cookie += "; SameSite=None"
case "":
default:
return errors.New("samesite attribute is limited between the values Strict, Lax and None")
}
return c.SetHeader("Set-Cookie", cookie)
}
func (c *Context) response(code int, body []byte) error {
status := handleCors(c)
if status != 0 {
code = status
body = []byte{}
}
response := fmt.Sprint("HTTP/1.1 ", code, "\r\n")
response += fmt.Sprint("Date: ", formatTime(0), "\r\n")
response += "X-Powered-By: fox\r\n"
response += "Connection: keep-alive\r\n"
for key, values := range c.setHeaders {
for _, value := range values {
response += key + ": " + value + "\r\n"
}
}
response += "Content-Length: " + fmt.Sprint(len(body)) + "\r\n\r\n"
response_bytes := []byte(response)
if c.Method != "HEAD" {
response_bytes = append(response_bytes, body...)
}
_, err := c._conn.Write(response_bytes)
c._conn.Close()
return err
}