forked from 30x/libgozerian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
188 lines (163 loc) · 4.11 KB
/
http.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
package main
import (
"bytes"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
)
const (
// HTTP grammar regexps borrowed from Trireme source
ctl = "\\x00-\\x1f\\x7f"
digits = "[0-9]"
lws = "[ \\t]"
notCtl = "[^" + ctl + "]"
separator = "\\(\\)<>@,;:\"/\\[\\]?+{} \t\\\\"
// Huh? Texts = "[[ \t][^" + Ctl + "]]"
texts = "[^" + ctl + "]"
tokens = "[^" + separator + ctl + "]"
headerLine = "^(" + tokens + "+):" + lws + "*(" + notCtl + "*)" + lws + "*$"
requestLine = "^(" + tokens + "+) (" + texts + "+) HTTP/(" + digits + ").(" + digits + ")" + lws + "*$"
)
var requestLineRe = regexp.MustCompile(requestLine)
var headerLineRe = regexp.MustCompile(headerLine)
func parseHTTPHeaders(rawHeaders string, hasRequestLine bool) (*http.Request, error) {
req := http.Request{
Header: make(map[string][]string),
}
lines := strings.Split(rawHeaders, "\r\n")
for i, line := range lines {
if hasRequestLine && (i == 0) {
err := parseRequestLine(line, &req)
if err != nil {
return nil, err
}
} else {
err := parseHeaderLine(line, &req)
if err != nil {
return nil, err
}
}
}
return &req, nil
}
func parseHTTPResponse(status uint32, rawHeaders string) (*http.Response, error) {
resp := http.Response{
Header: make(map[string][]string),
StatusCode: int(status),
Status: http.StatusText(int(status)),
// Faking this for now
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
parseHeaders(resp.Header, rawHeaders)
clHeader := resp.Header.Get("Content-Length")
if clHeader != "" {
cl, err := strconv.ParseInt(clHeader, 10, 64)
if err != nil {
resp.ContentLength = cl
}
}
closeHeader := resp.Header.Get("Connection")
if closeHeader == "close" {
resp.Close = true
}
return &resp, nil
}
//serialize the headersMap back to a string
func serializeHeaders(headerMap http.Header) string {
var buffer bytes.Buffer
for key := range headerMap {
values := headerMap[key]
var valuesBuffer bytes.Buffer
for i := 0; i < len(values); i++ {
if values[i] != "" {
if i > 0 {
valuesBuffer.WriteString(",")
}
valuesBuffer.WriteString(values[i])
}
}
val := valuesBuffer.String()
buffer.WriteString(key)
buffer.WriteString(": ")
buffer.WriteString(val)
buffer.WriteString("\n")
}
serializedHeaders := buffer.String()
return serializedHeaders
}
/*
* Parse the simplified header serialization format supported by
* "serializeHeaders." This format is not the same as the HTTP standard.
*/
func parseHeaders(headerMap http.Header, rawHeaders string) {
headerValues := strings.Split(rawHeaders, "\n")
for _, header := range headerValues {
keyValue := strings.Split(header, ": ")
if len(keyValue) == 2 {
key := keyValue[0]
valueString := keyValue[1]
if valueString != "" {
values := strings.Split(valueString, ",")
if _, ok := headerMap[key]; !ok {
headerMap[key] = make([]string, len(values))
}
for i := 0; i < len(values); i++ {
headerMap[key][i] = values[i]
}
}
}
}
}
func parseRequestLine(line string, req *http.Request) error {
matches := requestLineRe.FindStringSubmatch(line)
if matches == nil {
return fmt.Errorf("Invalid HTTP request line: \"%s\"", line)
}
url, err := url.ParseRequestURI(matches[2])
if err != nil {
return err
}
major, err := strconv.Atoi(matches[3])
if err != nil {
return err
}
minor, err := strconv.Atoi(matches[4])
if err != nil {
return err
}
req.URL = url
req.RequestURI = matches[2]
req.Method = matches[1]
req.ProtoMajor = major
req.ProtoMinor = minor
req.Proto = fmt.Sprintf("HTTP/%d.%d", major, minor)
return nil
}
func parseHeaderLine(line string, req *http.Request) error {
if "" == line {
return nil
}
matches := headerLineRe.FindStringSubmatch(line)
if matches == nil {
return fmt.Errorf("Invalid HTTP header line: \"%s\"", line)
}
key := http.CanonicalHeaderKey(matches[1])
val := matches[2]
req.Header.Add(key, val)
switch key {
case "Host":
req.Host = val
case "Content-Length":
len, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
req.ContentLength = len
}
return nil
}