forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
337 lines (293 loc) · 7.72 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
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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strconv"
"strings"
)
type Header struct {
ContLen int64
Chunking bool
KeepAlive bool
}
type Request struct {
Method string
URL *URL
Proto string
Header
isConnect bool
raw bytes.Buffer
}
func (r *Request) String() (s string) {
s = fmt.Sprintf("%s %s%s", r.Method,
r.URL.Host, r.URL.Path)
if verbose {
s += fmt.Sprintf("\n%v", r.raw.String())
}
return
}
type Response struct {
Status string
Reason string
Header
raw bytes.Buffer
}
func (rp *Response) String() string {
var r string
if verbose {
r = rp.raw.String()
} else {
r = fmt.Sprintf("%s %s", rp.Status, rp.Reason)
}
return r
}
type URL struct {
Host string // must contain port
Path string
Scheme string
}
func (url *URL) String() string {
return url.Host + url.Path
}
func (url *URL) toURI() string {
return url.Scheme + "://" + url.String()
}
// headers of interest to a proxy
// Define them as constant and use editor's completion to avoid typos.
// Note RFC2616 only says about "Connection", no "Proxy-Connection", but firefox
// send this header.
// See more at http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/web-proxy-connection-header.html
const (
headerContentLength = "content-length"
headerTransferEncoding = "transfer-encoding"
headerConnection = "connection"
headerProxyConnection = "proxy-connection"
)
// For port, return empty string if no port specified.
func splitHostPort(s string) (host, port string) {
// Common case should has no port, check the last char first
if !IsDigit(s[len(s)-1]) {
return s, ""
}
// Scan back, make sure we find ':'
for i := len(s) - 2; i > 0; i-- {
c := s[i]
switch {
case c == ':':
return s[:i], s[i+1:]
case !IsDigit(c):
return s, ""
}
}
return s, ""
}
// net.ParseRequestURI will unescape encoded path, but the proxy don't need
// Assumes the input rawurl valid. Even if rawurl is not valid, net.Dial
// will check the correctness of the host.
func ParseRequestURI(rawurl string) (*URL, error) {
if rawurl[0] == '/' {
// OS X seems to send only path to the server if the url is 127.0.0.1
return &URL{Host: "", Path: rawurl}, nil
// return nil, errors.New("Invalid proxy request URI: " + rawurl)
}
var f []string
var rest, scheme string
f = strings.SplitN(rawurl, "://", 2)
if len(f) == 1 {
rest = f[0]
scheme = "http" // default to http
} else {
scheme = strings.ToLower(f[0])
if scheme != "http" && scheme != "https" {
return nil, errors.New(scheme + " protocol not supported")
}
rest = f[1]
}
var host, path string
f = strings.SplitN(rest, "/", 2)
host = f[0]
if len(f) == 1 || f[1] == "" {
path = "/"
} else {
path = "/" + f[1]
}
_, port := splitHostPort(host)
if port == "" {
host += ":80"
}
return &URL{Host: host, Path: path, Scheme: scheme}, nil
}
func splitHeader(s string) (name, val string, err error) {
var f []string
if f = strings.SplitN(strings.ToLower(s), ":", 2); len(f) != 2 {
// TODO Fix this when encounter such web servers
return "", "", errors.New("Multi-line header not supported")
}
return f[0], f[1], nil
}
func (h *Header) parseContentLength(s string) (err error) {
h.ContLen, err = strconv.ParseInt(strings.TrimSpace(s), 10, 64)
return err
}
func (h *Header) parseConnection(s string) error {
h.KeepAlive = strings.Contains(s, "keep-alive")
return nil
}
func (h *Header) parseTransferEncoding(s string) error {
h.Chunking = strings.Contains(s, "chunked")
return nil
}
type HeaderParserFunc func(*Header, string) error
// Using Go's method expression
var headerParser = map[string]HeaderParserFunc{
headerConnection: (*Header).parseConnection,
headerProxyConnection: (*Header).parseConnection,
headerContentLength: (*Header).parseContentLength,
headerTransferEncoding: (*Header).parseTransferEncoding,
}
// Only add headers that are of interest for a proxy into request's header map.
func (h *Header) parseHeader(reader *bufio.Reader, raw *bytes.Buffer, addHeader string) (err error) {
// Read request header and body
var s, name, val string
for {
if s, err = ReadLine(reader); err != nil {
return
}
if s == "" {
// Connection close, no content length specification
// Use chunked encoding to pass content back to client
if !h.KeepAlive && !h.Chunking && h.ContLen == -1 {
raw.WriteString("Transfer-Encoding: chunked\r\n")
}
raw.WriteString(addHeader)
raw.WriteString("\r\n")
return
}
if name, val, err = splitHeader(s); err != nil {
return
}
if parseFunc, ok := headerParser[name]; ok {
parseFunc(h, val)
if name == headerConnection || name == headerProxyConnection {
// Don't pass connection header to server or client
continue
}
}
raw.WriteString(s)
raw.WriteString("\r\n")
// debug.Printf("len %d %s", len(s), s)
}
return
}
// Consume all http header. Used for CONNECT method.
func drainHeader(reader *bufio.Reader) (err error) {
// Read request header and body
var s string
for {
s, err = ReadLine(reader)
if s == "" || err != nil {
return
}
}
return
}
// Parse the initial line and header, does not touch body
func parseRequest(reader *bufio.Reader) (r *Request, err error) {
r = new(Request)
r.ContLen = -1
var s string
// parse initial request line
if s, err = ReadLine(reader); err != nil {
return nil, err
}
// debug.Printf("Request initial line %s", s)
var f []string
if f = strings.SplitN(s, " ", 3); len(f) < 3 {
return nil, errors.New(fmt.Sprintf("malformed HTTP request: %s", s))
}
var requestURI string
r.Method, requestURI, r.Proto = f[0], f[1], f[2]
// Parse URI into host and path
r.URL, err = ParseRequestURI(requestURI)
if err != nil {
return
}
if r.Method == "CONNECT" {
// Consume remaining header and just return. Headers are not used for
// CONNECT method.
r.isConnect = true
err = drainHeader(reader)
return
}
r.genRequestLine()
// Read request header
if err = r.parseHeader(reader, &r.raw, ""); err != nil {
errl.Printf("Parsing request header: %v\n", err)
return nil, err
}
return
}
func (r *Request) genRequestLine() {
r.raw.WriteString(r.Method + " " + r.URL.Path)
r.raw.WriteString(" HTTP/1.1\r\nConnection: Keep-Alive\r\n")
}
var crlfBuf = make([]byte, 2)
func readCheckCRLF(reader *bufio.Reader) error {
if _, err := io.ReadFull(reader, crlfBuf); err != nil {
return err
}
if crlfBuf[0] != '\r' || crlfBuf[1] != '\n' {
return errors.New("Not CRLF")
}
return nil
}
// If an http response may have message body
func (rp *Response) hasBody(method string) bool {
// when we have tenary search tree, can optimize this a little
return !(method == "HEAD" ||
rp.Status == "304" ||
rp.Status == "204" ||
strings.HasPrefix(rp.Status, "1"))
}
var malformedHTTPResponseErr = errors.New("malformed HTTP response")
// Parse response status and headers.
func parseResponse(reader *bufio.Reader) (rp *Response, err error) {
rp = new(Response)
rp.ContLen = -1
var s string
START:
if s, err = ReadLine(reader); err != nil {
if err != io.EOF {
// err maybe timeout caused by explicity setting deadline
debug.Printf("Reading Response status line: %v\n", err)
}
return nil, err
}
var f []string
if f = strings.SplitN(s, " ", 3); len(f) < 2 {
errl.Println("malformed HTTP response status line:", s)
return nil, malformedHTTPResponseErr
}
// Handle 1xx response
if f[1] == "100" {
if err = readCheckCRLF(reader); err != nil {
errl.Printf("Reading CRLF after 1xx response: %v\n", err)
return nil, err
}
goto START
}
rp.Status = f[1]
if len(f) == 3 {
rp.Reason = f[2]
}
rp.raw.WriteString(s)
rp.raw.WriteString("\r\n")
if err = rp.parseHeader(reader, &rp.raw, "Connection: Keep-Alive\r\n"); err != nil {
errl.Println("Reading response header:", err)
return nil, err
}
return rp, nil
}