-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc3986.go
47 lines (41 loc) · 857 Bytes
/
rfc3986.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
package oauth
import "bytes"
var hexToByte = map[byte]byte{
0x0: 0x30,
0x1: 0x31,
0x2: 0x32,
0x3: 0x33,
0x4: 0x34,
0x5: 0x35,
0x6: 0x36,
0x7: 0x37,
0x8: 0x38,
0x9: 0x39,
0xA: 0x41,
0xB: 0x42,
0xC: 0x43,
0xD: 0x44,
0xE: 0x45,
0xF: 0x46,
}
// Twitter wants RFC 3986, Go says "f you" I'm gonna use +'s
func percentEncode(s string) string {
var buf bytes.Buffer
for _, b := range []byte(s) {
if !validASCII(b) {
buf.WriteByte(0x25)
buf.WriteByte(hexToByte[b&0xF0>>4])
buf.WriteByte(hexToByte[b&0xF])
} else {
buf.WriteByte(b)
}
}
return buf.String()
}
// rfc3986 returns whether or not the byte is an acceptable ASCII value to rfc3986
func validASCII(b byte) bool {
return (b >= 0x30 && b <= 0x39) ||
(b >= 0x41 && b <= 0x5A) ||
(b >= 0x61 && b <= 0x7A) ||
(b == 0x2D || b == 0x2E || b == 0x5F || b == 0x7E)
}