Skip to content

Commit 12825f2

Browse files
authored
fix: prevent the silent ignoring of exceptions (#97)
1 parent 8154dda commit 12825f2

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

web-impl-netty/src/main/java/eu/cloudnetservice/ext/rest/netty/NettyHttpServerHandler.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@
3939
import io.netty5.handler.codec.http.HttpResponseStatus;
4040
import io.netty5.handler.codec.http.HttpUtil;
4141
import io.netty5.util.AttributeKey;
42+
import io.netty5.util.Resource;
4243
import io.netty5.util.Send;
4344
import io.netty5.util.concurrent.Future;
4445
import java.net.URI;
46+
import java.net.URISyntaxException;
4547
import java.util.HashMap;
4648
import java.util.concurrent.ExecutorService;
49+
import java.util.concurrent.RejectedExecutionException;
4750
import lombok.NonNull;
4851
import org.jetbrains.annotations.Nullable;
4952
import org.slf4j.Logger;
@@ -134,7 +137,22 @@ protected void messageReceived(@NonNull ChannelHandlerContext ctx, @NonNull Http
134137
buffer = null;
135138
}
136139

137-
this.executorService.submit(() -> this.handleMessage(ctx.channel(), msg, buffer));
140+
try {
141+
this.executorService.submit(() -> {
142+
try {
143+
this.handleMessage(ctx.channel(), msg, buffer);
144+
} catch (Throwable throwable) {
145+
NettyHttpServerUtil.sendResponseAndClose(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
146+
LOGGER.debug("Exception caught during processing of http request", throwable);
147+
} finally {
148+
Resource.dispose(buffer);
149+
}
150+
});
151+
} catch (RejectedExecutionException exception) {
152+
NettyHttpServerUtil.sendResponseAndClose(ctx, HttpResponseStatus.SERVICE_UNAVAILABLE);
153+
LOGGER.debug("Unable to submit request to executor service, rejecting request", exception);
154+
Resource.dispose(buffer);
155+
}
138156
}
139157

140158
/**
@@ -150,10 +168,18 @@ private void handleMessage(
150168
@NonNull HttpRequest httpRequest,
151169
@Nullable Send<Buffer> buffer
152170
) {
171+
URI uri;
172+
try {
173+
uri = new URI(httpRequest.uri());
174+
} catch (URISyntaxException exception) {
175+
NettyHttpServerUtil.sendResponseAndClose(channel, HttpResponseStatus.BAD_REQUEST);
176+
LOGGER.debug("Unable to parse request uri '{}', rejecting request", httpRequest.uri(), exception);
177+
return;
178+
}
179+
153180
// if an opaque uri is sent to the server we reject the request immediately as it does
154181
// not contain the required information to properly process the request (especially due
155182
// to the lack of path information which is the base of our internal handling)
156-
var uri = URI.create(httpRequest.uri());
157183
if (uri.isOpaque()) {
158184
NettyHttpServerUtil.sendResponseAndClose(channel, HttpResponseStatus.BAD_REQUEST);
159185
return;

0 commit comments

Comments
 (0)