Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only call destroyContext() when LastHttpContent was received and sent #11

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ public class OHttpServerCodec extends MessageToMessageCodec<HttpObject, HttpObje

private HttpRequest request;
private boolean sentResponse;
private OHttpRequestResponseContext oHttpContext;
private OHttpServerRequestResponseContext oHttpContext;
private ByteBuf cumulationBuffer = Unpooled.EMPTY_BUFFER;
private boolean destroyed;

@@ -143,9 +143,16 @@ protected final void decode(ChannelHandlerContext ctx, HttpObject msg, List<Obje
}
if (oHttpContext != null) {
if (msg instanceof HttpContent) {
ByteBuf content = ((HttpContent) msg).content();
cumulationBuffer = MERGE_CUMULATOR.cumulate(content.alloc(), cumulationBuffer, content.retain());
oHttpContext.parse(cumulationBuffer, msg instanceof LastHttpContent, out);
boolean isLast = msg instanceof LastHttpContent;
try {
ByteBuf content = ((HttpContent) msg).content();
cumulationBuffer = MERGE_CUMULATOR.cumulate(content.alloc(), cumulationBuffer, content.retain());
oHttpContext.parse(cumulationBuffer, isLast, out);
} finally {
if (isLast && oHttpContext.receivedLastHttpContent()) {
destroyContext();
}
}
}
} else {
out.add(ReferenceCountUtil.retain(msg));
@@ -157,7 +164,6 @@ protected final void decode(ChannelHandlerContext ctx, HttpObject msg, List<Obje

@Override
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
destroyContext();
if (!sentResponse && request != null) {
sentResponse = true;
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
@@ -196,7 +202,7 @@ protected final void encode(ChannelHandlerContext ctx, HttpObject msg, List<Obje
new DefaultHttpContent(contentBytes);
out.add(content);
} finally {
if (isLast) {
if (isLast && oHttpContext.sendLastHttpContent()) {
destroyContext();
}
}
@@ -233,6 +239,8 @@ private static final class OHttpServerRequestResponseContext extends OHttpReques
private final HybridPublicKeyEncryption encryption;
private final OHttpServerKeys keys;
private OHttpCryptoReceiver receiver;
private boolean receivedLastHttpContent;
private boolean sendLastHttpContent;

public OHttpServerRequestResponseContext(
OHttpVersion version, HybridPublicKeyEncryption encryption, OHttpServerKeys keys) {
@@ -291,5 +299,27 @@ protected void encryptChunk(ByteBuf chunk, int chunkLength, boolean isFinal, Byt
checkPrefixDecoded();
receiver.encrypt(chunk, chunkLength, isFinal, out);
}

/**
* Called when a {@link LastHttpContent} was received.
*
* @return {@code true} if a {@link LastHttpContent} was received and also sent.
* In this case {@link #destroyContext()} must be called.
*/
boolean receivedLastHttpContent() {
receivedLastHttpContent = true;
return sendLastHttpContent;
}

/**
* Called when a {@link LastHttpContent} was sent.
*
* @return {@code true} if a {@link LastHttpContent} was received and also sent.
* In this case {@link #destroyContext()} must be called.
*/
boolean sendLastHttpContent() {
sendLastHttpContent = true;
return receivedLastHttpContent;
}
}
}