Skip to content

Commit e062266

Browse files
committed
get finally rid of commons codec
1 parent 07f085c commit e062266

File tree

7 files changed

+63
-36
lines changed

7 files changed

+63
-36
lines changed

pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<commons-lang3.version>3.17.0</commons-lang3.version>
3737
<commons-io.version>2.18.0</commons-io.version>
3838
<commons-logging.version>1.3.5</commons-logging.version>
39-
<commons-codec.version>1.18.0</commons-codec.version>
4039
<brotli.version>0.1.2</brotli.version>
4140

4241

@@ -1216,10 +1215,6 @@
12161215
<artifactId>httpmime</artifactId>
12171216
<version>${httpcomponents.version}</version>
12181217
<exclusions>
1219-
<exclusion>
1220-
<groupId>commons-codec</groupId>
1221-
<artifactId>commons-codec</artifactId>
1222-
</exclusion>
12231218
<exclusion>
12241219
<groupId>commons-logging</groupId>
12251220
<artifactId>commons-logging</artifactId>
@@ -1279,11 +1274,6 @@
12791274
<artifactId>commons-logging</artifactId>
12801275
<version>${commons-logging.version}</version>
12811276
</dependency>
1282-
<dependency>
1283-
<groupId>commons-codec</groupId>
1284-
<artifactId>commons-codec</artifactId>
1285-
<version>${commons-codec.version}</version>
1286-
</dependency>
12871277
<dependency>
12881278
<groupId>org.brotli</groupId>
12891279
<artifactId>dec</artifactId>

src/main/java/org/htmlunit/javascript/host/WindowOrWorkerGlobalScopeMixin.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,17 @@ public static String btoa(final String stringToEncode, final HtmlUnitScriptable
8787
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
8888
}
8989
}
90+
9091
final byte[] bytes = stringToEncode.getBytes(StandardCharsets.ISO_8859_1);
91-
return new String(Base64.getEncoder().encode(bytes), StandardCharsets.UTF_8);
92+
try {
93+
return new String(Base64.getEncoder().encode(bytes), StandardCharsets.UTF_8);
94+
}
95+
catch (final IllegalArgumentException e) {
96+
throw JavaScriptEngine.asJavaScriptException(
97+
scriptable,
98+
"Failed to execute btoa(): " + e.getMessage(),
99+
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
100+
}
92101
}
93102

