-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
150 lines (126 loc) · 3.64 KB
/
parser.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
package httpserver
import (
"bufio"
"fmt"
"io"
"net"
"net/url"
"strconv"
"strings"
)
type Header map[string]string
type HttpRequest struct {
Method string
Path string
Version string
Headers []Header
ContentLength int64
ContentType string
Query url.Values
// why not a string? the std lib implements it this way, so just a opportunity for me to learn
// How is this better?
// if we make this a string then we store the entire request body in Memory while serving a request
// this is bad because again we will parse the string to create a data structure with same bytes
// The moment we establish a TCP connection and the clients sends the HTTP request we already have the request in our Memory
// So O(n) space is already occcupied, now parsing the request should be optmizing memory more and more
// we can say O(3n) space if we use String, else O(n) when using Reader/Writer
// Another reason is the optimization the body parser/decoder like json decoder, xml decoder offers which
// takes in a Reader/Writer interface to perform read the body using *academic* parsing algorithms
Body io.ReadCloser
}
/*
HTTP REQUEST STRUCTURE:
METHOD path http_version (Called RequestLine)
KEY1 VAL1
KEY2 VAL2
body
*/
func Parse(conn net.Conn) (*HttpRequest, error) {
reader := bufio.NewReader(conn)
method, path, version, err := getRequestLine(reader)
if err != nil {
return nil, err
}
query, err := url.ParseQuery(path)
if err != nil {
return nil, err
}
headers, err := getHeaders(reader)
if err != nil {
return nil, err
}
// having a body in a request is not mandatory, so lets be careful
// the fd of our connection is now at the start of the body, as the getHeaders already read the empty line
// which is the seperator for the body from the header
contentLength, err := getContentLength(headers)
if err != nil {
return nil, err
}
contentType := getContentType(headers)
body := getBody(reader, contentLength)
request := &HttpRequest{
Method: method,
Path: path,
Version: version,
Headers: headers,
ContentLength: contentLength,
ContentType: contentType,
Query: query,
Body: body,
}
return request, nil
}
func getRequestLine(reader *bufio.Reader) (method, path, version string, err error) {
requestLine, err := reader.ReadString('\n')
if err != nil {
return "", "", "", err
}
parts := strings.SplitN(requestLine, " ", 3)
if len(parts) != 3 {
return "", "", "", fmt.Errorf("invalid request line")
}
method = parts[0]
path = parts[1]
version = strings.TrimRight(parts[2], "\r\n")
return
}
func getHeaders(reader *bufio.Reader) (headers []Header, err error) {
for {
line, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
if line == "\r" || line == "\r\n" {
break
}
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid header line")
}
headers = append(headers, map[string]string{strings.TrimSpace(parts[0]): strings.TrimSpace(parts[1])})
}
return
}
func getContentLength(headers []Header) (int64, error) {
for _, header := range headers {
if value, exists := header["Content-Length"]; exists {
contentLength, err := strconv.Atoi(strings.TrimSpace(value))
if err != nil {
return 0, fmt.Errorf("cannot parse content length")
}
return int64(contentLength), nil
}
}
return 0, nil
}
func getContentType(headers []Header) string {
for _, header := range headers {
if value, exists := header["Content-Type"]; exists {
return value
}
}
return ""
}
func getBody(reader *bufio.Reader, contentLength int64) io.ReadCloser {
return io.NopCloser(io.LimitReader(reader, contentLength))
}