Skip to content

Commit b23dcbe

Browse files
authored
Merge pull request #885 from gifvex/gifvex/uri_from_path
Account for lowercase drive letter on Windows
2 parents 7a6dcf3 + dbd8be4 commit b23dcbe

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/core_ext/uri.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class << self
99
sig { params(path: String, scheme: String).returns(URI::Generic) }
1010
def from_path(path:, scheme: "file")
1111
# On Windows, if the path begins with the disk name, we need to add a leading slash to make it a valid URI
12-
escaped_path = if /^[A-Z]:/.match?(path)
12+
escaped_path = if /^[A-Z]:/i.match?(path)
1313
DEFAULT_PARSER.escape("/#{path}")
1414
else
1515
DEFAULT_PARSER.escape(path)
@@ -26,15 +26,15 @@ def to_standardized_path
2626
parsed_path = path
2727
return unless parsed_path
2828

29+
unescaped_path = CGI.unescape(parsed_path)
30+
2931
# On Windows, when we're getting the file system path back from the URI, we need to remove the leading forward
3032
# slash
31-
actual_path = if %r{^/[A-Z]:}.match?(parsed_path)
32-
parsed_path.delete_prefix("/")
33+
if %r{^/[A-Z]:}i.match?(unescaped_path)
34+
unescaped_path.delete_prefix("/")
3335
else
34-
parsed_path
36+
unescaped_path
3537
end
36-
37-
CGI.unescape(actual_path)
3838
end
3939

4040
sig { returns(String) }

test/requests/support/uri_test.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,29 @@ def test_from_path_on_windows
1515
assert_equal("/C:/some/windows/path/to/file.rb", uri.path)
1616
end
1717

18-
def test_to_path_on_unix
18+
def test_from_path_on_windows_with_lowercase_drive
19+
uri = URI::Generic.from_path(path: "c:/some/windows/path/to/file.rb")
20+
assert_equal("/c:/some/windows/path/to/file.rb", uri.path)
21+
end
22+
23+
def test_to_standardized_path_on_unix
1924
uri = URI::Generic.from_path(path: "/some/unix/path/to/file.rb")
2025
assert_equal(uri.path, uri.to_standardized_path)
2126
end
2227

23-
def test_to_path_on_windows
28+
def test_to_standardized_path_on_windows
2429
uri = URI::Generic.from_path(path: "C:/some/windows/path/to/file.rb")
2530
assert_equal("C:/some/windows/path/to/file.rb", uri.to_standardized_path)
2631
end
32+
33+
def test_to_standardized_path_on_windows_with_lowercase_drive
34+
uri = URI::Generic.from_path(path: "c:/some/windows/path/to/file.rb")
35+
assert_equal("c:/some/windows/path/to/file.rb", uri.to_standardized_path)
36+
end
37+
38+
def test_to_standardized_path_on_windows_with_received_uri
39+
uri = URI("file:///c%3A/some/windows/path/to/file.rb")
40+
assert_equal("c:/some/windows/path/to/file.rb", uri.to_standardized_path)
41+
end
2742
end
2843
end

0 commit comments

Comments
 (0)