diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2023-10-06 14:46:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-06 14:46:43 +0100 |
commit | 677a591e56251821051412ca13beae5d6ff29f69 (patch) | |
tree | 1c131bd627675459be8f9beb9736230f8c78577e | |
parent | f0608a5b9173fbcab4077e0f6a8da2b8c98685dd (diff) |
fix(lsp): percent-encode host in deno: specifiers (#20811)
-rw-r--r-- | cli/lsp/urls.rs | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/cli/lsp/urls.rs b/cli/lsp/urls.rs index 8b16292e9..f5c170277 100644 --- a/cli/lsp/urls.rs +++ b/cli/lsp/urls.rs @@ -171,16 +171,16 @@ impl LspUrlMap { extension ) } else { - let mut path = - specifier[..Position::BeforePath].replacen("://", "/", 1); - let parts: Vec<String> = specifier[Position::BeforePath..] - .split('/') - .map(|p| { - percent_encoding::utf8_percent_encode(p, COMPONENT).to_string() - }) - .collect(); - path.push_str(&parts.join("/")); - format!("deno:/{path}") + let mut str = String::with_capacity(specifier.as_str().len() + 6); + str.push_str("deno:/"); + str.push_str(specifier.scheme()); + for p in specifier[Position::BeforeHost..].split('/') { + str.push('/'); + str.push_str( + &percent_encoding::utf8_percent_encode(p, COMPONENT).to_string(), + ); + } + str }; let url = LspClientUrl(Url::parse(&specifier_str)?); inner.put(specifier.clone(), url.clone()); @@ -292,6 +292,22 @@ mod tests { assert_eq!(actual_specifier, fixture); } + #[test] + fn test_lsp_url_map_host_with_port() { + let map = LspUrlMap::default(); + let fixture = resolve_url("http://localhost:8000/mod.ts").unwrap(); + let actual_url = map + .normalize_specifier(&fixture) + .expect("could not handle specifier"); + let expected_url = + Url::parse("deno:/http/localhost%3A8000/mod.ts").unwrap(); + assert_eq!(actual_url.as_url(), &expected_url); + + let actual_specifier = + map.normalize_url(actual_url.as_url(), LspUrlKind::File); + assert_eq!(actual_specifier, fixture); + } + #[cfg(windows)] #[test] fn test_normalize_windows_path() { |