Skip to content

Commit 4a9465b

Browse files
committed
Interrupted request leads to NPE using Netty + AccessLog fix jooby-project#2260
- Make sure Context.remoteAddress returns a non-null value - Update access log to get remote address at the beginning of requests (less chance of loosing it)
1 parent bf3136a commit 4a9465b

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

jooby/src/main/java/io/jooby/AccessLogHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,11 @@ public AccessLogHandler() {
218218
@Nonnull @Override public Route.Handler apply(@Nonnull Route.Handler next) {
219219
long timestamp = System.currentTimeMillis();
220220
return ctx -> {
221+
// Take remote address here (less chances of loosing it on interrupted requests).
222+
String remoteAddr = ctx.getRemoteAddress();
221223
ctx.onComplete(context -> {
222224
StringBuilder sb = new StringBuilder(MESSAGE_SIZE);
223-
sb.append(ctx.getRemoteAddress());
225+
sb.append(remoteAddr);
224226
sb.append(SP).append(DASH).append(SP);
225227
sb.append(userId.apply(ctx));
226228
sb.append(SP);

jooby/src/main/java/io/jooby/Context.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,8 @@ public interface Context extends Registry {
553553
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
554554
* please consider to set {@link Router#setTrustProxy(boolean)} option.
555555
*
556-
* @return The IP address of the client or last proxy that sent the request.
556+
* @return The IP address of the client or last proxy that sent the request or
557+
* <code>empty string</code> for interrupted requests.
557558
*/
558559
@Nonnull String getRemoteAddress();
559560

modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.io.InputStream;
5555
import java.io.OutputStream;
5656
import java.io.PrintWriter;
57+
import java.net.InetSocketAddress;
5758
import java.nio.ByteBuffer;
5859
import java.nio.channels.Channels;
5960
import java.nio.channels.FileChannel;
@@ -67,6 +68,7 @@
6768
import java.util.LinkedHashMap;
6869
import java.util.List;
6970
import java.util.Map;
71+
import java.util.Optional;
7072
import java.util.concurrent.Executor;
7173

7274
import static org.eclipse.jetty.http.HttpHeader.CONTENT_TYPE;
@@ -262,7 +264,13 @@ public JettyContext(Request request, Router router, int bufferSize, long maxRequ
262264
}
263265

264266
@Nonnull @Override public String getRemoteAddress() {
265-
return remoteAddress == null ? request.getRemoteAddr() : remoteAddress;
267+
if (remoteAddress == null) {
268+
String remoteAddr = Optional.ofNullable(request.getRemoteAddr())
269+
.orElse("")
270+
.trim();
271+
return remoteAddr;
272+
}
273+
return remoteAddress;
266274
}
267275

268276
@Nonnull @Override public Context setRemoteAddress(@Nonnull String remoteAddress) {

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,13 @@ boolean isHttpGet() {
259259

260260
@Nonnull @Override public String getRemoteAddress() {
261261
if (this.remoteAddress == null) {
262-
InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
263-
String hostAddress = remoteAddress.getAddress().getHostAddress();
264-
int i = hostAddress.lastIndexOf('%');
265-
this.remoteAddress = i > 0 ? hostAddress.substring(0, i) : hostAddress;
262+
InetSocketAddress inetAddress = (InetSocketAddress) ctx.channel().remoteAddress();
263+
if (inetAddress != null) {
264+
String hostAddress = inetAddress.getAddress().getHostAddress();
265+
int i = hostAddress.lastIndexOf('%');
266+
this.remoteAddress = i > 0 ? hostAddress.substring(0, i) : hostAddress;
267+
}
268+
return "";
266269
}
267270
return remoteAddress;
268271
}

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.InputStream;
1717
import java.io.OutputStream;
1818
import java.io.PrintWriter;
19+
import java.net.InetSocketAddress;
1920
import java.nio.ByteBuffer;
2021
import java.nio.channels.Channels;
2122
import java.nio.channels.FileChannel;
@@ -28,6 +29,7 @@
2829
import java.util.Iterator;
2930
import java.util.LinkedHashMap;
3031
import java.util.Map;
32+
import java.util.Optional;
3133
import java.util.concurrent.Executor;
3234

3335
import javax.annotation.Nonnull;
@@ -185,7 +187,14 @@ boolean isHttpGet() {
185187
}
186188

187189
@Nonnull @Override public String getRemoteAddress() {
188-
return remoteAddress == null ? exchange.getSourceAddress().getHostString() : remoteAddress;
190+
if (remoteAddress == null) {
191+
String remoteAddr = Optional.ofNullable(exchange.getSourceAddress())
192+
.map(InetSocketAddress::getHostString)
193+
.orElse("")
194+
.trim();
195+
return remoteAddr;
196+
}
197+
return remoteAddress;
189198
}
190199

191200
@Nonnull @Override public Context setRemoteAddress(@Nonnull String remoteAddress) {

0 commit comments

Comments
 (0)