Skip to content

Commit

Permalink
fix(webserver): resolve_file should encode path into percent encoded … (
Browse files Browse the repository at this point in the history
#2079)

* fix(webserver): resolve_file should encode path into percent encoded string

* update

* update

* update
  • Loading branch information
wsxiaoys authored May 9, 2024
1 parent d86d413 commit d7c6c6d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions ee/tabby-webserver/src/routes/repositories/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ impl ResolveState {

/// Resolve a file
pub async fn resolve_file(&self, root: PathBuf, repo: &ResolveParams) -> Result<Response> {
let uri = if !repo.path_str().starts_with('/') {
let path = format!("/{}", repo.path_str());
Uri::from_str(path.as_str())?
let path = if !repo.path_str().starts_with('/') {
format!("/{}", repo.path_str())
} else {
Uri::from_str(repo.path_str())?
repo.path_str().to_string()
};

let encoded_path = encode_path(&path)?;
let uri = Uri::from_str(&encoded_path)?;

let req = Request::builder().uri(uri).body(Body::empty())?;
let resp = ServeDir::new(root).oneshot(req).await?;

Expand All @@ -132,3 +134,21 @@ impl ResolveState {
Some(repository.dir)
}
}

fn encode_path(path: &str) -> Result<String> {
let url = url::Url::from_file_path(path).map_err(|_| anyhow::anyhow!("Invalid path"))?;
Ok(url.path().to_string())
}

#[cfg(test)]
mod tests {
use super::encode_path;

#[test]
fn test_encode_path() {
assert_eq!(
encode_path("/foo/c dbar/a & c").unwrap(),
"/foo/c%20dbar/a%20&%20c"
);
}
}

0 comments on commit d7c6c6d

Please sign in to comment.