From 0e2bfccd838ba597ea9028c1ddd523f4e2c938cf Mon Sep 17 00:00:00 2001 From: Ben Liblit Date: Sat, 20 Sep 2025 17:09:49 -0400 Subject: [PATCH] Simplify a URL utility function Personally, I'm skeptical of the nonstandard normalizations that `relativeToAbsoluteUrl` initially applies to `urlFound`: replacing `\` with `/` and converting to lower case. But that's what was done before, presumably for good reason. Note that the new implementation of `relativeToAbsoluteUrl` differs from the old implementation in how it handles `..` in the interior of the `urlFound` path. The new implementation normalizes those away, whereas the old implementation did not. I'm OK with this change, so I've updated the test suite accordingly. --- .../ibm/wala/cast/js/html/UrlManipulator.java | 56 ++----------------- .../wala/cast/js/html/UrlManipulatorTest.java | 2 +- 2 files changed, 5 insertions(+), 53 deletions(-) diff --git a/cast/js/src/main/java/com/ibm/wala/cast/js/html/UrlManipulator.java b/cast/js/src/main/java/com/ibm/wala/cast/js/html/UrlManipulator.java index 40c67adbcd..04f37a6409 100644 --- a/cast/js/src/main/java/com/ibm/wala/cast/js/html/UrlManipulator.java +++ b/cast/js/src/main/java/com/ibm/wala/cast/js/html/UrlManipulator.java @@ -11,7 +11,6 @@ package com.ibm.wala.cast.js.html; import java.net.MalformedURLException; -import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -25,57 +24,10 @@ public static URL relativeToAbsoluteUrl(String urlFound, URL context) throws MalformedURLException { urlFound = urlFound.replace("\\", "/").toLowerCase(); - URI absoluteUri; - if (!isAbsoluteUrl(urlFound)) - try { - if (urlFound.startsWith("//")) { - // create URL taking only the protocol from the context - String origHostAndPath = urlFound.substring(2); // removing "//" - String host; - String path; - int indexOf = origHostAndPath.indexOf('/'); - if (indexOf > 0) { - host = origHostAndPath.substring(0, indexOf); - path = origHostAndPath.substring(indexOf); - } else { - host = origHostAndPath; - path = ""; - } - absoluteUri = new URI(context.getProtocol(), host, path, null); - } else if (urlFound.startsWith("/")) { - // create URL taking the protocol and the host from the context - absoluteUri = new URI(context.getProtocol(), context.getHost(), urlFound, null); - } else { - // "concat" URL to context - int backDir = 0; // removing directories due to "../" - while (urlFound.startsWith("../")) { - urlFound = urlFound.substring(3); - backDir++; - } - StringBuilder contextPath = new StringBuilder(); - String path = context.getPath().replace("\\", "/"); - boolean isContextDirectory = path.endsWith("/"); - String[] split = path.split("/"); - // we are also removing last element in case of a directory - int rightTrimFromPath = (isContextDirectory ? 0 : 1) + backDir; - - for (int i = 0; i < split.length - rightTrimFromPath; i++) { - contextPath.append(split[i]); - contextPath.append('/'); - } - absoluteUri = - new URI(context.getProtocol(), context.getHost(), contextPath + urlFound, null); - } - } catch (URISyntaxException problem) { - throw new IllegalArgumentException(problem); - } - else { - absoluteUri = URI.create(urlFound); + try { + return context.toURI().resolve(urlFound).toURL(); + } catch (final URISyntaxException problem) { + throw new IllegalArgumentException(problem); } - return absoluteUri.toURL(); - } - - private static boolean isAbsoluteUrl(String orig) { - return orig.startsWith("http"); } } diff --git a/cast/js/src/test/java/com/ibm/wala/cast/js/html/UrlManipulatorTest.java b/cast/js/src/test/java/com/ibm/wala/cast/js/html/UrlManipulatorTest.java index 62848b6ef7..8c20fc4db8 100644 --- a/cast/js/src/test/java/com/ibm/wala/cast/js/html/UrlManipulatorTest.java +++ b/cast/js/src/test/java/com/ibm/wala/cast/js/html/UrlManipulatorTest.java @@ -111,7 +111,7 @@ public void testComplexRelativePath() { check( "https://example.com/dir/subdir/", "../../subdir2/resource/../../file", - "https://example.com/subdir2/resource/../../file"); + "https://example.com/file"); } @Test