-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_host.go
More file actions
95 lines (91 loc) · 2.94 KB
/
Copy pathhttp_host.go
File metadata and controls
95 lines (91 loc) · 2.94 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
package proxy
import (
"bufio"
"bytes"
"io"
"net/http"
"strings"
)
// peekHTTPHost reads enough bytes from r to parse the HTTP/1.x request line
// and Host header, returning the peeked buffer and the host. Like peekSNI
// but for plain HTTP on port 80. The caller prepends the buffer to subsequent
// reads so the upstream sees the full request.
//
// Returns an empty host when the bytes are not a valid HTTP/1.x request
// (binary protocol, partial data, malformed). In that case the caller should
// fall back to IP-based policy. Reads are bounded by maxBytes to avoid
// hanging on slow clients or very long header sets.
func peekHTTPHost(r io.Reader, maxBytes int) ([]byte, string, error) {
buf := make([]byte, 0, maxBytes)
tmp := make([]byte, 4096)
for len(buf) < maxBytes {
// Cap each read so a single big chunk does not push buf
// past maxBytes.
want := maxBytes - len(buf)
if want > len(tmp) {
want = len(tmp)
}
n, err := r.Read(tmp[:want])
if n > 0 {
buf = append(buf, tmp[:n]...)
}
// Quick reject: HTTP/1.x request lines start with a method like
// GET/POST/HEAD/etc. Method tokens are uppercase ASCII letters.
// If the first byte is not in the [A-Z] range, this is not HTTP.
// Returning early on the first read avoids waiting maxBytes worth
// of data for a binary protocol that happens to be on port 80.
if len(buf) >= 1 && (buf[0] < 'A' || buf[0] > 'Z') {
return buf, "", nil
}
// Look for end of headers. http.ReadRequest needs the full header
// section before it returns; calling it on partial data yields
// io.ErrUnexpectedEOF, which we treat as "keep reading".
if idx := bytes.Index(buf, []byte("\r\n\r\n")); idx >= 0 {
host, ok := extractHTTPHost(buf[:idx+4])
if ok {
return buf, host, nil
}
return buf, "", nil
}
if err != nil {
if len(buf) > 0 {
return buf, "", nil
}
return nil, "", err
}
}
return buf, "", nil
}
// extractHTTPHost parses an HTTP/1.x request prefix terminated by \r\n\r\n
// and returns the Host header value with any port stripped. The fast-path
// uses net/http's parser, which handles obs-fold, mixed case, multiple
// Host header rules, and request-line validation in one pass.
func extractHTTPHost(prefix []byte) (string, bool) {
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(prefix)))
if err != nil {
return "", false
}
host := req.Host
if host == "" {
host = req.Header.Get("Host")
}
host = strings.TrimSpace(host)
if host == "" {
return "", false
}
// Strip port if present. IPv6 hosts in Host headers appear as
// "[::1]:80" so only strip the trailing :port when there is no
// closing bracket after the last colon.
if i := strings.LastIndex(host, ":"); i >= 0 {
if !strings.Contains(host[i:], "]") {
host = host[:i]
}
}
// IPv6 hosts may still be wrapped in [] — strip those.
host = strings.TrimPrefix(host, "[")
host = strings.TrimSuffix(host, "]")
if host == "" {
return "", false
}
return host, true
}