/** * Decodes a segment of an URI encoded by a browser. * * The string is expected to be encoded as per RFC 3986, Section 2. This is the encoding used by JavaScript functions * encodeURI and encodeURIComponent, but not escape. For example in this encoding, é (in Unicode U+00E9 or in * UTF-8 0xC3 0xA9) is encoded as %C3%A9 or %c3%a9. * * Plus signs '+' will be handled as spaces and encoded using the default JDK URLEncoder class. * * @param s string to decode * * @return decoded string */ public static String decodeURIComponent(String s) { return decodeURIComponent(s, true); }
public List<JavaFileObject> find(String packageName) throws IOException { String javaPackageName = packageName.replaceAll("\\.", "/"); List<JavaFileObject> result = new ArrayList<>(); Enumeration<URL> urlEnumeration = classLoader.getResources(javaPackageName); while (urlEnumeration.hasMoreElements()) { URL resource = urlEnumeration.nextElement(); //Need to urldecode it too, since bug in JDK URL class which does not url decode it, so if it contains spaces you are screwed final File directory = new File(decodeURIComponent(resource.getFile(), false)); if (directory.isDirectory()) { result.addAll(browseDir(packageName, directory)); } else { result.addAll(browseJar(resource)); } } return result; }
private synchronized File unpackFromFileURL(URL url, String fileName, ClassLoader cl) { final File resource = new File(decodeURIComponent(url.getPath(), false)); boolean isDirectory = resource.isDirectory(); File cacheFile = new File(cacheDir, fileName); if (!isDirectory) { cacheFile.getParentFile().mkdirs(); try { if (this.enableCaching) { Files.copy(resource.toPath(), cacheFile.toPath()); } else { Files.copy(resource.toPath(), cacheFile.toPath(), StandardCopyOption.REPLACE_EXISTING); } } catch (FileAlreadyExistsException ignore) { } catch (IOException e) { throw new VertxException(e); } } else { cacheFile.mkdirs(); String[] listing = resource.list(); for (String file: listing) { String subResource = fileName + "/" + file; URL url2 = cl.getResource(subResource); unpackFromFileURL(url2, subResource, cl); } } return cacheFile; }
File file = new File(decodeURIComponent(path.substring(5, idx1 + 4), false)); zip = new ZipFile(file); } else {
@Test public void testNonLatin() { assertEquals("/foo/ñ/blah/婴儿服饰/eek/ฌ", decodeURIComponent("/foo/%C3%B1/blah/%E5%A9%B4%E5%84%BF%E6%9C%8D%E9%A5%B0/eek/%E0%B8%8C")); assertEquals("/foo/\u00F1/blah/\u5a74\u513f\u670d\u9970/eek/\u0E0C", decodeURIComponent("/foo/%C3%B1/blah/%E5%A9%B4%E5%84%BF%E6%9C%8D%E9%A5%B0/eek/%E0%B8%8C", false)); }
final File sourceFile = new File(decodeURIComponent(resource.getFile(), false)); if (!sourceFile.canRead()) { throw new RuntimeException("File not found: " + sourceFile.getAbsolutePath() + " current dir is: " + new File(".").getAbsolutePath());
@Test public void testSingleDecode() { assertEquals("../blah", decodeURIComponent("%2E%2E%2Fblah")); assertEquals("%20", decodeURIComponent("%2520")); }
@Test public void testDecode() throws Exception { String original = "ein verr+++ückter text mit Leerzeichen, Plus und Umlauten"; String encoded = URLEncoder.encode(original, "UTF-8"); assertEquals(original, decodeURIComponent(encoded, true)); }
@Test public void testIncomplete() { try { decodeURIComponent("a%"); fail("should fail"); } catch (RuntimeException e) { // expected } }
@Test public void testPlusAsSpace() { assertEquals("foo bar", decodeURIComponent("foo+bar")); }
@Test public void testSpaces() { assertEquals("foo bar", decodeURIComponent("foo%20bar")); }
@Test public void testCaseInsensitive() { assertEquals("../blah", decodeURIComponent("%2e%2e%2fblah")); }
@Test public void testPlusAsPlus() { assertEquals("foo+bar", decodeURIComponent("foo+bar", false)); }
@Test public void testFromRFC() { assertEquals("/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", decodeURIComponent("/%20%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2F%30%31%32%33%34%35%36%37%38%39%3A%3B%3C%3D%3E%3F%40%41%42%43%44%45%46%47%48%49%4A%4B%4C%4D%4E%4F%50%51%52%53%54%55%56%57%58%59%5A%5B%5C%5D%5E%5F%60%61%62%63%64%65%66%67%68%69%6A%6B%6C%6D%6E%6F%70%71%72%73%74%75%76%77%78%79%7A%7B%7C%7D%7E", false)); }
@Test public void testSingleDecode() { assertEquals("../blah", decodeURIComponent("%2E%2E%2Fblah")); assertEquals("%20", decodeURIComponent("%2520")); }
@Test public void testNonLatin() { assertEquals("/foo/ñ/blah/婴儿服饰/eek/ฌ", decodeURIComponent("/foo/%C3%B1/blah/%E5%A9%B4%E5%84%BF%E6%9C%8D%E9%A5%B0/eek/%E0%B8%8C")); assertEquals("/foo/\u00F1/blah/\u5a74\u513f\u670d\u9970/eek/\u0E0C", decodeURIComponent("/foo/%C3%B1/blah/%E5%A9%B4%E5%84%BF%E6%9C%8D%E9%A5%B0/eek/%E0%B8%8C", false)); }
@Test public void testDecode() throws Exception { String original = "ein verr+++ückter text mit Leerzeichen, Plus und Umlauten"; String encoded = URLEncoder.encode(original, "UTF-8"); assertEquals(original, decodeURIComponent(encoded, true)); }
private void addPathParam(RoutingContext context, String name, String value) { HttpServerRequest request = context.request(); final String decodedValue = URIDecoder.decodeURIComponent(value, false); if (!request.params().contains(name)) { request.params().add(name, decodedValue); } context.pathParams().put(name, decodedValue); }
@Test public void testIncomplete() { try { decodeURIComponent("a%"); fail("should fail"); } catch (RuntimeException e) { // expected } }
@Test public void testCaseInsensitive() { assertEquals("../blah", decodeURIComponent("%2e%2e%2fblah")); }