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 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
@ -120,10 +120,7 @@ public class HttpMethod implements Comparable<HttpMethod> {
* <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>
*/
public HttpMethod(String name) {
name = requireNonNull(name, "name").trim();
if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}
name = checkNonEmptyAfterTrim(name, "name");
for (int i = 0; i < name.length(); i ++) {
char c = name.charAt(i);

View File

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

View File

@ -16,6 +16,7 @@
package io.netty.handler.codec.http;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
import static java.util.Objects.requireNonNull;
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.
*/
public HttpVersion(String text, boolean keepAliveDefault) {
requireNonNull(text, "text");
text = text.trim().toUpperCase();
if (text.isEmpty()) {
throw new IllegalArgumentException("empty text");
}
text = checkNonEmptyAfterTrim(text, "text").toUpperCase();
Matcher m = VERSION_PATTERN.matcher(text);
if (!m.matches()) {
@ -148,12 +144,7 @@ public class HttpVersion implements Comparable<HttpVersion> {
private HttpVersion(
String protocolName, int majorVersion, int minorVersion,
boolean keepAliveDefault, boolean bytes) {
requireNonNull(protocolName, "protocolName");
protocolName = protocolName.trim().toUpperCase();
if (protocolName.isEmpty()) {
throw new IllegalArgumentException("empty protocolName");
}
protocolName = checkNonEmptyAfterTrim(protocolName, "protocolName").toUpperCase();
for (int i = 0; i < protocolName.length(); 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.validateAttributeValue;
import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
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.
*/
public DefaultCookie(String name, String value) {
name = requireNonNull(name, "name").trim();
if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}
this.name = name;
this.name = checkNonEmptyAfterTrim(name, "name");
setValue(value);
}

View File

@ -15,6 +15,8 @@
*/
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.HttpMethod;
@ -378,10 +380,7 @@ public final class CorsConfigBuilder {
* @param value the value that will be returned when the call method is invoked.
*/
private ConstantValueGenerator(final Object value) {
if (value == null) {
throw new IllegalArgumentException("value must not be null");
}
this.value = value;
this.value = checkNotNullWithIAE(value, "value");
}
@Override

View File

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

View File

@ -15,7 +15,7 @@
*/
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.ChannelHandlerContext;
@ -52,11 +52,7 @@ public class WebSocketClientExtensionHandler implements ChannelHandler {
* with fallback configuration.
*/
public WebSocketClientExtensionHandler(WebSocketClientExtensionHandshaker... extensionHandshakers) {
requireNonNull(extensionHandshakers, "extensionHandshakers");
if (extensionHandshakers.length == 0) {
throw new IllegalArgumentException("extensionHandshakers must contains at least one handshaker");
}
this.extensionHandshakers = Arrays.asList(extensionHandshakers);
this.extensionHandshakers = Arrays.asList(checkNonEmpty(extensionHandshakers, "extensionHandshakers"));
}
@Override

View File

@ -15,7 +15,7 @@
*/
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.ChannelHandler;
@ -56,11 +56,7 @@ public class WebSocketServerExtensionHandler implements ChannelHandler {
* with fallback configuration.
*/
public WebSocketServerExtensionHandler(WebSocketServerExtensionHandshaker... extensionHandshakers) {
requireNonNull(extensionHandshakers, "extensionHandshakers");
if (extensionHandshakers.length == 0) {
throw new IllegalArgumentException("extensionHandshakers must contains at least one handshaker");
}
this.extensionHandshakers = Arrays.asList(extensionHandshakers);
this.extensionHandshakers = Arrays.asList(checkNonEmpty(extensionHandshakers, "extensionHandshakers"));
}
@Override

View File

@ -15,7 +15,7 @@
*/
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;
@ -119,13 +119,7 @@ public final class RtspMethods {
* will be returned. Otherwise, a new instance will be returned.
*/
public static HttpMethod valueOf(String name) {
requireNonNull(name, "name");
name = name.trim().toUpperCase();
if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}
name = checkNonEmptyAfterTrim(name, "name").toUpperCase();
HttpMethod result = methodMap.get(name);
if (result != null) {
return result;