Use ObjectUtil#checkPositive and Add missing JavaDoc In InboundHttp2ToHttpAdapter (#10637)

Motivation:
We can use ObjectUtil#checkPositive instead of manually checking maxContentLength. Also, there was a missing JavaDoc for Http2Exception,

Modification:
Used ObjectUtil#checkPositive for checking maxContentLength.
Added missing JavaDoc.

Result:
More readable code.
This commit is contained in:
Aayush Atharva 2020-10-07 14:42:00 +05:30 committed by GitHub
parent 69b44c6d06
commit 3298f5efee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,7 @@ import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError; import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.handler.codec.http.HttpResponseStatus.OK; import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositive;
/** /**
* This adapter provides just header/data events from the HTTP message flow defined * This adapter provides just header/data events from the HTTP message flow defined
@ -71,12 +72,8 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
protected InboundHttp2ToHttpAdapter(Http2Connection connection, int maxContentLength, protected InboundHttp2ToHttpAdapter(Http2Connection connection, int maxContentLength,
boolean validateHttpHeaders, boolean propagateSettings) { boolean validateHttpHeaders, boolean propagateSettings) {
if (maxContentLength <= 0) {
throw new IllegalArgumentException("maxContentLength: " + maxContentLength + " (expected: > 0)");
}
this.connection = checkNotNull(connection, "connection"); this.connection = checkNotNull(connection, "connection");
this.maxContentLength = maxContentLength; this.maxContentLength = checkPositive(maxContentLength, "maxContentLength");
this.validateHttpHeaders = validateHttpHeaders; this.validateHttpHeaders = validateHttpHeaders;
this.propagateSettings = propagateSettings; this.propagateSettings = propagateSettings;
sendDetector = DEFAULT_SEND_DETECTOR; sendDetector = DEFAULT_SEND_DETECTOR;
@ -147,14 +144,14 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
* <li>{@code false} not to validate HTTP headers in the http-codec</li> * <li>{@code false} not to validate HTTP headers in the http-codec</li>
* </ul> * </ul>
* @param alloc The {@link ByteBufAllocator} to use to generate the content of the message * @param alloc The {@link ByteBufAllocator} to use to generate the content of the message
* @throws Http2Exception * @throws Http2Exception If there is an error when creating {@link FullHttpMessage} from
* {@link Http2Stream} and {@link Http2Headers}
*/ */
protected FullHttpMessage newMessage(Http2Stream stream, Http2Headers headers, boolean validateHttpHeaders, protected FullHttpMessage newMessage(Http2Stream stream, Http2Headers headers, boolean validateHttpHeaders,
ByteBufAllocator alloc) ByteBufAllocator alloc) throws Http2Exception {
throws Http2Exception {
return connection.isServer() ? HttpConversionUtil.toFullHttpRequest(stream.id(), headers, alloc, return connection.isServer() ? HttpConversionUtil.toFullHttpRequest(stream.id(), headers, alloc,
validateHttpHeaders) : HttpConversionUtil.toFullHttpResponse(stream.id(), headers, alloc, validateHttpHeaders) : HttpConversionUtil.toFullHttpResponse(stream.id(), headers, alloc,
validateHttpHeaders); validateHttpHeaders);
} }
/** /**
@ -182,7 +179,8 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
* @throws Http2Exception If the stream id is not in the correct state to process the headers request * @throws Http2Exception If the stream id is not in the correct state to process the headers request
*/ */
protected FullHttpMessage processHeadersBegin(ChannelHandlerContext ctx, Http2Stream stream, Http2Headers headers, protected FullHttpMessage processHeadersBegin(ChannelHandlerContext ctx, Http2Stream stream, Http2Headers headers,
boolean endOfStream, boolean allowAppend, boolean appendToTrailer) throws Http2Exception { boolean endOfStream, boolean allowAppend, boolean appendToTrailer)
throws Http2Exception {
FullHttpMessage msg = getMessage(stream); FullHttpMessage msg = getMessage(stream);
boolean release = true; boolean release = true;
if (msg == null) { if (msg == null) {
@ -227,7 +225,7 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
@Override @Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream)
throws Http2Exception { throws Http2Exception {
Http2Stream stream = connection.stream(streamId); Http2Stream stream = connection.stream(streamId);
FullHttpMessage msg = getMessage(stream); FullHttpMessage msg = getMessage(stream);
if (msg == null) { if (msg == null) {
@ -238,7 +236,7 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
final int dataReadableBytes = data.readableBytes(); final int dataReadableBytes = data.readableBytes();
if (content.readableBytes() > maxContentLength - dataReadableBytes) { if (content.readableBytes() > maxContentLength - dataReadableBytes) {
throw connectionError(INTERNAL_ERROR, throw connectionError(INTERNAL_ERROR,
"Content length exceeded max of %d for stream id %d", maxContentLength, streamId); "Content length exceeded max of %d for stream id %d", maxContentLength, streamId);
} }
content.writeBytes(data, data.readerIndex(), dataReadableBytes); content.writeBytes(data, data.readerIndex(), dataReadableBytes);
@ -253,7 +251,7 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
@Override @Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding,
boolean endOfStream) throws Http2Exception { boolean endOfStream) throws Http2Exception {
Http2Stream stream = connection.stream(streamId); Http2Stream stream = connection.stream(streamId);
FullHttpMessage msg = processHeadersBegin(ctx, stream, headers, endOfStream, true, true); FullHttpMessage msg = processHeadersBegin(ctx, stream, headers, endOfStream, true, true);
if (msg != null) { if (msg != null) {
@ -263,7 +261,8 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
@Override @Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency,
short weight, boolean exclusive, int padding, boolean endOfStream) throws Http2Exception { short weight, boolean exclusive, int padding, boolean endOfStream)
throws Http2Exception {
Http2Stream stream = connection.stream(streamId); Http2Stream stream = connection.stream(streamId);
FullHttpMessage msg = processHeadersBegin(ctx, stream, headers, endOfStream, true, true); FullHttpMessage msg = processHeadersBegin(ctx, stream, headers, endOfStream, true, true);
if (msg != null) { if (msg != null) {
@ -292,7 +291,7 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
@Override @Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId,
Http2Headers headers, int padding) throws Http2Exception { Http2Headers headers, int padding) throws Http2Exception {
// A push promise should not be allowed to add headers to an existing stream // A push promise should not be allowed to add headers to an existing stream
Http2Stream promisedStream = connection.stream(promisedStreamId); Http2Stream promisedStream = connection.stream(promisedStreamId);
if (headers.status() == null) { if (headers.status() == null) {
@ -306,7 +305,7 @@ public class InboundHttp2ToHttpAdapter extends Http2EventAdapter {
FullHttpMessage msg = processHeadersBegin(ctx, promisedStream, headers, false, false, false); FullHttpMessage msg = processHeadersBegin(ctx, promisedStream, headers, false, false, false);
if (msg == null) { if (msg == null) {
throw connectionError(PROTOCOL_ERROR, "Push Promise Frame received for pre-existing stream id %d", throw connectionError(PROTOCOL_ERROR, "Push Promise Frame received for pre-existing stream id %d",
promisedStreamId); promisedStreamId);
} }
msg.headers().setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_PROMISE_ID.text(), streamId); msg.headers().setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_PROMISE_ID.text(), streamId);