39
39
import io .netty5 .handler .codec .http .HttpResponseStatus ;
40
40
import io .netty5 .handler .codec .http .HttpUtil ;
41
41
import io .netty5 .util .AttributeKey ;
42
+ import io .netty5 .util .Resource ;
42
43
import io .netty5 .util .Send ;
43
44
import io .netty5 .util .concurrent .Future ;
44
45
import java .net .URI ;
46
+ import java .net .URISyntaxException ;
45
47
import java .util .HashMap ;
46
48
import java .util .concurrent .ExecutorService ;
49
+ import java .util .concurrent .RejectedExecutionException ;
47
50
import lombok .NonNull ;
48
51
import org .jetbrains .annotations .Nullable ;
49
52
import org .slf4j .Logger ;
@@ -134,7 +137,22 @@ protected void messageReceived(@NonNull ChannelHandlerContext ctx, @NonNull Http
134
137
buffer = null ;
135
138
}
136
139
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
+ }
138
156
}
139
157
140
158
/**
@@ -150,10 +168,18 @@ private void handleMessage(
150
168
@ NonNull HttpRequest httpRequest ,
151
169
@ Nullable Send <Buffer > buffer
152
170
) {
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
+
153
180
// if an opaque uri is sent to the server we reject the request immediately as it does
154
181
// not contain the required information to properly process the request (especially due
155
182
// to the lack of path information which is the base of our internal handling)
156
- var uri = URI .create (httpRequest .uri ());
157
183
if (uri .isOpaque ()) {
158
184
NettyHttpServerUtil .sendResponseAndClose (channel , HttpResponseStatus .BAD_REQUEST );
159
185
return ;
0 commit comments