Skip to content

Commit e1203b4

Browse files
committed
reduce dependencies
1 parent 7a3c32b commit e1203b4

File tree

6 files changed

+238
-58
lines changed

6 files changed

+238
-58
lines changed

pom.xml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
<maven.compiler.target>8</maven.compiler.target>
2727

2828
<htmlunitcssparser.version>4.8.0-SNAPSHOT</htmlunitcssparser.version>
29-
<htmlunitcsp.version>4.7.0</htmlunitcsp.version>
29+
<htmlunitcsp.version>4.8.0-SNAPSHOT</htmlunitcsp.version>
3030
<htmlunitneko.version>4.7.0</htmlunitneko.version>
31-
<htmlunitxpath.version>4.7.0</htmlunitxpath.version>
31+
<htmlunitxpath.version>4.8.0-SNAPSHOT</htmlunitxpath.version>
3232
<htmlunitcorejs.version>4.8.0-SNAPSHOT</htmlunitcorejs.version>
3333
<htmlunitwebsocketclient.version>4.7.0</htmlunitwebsocketclient.version>
3434

@@ -1204,10 +1204,6 @@
12041204
<artifactId>httpmime</artifactId>
12051205
<version>${httpcomponents.version}</version>
12061206
<exclusions>
1207-
<exclusion>
1208-
<groupId>commons-codec</groupId>
1209-
<artifactId>commons-codec</artifactId>
1210-
</exclusion>
12111207
<exclusion>
12121208
<groupId>commons-logging</groupId>
12131209
<artifactId>commons-logging</artifactId>
@@ -1267,16 +1263,6 @@
12671263
<artifactId>commons-logging</artifactId>
12681264
<version>1.3.4</version>
12691265
</dependency>
1270-
<dependency>
1271-
<groupId>commons-net</groupId>
1272-
<artifactId>commons-net</artifactId>
1273-
<version>3.11.1</version>
1274-
</dependency>
1275-
<dependency>
1276-
<groupId>commons-codec</groupId>
1277-
<artifactId>commons-codec</artifactId>
1278-
<version>1.17.1</version>
1279-
</dependency>
12801266
<dependency>
12811267
<groupId>org.brotli</groupId>
12821268
<artifactId>dec</artifactId>

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
<body>
1010
<release version="4.8.0" date="January xx, 2025" description="Bugfixes">
11+
<action type="update" dev="rbri">
12+
Apache commons-net is no longer a runtime dependency.
13+
</action>
1114
<action type="update" dev="rbri">
1215
Apache commons-text is no longer a runtime dependency.
1316
</action>

src/main/java/org/htmlunit/javascript/proxyautoconfig/ProxyAutoConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import java.util.TimeZone;
2222

2323
import org.apache.commons.lang3.StringUtils;
24-
import org.apache.commons.net.util.SubnetUtils;
2524
import org.htmlunit.javascript.HtmlUnitScriptable;
2625
import org.htmlunit.javascript.JavaScriptEngine;
2726
import org.htmlunit.javascript.configuration.JsxClass;
2827
import org.htmlunit.javascript.configuration.JsxFunction;
28+
import org.htmlunit.util.SubnetUtils;
2929

