Skip to main content

for_uri_path

Function for_uri_path 

Source
pub fn for_uri_path(input: &str) -> String
Expand description

percent-encodes input for safe use as a URI path.

this encoder preserves forward-slash (/) separators while encoding each path segment. only unreserved characters per RFC 3986 and / pass through unencoded: A-Z, a-z, 0-9, -, ., _, ~, /. everything else is encoded as percent-encoded UTF-8 bytes.

use this when you need to encode a full URI path from untrusted input. for individual path segments or query parameters, use for_uri_component instead (which also encodes /).

§security notes

  • this encoder does not normalize . or .. segments. callers must validate and normalize paths separately to prevent path traversal.
  • multiple consecutive slashes are preserved as-is.

§examples

use contextual_encoder::for_uri_path;

assert_eq!(for_uri_path("/users/café/profile"), "/users/caf%C3%A9/profile");
assert_eq!(for_uri_path("/a b/c&d"), "/a%20b/c%26d");
assert_eq!(for_uri_path("/safe-text_v2.0/~user"), "/safe-text_v2.0/~user");
assert_eq!(for_uri_path("/path/segment"), "/path/segment");