Skip to content

Commit

Permalink
fix percent encoding for < 0x10 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Dec 25, 2024
1 parent a86eac8 commit 09ff4f9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/core/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ let percent_encode ?(skip = fun _ -> false) s =
| c when skip c -> Buffer.add_char buf c
| ( ' ' | '!' | '"' | '#' | '$' | '%' | '&' | '\'' | '(' | ')' | '*' | '+'
| ',' | '/' | ':' | ';' | '=' | '?' | '@' | '[' | ']' | '~' ) as c ->
Printf.bprintf buf "%%%X" (Char.code c)
Printf.bprintf buf "%%%02X" (Char.code c)
| c when Char.code c < 32 || Char.code c > 127 ->
Printf.bprintf buf "%%%X" (Char.code c)
Printf.bprintf buf "%%%02X" (Char.code c)
| c -> Buffer.add_char buf c)
s;
Buffer.contents buf
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/t_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module U = Util

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

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

let () =
add_qcheck
@@ QCheck.Test.make ~count:1_000 ~long_factor:20 Q.string (fun s ->
@@ QCheck.Test.make ~name:__LOC__ ~count:1_000 ~long_factor:20 Q.string
(fun s ->
String.iter (fun c -> Q.assume @@ is_ascii_char c) s;
match U.percent_decode (U.percent_encode s) with
| Some s' -> s = s'
| None -> Q.Test.fail_report "invalid percent encoding")
| None ->
Q.Test.fail_reportf
"invalid percent encoding of %S (encoding is %S, fails to decode)"
s (U.percent_encode s))

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

let () =
add_qcheck
@@ QCheck.Test.make ~long_factor:20 ~count:1_000
@@ QCheck.Test.make ~name:__LOC__ ~long_factor:20 ~count:1_000
Q.(small_list (pair string string))
(fun l ->
List.iter
Expand Down

0 comments on commit 09ff4f9

Please sign in to comment.