Skip to content

Commit

Permalink
Issue #11488 - unset port is -1 to be compatible with Java classlib (#…
Browse files Browse the repository at this point in the history
…11578)

+ Changing unset port in HttpURI to -1 (from 0)
+ Changing unset port in HostPort to -1 (from 0)
  • Loading branch information
joakime authored Apr 4, 2024
1 parent ecb610a commit 6320839
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private Immutable(String scheme, String host, int port, String path, String quer
_scheme = URIUtil.normalizeScheme(scheme);
_user = null;
_host = host;
_port = port;
_port = (port > 0) ? port : URIUtil.UNDEFINED_PORT;
_path = path;
_canonicalPath = _path == null ? null : URIUtil.canonicalPath(_path);
_param = null;
Expand Down Expand Up @@ -626,7 +626,7 @@ private enum State
private String _scheme;
private String _user;
private String _host;
private int _port;
private int _port = URIUtil.UNDEFINED_PORT;
private String _path;
private String _param;
private String _query;
Expand Down Expand Up @@ -673,7 +673,6 @@ private Mutable(HttpURI baseURI, String path, String param, String query)

private Mutable(String uri)
{
_port = -1;
parse(State.START, uri);
}

Expand All @@ -700,7 +699,7 @@ private Mutable(String scheme, String host, int port, String pathQuery)

_scheme = URIUtil.normalizeScheme(scheme);
_host = host;
_port = port;
_port = (port > 0) ? port : URIUtil.UNDEFINED_PORT;

if (pathQuery != null)
parse(State.PATH, pathQuery);
Expand Down Expand Up @@ -729,7 +728,7 @@ public Mutable authority(String host, int port)
throw new IllegalArgumentException("Relative path with authority");
_user = null;
_host = host;
_port = port;
_port = (port > 0) ? port : URIUtil.UNDEFINED_PORT;
_uri = null;
return this;
}
Expand Down Expand Up @@ -764,7 +763,7 @@ public Mutable clear()
_scheme = null;
_user = null;
_host = null;
_port = -1;
_port = URIUtil.UNDEFINED_PORT;
_path = null;
_param = null;
_query = null;
Expand Down Expand Up @@ -1003,7 +1002,7 @@ public Mutable pathQuery(String pathQuery)

public Mutable port(int port)
{
_port = port;
_port = (port > 0) ? port : URIUtil.UNDEFINED_PORT;
_uri = null;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.toolchain.test.Net;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.URIUtil;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -2117,7 +2118,7 @@ public void testHost(String eoln)
HttpParser parser = new HttpParser(handler);
parser.parseNext(buffer);
assertEquals("host", _host);
assertEquals(0, _port);
assertEquals(URIUtil.UNDEFINED_PORT, _port);
}

@ParameterizedTest
Expand Down Expand Up @@ -2182,7 +2183,7 @@ public void testIPHost(String eoln)
HttpParser parser = new HttpParser(handler);
parser.parseNext(buffer);
assertEquals("192.168.0.1", _host);
assertEquals(0, _port);
assertEquals(URIUtil.UNDEFINED_PORT, _port);
}

