77import express .http .cookie .Cookie ;
88
99import java .io .*;
10+ import java .net .InetAddress ;
11+ import java .net .InetSocketAddress ;
1012import java .net .URI ;
1113import java .util .HashMap ;
1214import java .util .List ;
@@ -20,10 +22,11 @@ public class Request {
2022 private final HttpExchange HTTP_EXCHANGE ;
2123 private final URI URI ; // Request URI
2224 private final InputStream BODY ; // Request body
23- private final Headers HEADERS ; // Request Headers
25+ private final Headers HEADERS ; // Request Headers
2426 private final String CONTENT_TYPE ; // Request content-type
27+ private final long CONTENT_LENGTH ; // Request content-length
2528 private final Authorization AUTH ; // Authorization header parsed
26- private final long CONTENT_LENGTH ;
29+ private final InetSocketAddress INET ; // Client socket adress
2730
2831 private final HashMap <String , Object > MIDDLEWARE ; // Middleware Data
2932 private final HashMap <String , Cookie > COOKIES ; // Request cookies
@@ -44,6 +47,7 @@ public Request(HttpExchange exchange) {
4447 this .URI = exchange .getRequestURI ();
4548 this .HEADERS = exchange .getRequestHeaders ();
4649 this .BODY = exchange .getRequestBody ();
50+ this .INET = exchange .getRemoteAddress ();
4751
4852 // Parse content length
4953 String contentLength = HEADERS .get ("Content-Length" ) != null ? HEADERS .get ("Content-Length" ).get (0 ) : null ;
@@ -158,6 +162,24 @@ public String getHost() {
158162 return HEADERS .get ("Host" ).get (0 );
159163 }
160164
165+ /**
166+ * Returns the InetAdress from the client.
167+ *
168+ * @return The InetAdress.
169+ */
170+ public InetAddress getAddress () {
171+ return INET .getAddress ();
172+ }
173+
174+ /**
175+ * Returns the IP-Adress from the client.
176+ *
177+ * @return The IP-Adress.
178+ */
179+ public String getIp () {
180+ return INET .getAddress ().getHostAddress ();
181+ }
182+
161183 /**
162184 * @return The request content-type.
163185 */
0 commit comments