94103
/**

src/main/java/org/htmlunit/protocol/data/DataUrlDecoder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import java.nio.charset.Charset;
2323
import java.nio.charset.IllegalCharsetNameException;
2424
import java.nio.charset.UnsupportedCharsetException;
25+
import java.util.Base64;
2526

26-
import org.apache.commons.codec.binary.Base64;
2727
import org.apache.commons.lang3.StringUtils;
2828
import org.htmlunit.util.MimeType;
2929
import org.htmlunit.util.UrlUtils;
@@ -90,10 +90,9 @@ public static DataUrlDecoder decodeDataURL(final String url) throws UnsupportedE
9090

9191
try {
9292
byte[] data = url.substring(comma + 1).getBytes(charset);
93-
data = UrlUtils.decodeDataUrl(data);
93+
data = UrlUtils.decodeDataUrl(data, base64);
9494
if (base64) {
95-
// the commons codec decoder skip's invalid chars
96-
data = Base64.decodeBase64(data);
95+
data = Base64.getDecoder().decode(data);
9796
}
9897
return new DataUrlDecoder(data, mediaType, charset);
9998
}

src/main/java/org/htmlunit/util/UrlUtils.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,28 +1384,47 @@ public static URL removeRedundantPort(final URL url) throws MalformedURLExceptio
13841384
* @param bytes array of URL safe characters
13851385
* @return array of original bytes
13861386
* @throws IllegalArgumentException in case of error
1387+
*
1388+
* @deprecated as of version 4.11.0; use {@link #decodeDataUrl(byte[], boolean)} instead
13871389
*/
1390+
@Deprecated
13881391
public static byte[] decodeDataUrl(final byte[] bytes) throws IllegalArgumentException {
1392+
return decodeDataUrl(bytes, false);
1393+
}
1394+
1395+
/**
1396+
* Decodes an array of URL safe 7-bit characters into an array of original bytes.
1397+
* Escaped characters are converted back to their original representation.
1398+
* @param bytes array of URL safe characters
1399+
* @param removeWhitespace if true don't add whitespace chars to the output
1400+
* @return array of original bytes
1401+
* @throws IllegalArgumentException in case of error
1402+
*/
1403+
public static byte[] decodeDataUrl(final byte[] bytes, final boolean removeWhitespace)
1404+
throws IllegalArgumentException {
13891405
// adapted from apache commons codec
13901406
if (bytes == null) {
13911407
return null;
13921408
}
13931409
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
13941410
for (int i = 0; i < bytes.length; i++) {
1395-
final int b = bytes[i];
1411+
int b = bytes[i];
13961412
if (b == '%') {
13971413
try {
13981414
final int u = digit16(bytes[++i]);
13991415
final int l = digit16(bytes[++i]);
1400-
buffer.write((char) ((u << 4) + l));
1416+
b = (u << 4) + l;
14011417
}
14021418
catch (final ArrayIndexOutOfBoundsException e) {
14031419
throw new IllegalArgumentException("Invalid URL encoding: ", e);
14041420
}
14051421
}
1406-
else {
1407-
buffer.write(b);
1422+
if (removeWhitespace
1423+
&& (b == 9 || b == 10 || b == 12 || b == 13 || b == 32)) {
1424+
continue;
14081425
}
1426+
1427+
buffer.write(b);
14091428
}
14101429
return buffer.toByteArray();
14111430
}

src/test/java/org/htmlunit/ExternalTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,6 @@ private static boolean isIgnored(@SuppressWarnings("unused") final String groupI
327327
return true;
328328
}
329329

330-
if ("commons-codec".equals(groupId)
331-
&& "commons-codec".equals(artifactId)
332-
&& "20041127.091804".equals(version)) {
333-
return true;
334-
}
335-
336330
// version > 3.12.0 does not work with our site.xml and also not with a refactored one
337331
if ("maven-site-plugin".equals(artifactId)
338332
&& (version.startsWith("3.12.1") || version.startsWith("3.20.") || version.startsWith("3.21."))) {

src/test/java/org/htmlunit/archunit/ArchitectureTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,6 @@ public class ArchitectureTest {
9090
.and().resideOutsideOfPackage("org.htmlunit.jetty..")
9191
.should().dependOnClassesThat().resideInAnyPackage("java.awt..");
9292

93-
/**
94-
* Do not use org.apache.commons.codec.binary.Base64 - use the jdk instead.
95-
*/
96-
@ArchTest
97-
public static final ArchRule jdkBase64Rule = noClasses()
98-
.that()
99-
.resideOutsideOfPackage("org.htmlunit.jetty..")
100-
.and().doNotHaveFullyQualifiedName("org.htmlunit.protocol.data.DataUrlDecoder")
101-
.should().dependOnClassesThat().haveFullyQualifiedName("org.apache.commons.codec.binary.Base64");
102-
103-
10493
/**
10594
* JsxClasses are always in the javascript package.
10695
*/

src/test/java/org/htmlunit/javascript/host/Window2Test.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,33 @@ public void atobWhitespace() throws Exception {
242242
loadPageVerifyTitle2(html);
243243
}
244244

245+
/**
246+
* @throws Exception if the test fails
247+
*/
248+
@Test
249+
@Alerts({"SGVsbG8gV29ybGQh", "InvalidCharacterError/DOMException",
250+
"InvalidCharacterError/DOMException", "InvalidCharacterError/DOMException"})
251+
public void atobNbsp() throws Exception {
252+
final String html
253+
= "<html><head></head><body>\n"
254+
+ "<script>\n"
255+
+ LOG_TITLE_FUNCTION
256+
+ " var data = window.btoa('Hello World!');\n"
257+
+ " log(data);\n"
258+
+ " try {\n"
259+
+ " log(window.atob('\\xA0' + data));\n"
260+
+ " } catch(e) { logEx(e) }\n"
261+
+ " try {\n"
262+
+ " log(window.atob(data + '\\xA0'));\n"
263+
+ " } catch(e) { logEx(e) }\n"
264+
+ " try {\n"
265+
+ " log(window.atob(data.substr(0, 2) + '\\xA0' + data.substr(2)));\n"
266+
+ " } catch(e) { logEx(e) }\n"
267+
+ "</script>\n"
268+
+ "</body></html>";
269+
loadPageVerifyTitle2(html);
270+
}
271+
245272
/**
246273
* @throws Exception if the test fails
247274
*/

0 commit comments

Comments
 (0)