@ParameterizedTest
Expand All @@ -2200,7 +2201,7 @@ public void testIPv6Host(String eoln)
HttpParser parser = new HttpParser(handler);
parser.parseNext(buffer);
assertEquals("[::1]", _host);
assertEquals(0, _port);
assertEquals(URIUtil.UNDEFINED_PORT, _port);
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
public class HostPort
{
private static final Logger LOG = LoggerFactory.getLogger(HostPort.class);
private static final int BAD_PORT = -1;
/**
* Indicator that the provided port string was bad see unsafe mode at {@link #parsePort(String, boolean)}
*/
private static final int BAD_PORT = -2;
private final String _host;
private final int _port;

Expand All @@ -51,7 +54,7 @@ public static HostPort unsafe(String authority)
public HostPort(String host, int port)
{
_host = normalizeHost(host);
_port = port;
_port = (port > 0) ? port : URIUtil.UNDEFINED_PORT;
}

public HostPort(String authority) throws IllegalArgumentException
Expand All @@ -63,23 +66,22 @@ public HostPort(String authority) throws IllegalArgumentException
private HostPort(String authority, boolean unsafe)
{
String host;
//noinspection UnusedAssignment
int port = 0;
int port = URIUtil.UNDEFINED_PORT;

if (authority == null)
{
LOG.warn("Bad Authority [<null>]");
if (!unsafe)
throw new IllegalArgumentException("No Authority");
_host = "";
_port = 0;
_port = URIUtil.UNDEFINED_PORT;
return;
}

if (authority.isEmpty())
{
_host = authority;
_port = 0;
_port = URIUtil.UNDEFINED_PORT;
return;
}

Expand Down Expand Up @@ -117,22 +119,22 @@ private HostPort(String authority, boolean unsafe)
if (!unsafe)
throw new IllegalArgumentException("Bad IPv6 port");
host = authority; // whole authority (no substring)
port = 0; // no port
port = URIUtil.UNDEFINED_PORT; // no port
}
else
{
port = parsePort(authority.substring(close + 2), unsafe);
// horribly bad port during unsafe
if (unsafe && (port == BAD_PORT))
// horribly bad port during unsafe (eg: "Host:xxx")
if (unsafe && port == BAD_PORT)
{
host = authority; // whole authority (no substring)
port = 0;
port = URIUtil.UNDEFINED_PORT;
}
}
}
else
{
port = 0;
port = URIUtil.UNDEFINED_PORT;
}
}
else
Expand All @@ -144,7 +146,7 @@ private HostPort(String authority, boolean unsafe)
if (c != authority.indexOf(':'))
{
// ipv6address no port
port = 0;
port = URIUtil.UNDEFINED_PORT;
host = "[" + authority + "]";
if (!isValidIpAddress(host))
{
Expand Down Expand Up @@ -176,11 +178,11 @@ else if (!isValidHostName(host))
}

port = parsePort(authority.substring(c + 1), unsafe);
// horribly bad port during unsafe
if (unsafe && (port == BAD_PORT))
// horribly bad port during unsafe (eg: "Host:xxx")
if (unsafe && port == BAD_PORT)
{
host = authority; // whole authority (no substring)
port = 0;
port = URIUtil.UNDEFINED_PORT;
}
}
}
Expand All @@ -194,7 +196,7 @@ else if (!isValidHostName(host))
if (!unsafe)
throw new IllegalArgumentException("Bad Authority");
}
port = 0;
port = URIUtil.UNDEFINED_PORT;
}
}
}
Expand All @@ -203,17 +205,17 @@ else if (!isValidHostName(host))
if (!unsafe)
throw iae;
host = authority;
port = 0;
port = URIUtil.UNDEFINED_PORT;
}
catch (Exception ex)
{
if (!unsafe)
throw new IllegalArgumentException("Bad HostPort", ex);
host = authority;
port = 0;
port = URIUtil.UNDEFINED_PORT;
}
_host = host;
_port = port;
_port = (port > 0) ? port : URIUtil.UNDEFINED_PORT;
}

protected boolean isValidIpAddress(String ip)
Expand Down Expand Up @@ -327,9 +329,19 @@ public static int parsePort(String rawPort) throws IllegalArgumentException
/**
* Parse a potential port.
*
* <p>
* In safe mode, this will either return a port in the valid range of 0 to 65535, or throw an {@link IllegalArgumentException}.
* </p>
* <p>
* In unsafe mode, this will return a port in the valid range of 0 to 65535, or {@link #BAD_PORT} indicating that the
* port provided was not a number (eg: {@code ":xxx"}) or was not in the valid range (eg: {@code ":-80"} or {@code ":11222333"})
* </p>
* <p>
* In both safe and unsafe, an empty {@code rawPort} will result in a return value of {@link URIUtil#UNDEFINED_PORT}
* </p>
*
* @param rawPort the raw port string to parse
* @param unsafe true to always return a port in the range 0 to 65535 (or -1 for undefined if rawPort is horribly bad), false to return
* the provided port (or {@link IllegalArgumentException} if it is horribly bad)
* @param unsafe true for unsafe mode (no exceptions), false for safe mode (with exceptions if there is a problem).
* @return the port
* @throws IllegalArgumentException if unable to parse a valid port and {@code unsafe} is false
*/
Expand All @@ -338,8 +350,8 @@ private int parsePort(String rawPort, boolean unsafe)
if (StringUtil.isEmpty(rawPort))
{
if (!unsafe)
throw new IllegalArgumentException("Bad port [" + rawPort + "]");
return 0;
throw new IllegalArgumentException("Blank port");
return URIUtil.UNDEFINED_PORT;
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public final class URIUtil
{
private static final Logger LOG = LoggerFactory.getLogger(URIUtil.class);

/**
* Port number indicating that the port is undefined.
* <p>
* This is the same value as used in {@link java.net.URL} and {@link java.net.URI} classes.
* </p>
*/
public static final int UNDEFINED_PORT = -1;

// From https://www.rfc-editor.org/rfc/rfc3986
private static final String UNRESERVED = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~";
private static final String SUBDELIMS = "!$&'()*+,;=";
Expand Down
Loading

0 comments on commit 6320839

Please sign in to comment.