From 2c55514179ad79cfa8b06f5487589fa79f102378 Mon Sep 17 00:00:00 2001 From: Cuihtlauac Alvarado Date: Mon, 20 Feb 2023 15:39:51 +0100 Subject: [PATCH] Drop extra slashes (#866) * Drop extra slashes Handle the request unchanged if the target seems canonical, otherwise: * Turns repeated slashes into a single one * Drop the last slash * Redirect to the resulting target Note: Dream.split_targer bugged? When passed a string beginning with exactly two slashes, split_target drops everything before the first slash. See: https://github.com/aantron/dream/issues/248 As a work around, three slashes are preprended to all targets * Formatting --------- Co-authored-by: Cuihtlauac ALVARADO --- src/ocamlorg_web/lib/middleware.ml | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/ocamlorg_web/lib/middleware.ml b/src/ocamlorg_web/lib/middleware.ml index ddca715620..c618590586 100644 --- a/src/ocamlorg_web/lib/middleware.ml +++ b/src/ocamlorg_web/lib/middleware.ml @@ -1,11 +1,13 @@ let no_trailing_slash next_handler request = - let target = Dream.target request in - match target with - | "/" -> next_handler request - | _ -> - if String.ends_with ~suffix:"/" target then - Dream.redirect request (String.sub target 0 (String.length target - 1)) - else next_handler request + let target = "///" ^ Dream.target request in + (* FIXME: https://github.com/aantron/dream/issues/248 *) + let path, query = target |> Dream.split_target in + let path = + path |> Dream.from_path |> Dream.drop_trailing_slash |> Dream.to_path + in + let target = path ^ if query = "" then "" else "?" ^ query in + if Dream.target request = target then next_handler request + else Dream.redirect request target let head handler request = match Dream.method_ request with