-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
794 lines (656 loc) · 19.1 KB
/
Copy pathrequest.go
File metadata and controls
794 lines (656 loc) · 19.1 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
package zttp
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"mime"
"mime/multipart"
"net"
"net/http"
"net/textproto"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
const (
// drwxr-xr-x
DefaultDirPerm = os.ModeDir | 0755
// -rw-------
DefaultFilePerm = 0600
)
type AcceptPart struct {
part string
q float32
}
type FormFile struct {
Filename string
Content []byte
Header textproto.MIMEHeader
}
type Req struct {
LocalAddress string
Method string
Path string
Body string
Headers map[string]string
Params map[string]string
Queries map[string]string
Cookies map[string]string
*Ctx
}
// Return the value of the passed header key
func (req *Req) Header(key string) string {
return req.Headers[key]
}
// Return the value of the passed param key
func (req *Req) Param(key string) string {
return req.Params[key]
}
// Return the value of the passed query key
func (req *Req) Query(key string) string {
return req.Queries[key]
}
// Parse the request body into the target struct
// Note that the target MUST be a pointer
func (req *Req) ParseJson(target any) error {
return json.Unmarshal([]byte(req.Body), target)
}
// Return true when the response is still “fresh” in the client's cache.
// otherwise false is returned to indicate that the client cache is now stale
// and the full response should be sent.
// When a client sends the Cache-Control: no-cache request header to indicate an end-to-end
// reload request, this will return false to make handling these requests transparent.
// This logic is heavily inspired by the official gofiber source code, with some touches of mine:
// https://github.com/gofiber/fiber/blob/main/ctx.go
func (req *Req) Fresh() bool {
etagMatched := true
modifiedSinceMatched := true
etagMissing := false
// Check for conditional request headers
modifiedSince := req.Header("If-Modified-Since")
noneMatch := req.Header("If-None-Match")
// The request is unconditional
if modifiedSince == "" && noneMatch == "" {
log.Println("The request is unconditional")
return false
}
// Check `Cache-Control` request header to see if the
// request is intended to be an end-to-end request
cacheControl := req.Header("Cache-Control")
if cacheControl != "" && hasNoCacheDirective(cacheControl) {
log.Println("The request has the `Cache-Control: no-cache` header")
return false
}
// Start comparing conditional request headers with response headers
if noneMatch != "" && noneMatch != "*" {
etags := req.Ctx.Res.Headers["ETag"]
if len(etags) == 0 {
log.Println("`ETag` response header not found")
etagMatched = false
}
etag := etags[0]
if etag == "" {
log.Println("`ETag` response header not found")
etagMatched = false
}
// Check `Etag` and `If-None-Match` headers first
if isEtagStale(etag, []byte(noneMatch)) {
log.Println("`ETAG` response header didn't match with `If-None-Match` request header")
etagMatched = false
}
} else {
etagMatched = false
etagMissing = true
}
if modifiedSince != "" {
lastModifiedHeaders := req.Ctx.Res.Headers["Last-Modified"]
if len(lastModifiedHeaders) == 0 {
log.Println("`Last-Modified` response header not found")
modifiedSinceMatched = false
}
lastModified := lastModifiedHeaders[0]
if lastModified == "" {
log.Println("`Last-Modified` response header not found")
modifiedSinceMatched = false
}
if lastModified != "" {
lastModifiedTime, err := http.ParseTime(lastModified)
if err != nil {
log.Println("Could not parse last modified time")
modifiedSinceMatched = false
}
modifiedSinceTime, err := http.ParseTime(modifiedSince)
if err != nil {
log.Println("Could not parse modified since time")
modifiedSinceMatched = false
}
// Return true if modifiedSinceTime is not after lastModifiedTime
if lastModifiedTime.After(modifiedSinceTime) {
log.Println("Resource modified")
modifiedSinceMatched = false
} else {
log.Println("Resource wasn't modified")
}
}
}
return etagMatched || (etagMissing && modifiedSinceMatched)
}
// If the request is not fresh, then it's stale
func (req *Req) Stale() bool {
return !req.Fresh()
}
// Return the reference to the app this request is associated with
func (req *Req) App() *App {
return req.App()
}
// Return the base URL of the request derived from the `Host` HTTP header
// TODO: Should check the `X-Forwarded-Host` HTTP header also
func (req *Req) Host() string {
return req.Header("Host")
}
// Return the value of the specified part if the request is multipart
func (req *Req) FormValue(key string) string {
// Get the multipart reader if the request is multipart
form, err := parseMultipart(req.Headers, []byte(req.Body))
if err != nil {
return ""
}
defer form.RemoveAll()
// Return the first matching part value
values := form.Value[key]
if len(values) > 0 {
return values[0]
}
return ""
}
// Return the file of the specified part if the request is multipart
func (req *Req) FormFile(name string) (*FormFile, error) {
// Get the multipart reader if the request is multipart
form, err := parseMultipart(req.Headers, []byte(req.Body))
if err != nil {
return nil, err
}
defer form.RemoveAll()
// Return the first matching part value
files := form.File[name]
if len(files) == 0 {
return nil, fmt.Errorf("file %s not found", name)
}
// Open the file to prepare the bytes we will store in the content field of the
// FormFile struct
fileHeader := files[0]
file, err := fileHeader.Open()
if err != nil {
return nil, err
}
defer file.Close()
// Copy the bytes
content, err := io.ReadAll(file)
if err != nil {
return nil, err
}
return &FormFile{
Filename: fileHeader.Filename,
Content: content,
Header: fileHeader.Header,
}, nil
}
// Save the multipart form file directly to disk
// TODO: I think permissions should be a config later
func (req *Req) Save(formFile *FormFile, destination string) error {
// Check if formFile is nil first
if formFile == nil {
return fmt.Errorf("nil FormFile")
}
// Join the file name with the specified destination
// TODO: Should be sanitized first
fullPath := filepath.Join(destination, formFile.Filename)
// Create the directory
err := os.MkdirAll(destination, DefaultDirPerm)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
// Write the file to the destination
err = os.WriteFile(fullPath, formFile.Content, DefaultFilePerm)
if err != nil {
return fmt.Errorf("failed to save file: %w", err)
}
// No errors happened
return nil
}
// Checks if the specified types are accepted from the HTTP client
// TODO: Fix Canonicalization
func (req *Req) Accepts(offered ...string) string {
acceptHeader := req.Header("Accept")
if acceptHeader == "" || len(offered) == 0 {
// TODO: Align with RFC 9110 standards
if len(offered) != 0 {
return offered[0]
} else {
return ""
}
}
clientTypes := parseAcceptHeader(acceptHeader)
for _, clientType := range clientTypes {
for _, offered := range offered {
if matches(clientType.part, offered) {
return offered
}
}
}
return ""
}
// Checks if the specified types are accepted from the HTTP client
func (req *Req) AcceptsCharsets(offered ...string) string {
charsetHeader := req.Header("Accept-Charset")
if charsetHeader == "" {
// TODO: Align with RFC 2616 standards
if len(offered) != 0 {
return offered[0]
} else {
return ""
}
}
clientCharsets := parseAcceptHeader(charsetHeader)
// Handle wildcard
for _, cc := range clientCharsets {
if cc.part == "*" && cc.q > 0 {
return offered[0]
}
}
for _, cc := range clientCharsets {
for _, charset := range offered {
if strings.EqualFold(cc.part, charset) && cc.q > 0 {
return charset
}
}
}
return ""
}
func (req *Req) AcceptsEncodings(offered ...string) string {
encodingsHeader := req.Header("Accept-Encoding")
if encodingsHeader == "" {
// TODO: Align with RFC 9110 standards
if len(offered) != 0 {
return offered[0]
} else {
return ""
}
}
clientEncodings := parseAcceptHeader(encodingsHeader)
// Special cases (RFC 7231)
for _, enc := range clientEncodings {
// "identity" is always acceptable unless explicitly forbidden with q=0
if enc.part == "identity" && enc.q == 0 {
// Client explicitly refuses identity
return ""
}
}
for _, enc := range clientEncodings {
for _, offeredEnc := range offered {
if strings.EqualFold(enc.part, offeredEnc) && enc.q > 0 {
return offeredEnc
}
}
}
// Check for wildcard
for _, enc := range clientEncodings {
if enc.part == "*" && enc.q > 0 {
return offered[0]
}
}
return ""
}
func (req *Req) AcceptsLanguages(offered ...string) string {
langHeader := req.Header("Accept-Language")
if langHeader == "" {
// TODO: Align with RFC 9110 standards
if len(offered) != 0 {
return offered[0]
} else {
return ""
}
}
acceptedLangs := parseAcceptHeader(langHeader)
// Check for wildcard
for _, lang := range acceptedLangs {
if lang.part == "*" && lang.q > 0 {
return offered[0]
}
}
// Check exact match
for _, lang := range acceptedLangs {
for _, offeredLang := range offered {
if strings.EqualFold(lang.part, offeredLang) && lang.q > 0 {
return offeredLang
}
}
}
// Check primary language matches like `en` matches `en-US`
for _, lang := range acceptedLangs {
if lang.q == 0 {
continue
}
primaryLang := strings.Split(lang.part, "-")[0]
for _, offeredLang := range offered {
offeredPrimary := strings.Split(offeredLang, "-")[0]
if strings.EqualFold(primaryLang, offeredPrimary) {
return offeredLang
}
}
}
return ""
}
// TODO: Align with RFC 7239 standards
func (req *Req) IP() string {
// First, try the `X-Forwarded-For` request header
xff := req.Header("X-Forwarded-For")
if xff != "" {
// Could be a list: client, proxy1, proxy2
parts := strings.Split(xff, ",")
ip := strings.TrimSpace(parts[0])
host, _, err := net.SplitHostPort(ip)
if err != nil {
return ip
}
return host
}
// Second, try the `X-Real-IP` request header
realIP := req.Header("X-Real-IP")
if realIP != "" {
ip := strings.TrimSpace(realIP)
host, _, err := net.SplitHostPort(ip)
if err != nil {
return ip
}
return host
}
// Fallback to the default request tcp socket local address
host, _, err := net.SplitHostPort(req.LocalAddress)
if err != nil {
// If an error happened during the split, just return it with the port number
return req.LocalAddress
}
return host
}
// TODO: Align with RFC 7230 standards
// TODO: Handle IPv4 and IPv6 hosts
func (req *Req) Hostname() string {
host := req.Header("Host")
if host == "" {
return ""
}
// Some gophery ahhh type code
if parsedHost, port, err := net.SplitHostPort(host); err == nil {
if _, err := strconv.Atoi(port); err != nil {
return ""
}
return parsedHost
}
// Check if it's just an IPv6 address without port
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
return host[1 : len(host)-1]
}
// Check if it's just an invalid IPv6 address without port
if strings.HasPrefix(host, "[") != strings.HasSuffix(host, "]") {
return ""
}
// Check for multiple colons in a non-IPv6 format
// I'm not really comfortable with this check
// but it passes the current tests, so I just keep it
if strings.Count(host, ":") > 1 {
return ""
}
// Guard against obvious invalid characters
if strings.ContainsAny(host, "/\\@") {
return ""
}
return strings.ToLower(strings.TrimSpace(host))
}
// Parse the `Accepts` HTTP client header and return a list of accepted types with their quality factors
func parseAcceptHeader(header string) []AcceptPart {
var items []AcceptPart
parts := strings.Split(header, ",")
for _, part := range parts {
trimmed := strings.TrimSpace(part)
segments := strings.Split(trimmed, ";")
mime := segments[0]
// Default q-value
q := float32(1.0)
// Parse quality factor if present
if len(segments) > 1 && strings.HasPrefix(segments[1], "q=") {
fmt.Sscanf(segments[1][2:], "%f", &q)
}
items = append(items, AcceptPart{part: mime, q: q})
}
// Sort the accepted types according to the quality factor from highest to lowest
sort.Slice(items, func(i, j int) bool {
return items[i].q > items[j].q
})
return items
}
// Checks for matching between the types of the client header and the specified type
// TODO: Support case-insensitivity later
func matches(clientType, offeredType string) bool {
// Exact match
if clientType == offeredType {
return true
}
// Wildcard support
if strings.HasSuffix(clientType, "/*") && clientType != "*/*" {
return strings.Split(clientType, "/")[0] == strings.Split(offeredType, "/")[0]
}
// Catch-all wildcard
return clientType == "*/*"
}
// Checks if the request is multipart or not and return back the multipart form reader reference
func parseMultipart(headers map[string]string, body []byte) (*multipart.Form, error) {
// Check if it's a multipart request or not
contentType := headers["Content-Type"]
if contentType == "" {
return nil, http.ErrNotMultipart
}
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, err
}
if !strings.HasPrefix(mediaType, "multipart/") {
return nil, http.ErrNotMultipart
}
// Extract the boundary that separates between different parts
boundary := params["boundary"]
if boundary == "" {
log.Println("no boundary found in Content-Type")
return nil, http.ErrNotMultipart
}
// Create a multipart reader from the request body and the parsed boundary
reader := multipart.NewReader(bytes.NewReader(body), boundary)
// 32 MB memory limit + 10 MB added by default
// TODO: Should be a configuration later
return reader.ReadForm(32 << 20)
}
// Check if the Cache-Control header contains a valid 'no-cache' directive
func hasNoCacheDirective(cacheControl string) bool {
const directive = "no-cache"
// Check if the directive exists at all
pos := strings.Index(cacheControl, directive)
if pos < 0 {
return false
}
// Check the character before the directive
if pos > 0 {
prevChar := cacheControl[pos-1]
if prevChar != ' ' && prevChar != ',' {
return false
}
}
endPos := pos + len(directive)
// Case 1: Directive at end of string
if endPos == len(cacheControl) {
return true
}
// Case 2: Check character after directive
nextChar := cacheControl[endPos]
return nextChar == ',' || nextChar == ' '
}
// Checks if two ETags match according to RFC 7232
func compareETags(clientTag, serverTag string) bool {
// Direct match
if clientTag == serverTag {
return true
}
// Weak tag comparison cases
if strings.HasPrefix(clientTag, "W/") {
return clientTag[2:] == serverTag || "W/"+serverTag == clientTag
}
if strings.HasPrefix(serverTag, "W/") {
return serverTag[2:] == clientTag || "W/"+clientTag == serverTag
}
return false
}
// Directly taken from the official gofiber source code:
// https://github.com/gofiber/fiber/blob/main/ctx.go
func isEtagStale(etag string, noneMatchBytes []byte) bool {
var start, end int
// Adapted from:
// https://github.com/jshttp/fresh/blob/10e0471669dbbfbfd8de65bc6efac2ddd0bfa057/index.js#L110
for i := range noneMatchBytes {
switch noneMatchBytes[i] {
case 0x20:
if start == end {
start = i + 1
end = i + 1
}
case 0x2c:
if compareETags(string(noneMatchBytes[start:end]), etag) {
return false
}
start = i + 1
end = i + 1
default:
end = i + 1
}
}
return !compareETags(string(noneMatchBytes[start:end]), etag)
}
// Extract the request line from the buffer of the current client tcp socket
func extractRequestLine(rdr *bufio.Reader, socket net.Conn) []string {
var requestParts []string
// The request line is always the first line in the request
requestLine, err := rdr.ReadString('\n')
if err != nil {
if err == io.EOF {
log.Println("connection closed by client")
return requestParts
}
log.Println("err reading from socket... " + err.Error())
return requestParts
}
// Remove all leading and trailing white spaces
requestLine = strings.TrimSpace(requestLine)
// Log the incoming request
// TODO: must be a configuration detail later
fmt.Println("Incoming request: " + requestLine)
// Request line is empty, bad request
if requestLine == "" {
log.Println("empty request line, sending 'Bad Request' response")
sendResponse(socket, []byte("Bad Request"), 400, "text/plain", nil)
return requestParts
}
// Split the request line into three parts and return them as a slice
requestParts = strings.SplitN(requestLine, " ", 3)
if len(requestParts) < 2 {
log.Println("invalid request line: " + requestLine)
sendResponse(socket, []byte("Bad Request"), 400, "text/plain", nil)
return requestParts
}
return requestParts
}
// Extract the request headers and the body's content length (if exists) from the buffer of the current client tcp socket
func extractHeaders(rdr *bufio.Reader) (map[string]string, int) {
headers := make(map[string]string)
var contentLength int = 0
// Keep reading each line and parse it as a header until reaching an empty line
for {
line, err := rdr.ReadString('\n')
if err != nil {
log.Println("err reading headers... " + err.Error())
return nil, 0
}
// If the current header is the `Content-Length` header, store its value to return later
if strings.HasPrefix(line, "Content-Length") {
parts := strings.Split(line, ":")
lengthStr := strings.TrimSpace(parts[1])
contentLength, err = strconv.Atoi(lengthStr)
}
// Remove all leading and trailing white spaces and detect the end of the headers section
line = strings.TrimSpace(line)
if line == "" {
break
}
// Parse the header and store it
parts := strings.SplitN(line, ": ", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
headers[key] = value
}
return headers, contentLength
}
// Extract the request body from the buffer of the current client tcp socket
func extractBody(rdr *bufio.Reader, contentLength int) string {
body := ""
// Read exactly the next `contentLength` bytes in the buffer
if contentLength > 0 {
bodyBuffer := make([]byte, contentLength)
_, err := io.ReadFull(rdr, bodyBuffer)
if err != nil {
log.Println("err reading body... " + err.Error())
return ""
}
body = string(bodyBuffer)
}
return body
}
// Extract the request queries from the buffer of the current client tcp socket
func extractQueries(rawPath string) map[string]string {
queries := make(map[string]string)
if rawPath == "" {
return queries
}
// Split the raw path with the `&` delimiter
pairs := strings.SplitSeq(rawPath, "&")
// Parse the query and store it
for pair := range pairs {
parts := strings.SplitN(pair, "=", 2)
key := parts[0]
value := ""
if len(parts) > 1 {
value = parts[1]
}
queries[key] = value
}
return queries
}
// Extract the request cookies from the request headers
func extractCookies(headers map[string]string) map[string]string {
cookies := make(map[string]string)
if cookieHeader, ok := headers["Cookie"]; ok {
pairs := strings.Split(cookieHeader, ";")
for _, pair := range pairs {
pair = strings.TrimSpace(pair)
kv := strings.Split(pair, "=")
if len(kv) == 2 {
cookies[kv[0]] = kv[1]
}
}
}
return cookies
}