Skip to content

Commit 38ca22a

Browse files
committed
[#2127]remove regex match for remote address resolution
1 parent 5e564b5 commit 38ca22a

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

framework/src/play/server/PlayHandler.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
import java.io.File;
2020
import java.io.InputStream;
2121
import java.io.UnsupportedEncodingException;
22-
import java.net.InetSocketAddress;
23-
import java.net.URLEncoder;
22+
import java.net.*;
2423
import java.nio.charset.Charset;
2524
import java.security.MessageDigest;
2625
import java.security.NoSuchAlgorithmException;
@@ -547,14 +546,14 @@ public void copyResponse(ChannelHandlerContext ctx, Request request, Response re
547546
}
548547

549548
static String getRemoteIPAddress(MessageEvent e) {
550-
String fullAddress = ((InetSocketAddress) e.getRemoteAddress()).getAddress().getHostAddress();
551-
if (fullAddress.matches("/[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+[:][0-9]+")) {
552-
fullAddress = fullAddress.substring(1);
553-
fullAddress = fullAddress.substring(0, fullAddress.indexOf(":"));
554-
} else if (fullAddress.matches(".*[%].*")) {
555-
fullAddress = fullAddress.substring(0, fullAddress.indexOf("%"));
556-
}
557-
return fullAddress;
549+
final InetAddress inetAddress = ((InetSocketAddress) e.getRemoteAddress()).getAddress();
550+
final byte[] address = inetAddress.getAddress();
551+
try { //create a new inetaddress only from numeric ( without host and interface information)
552+
return InetAddress.getByAddress(address).getHostAddress();
553+
} catch (UnknownHostException unknownHostException) { //should never happen, else address is wrong
554+
Logger.error(unknownHostException,"Error: resolving numeric address: %s", inetAddress.getHostAddress());
555+
}
556+
return null;
558557
}
559558

560559
public Request parseRequest(ChannelHandlerContext ctx, HttpRequest nettyRequest, MessageEvent messageEvent) throws Exception {

0 commit comments

Comments
 (0)