/
parser.go
160 lines (132 loc) · 3.32 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
151
152
153
154
155
156
157
158
159
160
package http
import (
"bytes"
"encoding/base64"
"unsafe"
"github.com/pkg/errors"
)
const (
maxparams = 45 // support scrapes with up to 40 `info_hash` params
)
var (
invalidParse = errors.New("invalid parse")
)
type (
params [maxparams][]byte
parsed struct {
Path string
Params params
URLend int
Method string
pathstart int
pathend int
}
)
// Custom HTTP parser
// only supports GET request and up to `maxparams` params but uses no heap memory
func parse(data []byte, size int) (parsed, error) {
// uTorrent sometimes encodes scrape req in b64
// Example: R0VUIC9zY3JhcGU/aW5mb19oYXNoPS = GET /scrape?info_hash=
if bytes.HasPrefix(data, []byte("R0VU")) {
decoded, err := base64.StdEncoding.Decode(data, data[:size])
if err != nil {
return parsed{}, errors.Wrap(err, "failed to decode base64 encoded request")
}
data = data[:decoded] // this only modifies the local copy since `len int` vs `data uintptr`
}
p := parsed{
URLend: bytes.Index(data, []byte(" HTTP/")),
pathstart: bytes.Index(data, []byte("GET /")) + 4, // includes leading slash
pathend: bytes.Index(data, []byte("?")),
}
methodend := bytes.Index(data, []byte(" /"))
if methodend == -1 {
return parsed{}, invalidParse
}
tmp := data[:methodend]
p.Method = *(*string)(unsafe.Pointer(&tmp))
if p.URLend == -1 {
return parsed{}, invalidParse
}
// less than "GET / HTTP..."
if p.URLend < 5 {
return parsed{}, invalidParse
}
// pathstart should come before URLend
if p.pathstart > p.URLend {
return parsed{}, invalidParse
}
// if the ? is part of a query then parse it
if p.pathend != -1 && p.pathend < p.URLend {
if p.pathend < p.pathstart {
return parsed{}, invalidParse
}
paramsBytes := data[p.pathend+1 : p.URLend]
var pos, pIndex int
for i := 0; i < len(paramsBytes) && pIndex < maxparams; i++ {
if paramsBytes[i] == '&' {
p.Params[pIndex] = paramsBytes[pos:i]
pos = i + 1
pIndex++
} else if i == len(paramsBytes)-1 {
p.Params[pIndex] = paramsBytes[pos : i+1]
}
}
if pIndex == maxparams {
pIndex--
}
for i := 0; i <= pIndex; i++ {
p.Params[i] = unescapeFast(p.Params[i])
// nil if escape was invalid
if p.Params[i] == nil {
return parsed{}, invalidParse
}
}
tmp = data[p.pathstart:p.pathend]
p.Path = *(*string)(unsafe.Pointer(&tmp))
} else {
tmp = data[p.pathstart:p.URLend]
p.Path = *(*string)(unsafe.Pointer(&tmp))
}
return p, nil
}
// from stdlib
// https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/encoding/hex/hex.go#L84
func fromHexChar(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}
// unescapeFast unescapes url encoded []byte
// returns nil if escape is invalid
func unescapeFast(bs []byte) []byte {
l := len(bs)
for i := 0; i < l; i++ {
// match escape
if bs[i] == '%' {
// make sure there's 2 escape chars
if i+2 >= l {
return nil
}
// get hex chars
a := fromHexChar(bs[i+1])
b := fromHexChar(bs[i+2])
// change percent to real byte
bs[i] = (a << 4) | b
// shift everything left by 2
for x := i; x < len(bs)-3; x++ {
bs[x+1] = bs[x+3]
}
// decrease slice length by 2
l -= 2
}
}
bs = bs[:l]
return bs
}