From b606e86e005c98e6b4039bf8e4541ed76273a0da Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 12 Oct 2009 23:33:06 +0200 Subject: [PATCH] Added ways to get the addresses of the request's connection. --- src/dolda/jsvc/Request.java | 3 +++ src/dolda/jsvc/j2ee/J2eeRequest.java | 20 ++++++++++++++++++++ src/dolda/jsvc/test/TestResponder.java | 1 + 3 files changed, 24 insertions(+) diff --git a/src/dolda/jsvc/Request.java b/src/dolda/jsvc/Request.java index 4dc9cf6..6c779ac 100644 --- a/src/dolda/jsvc/Request.java +++ b/src/dolda/jsvc/Request.java @@ -2,6 +2,7 @@ package dolda.jsvc; import java.io.*; import java.net.URL; +import java.net.SocketAddress; import java.util.Map; public interface Request { @@ -22,4 +23,6 @@ public interface Request { /* Misc. */ public Map props(); public ServerContext ctx(); + public SocketAddress remoteaddr(); + public SocketAddress localaddr(); } diff --git a/src/dolda/jsvc/j2ee/J2eeRequest.java b/src/dolda/jsvc/j2ee/J2eeRequest.java index 1e7d789..9811591 100644 --- a/src/dolda/jsvc/j2ee/J2eeRequest.java +++ b/src/dolda/jsvc/j2ee/J2eeRequest.java @@ -52,6 +52,26 @@ public class J2eeRequest extends ResponseBuffer { return(new J2eeContext(cfg, req, resp)); } + public SocketAddress remoteaddr() { + try { + return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort())); + } catch(UnknownHostException e) { + /* req.getRemoteAddr should always be a valid IP address, + * so this should never happen. */ + throw(new Error(e)); + } + } + + public SocketAddress localaddr() { + try { + return(new InetSocketAddress(InetAddress.getByName(req.getLocalAddr()), req.getLocalPort())); + } catch(UnknownHostException e) { + /* req.getRemoteAddr should always be a valid IP address, + * so this should never happen. */ + throw(new Error(e)); + } + } + public URL url() { return(url); } diff --git a/src/dolda/jsvc/test/TestResponder.java b/src/dolda/jsvc/test/TestResponder.java index d466156..d7541b4 100644 --- a/src/dolda/jsvc/test/TestResponder.java +++ b/src/dolda/jsvc/test/TestResponder.java @@ -20,6 +20,7 @@ public class TestResponder implements Responder { out.println("Bardslen."); out.println(req.inheaders()); out.println(req.ctx().starttime()); + out.println(req.remoteaddr() + "<->" + req.localaddr()); out.println(""); out.println(""); out.flush(); -- 2.11.0