Skip to content

Commit

Permalink
Drop extra slashes (#866)
Browse files Browse the repository at this point in the history
* 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: aantron/dream#248
As a work around, three slashes are preprended to all targets

* Formatting

---------

Co-authored-by: Cuihtlauac ALVARADO <[email protected]>
  • Loading branch information
cuihtlauac and Cuihtlauac ALVARADO authored Feb 20, 2023
1 parent 9b0d5b3 commit 2c55514
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/ocamlorg_web/lib/middleware.ml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 2c55514

Please sign in to comment.