Skip to content

Commit

Permalink
fix(services/webdav): decode path before set Entry (#3020)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-XD authored Sep 7, 2023
1 parent 098ef52 commit d68078d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/src/raw/http_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub use header::parse_last_modified;
pub use header::parse_location;

mod uri;
pub use uri::percent_decode_path;
pub use uri::percent_encode_path;

mod error;
Expand Down
50 changes: 50 additions & 0 deletions core/src/raw/http_util/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use percent_encoding::percent_decode_str;
use percent_encoding::utf8_percent_encode;
use percent_encoding::AsciiSet;
use percent_encoding::NON_ALPHANUMERIC;
Expand Down Expand Up @@ -47,6 +48,16 @@ pub fn percent_encode_path(path: &str) -> String {
utf8_percent_encode(path, &PATH_ENCODE_SET).to_string()
}

/// percent_decode_path will do percent decoding for http decode path.
///
/// If the input is not percent encoded or not valid utf8, return the input.
pub fn percent_decode_path(path: &str) -> String {
match percent_decode_str(path).decode_utf8() {
Ok(v) => v.to_string(),
Err(_) => path.to_string(),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -79,4 +90,43 @@ mod tests {
assert_eq!(actual, expected, "{name}");
}
}

#[test]
fn test_percent_decode_path() {
let cases = vec![
(
"Reserved Characters",
"%3B%2C/%3F%3A%40%26%3D%2B%24",
";,/?:@&=+$",
),
("Unescaped Characters", "-_.!~*'()", "-_.!~*'()"),
("Number Sign", "%23", "#"),
(
"Alphanumeric Characters + Space",
"ABC%20abc%20123",
"ABC abc 123",
),
(
"Unicode Characters",
"%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%EF%BC%81%E2%9D%A4",
"你好,世界!❤",
),
(
"Double Encoded Characters",
"Double%2520Encoded",
"Double%20Encoded",
),
(
"Not Percent Encoded Characters",
"/not percent encoded/path;,/?:@&=+$-",
"/not percent encoded/path;,/?:@&=+$-",
),
];

for (name, input, expected) in cases {
let actual = percent_decode_path(input);

assert_eq!(actual, expected, "{name}");
}
}
}
6 changes: 4 additions & 2 deletions core/src/services/webdav/pager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ impl oio::Page for WebdavPager {
}

let normalized_path = build_rel_path(&self.root, path);
if normalized_path == self.path {
let decoded_path = percent_decode_path(normalized_path.as_str());

if normalized_path == self.path || decoded_path == self.path {
// WebDav server may return the current path as an entry.
continue;
}

let meta = res.parse_into_metadata()?;
entries.push(oio::Entry::new(&normalized_path, meta))
entries.push(oio::Entry::new(&decoded_path, meta))
}

Ok(Some(entries))
Expand Down

0 comments on commit d68078d

Please sign in to comment.