Skip to content

Commit

Permalink
reduce dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jan 2, 2025
1 parent 7a3c32b commit e1203b4
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 58 deletions.
18 changes: 2 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
<maven.compiler.target>8</maven.compiler.target>

<htmlunitcssparser.version>4.8.0-SNAPSHOT</htmlunitcssparser.version>
<htmlunitcsp.version>4.7.0</htmlunitcsp.version>
<htmlunitcsp.version>4.8.0-SNAPSHOT</htmlunitcsp.version>
<htmlunitneko.version>4.7.0</htmlunitneko.version>
<htmlunitxpath.version>4.7.0</htmlunitxpath.version>
<htmlunitxpath.version>4.8.0-SNAPSHOT</htmlunitxpath.version>
<htmlunitcorejs.version>4.8.0-SNAPSHOT</htmlunitcorejs.version>
<htmlunitwebsocketclient.version>4.7.0</htmlunitwebsocketclient.version>

Expand Down Expand Up @@ -1204,10 +1204,6 @@
<artifactId>httpmime</artifactId>
<version>${httpcomponents.version}</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
Expand Down Expand Up @@ -1267,16 +1263,6 @@
<artifactId>commons-logging</artifactId>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="4.8.0" date="January xx, 2025" description="Bugfixes">
<action type="update" dev="rbri">
Apache commons-net is no longer a runtime dependency.
</action>
<action type="update" dev="rbri">
Apache commons-text is no longer a runtime dependency.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.util.TimeZone;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.util.SubnetUtils;
import org.htmlunit.javascript.HtmlUnitScriptable;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxFunction;
import org.htmlunit.util.SubnetUtils;

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

final SubnetUtils subnetUtils = new SubnetUtils(pattern, mask);
return subnetUtils.getInfo().isInRange(dnsResolve);
return subnetUtils.isInRange(dnsResolve);
}

/**
Expand Down
37 changes: 2 additions & 35 deletions src/main/java/org/htmlunit/protocol/data/DataUrlDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.htmlunit.protocol.data.DataURLConnection.DATA_PREFIX;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.Charset;
Expand All @@ -27,6 +26,7 @@

import org.apache.commons.lang3.StringUtils;
import org.htmlunit.util.MimeType;
import org.htmlunit.util.UrlUtils;

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

try {
byte[] data = url.substring(comma + 1).getBytes(charset);
data = decodeUrl(data);
data = UrlUtils.decodeDataUrl(data);
if (base64) {
data = Base64.getDecoder().decode(data);
}
Expand Down Expand Up @@ -164,37 +164,4 @@ public byte[] getBytes() {
public String getDataAsString() throws UnsupportedEncodingException {
return new String(content_, charset_);
}

// adapted from apache commons codec
private static byte[] decodeUrl(final byte[] bytes) throws IllegalArgumentException {
if (bytes == null) {
return null;
}
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
final int b = bytes[i];
if (b == '%') {
try {
final int u = digit16(bytes[++i]);
final int l = digit16(bytes[++i]);
buffer.write((char) ((u << 4) + l));
}
catch (final ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException("Invalid URL encoding: ", e);
}
}
else {
buffer.write(b);
}
}
return buffer.toByteArray();
}

private static int digit16(final byte b) throws IllegalArgumentException {
final int i = Character.digit((char) b, 16);
if (i == -1) {
throw new IllegalArgumentException("Invalid URL encoding: not a valid digit (radix 16): " + b);
}
return i;
}
}
137 changes: 137 additions & 0 deletions src/main/java/org/htmlunit/util/SubnetUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2002-2025 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Performs subnet calculations given a network address and a subnet mask.
* Inspired by org.apache.commons.net.util.SubnetUtils.
*
* @see "http://www.faqs.org/rfcs/rfc1519.html"
*
* @author Ronald Brill
*/
public class SubnetUtils {

private static final String IP_ADDRESS = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})";
private static final Pattern ADDRESS_PATTERN = Pattern.compile(IP_ADDRESS);
private static final String PARSE_FAIL = "Could not parse [%s]";
private static final long UNSIGNED_INT_MASK = 0x0FFFFFFFFL;

private final int netmask_;
private final int address_;
private final int network_;
private final int broadcast_;

/**
* Constructs an instance from a dotted decimal address and a dotted decimal mask.
*
* @param address An IP address, e.g. "192.168.0.1"
* @param mask A dotted decimal netmask e.g. "255.255.0.0"
* @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
*/
public SubnetUtils(final String address, final String mask) {
address_ = toInteger(address);
netmask_ = toInteger(mask);

if ((netmask_ & -netmask_) - 1 != ~netmask_) {
throw new IllegalArgumentException(String.format(PARSE_FAIL, mask));
}

network_ = address_ & netmask_;
broadcast_ = network_ | ~netmask_;
}

/*
* Extracts the components of a dotted decimal address and pack into an integer using a regex match
*/
private static int matchAddress(final Matcher matcher) {
int addr = 0;
for (int i = 1; i <= 4; ++i) {
final int n = rangeCheck(Integer.parseInt(matcher.group(i)), 0, 255);
addr |= (n & 0xff) << 8 * (4 - i);
}
return addr;
}

/*
* 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.
*/
private static int rangeCheck(final int value, final int begin, final int end) {
// (begin,end]
if (value >= begin && value <= end) {
return value;
}
throw new IllegalArgumentException("Value [" + value + "] not in range [" + begin + "," + end + "]");
}

/*
* Converts a dotted decimal format address to a packed integer format
*/
private static int toInteger(final String address) {
final Matcher matcher = ADDRESS_PATTERN.matcher(address);
if (matcher.matches()) {
return matchAddress(matcher);
}
throw new IllegalArgumentException(String.format(PARSE_FAIL, address));
}

private long broadcastLong() {
return broadcast_ & UNSIGNED_INT_MASK;
}

private int high() {
return broadcastLong() - networkLong() > 1 ? broadcast_ - 1 : 0;
}

private int low() {
return broadcastLong() - networkLong() > 1 ? network_ + 1 : 0;
}

/** Long versions of the values (as unsigned int) which are more suitable for range checking. */
private long networkLong() {
return network_ & UNSIGNED_INT_MASK;
}

/**
* Tests if the parameter <code>address</code> is in the range of usable endpoint addresses for this subnet. This excludes the network and broadcast
* addresses by default. Use {@link SubnetUtils#setInclusiveHostCount(boolean)} to change this.
*
* @param address the address to check
* @return true if it is in range
*/
private boolean isInRange(final int address) {
if (address == 0) {
return false;
}
final long addLong = address & UNSIGNED_INT_MASK;
final long lowLong = low() & UNSIGNED_INT_MASK;
final long highLong = high() & UNSIGNED_INT_MASK;
return addLong >= lowLong && addLong <= highLong;
}

/**
* Tests if the parameter <code>address</code> is in the range of usable endpoint addresses for this subnet. This excludes the network and broadcast
* addresses. Use {@link SubnetUtils#setInclusiveHostCount(boolean)} to change this.
*
* @param address A dot-delimited IPv4 address, e.g. "192.168.0.1"
* @return true if in range, false otherwise
*/
public boolean isInRange(final String address) {
return isInRange(toInteger(address));
}
}
Loading

0 comments on commit e1203b4

Please sign in to comment.