Skip to content

Commit

Permalink
Merge pull request #176 from last-genius/add-path-unencoded
Browse files Browse the repository at this point in the history
Add path_unencoded function
  • Loading branch information
dinosaure committed Jul 22, 2024
2 parents 60fce85 + 89e0b11 commit 8d6da91
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/uri.ml
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ module Path = struct
) "" buf p;
Pct.cast_encoded (Buffer.contents buf)

let decoded_of_path p =
let len = List.fold_left (fun c tok -> String.length tok + c) 0 p in
let buf = Buffer.create len in
iter_concat (fun buf s -> Buffer.add_string buf s) "" buf p;
Pct.cast_decoded (Buffer.contents buf)

(* Subroutine for resolve <http://tools.ietf.org/html/rfc3986#section-5.2.3> *)
let merge bhost bpath relpath =
match bhost, List.rev bpath with
Expand All @@ -448,6 +454,7 @@ end

let path_of_encoded = Path.path_of_encoded
let encoded_of_path ?scheme ~component = Path.encoded_of_path ?scheme ~component
let decoded_of_path = Path.decoded_of_path

(* Query string handling, to and from an assoc list of key/values *)
module Query = struct
Expand Down Expand Up @@ -754,6 +761,8 @@ let with_path uri path =
| None, _ | Some _, "/"::_ | Some _, [] -> { uri with path=path }
| Some _, _ -> { uri with path="/"::path }

let path_unencoded uri = Pct.uncast_decoded (decoded_of_path uri.path)

let fragment uri = get_decoded_opt uri.fragment
let with_fragment uri =
function
Expand Down
3 changes: 3 additions & 0 deletions lib/uri.mli
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ val path : ?pct_encoder:pct_encoder -> t -> string
(** Get the encoded path and query components of a URI *)
val path_and_query : t -> string

(** Get the unencoded path component of a URI *)
val path_unencoded : t -> string

(** Replace the path URI with the supplied encoded path.
If a host is present in the supplied URI, the path is made absolute but not
resolved. If the path is empty, the path component is removed.
Expand Down

0 comments on commit 8d6da91

Please sign in to comment.