fix(mainhttp): send 404 at end of pipeline

This should allow other handlers that accept HTTP to also get their chance
This commit is contained in:
Johannes Frohnmeyer 2024-10-14 22:21:41 +02:00
parent 0c22ba6356
commit 6bb91ddba0
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 22 additions and 3 deletions

View File

@ -41,9 +41,16 @@ public class HttpDecoder extends ChannelInboundHandlerAdapter {
buf.readBytes(data); buf.readBytes(data);
buf.release(); buf.release();
// Process request // Process request
byte[] response = MainHttp.handle(data);
if (response == null) {
// We don't handle this request, maybe some other mod does
passOn = true;
ctx.channel().pipeline().addLast("libjf_http_404", new NotFoundAdapter());
return;
}
ctx.pipeline() ctx.pipeline()
.firstContext() .firstContext()
.writeAndFlush(Unpooled.wrappedBuffer(MainHttp.handle(data))) .writeAndFlush(Unpooled.wrappedBuffer(response))
.addListener(ChannelFutureListener.CLOSE); .addListener(ChannelFutureListener.CLOSE);
} catch (RuntimeException re) { } catch (RuntimeException re) {
MainHttp.LOGGER.error("Could not process HTTP", re); MainHttp.LOGGER.error("Could not process HTTP", re);
@ -55,4 +62,15 @@ public class HttpDecoder extends ChannelInboundHandlerAdapter {
} }
} }
} }
private static class NotFoundAdapter extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// We didn't handle this request, and neither did any other mod
ctx.pipeline()
.firstContext()
.writeAndFlush(Unpooled.wrappedBuffer(MainHttp.NOT_FOUND))
.addListener(ChannelFutureListener.CLOSE);
}
}
} }

View File

@ -19,7 +19,7 @@ public class MainHttp {
.stream() .stream()
.filter(MainHttpHandler::isActive) .filter(MainHttpHandler::isActive)
.toList(); .toList();
private static final byte[] NOT_FOUND = """ public static final byte[] NOT_FOUND = """
HTTP/1.1 404 Not Found HTTP/1.1 404 Not Found
Connection: keep-alive Connection: keep-alive
Content-Length: 0 Content-Length: 0

View File

@ -11,6 +11,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
public class ServerNetworkIo$1Mixin { public class ServerNetworkIo$1Mixin {
@Inject(method = "initChannel(Lio/netty/channel/Channel;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;addHandlers(Lio/netty/channel/ChannelPipeline;Lnet/minecraft/network/NetworkSide;ZLnet/minecraft/network/handler/PacketSizeLogger;)V")) @Inject(method = "initChannel(Lio/netty/channel/Channel;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;addHandlers(Lio/netty/channel/ChannelPipeline;Lnet/minecraft/network/NetworkSide;ZLnet/minecraft/network/handler/PacketSizeLogger;)V"))
private void inject(Channel channel, CallbackInfo ci) { private void inject(Channel channel, CallbackInfo ci) {
channel.pipeline().addAfter("legacy_query", "libjf_http", new HttpDecoder()); // directly after legacy_query
channel.pipeline().addLast("libjf_http", new HttpDecoder());
} }
} }