3030
/**
3131
* Provides an implementation of Proxy Auto-Config (PAC).
@@ -107,7 +107,7 @@ public static boolean isInNet(final String host, final String pattern, final Str
107107
}
108108

109109
final SubnetUtils subnetUtils = new SubnetUtils(pattern, mask);
110-
return subnetUtils.getInfo().isInRange(dnsResolve);
110+
return subnetUtils.isInRange(dnsResolve);
111111
}
112112

113113
/**

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static java.nio.charset.StandardCharsets.US_ASCII;
1818
import static org.htmlunit.protocol.data.DataURLConnection.DATA_PREFIX;
1919

20-
import java.io.ByteArrayOutputStream;
2120
import java.io.UnsupportedEncodingException;
2221
import java.net.URL;
2322
import java.nio.charset.Charset;
@@ -27,6 +26,7 @@
2726

2827
import org.apache.commons.lang3.StringUtils;
2928
import org.htmlunit.util.MimeType;
29+
import org.htmlunit.util.UrlUtils;
3030

3131
/**
3232
* Helper to work with data URLs.
@@ -90,7 +90,7 @@ public static DataUrlDecoder decodeDataURL(final String url) throws UnsupportedE
9090

9191
try {
9292
byte[] data = url.substring(comma + 1).getBytes(charset);
93-
data = decodeUrl(data);
93+
data = UrlUtils.decodeDataUrl(data);
9494
if (base64) {
9595
data = Base64.getDecoder().decode(data);
9696
}
@@ -164,37 +164,4 @@ public byte[] getBytes() {
164164
public String getDataAsString() throws UnsupportedEncodingException {
165165
return new String(content_, charset_);
166166
}
167-
168-
// adapted from apache commons codec
169-
private static byte[] decodeUrl(final byte[] bytes) throws IllegalArgumentException {
170-
if (bytes == null) {
171-
return null;
172-
}
173-
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
174-
for (int i = 0; i < bytes.length; i++) {
175-
final int b = bytes[i];
176-
if (b == '%') {
177-
try {
178-
final int u = digit16(bytes[++i]);
179-
final int l = digit16(bytes[++i]);
180-
buffer.write((char) ((u << 4) + l));
181-
}
182-
catch (final ArrayIndexOutOfBoundsException e) {
183-
throw new IllegalArgumentException("Invalid URL encoding: ", e);
184-
}
185-
}
186-
else {
187-
buffer.write(b);
188-
}
189-
}
190-
return buffer.toByteArray();
191-
}
192-
193-
private static int digit16(final byte b) throws IllegalArgumentException {
194-
final int i = Character.digit((char) b, 16);
195-
if (i == -1) {
196-
throw new IllegalArgumentException("Invalid URL encoding: not a valid digit (radix 16): " + b);
197-
}
198-
return i;
199-
}
200167
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2002-2025 Gargoyle Software Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* https://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.htmlunit.util;
16+
17+
import java.util.regex.Matcher;
18+
import java.util.regex.Pattern;
19+
20+
/**
21+
* Performs subnet calculations given a network address and a subnet mask.
22+
* Inspired by org.apache.commons.net.util.SubnetUtils.
23+
*
24+
* @see "http://www.faqs.org/rfcs/rfc1519.html"
25+
*
26+
* @author Ronald Brill
27+
*/
28+
public class SubnetUtils {
29+
30+
private static final String IP_ADDRESS = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})";
31+
private static final Pattern ADDRESS_PATTERN = Pattern.compile(IP_ADDRESS);
32+
private static final String PARSE_FAIL = "Could not parse [%s]";
33+
private static final long UNSIGNED_INT_MASK = 0x0FFFFFFFFL;
34+
35+
private final int netmask_;
36+
private final int address_;
37+
private final int network_;
38+
private final int broadcast_;
39+
40+
/**
41+
* Constructs an instance from a dotted decimal address and a dotted decimal mask.
42+
*
43+
* @param address An IP address, e.g. "192.168.0.1"
44+
* @param mask A dotted decimal netmask e.g. "255.255.0.0"
45+
* @throws IllegalArgumentException if the address or mask is invalid, i.e. does not match n.n.n.n where n=1-3 decimal digits and the mask is not all zeros
46+
*/
47+
public SubnetUtils(final String address, final String mask) {
48+
address_ = toInteger(address);
49+
netmask_ = toInteger(mask);
50+
51+
if ((netmask_ & -netmask_) - 1 != ~netmask_) {
52+
throw new IllegalArgumentException(String.format(PARSE_FAIL, mask));
53+
}
54+
55+
network_ = address_ & netmask_;
56+
broadcast_ = network_ | ~netmask_;
57+
}
58+
59+
/*
60+
* Extracts the components of a dotted decimal address and pack into an integer using a regex match
61+
*/
62+
private static int matchAddress(final Matcher matcher) {
63+
int addr = 0;
64+
for (int i = 1; i <= 4; ++i) {
65+
final int n = rangeCheck(Integer.parseInt(matcher.group(i)), 0, 255);
66+
addr |= (n & 0xff) << 8 * (4 - i);
67+
}
68+
return addr;
69+
}
70+
71+
/*
72+
* Checks integer boundaries. Checks if a value x is in the range [begin,end]. Returns x if it is in range, throws an exception otherwise.
73+
*/
74+
private static int rangeCheck(final int value, final int begin, final int end) {
75+
// (begin,end]
76+
if (value >= begin && value <= end) {
77+
return value;
78+
}
79+
throw new IllegalArgumentException("Value [" + value + "] not in range [" + begin + "," + end + "]");
80+
}
81+
82+
/*
83+
* Converts a dotted decimal format address to a packed integer format
84+
*/
85+
private static int toInteger(final String address) {
86+
final Matcher matcher = ADDRESS_PATTERN.matcher(address);
87+
if (matcher.matches()) {
88+
return matchAddress(matcher);
89+
}
90+
throw new IllegalArgumentException(String.format(PARSE_FAIL, address));
91+
}
92+
93+
private long broadcastLong() {
94+
return broadcast_ & UNSIGNED_INT_MASK;
95+
}
96+
97+
private int high() {
98+
return broadcastLong() - networkLong() > 1 ? broadcast_ - 1 : 0;
99+
}
100+
101+
private int low() {
102+
return broadcastLong() - networkLong() > 1 ? network_ + 1 : 0;
103+
}
104+
105+
/** Long versions of the values (as unsigned int) which are more suitable for range checking. */
106+
private long networkLong() {
107+
return network_ & UNSIGNED_INT_MASK;
108+
}
109+
110+
/**
111+
* Tests if the parameter <code>address</code> is in the range of usable endpoint addresses for this subnet. This excludes the network and broadcast
112+
* addresses by default. Use {@link SubnetUtils#setInclusiveHostCount(boolean)} to change this.
113+
*
114+
* @param address the address to check
115+
* @return true if it is in range
116+
*/
117+
private boolean isInRange(final int address) {
118+
if (address == 0) {
119+
return false;
120+
}
121+
final long addLong = address & UNSIGNED_INT_MASK;
122+
final long lowLong = low() & UNSIGNED_INT_MASK;
123+
final long highLong = high() & UNSIGNED_INT_MASK;
124+
return addLong >= lowLong && addLong <= highLong;
125+
}
126+
127+
/**
128+
* Tests if the parameter <code>address</code> is in the range of usable endpoint addresses for this subnet. This excludes the network and broadcast
129+
* addresses. Use {@link SubnetUtils#setInclusiveHostCount(boolean)} to change this.
130+
*
131+
* @param address A dot-delimited IPv4 address, e.g. "192.168.0.1"
132+
* @return true if in range, false otherwise
133+
*/
134+
public boolean isInRange(final String address) {
135+
return isInRange(toInteger(address));
136+
}
137+
}

0 commit comments

Comments
 (0)