
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | // std/http query/form parsing (Phase 5): the runtime parser (http.form, |
| 2 | // tl_http_parse_query) against a reference written with string.split, |
| 3 | // string.join and http.url_decode — what http.form was — over fixed |
| 4 | // tricky cases, and a round-trip of generated pairs. |
| 5 | |
| 6 | import std/bytes |
| 7 | import std/http |
| 8 | import std/int |
| 9 | import std/io |
| 10 | import std/list |
| 11 | import std/string |
| 12 | |
| 13 | // The recipe http.form replaced. |
| 14 | fn reference :: body: String -> List<(String, String)> |
| 15 | { |
| 16 | use pair <- list.map (list.filter (string.split body "&") fn p { p != "" }) |
| 17 | // Only the first `=` separates the name from the value. |
| 18 | match string.split pair "=" |
| 19 | { [k] -> (http.url_decode k, "") |
| 20 | , [k, ..rest] -> (http.url_decode k, http.url_decode (string.join rest "=")) |
| 21 | , [] -> ("", "") |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn check :: body: String -> () / <io> |
| 26 | { |
| 27 | let parsed = http.form body |
| 28 | io.println (show (parsed == reference body) <> " " <> show parsed) |
| 29 | } |
| 30 | |
| 31 | fn hex_digit :: d: Int -> String |
| 32 | { |
| 33 | match d |
| 34 | { 10 -> "A" |
| 35 | , 11 -> "B" |
| 36 | , 12 -> "C" |
| 37 | , 13 -> "D" |
| 38 | , 14 -> "E" |
| 39 | , 15 -> "F" |
| 40 | , _ -> show d |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Every byte percent-encoded (uppercase hex): the parser must give the |
| 45 | // fragment back unchanged. |
| 46 | fn encode :: s: String -> String |
| 47 | { |
| 48 | string.concat |
| 49 | ( list.map |
| 50 | (bytes.to_list (bytes.from_string s)) |
| 51 | fn b |
| 52 | { |
| 53 | "%" <> hex_digit (int.shift_right b 4) <> hex_digit (int.and b 15) |
| 54 | } |
| 55 | ) |
| 56 | } |
| 57 | |
| 58 | pub fn main :: -> () / <io> |
| 59 | { |
| 60 | // Fixed edge cases: empty pairs, `=` in values, a missing `=`, lone |
| 61 | // and invalid `%`, `+` at the edges, empty input. |
| 62 | list.each |
| 63 | [ "" |
| 64 | , "&" |
| 65 | , "&&" |
| 66 | , "a" |
| 67 | , "a=" |
| 68 | , "=b" |
| 69 | , "=" |
| 70 | , "==" |
| 71 | , "a=b=c" |
| 72 | , "a&&b" |
| 73 | , "a=1&b=two+words&bare&c=x%3Dy" |
| 74 | , "%" |
| 75 | , "%4" |
| 76 | , "%41" |
| 77 | , "%zz" |
| 78 | , "%4z" |
| 79 | , "+" |
| 80 | , "a+b" |
| 81 | , "+a+" |
| 82 | , "%ff" |
| 83 | , "%c3%a9" |
| 84 | , "é=x" |
| 85 | , "%e2%82%ac" |
| 86 | , "a=%ff%fe" |
| 87 | , "&&&" |
| 88 | , "=&=" |
| 89 | , "k=+%20" |
| 90 | , "%2f=%2F" |
| 91 | , "a=b&" |
| 92 | , "&a=b" |
| 93 | , "%%41" |
| 94 | , "%4%41" |
| 95 | , "a%26b=c%3Dd" |
| 96 | ] |
| 97 | check |
| 98 | // The product of tricky fragments, encoded and parsed back. |
| 99 | let fragments = |
| 100 | [ "" |
| 101 | , "a" |
| 102 | , "A Z" |
| 103 | , "a+b" |
| 104 | , "100%" |
| 105 | , "%41" |
| 106 | , "=" |
| 107 | , "&" |
| 108 | , "x=y" |
| 109 | , "é" |
| 110 | , "☃" |
| 111 | , "a/b?c" |
| 112 | , " " |
| 113 | , "+" |
| 114 | , "%" |
| 115 | , "\t" |
| 116 | , "ünïcodé" |
| 117 | ] |
| 118 | let expected = |
| 119 | list.flat_map fragments fn n { list.map fragments fn v { (n, v) } } |
| 120 | let encoded = |
| 121 | string.join (list.map expected fn p { encode p.0 <> "=" <> encode p.1 }) "&" |
| 122 | let parsed = http.form encoded |
| 123 | io.println (show (parsed == expected)) |
| 124 | io.println (show (parsed == reference encoded)) |
| 125 | io.println (show (list.length parsed)) |
| 126 | } |
| 127 | //> True [] |
| 128 | //> True [] |
| 129 | //> True [] |
| 130 | //> True [(a, )] |
| 131 | //> True [(a, )] |
| 132 | //> True [(, b)] |
| 133 | //> True [(, )] |
| 134 | //> True [(, =)] |
| 135 | //> True [(a, b=c)] |
| 136 | //> True [(a, ), (b, )] |
| 137 | //> True [(a, 1), (b, two words), (bare, ), (c, x=y)] |
| 138 | //> True [(%, )] |
| 139 | //> True [(%4, )] |
| 140 | //> True [(A, )] |
| 141 | //> True [(%zz, )] |
| 142 | //> True [(%4z, )] |
| 143 | //> True [( , )] |
| 144 | //> True [(a b, )] |
| 145 | //> True [( a , )] |
| 146 | //> True [(�, )] |
| 147 | //> True [(é, )] |
| 148 | //> True [(é, x)] |
| 149 | //> True [(€, )] |
| 150 | //> True [(a, ��)] |
| 151 | //> True [] |
| 152 | //> True [(, ), (, )] |
| 153 | //> True [(k, )] |
| 154 | //> True [(/, /)] |
| 155 | //> True [(a, b)] |
| 156 | //> True [(a, b)] |
| 157 | //> True [(%A, )] |
| 158 | //> True [(%4A, )] |
| 159 | //> True [(a&b, c=d)] |
| 160 | //> True |
| 161 | //> True |
| 162 | //> 289 |