-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
145 lines (117 loc) · 3.25 KB
/
request.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
package minhttp
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net/textproto"
"strconv"
"strings"
)
type HttpRequest struct {
Method string
Path string
Version string
Headers *HeadersCollection
Params map[string]string
ContentLength int64
AcceptEncoding []string
Body io.Reader
}
type HeadersCollection struct {
rawHeaders map[string]string
}
func NewHeadersCollection() *HeadersCollection {
return &HeadersCollection{
rawHeaders: map[string]string{},
}
}
func (headers *HeadersCollection) AddRaw(headerBytes []byte) {
key, value, found := bytes.Cut(headerBytes, []byte{':'})
if !found {
return
}
keyText := strings.TrimSpace(string(key))
valueText := strings.TrimSpace(string(value))
current, ok := headers.Get(keyText)
if ok {
current = fmt.Sprintf("%s,%s", current, valueText)
return
}
headers.rawHeaders[keyText] = valueText
}
func (headers *HeadersCollection) Add(header string, value string) {
headers.rawHeaders[header] = value
}
func (headers *HeadersCollection) Get(header string) (string, bool) {
value, ok := headers.rawHeaders[header]
return value, ok
}
func (headers *HeadersCollection) Remove(header string) {
delete(headers.rawHeaders, header)
}
func (headers *HeadersCollection) Len() int {
return len(headers.rawHeaders)
}
func (headers *HeadersCollection) UserAgent() string {
userAgent, _ := headers.Get("User-Agent")
return userAgent
}
func (headers *HeadersCollection) ContentLength() int64 {
contentLengthStr, ok := headers.Get("Content-Length")
if !ok {
return 0
}
contentLength, err := strconv.Atoi(contentLengthStr)
if err != nil {
return 0
}
return int64(contentLength)
}
func (headers *HeadersCollection) AcceptEncoding() []string {
encodings, ok := headers.Get("Accept-Encoding")
if !ok {
return []string{}
}
if len(encodings) == 0 {
return []string{}
}
acceptedEncodings := strings.Split(encodings, ", ")
return acceptedEncodings
}
func ReadRequest(reader io.Reader) (HttpRequest, error) {
bufReader := bufio.NewReader(reader)
textReader := textproto.NewReader(bufReader)
statusLine, err := textReader.ReadLine()
method, target, version, err := decodeStatusLine(statusLine)
if err != nil {
return HttpRequest{}, err
}
readHeaders, err := textReader.ReadMIMEHeader()
if err != nil {
return HttpRequest{}, err
}
headers := NewHeadersCollection()
for key, value := range readHeaders {
headers.Add(key, value[0])
}
bodyReader := io.LimitReader(bufReader, headers.ContentLength())
return HttpRequest{
Method: method,
Path: target,
Version: version,
Headers: headers,
Params: make(map[string]string),
ContentLength: headers.ContentLength(),
AcceptEncoding: headers.AcceptEncoding(),
Body: bodyReader,
}, nil
}
func decodeStatusLine(statusLine string) (string, string, string, error) {
lineData := strings.Split(statusLine, " ")
if len(lineData) < 3 {
return "", "", "", errors.New("failed to read the status line.")
}
return lineData[0], lineData[1], lineData[2], nil
}