Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Add support for handling remote file URIs. #629

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkl-core/src/main/java/org/pkl/core/util/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ private IoUtils() {}

public static URL toUrl(URI uri) throws IOException {
try {
if ("file".equalsIgnoreCase(uri.getScheme()) && uri.getHost() != null) {
throw new UnsupportedOperationException("Remote file URIs are not supported: " + uri);
}
return uri.toURL();
} catch (Error e) {
// best we can do for now
// rely on caller to provide context, e.g., the requested module URI
if (e.getClass().getName().equals("com.oracle.svm.core.jdk.UnsupportedFeatureError")) {
throw new IOException("Unsupported protocol: " + uri.getScheme());
}

throw e;
}
}
Expand Down Expand Up @@ -140,6 +142,10 @@ public static String readString(InputStream inputStream) throws IOException {
}

public static byte[] readBytes(URI uri) throws IOException {
if ("file".equalsIgnoreCase(uri.getScheme()) && uri.getHost() != null) {
throw new UnsupportedOperationException("Remote file URIs are not supported: " + uri);
}

if (HttpUtils.isHttpUrl(uri)) {
throw new IllegalArgumentException("Should use HTTP client to GET " + uri);
}
Expand Down
7 changes: 7 additions & 0 deletions pkl-core/src/test/kotlin/org/pkl/core/util/IoUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ class IoUtilsTest {
assertThrows<IllegalArgumentException> { IoUtils.readString(URI("http://example.com").toURL()) }
}

@Test
fun `toUrl() throws UnsupportedOperationException for file URIs with remote hostnames`() {
val uri = URI("file://example.com/bar/baz.pkl")
val exception = assertThrows<UnsupportedOperationException> { IoUtils.toUrl(uri) }
assertThat(exception.message).contains("Remote file URIs are not supported")
}

@Test
fun `encodePath encodes characters reserved on windows`() {
assertThat(IoUtils.encodePath("foo:bar")).isEqualTo("foo(3a)bar")
Expand Down