-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc3986.go
165 lines (146 loc) · 2.87 KB
/
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
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
// RFC 3986 URI Query Escape/Unescape inspired from "net/url" written in Go
package rfc3986
import (
"strconv"
"strings"
)
const upperhex = "0123456789ABCDEF"
type (
EscapeError string
InvalidHostError string
)
func (e EscapeError) Error() string {
return "invalid URL escape " + strconv.Quote(string(e))
}
func (e InvalidHostError) Error() string {
return "invalid character " + strconv.Quote(string(e)) + " in host name"
}
// QueryUnescape does the inverse transformation of QueryEscape,
// converting each 3-byte encoded substring of the form "%AB" into the
// hex-decoded byte 0xAB.
// It returns an error if any % is not followed by two hexadecimal
// digits.
func QueryUnescape(s string) (string, error) {
return unescape(s)
}
// QueryEscape escapes the string so it can be safely placed
// inside a URL query.
func QueryEscape(s string) string {
return escape(s)
}
func ishex(c byte) bool {
switch {
case '0' <= c && c <= '9':
return true
case 'a' <= c && c <= 'f':
return true
case 'A' <= c && c <= 'F':
return true
}
return false
}
func unhex(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
}
func shouldEscape(c byte) bool {
// §2.3 Unreserved characters (alphanum)
if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
return false
}
switch c {
case '-', '_', '.', '~': // §2.3 Unreserved characters (mark)
return false
default:
// Everything else must be escaped.
return true
}
}
// unescape unescapes a string
func unescape(s string) (string, error) {
var (
// Count %, check that they're well-formed.
n int = 0
lenS int = len(s)
hasPlus bool = false
)
for i := 0; i < lenS; {
switch s[i] {
case '%':
n++
if i+2 >= lenS || !ishex(s[i+1]) || !ishex(s[i+2]) {
s = s[i:]
if lenS > 3 {
s = s[:3]
}
return "", EscapeError(s)
}
i += 3
case '+':
hasPlus = true
i++
default:
i++
}
}
if n == 0 && !hasPlus {
return s, nil
}
var t strings.Builder
t.Grow(lenS - 2*n)
for i := 0; i < lenS; i++ {
switch s[i] {
case '%':
t.WriteByte(unhex(s[i+1])<<4 | unhex(s[i+2]))
i += 2
case '+':
t.WriteByte(' ')
default:
t.WriteByte(s[i])
}
}
return t.String(), nil
}
func escape(s string) string {
lenS, hexCount := len(s), 0
for i := 0; i < lenS; i++ {
c := s[i]
if shouldEscape(c) {
hexCount++
}
}
if hexCount == 0 {
return s
}
var (
buf [64]byte
t []byte
)
required := lenS + 2*hexCount
if required <= len(buf) {
t = buf[:required]
} else {
t = make([]byte, required)
}
j := 0
for i := 0; i < lenS; i++ {
switch c := s[i]; {
case shouldEscape(c):
t[j] = '%'
t[j+1] = upperhex[c>>4]
t[j+2] = upperhex[c&15]
j += 3
default:
t[j] = s[i]
j++
}
}
return string(t)
}