Skip to content

Commit

Permalink
Save HTTP Netty types unlucky type checks (netty/netty#12708)
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 authored and vietj committed Nov 7, 2022
1 parent 1807f72 commit 5ea7bc2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main/java/io/vertx/core/http/impl/Http1xConnectionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
package io.vertx.core.http.impl;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.FileRegion;
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
Expand Down Expand Up @@ -189,11 +193,26 @@ static long sizeOf(WebSocketFrame obj) {
}

static long sizeOf(Object obj) {
// https://github.com/netty/netty/issues/12708
// try first Netty HTTP singleton types, without any instanceof/checkcast bytecodes
if (obj == Unpooled.EMPTY_BUFFER || obj == LastHttpContent.EMPTY_LAST_CONTENT) {
return 0;
}
// try known vertx (non-interface) types: bi-morphic
if (obj instanceof AssembledHttpResponse) {
return ((AssembledHttpResponse) obj).content().readableBytes();
}
if (obj instanceof Buffer) {
return ((Buffer) obj).length();
} else if (obj instanceof ByteBuf) {
return ((ByteBuf) obj).readableBytes();
} else if (obj instanceof HttpContent) {
// see Netty's HttpObjectEncoder::acceptOutboundMessage:
// the order of checks is the same!
} else if (obj instanceof FullHttpMessage) {
return ((FullHttpMessage) obj).content().readableBytes();
} else if (obj instanceof LastHttpContent) {
return ((LastHttpContent) obj).content().readableBytes();
} else if (obj instanceof HttpContent) {
return ((HttpContent) obj).content().readableBytes();
} else if (obj instanceof WebSocketFrame) {
return sizeOf((WebSocketFrame) obj);
Expand Down

0 comments on commit 5ea7bc2

Please sign in to comment.