Skip to content

Commit 90213d4

Browse files
committed
Fix file path decoding
URLDecoder was being used for path decoding, even though it can't handle RFC2396 encoding. This resulted in plus characters being removed and possibly other weirdness. See https://docs.oracle.com/javase/8/docs/api/java/net/URL.html "The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL(). The URLEncoder and URLDecoder classes can also be used, but only for HTML form encoding, which is not the same as the encoding scheme defined in RFC2396." Fixes processing#4417
1 parent cc44272 commit 90213d4

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

app/src/processing/app/Platform.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.File;
2727
import java.io.FilenameFilter;
2828
import java.io.IOException;
29+
import java.net.URISyntaxException;
30+
import java.net.URL;
2931
import java.util.HashMap;
3032
import java.util.Map;
3133

@@ -281,9 +283,16 @@ static public boolean isLinux() {
281283
static public File getContentFile(String name) {
282284
if (processingRoot == null) {
283285
// Get the path to the .jar file that contains Base.class
284-
String path = Base.class.getProtectionDomain().getCodeSource().getLocation().getPath();
285-
// Path may have URL encoding, so remove it
286-
String decodedPath = PApplet.urlDecode(path);
286+
URL pathURL =
287+
Base.class.getProtectionDomain().getCodeSource().getLocation();
288+
// Decode URL
289+
String decodedPath;
290+
try {
291+
decodedPath = pathURL.toURI().getPath();
292+
} catch (URISyntaxException e) {
293+
e.printStackTrace();
294+
return null;
295+
}
287296

288297
if (decodedPath.contains("/app/bin")) { // This means we're in Eclipse
289298
final File build = new File(decodedPath, "../../build").getAbsoluteFile();
@@ -311,8 +320,7 @@ static public File getContentFile(String name) {
311320
System.err.println("Could not find lib folder via " +
312321
jarFolder.getAbsolutePath() +
313322
", switching to user.dir");
314-
final String userDir = System.getProperty("user.dir");
315-
processingRoot = new File(PApplet.urlDecode(userDir));
323+
processingRoot = new File(""); // resolves to "user.dir"
316324
}
317325
}
318326
}

core/src/processing/core/PApplet.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7389,10 +7389,10 @@ static protected String calcSketchPath() {
73897389
try {
73907390
folder = System.getProperty("user.dir");
73917391

7392-
String jarPath =
7393-
PApplet.class.getProtectionDomain().getCodeSource().getLocation().getPath();
7394-
// The jarPath from above will be URL encoded (%20 for spaces)
7395-
jarPath = urlDecode(jarPath);
7392+
URL jarURL =
7393+
PApplet.class.getProtectionDomain().getCodeSource().getLocation();
7394+
// Decode URL
7395+
String jarPath = jarURL.toURI().getPath();
73967396

73977397
// Workaround for bug in Java for OS X from Oracle (7u51)
73987398
// https://github.com/processing/processing/issues/2181
@@ -7543,12 +7543,17 @@ public File dataFile(String where) {
75437543
File why = new File(where);
75447544
if (why.isAbsolute()) return why;
75457545

7546-
String jarPath =
7547-
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
7546+
URL jarURL = getClass().getProtectionDomain().getCodeSource().getLocation();
7547+
// Decode URL
7548+
String jarPath;
7549+
try {
7550+
jarPath = jarURL.toURI().getPath();
7551+
} catch (URISyntaxException e) {
7552+
e.printStackTrace();
7553+
return null;
7554+
}
75487555
if (jarPath.contains("Contents/Java/")) {
7549-
// The path will be URL encoded (%20 for spaces) coming from above
7550-
// http://code.google.com/p/processing/issues/detail?id=1073
7551-
File containingFolder = new File(urlDecode(jarPath)).getParentFile();
7556+
File containingFolder = new File(jarPath).getParentFile();
75527557
File dataFolder = new File(containingFolder, "data");
75537558
return new File(dataFolder, where);
75547559
}
@@ -7633,6 +7638,11 @@ static public String urlEncode(String str) {
76337638
}
76347639

76357640

7641+
// DO NOT use for file paths, URLDecoder can't handle RFC2396
7642+
// "The recommended way to manage the encoding and decoding of
7643+
// URLs is to use URI, and to convert between these two classes
7644+
// using toURI() and URI.toURL()."
7645+
// https://docs.oracle.com/javase/8/docs/api/java/net/URL.html
76367646
static public String urlDecode(String str) {
76377647
try {
76387648
return URLDecoder.decode(str, "UTF-8");

0 commit comments

Comments
 (0)