Module Http.Path

Paths.

Paths

type t = path

The type for absolute URI paths represented as non-empty lists of percent-decoded path segments. The empty list denotes the absence of a path.

Path segments can be empty "". The root path / is represented by the list [""] and /a by ["a"], see more examples here.

WARNING. You should never concatenate these segments with a separator to get a file path because they may contain stray percent-decoded directory separators. Use the function Path.to_absolute_filepath to interpret paths as file paths.

val undot_and_compress : path -> path

undot_and_compress p removes "." and ".." according to the RFC 3986 algorithm and suppresses non-final empty "" segments.

val strip_prefix : prefix:path -> path -> path option

strip_prefix ~prefix p removes prefix prefix from p. If prefix is not a strict prefix of p or if the result is the empty list this is None. In particular when prefix = p this is None. A few examples:

  • strip_prefix [] [] = None
  • strip_prefix [] (a :: _ as l) = Some l
  • strip_prefix [""; "a"] [] = None
  • strip_prefix [""; "a"] [""; "a"] = None
  • strip_prefix [""; "a"] [""; "a"; ""] = Some [""]
  • strip_prefix [""; "a"; ""] [""; "a"] = None
  • strip_prefix [""; "a"; ""] [""; "a"; ""] = None
  • strip_prefix [""; "a"; ""] [""; "a"; "b"] = None
val concat : path -> path -> path

concat p0 p1 concatenates p0 and p1. This drops a potential last empty segment from p0.

File paths

val has_dir_seps : string -> bool

has_dir_seps s is true iff s contains a '/' or a '\\' character.

val to_absolute_filepath : path -> (fpath, string) Stdlib.result

to_absolute_filepath p is an absolute file path for undot_and_compress p. Errors if any of the path segments contains a stray slash or backslash or if p is the empty list. The result always uses / as a directory separator regardless of the platform and is guaranteed to be free of any . or .. segments.

val prefix_filepath : prefix:fpath -> fpath -> fpath

prefix_filepath ~prefix p prefixes p by prefix avoiding introducing empty segments. This function assumes / is the directory separator regardless of the platform.

val filepath_ext : fpath -> string

filepath_ext p is the file extension of file path p. This function assumes / is the directory separator regardless of the platform.

Converting

val encode : path -> string

encode p encodes an absolute-path for p as follows:

  1. In each segment percent-encode any byte that is not unreserved, sub-delims, ':' or '@' to produce a valid URI segment.
  2. Prepends each segment with a '/'.
  3. Concatenate the result.

The empty list is special cased and yields "". This is for encoding HTTP paths, use to_undotted_filepath to convert paths to file paths.

Here are a few examples:

  • encode [] = ""
  • encode [""] = "/"
  • encode [""; ""] = "//"
  • encode ["a";"b";"c"] = "/a/b/c"
  • encode ["a";"b";"";"c";] = "/a/b//c"
  • encode ["a";"b";"c";""] = "/a/b/c/"
  • encode ["a";"b";"c";" "] = "/a/b/c/%20"
  • encode ["a";"b";"c";"";""] = "/a/b/c//"
  • encode ["a"; "b/"; "c"] = "/a/b%2F/c"
  • encode ["r\xC3\xC9volte"] = "/r%C3%C9volte"
  • encode ["a"; "not%20"; "b"] = "/a/not%2520/b"
val decode : string -> (path, string) Stdlib.result

decode s decodes an absolute-path to its percent-decoded list of segments. By definition of absolute-path the list of segments is never empty.

Here are a few examples:

  • decode "/" = Ok [""]
  • decode "//" = Ok ["";""]
  • decode "/a/b/c" = Ok ["a";"b";"c"]
  • decode "/a/b//c" = Ok ["a";"b";"";"c"]
  • decode "/a/b/c/" = Ok ["a";"b";"c";""]
  • decode "/a/b/c/%20" = Ok ["a";"b";"c";" "]
  • decode "/a/b//c//" = Ok ["a";"b";"";"c";"";""]
  • decode "/a/b%2F/c" = Ok ["a"; "b/"; "c"]
  • decode "/r%C3%C9volte" = Ok ["r\xC3\xC9volte"]
  • decode "/a/not%2520/b" = Ok ["a"; "not%20"; "b"]
  • decode "" = Error _
  • decode "a/b/c" = Error _
val pp : Stdlib.Format.formatter -> path -> unit

pp is an unspecified formatter for paths.