Utilize i.n.u.internal.ObjectUtil to assert Preconditions (codec-http) (#11170) (#11187)

Motivation:

NullChecks resulting in a NullPointerException or IllegalArgumentException, numeric ranges (>0, >=0) checks, not empty strings/arrays checks must never be anonymous but with the parameter or variable name which is checked. They must be specific and should not be done with an "OR-Logic" (if a == null || b == null) throw new NullPointerEx.

Modifications:

* import static relevant checks
* Replace manual checks with ObjectUtil methods

Result:

All checks needed are done with ObjectUtil, some exception texts are improved.

Fixes #11170
This commit is contained in:
Boris Unckel 2021-04-23 08:08:28 +02:00 committed by Norman Maurer
parent 0e8f5c5f7c
commit 10758eef73
9 changed files with 20 additions and 58 deletions

View File

@ -18,7 +18,7 @@ package io.netty.handler.codec.http;
import io.netty.util.AsciiString; import io.netty.util.AsciiString;
import static io.netty.util.internal.MathUtil.findNextPositivePowerOfTwo; import static io.netty.util.internal.MathUtil.findNextPositivePowerOfTwo;
import static java.util.Objects.requireNonNull; import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
/** /**
* The request method of HTTP or its derived protocols, such as * The request method of HTTP or its derived protocols, such as
@ -120,10 +120,7 @@ public class HttpMethod implements Comparable<HttpMethod> {
* <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a> * <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>
*/ */
public HttpMethod(String name) { public HttpMethod(String name) {
name = requireNonNull(name, "name").trim(); name = checkNonEmptyAfterTrim(name, "name");
if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}
for (int i = 0; i < name.length(); i ++) { for (int i = 0; i < name.length(); i ++) {
char c = name.charAt(i); char c = name.charAt(i);

View File

@ -32,6 +32,7 @@ import io.netty.util.NetUtil;
import io.netty.util.internal.UnstableApi; import io.netty.util.internal.UnstableApi;
import static io.netty.util.internal.StringUtil.COMMA; import static io.netty.util.internal.StringUtil.COMMA;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/** /**
* Utility methods useful in the HTTP context. * Utility methods useful in the HTTP context.
@ -605,12 +606,7 @@ public final class HttpUtil {
} }
try { try {
final long value = Long.parseLong(firstField); final long value = Long.parseLong(firstField);
if (value < 0) { return checkPositiveOrZero(value, "Content-Length value");
// Reject the message as invalid
throw new IllegalArgumentException(
"Content-Length value must be >=0: " + value);
}
return value;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// Reject the message as invalid // Reject the message as invalid
throw new IllegalArgumentException( throw new IllegalArgumentException(

View File

@ -16,6 +16,7 @@
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero; import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -108,12 +109,7 @@ public class HttpVersion implements Comparable<HttpVersion> {
* the {@code "Connection"} header is set to {@code "close"} explicitly. * the {@code "Connection"} header is set to {@code "close"} explicitly.
*/ */
public HttpVersion(String text, boolean keepAliveDefault) { public HttpVersion(String text, boolean keepAliveDefault) {
requireNonNull(text, "text"); text = checkNonEmptyAfterTrim(text, "text").toUpperCase();
text = text.trim().toUpperCase();
if (text.isEmpty()) {
throw new IllegalArgumentException("empty text");
}
Matcher m = VERSION_PATTERN.matcher(text); Matcher m = VERSION_PATTERN.matcher(text);
if (!m.matches()) { if (!m.matches()) {
@ -148,12 +144,7 @@ public class HttpVersion implements Comparable<HttpVersion> {
private HttpVersion( private HttpVersion(
String protocolName, int majorVersion, int minorVersion, String protocolName, int majorVersion, int minorVersion,
boolean keepAliveDefault, boolean bytes) { boolean keepAliveDefault, boolean bytes) {
requireNonNull(protocolName, "protocolName"); protocolName = checkNonEmptyAfterTrim(protocolName, "protocolName").toUpperCase();
protocolName = protocolName.trim().toUpperCase();
if (protocolName.isEmpty()) {
throw new IllegalArgumentException("empty protocolName");
}
for (int i = 0; i < protocolName.length(); i ++) { for (int i = 0; i < protocolName.length(); i ++) {
if (Character.isISOControl(protocolName.charAt(i)) || if (Character.isISOControl(protocolName.charAt(i)) ||

View File

@ -19,7 +19,7 @@ import io.netty.handler.codec.http.cookie.CookieHeaderNames.SameSite;
import static io.netty.handler.codec.http.cookie.CookieUtil.stringBuilder; import static io.netty.handler.codec.http.cookie.CookieUtil.stringBuilder;
import static io.netty.handler.codec.http.cookie.CookieUtil.validateAttributeValue; import static io.netty.handler.codec.http.cookie.CookieUtil.validateAttributeValue;
import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
/** /**
@ -41,11 +41,7 @@ public class DefaultCookie implements Cookie {
* Creates a new cookie with the specified name and value. * Creates a new cookie with the specified name and value.
*/ */
public DefaultCookie(String name, String value) { public DefaultCookie(String name, String value) {
name = requireNonNull(name, "name").trim(); this.name = checkNonEmptyAfterTrim(name, "name");
if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}
this.name = name;
setValue(value); setValue(value);
} }

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.http.cors; package io.netty.handler.codec.http.cors;
import static io.netty.util.internal.ObjectUtil.checkNotNullWithIAE;
import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
@ -378,10 +380,7 @@ public final class CorsConfigBuilder {
* @param value the value that will be returned when the call method is invoked. * @param value the value that will be returned when the call method is invoked.
*/ */
private ConstantValueGenerator(final Object value) { private ConstantValueGenerator(final Object value) {
if (value == null) { this.value = checkNotNullWithIAE(value, "value");
throw new IllegalArgumentException("value must not be null");
}
this.value = value;
} }
@Override @Override

View File

@ -16,6 +16,7 @@
package io.netty.handler.codec.http.multipart; package io.netty.handler.codec.http.multipart;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelException; import io.netty.channel.ChannelException;
@ -47,11 +48,7 @@ public abstract class AbstractHttpData extends AbstractReferenceCounted implemen
name = REPLACE_PATTERN.matcher(name).replaceAll(" "); name = REPLACE_PATTERN.matcher(name).replaceAll(" ");
name = STRIP_PATTERN.matcher(name).replaceAll(""); name = STRIP_PATTERN.matcher(name).replaceAll("");
if (name.isEmpty()) { this.name = checkNonEmpty(name, "name");
throw new IllegalArgumentException("empty name");
}
this.name = name;
if (charset != null) { if (charset != null) {
setCharset(charset); setCharset(charset);
} }

View File

@ -15,7 +15,7 @@
*/ */
package io.netty.handler.codec.http.websocketx.extensions; package io.netty.handler.codec.http.websocketx.extensions;
import static java.util.Objects.requireNonNull; import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -52,11 +52,7 @@ public class WebSocketClientExtensionHandler implements ChannelHandler {
* with fallback configuration. * with fallback configuration.
*/ */
public WebSocketClientExtensionHandler(WebSocketClientExtensionHandshaker... extensionHandshakers) { public WebSocketClientExtensionHandler(WebSocketClientExtensionHandshaker... extensionHandshakers) {
requireNonNull(extensionHandshakers, "extensionHandshakers"); this.extensionHandshakers = Arrays.asList(checkNonEmpty(extensionHandshakers, "extensionHandshakers"));
if (extensionHandshakers.length == 0) {
throw new IllegalArgumentException("extensionHandshakers must contains at least one handshaker");
}
this.extensionHandshakers = Arrays.asList(extensionHandshakers);
} }
@Override @Override

View File

@ -15,7 +15,7 @@
*/ */
package io.netty.handler.codec.http.websocketx.extensions; package io.netty.handler.codec.http.websocketx.extensions;
import static java.util.Objects.requireNonNull; import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
@ -56,11 +56,7 @@ public class WebSocketServerExtensionHandler implements ChannelHandler {
* with fallback configuration. * with fallback configuration.
*/ */
public WebSocketServerExtensionHandler(WebSocketServerExtensionHandshaker... extensionHandshakers) { public WebSocketServerExtensionHandler(WebSocketServerExtensionHandshaker... extensionHandshakers) {
requireNonNull(extensionHandshakers, "extensionHandshakers"); this.extensionHandshakers = Arrays.asList(checkNonEmpty(extensionHandshakers, "extensionHandshakers"));
if (extensionHandshakers.length == 0) {
throw new IllegalArgumentException("extensionHandshakers must contains at least one handshaker");
}
this.extensionHandshakers = Arrays.asList(extensionHandshakers);
} }
@Override @Override

View File

@ -15,7 +15,7 @@
*/ */
package io.netty.handler.codec.rtsp; package io.netty.handler.codec.rtsp;
import static java.util.Objects.requireNonNull; import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
@ -119,13 +119,7 @@ public final class RtspMethods {
* will be returned. Otherwise, a new instance will be returned. * will be returned. Otherwise, a new instance will be returned.
*/ */
public static HttpMethod valueOf(String name) { public static HttpMethod valueOf(String name) {
requireNonNull(name, "name"); name = checkNonEmptyAfterTrim(name, "name").toUpperCase();
name = name.trim().toUpperCase();
if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}
HttpMethod result = methodMap.get(name); HttpMethod result = methodMap.get(name);
if (result != null) { if (result != null) {
return result; return result;