-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodule.ae
More file actions
307 lines (290 loc) · 9.4 KB
/
Copy pathmodule.ae
File metadata and controls
307 lines (290 loc) · 9.4 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
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
// std.url — RFC 3986 percent-encoding + query-string parsing (#629).
//
// Three encoding flavors because RFC 3986 keeps different reserved
// sets for different URL components:
//
// url.encode(s) — query-component (Go: url.QueryEscape):
// unreserved kept, space → '+', everything
// else %-encoded.
// url.encode_path(s) — path-segment (Go: url.PathEscape):
// unreserved kept, plus $&+:=@, everything
// else %-encoded INCLUDING '+' (which is
// NOT a space in the path component).
// url.encode_strict(s) — strict RFC 3986 unreserved-only: encodes
// everything that isn't [A-Za-z0-9-._~].
// Used for SigV4 canonical-request signing
// where any deviation breaks the signature.
//
// url.decode(s) reverses any of the above; '+' is decoded as space
// (the query-component reading; SigV4-style consumers shouldn't see
// '+' in their canonical-encoded input anyway).
//
// url.parse_query(s) walks a `?`-stripped query string ("a=1&b=2&a=3")
// and returns a list of (key, value) tuples — supports repeated keys
// (the form `?a=1&a=2` carries both values; HTTP form spec, JSON
// API conventions). Closes the parser hole #625 surfaced.
import std.string
import std.bytes
import std.collections
exports(
encode, encode_path, encode_strict, decode,
parse_query, query_get, query_get_all
)
// ---------- encoders ----------
fn hex_char(nibble: int) -> int {
if nibble < 10 {
return 48 + nibble
}
return 55 + nibble // 'A' + (nibble - 10)
}
// True for [A-Za-z0-9-._~] — the RFC 3986 "unreserved" set.
fn is_unreserved(c: int) -> int {
if c >= 65 && c <= 90 { return 1 }
if c >= 97 && c <= 122 { return 1 }
if c >= 48 && c <= 57 { return 1 }
if c == 45 || c == 46 || c == 95 || c == 126 { return 1 }
return 0
}
// True for the path-segment "kept" set (Go's url.PathEscape):
// unreserved + $&+:=@.
fn is_path_kept(c: int) -> int {
if is_unreserved(c) == 1 { return 1 }
if c == 36 || c == 38 || c == 43 || c == 58 || c == 61 || c == 64 { return 1 }
return 0
}
// Lower-level encoder. mode:
// 0 = query (unreserved kept, space → '+', else %-encoded)
// 1 = path (path-kept set, '+' encoded)
// 2 = strict (unreserved only)
fn encode_mode(s: string, mode: int) -> string {
if s == 0 {
return ""
}
n = string_length(s)
// Worst case 3x growth (every byte %XX) plus headroom.
buf = bytes.new(n * 3 + 1)
out = 0
i = 0
while i < n {
c = string.char_at_n(s, n, i) & 255
keep = 0
if mode == 0 {
if is_unreserved(c) == 1 { keep = 1 }
}
if mode == 1 {
if is_path_kept(c) == 1 { keep = 1 }
}
if mode == 2 {
if is_unreserved(c) == 1 { keep = 1 }
}
if keep == 1 {
bytes.set(buf, out, c)
out = out + 1
} else if mode == 0 && c == 32 {
// Query-mode shortcut: space → '+'.
bytes.set(buf, out, 43)
out = out + 1
} else {
bytes.set(buf, out, 37) // '%'
bytes.set(buf, out + 1, hex_char((c >> 4) & 15))
bytes.set(buf, out + 2, hex_char(c & 15))
out = out + 3
}
i = i + 1
}
return bytes.finish(buf, out)
}
// RFC 3986 query-component encoding (Go's url.QueryEscape).
// Space encoded as '+'.
encode(s: string) -> string {
return encode_mode(s, 0)
}
// RFC 3986 path-segment encoding (Go's url.PathEscape).
// Keeps unreserved plus $&+:=@; '+' is preserved literally (it is
// NOT a space in path-context).
encode_path(s: string) -> string {
return encode_mode(s, 1)
}
// Strict unreserved-only encoding — encodes everything except
// [A-Za-z0-9-._~]. Required for AWS SigV4 canonical-request signing
// where any kept-character deviation invalidates the signature.
encode_strict(s: string) -> string {
return encode_mode(s, 2)
}
// ---------- decoder ----------
fn hex_value(c: int) -> int {
if c >= 48 && c <= 57 { return c - 48 }
if c >= 65 && c <= 70 { return c - 55 }
if c >= 97 && c <= 102 { return c - 87 }
return -1
}
// Percent-decode a URL component. '+' is decoded as space (the
// query-component reading); literal '+' in path components should
// have been encoded as %2B by a conforming encoder.
//
// Returns ("", "malformed encoding") on a stray '%' not followed by
// two hex chars. Otherwise returns (decoded, "").
decode(s: string) -> {
if s == 0 {
return "", ""
}
n = string_length(s)
buf = bytes.new(n + 1)
out = 0
i = 0
while i < n {
c = string.char_at_n(s, n, i) & 255
if c == 37 {
// %XX
if i + 2 >= n {
bytes.free(buf)
return "", "malformed encoding"
}
h1 = hex_value(string.char_at_n(s, n, i + 1) & 255)
h2 = hex_value(string.char_at_n(s, n, i + 2) & 255)
if h1 < 0 || h2 < 0 {
bytes.free(buf)
return "", "malformed encoding"
}
bytes.set(buf, out, (h1 << 4) | h2)
out = out + 1
i = i + 3
} else if c == 43 {
bytes.set(buf, out, 32)
out = out + 1
i = i + 1
} else {
bytes.set(buf, out, c)
out = out + 1
i = i + 1
}
}
return bytes.finish(buf, out), ""
}
// ---------- query-string parser ----------
//
// parse_query takes a `?`-stripped query string and returns a
// std.collections.string_list of "key=value" entries (decoded). The
// list is the underlying storage; query_get / query_get_all operate
// on it. This decoupling lets callers iterate manually too.
// Parse a query string. Returns (list, ""), where list is a
// std.collections.string_list of decoded "key=value" entries.
// Returns ("", error) on a malformed percent-encoding inside any
// key or value.
//
// Leading '?' is tolerated (stripped before parsing) so callers can
// pass the raw `request.query_string` or a string they hand-built
// with `?` retained.
parse_query(s: string) -> {
list = string_list_new()
if s == 0 {
return list, ""
}
n = string_length(s)
if n == 0 {
return list, ""
}
start = 0
// Strip a leading '?'.
if string.char_at_n(s, n, 0) == 63 {
start = 1
}
i = start
while i <= n {
end = i
// Walk to next '&' or end-of-string.
while end < n {
if (string.char_at_n(s, n, end) & 255) == 38 {
break
}
end = end + 1
}
if end > i {
// Find the '=' inside [i, end).
eq = i
while eq < end {
if (string.char_at_n(s, n, eq) & 255) == 61 {
break
}
eq = eq + 1
}
// Decode key and value separately.
key_raw = string.substring_n(s, n, i, eq)
val_raw = ""
if eq < end {
val_raw = string.substring_n(s, n, eq + 1, end)
}
key, k_err = decode(key_raw)
if k_err != "" {
return list, k_err
}
val, v_err = decode(val_raw)
if v_err != "" {
return list, v_err
}
entry = string.concat(key, "=")
entry = string.concat(entry, val)
string_list_add(list, entry)
}
i = end + 1
}
return list, ""
}
// Get the first value for `name` from a parsed query list. Returns
// "" if the key is absent.
query_get(list: ptr, name: string) -> string {
n = string_list_size(list)
name_eq = string.concat(name, "=")
name_eq_len = string_length(name_eq)
i = 0
while i < n {
entry = string_list_get(list, i)
entry_len = string_length(entry)
if entry_len >= name_eq_len {
// Does entry start with name_eq?
ok = 1
j = 0
while j < name_eq_len {
if string.char_at_n(entry, entry_len, j) != string.char_at_n(name_eq, name_eq_len, j) {
ok = 0
break
}
j = j + 1
}
if ok == 1 {
return string.substring_n(entry, entry_len, name_eq_len, entry_len)
}
}
i = i + 1
}
return ""
}
// Get every value for `name` (for repeated-key queries like
// `?a=1&a=2`). Returns a fresh string_list. Empty list if absent.
query_get_all(list: ptr, name: string) -> ptr {
out = string_list_new()
n = string_list_size(list)
name_eq = string.concat(name, "=")
name_eq_len = string_length(name_eq)
i = 0
while i < n {
entry = string_list_get(list, i)
entry_len = string_length(entry)
if entry_len >= name_eq_len {
ok = 1
j = 0
while j < name_eq_len {
if string.char_at_n(entry, entry_len, j) != string.char_at_n(name_eq, name_eq_len, j) {
ok = 0
break
}
j = j + 1
}
if ok == 1 {
string_list_add(out, string.substring_n(entry, entry_len, name_eq_len, entry_len))
}
}
i = i + 1
}
return out
}