mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-05 19:00:32 -05:00
fix: improved percent encoding of paths
This commit is contained in:
parent
4aaf77b261
commit
2b95c181b8
3 changed files with 9 additions and 3 deletions
|
|
@ -1,7 +1,8 @@
|
|||
let percent_encode s =
|
||||
let percent_encode ?(skip=fun _->false) s =
|
||||
let buf = Buffer.create (String.length s) in
|
||||
String.iter
|
||||
(function
|
||||
| c when skip c -> Buffer.add_char buf c
|
||||
| ' ' -> Buffer.add_string buf "%20"
|
||||
| '!' -> Buffer.add_string buf "%21"
|
||||
| '"' -> Buffer.add_string buf "%22"
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@
|
|||
@since NEXT_RELEASE
|
||||
*)
|
||||
|
||||
val percent_encode : string -> string
|
||||
val percent_encode : ?skip:(char -> bool) -> string -> string
|
||||
(** Encode the string into a valid path following
|
||||
https://tools.ietf.org/html/rfc3986#section-2.1
|
||||
@param skip if provided, allows to preserve some characters, e.g. '/' in a path.
|
||||
*)
|
||||
|
||||
val percent_decode : string -> string option
|
||||
|
|
|
|||
|
|
@ -39,10 +39,14 @@ let human_size (x:int) : string =
|
|||
let header_html = "Content-Type", "text/html"
|
||||
let (//) = Filename.concat
|
||||
|
||||
let encode_path s =
|
||||
U.percent_encode ~skip:(fun c -> c='/') s
|
||||
|
||||
let html_list_dir ~top ~parent d : string =
|
||||
let entries = Sys.readdir @@ (top // d) in
|
||||
Array.sort compare entries;
|
||||
let body = Buffer.create 256 in
|
||||
(* TODO: breadcrumbs for the path, each element a link to the given ancestor dir *)
|
||||
Printf.bprintf body {|<head><title> http_of_dir %S</title>
|
||||
</head><body>
|
||||
<h2> Index of %S</h2>
|
||||
|
|
@ -65,7 +69,7 @@ let html_list_dir ~top ~parent d : string =
|
|||
with _ -> ""
|
||||
in
|
||||
Printf.bprintf body " <li> <a href=\"/%s\"> %s </a> %s%s </li>\n"
|
||||
(U.percent_encode (d // f)) f (if Sys.is_directory fpath then "[dir]" else "") size
|
||||
(encode_path (d // f)) f (if Sys.is_directory fpath then "[dir]" else "") size
|
||||
);
|
||||
)
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue