Skip to content

Commit 09ff4f9

Browse files
committed
fix percent encoding for < 0x10 chars
1 parent a86eac8 commit 09ff4f9

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/core/util.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ let percent_encode ?(skip = fun _ -> false) s =
55
| c when skip c -> Buffer.add_char buf c
66
| ( ' ' | '!' | '"' | '#' | '$' | '%' | '&' | '\'' | '(' | ')' | '*' | '+'
77
| ',' | '/' | ':' | ';' | '=' | '?' | '@' | '[' | ']' | '~' ) as c ->
8-
Printf.bprintf buf "%%%X" (Char.code c)
8+
Printf.bprintf buf "%%%02X" (Char.code c)
99
| c when Char.code c < 32 || Char.code c > 127 ->
10-
Printf.bprintf buf "%%%X" (Char.code c)
10+
Printf.bprintf buf "%%%02X" (Char.code c)
1111
| c -> Buffer.add_char buf c)
1212
s;
1313
Buffer.contents buf

tests/unit/t_util.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module U = Util
44

55
let () = assert_eq "hello%20world" (U.percent_encode "hello world")
66
let () = assert_eq "%23%25^%24%40^%40" (U.percent_encode "#%^$@^@")
7+
let () = assert_eq "%0F" (U.percent_encode "\015")
78

89
let () =
910
assert_eq "a%20ohm%2B5235%25%26%40%23%20---%20_"
@@ -13,11 +14,15 @@ let () = assert_eq (Some "?") (U.percent_decode @@ U.percent_encode "?")
1314

1415
let () =
1516
add_qcheck
16-
@@ QCheck.Test.make ~count:1_000 ~long_factor:20 Q.string (fun s ->
17+
@@ QCheck.Test.make ~name:__LOC__ ~count:1_000 ~long_factor:20 Q.string
18+
(fun s ->
1719
String.iter (fun c -> Q.assume @@ is_ascii_char c) s;
1820
match U.percent_decode (U.percent_encode s) with
1921
| Some s' -> s = s'
20-
| None -> Q.Test.fail_report "invalid percent encoding")
22+
| None ->
23+
Q.Test.fail_reportf
24+
"invalid percent encoding of %S (encoding is %S, fails to decode)"
25+
s (U.percent_encode s))
2126

2227
let () = assert_eq [ "a"; "b" ] (U.split_on_slash "/a/b")
2328
let () = assert_eq [ "coucou"; "lol" ] (U.split_on_slash "/coucou/lol")
@@ -34,7 +39,7 @@ let () = assert_eq (Ok [ "foo", "bar" ]) (U.parse_query "yolo#foo=bar")
3439

3540
let () =
3641
add_qcheck
37-
@@ QCheck.Test.make ~long_factor:20 ~count:1_000
42+
@@ QCheck.Test.make ~name:__LOC__ ~long_factor:20 ~count:1_000
3843
Q.(small_list (pair string string))
3944
(fun l ->
4045
List.iter

0 commit comments

Comments
 (0)