diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/HttpSession.java b/src/main/java/com/kttdevelopment/simplehttpserver/HttpSession.java index b5d72d7..c2b1e21 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/HttpSession.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/HttpSession.java @@ -30,7 +30,7 @@ public abstract class HttpSession { * @return a {@link HttpSession} * * @since 02.00.00 - * @author KTt Development + * @author Ktt Development */ synchronized static HttpSession create(){ return HttpSessionImpl.createHttpSession(); diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java index 5d41a6d..eb077d8 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java @@ -30,7 +30,7 @@ public class SimpleHttpCookie { /** * Creates an HTTP cookie. All fields except for name, secure, httpOnly, and value can be set to null if unused. * - * @deprecated Use {@link Builder} class instead. This method will be removed in the future. + * @deprecated Use {@link Builder} class instead. This method will be removed in the future * * @param name name of the cookie * @param value value of the cookie @@ -67,7 +67,7 @@ public SimpleHttpCookie(final String name, final String value, final String doma /** * Converts the cookie to a readable string for the cookie header. * - * @deprecated Use {@link #toCookieHeaderString()} instead. + * @deprecated Use {@link #toCookieHeaderString()} instead * * @return cookie header * diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java index 236c038..c34f3af 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java @@ -320,6 +320,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){ * * @param key name of cookie to set * @param value value of cookie + * @throws IllegalArgumentException if the cookie name is reserved by the server * * @see SimpleHttpCookie * @see #setCookie(SimpleHttpCookie) @@ -334,6 +335,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){ * Sets a cookie in the response header. * * @param cookie cookie to set + * @throws IllegalArgumentException if the cookie name is reserved by the server * * @see SimpleHttpCookie * @see #setCookie(String, String) diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeAuthenticator.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeAuthenticator.java deleted file mode 100644 index 7e7372f..0000000 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeAuthenticator.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.kttdevelopment.simplehttpserver; - -/** - * Before each request is processed, the authenticator determines whether to cancel the request or not. The authenticator may also choose to process the request ahead of the handler. By default it returns true. - * - * @see SimpleHttpExchange - * @since 01.00.00 - * @version 02.00.00 - */ -public interface SimpleHttpExchangeAuthenticator { - - /** - * Handles the given request and returns if the exchange was authenticated. - * - * @param exchange exchange from client - * @return if authentication was successful - * - * @see SimpleHttpExchange - * @since 01.00.00 - * @author Ktt Development - */ - @SuppressWarnings("SameReturnValue") - default boolean authenticate(final SimpleHttpExchange exchange){ return true; } - -} diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java index 40771ff..bb87038 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java @@ -316,7 +316,10 @@ public synchronized final void setCookie(final String key, final String value){ @Override public synchronized final void setCookie(final SimpleHttpCookie cookie){ - getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); + final String cstring = cookie.toCookieHeaderString(); + if(cstring.startsWith("__session-id=")) + throw new IllegalArgumentException("The cookie '__session-id' can not be set because it is reserved by the server"); + getResponseHeaders().add("Set-Cookie",cstring); } // @@ -325,11 +328,15 @@ public synchronized final void setCookie(final SimpleHttpCookie cookie){ public synchronized final HttpSession getHttpSession(){ final String sessionId; final HttpSession session; + if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){ session = HttpSession.create(); - setCookie( - new SimpleHttpCookie.Builder("__session-id",session.getSessionID()).build() - ); + final SimpleHttpCookie cookie = + new SimpleHttpCookie.Builder("__session-id",session.getSessionID()) + .setPath("/") + .setHttpOnly(true) + .build(); + getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); // bypass implementation }else{ session = HttpSession.sessions.get(sessionId); } diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpHandler.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpHandler.java index debbfa7..c461f27 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpHandler.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpHandler.java @@ -11,11 +11,11 @@ * @see HttpHandler * @see SimpleHttpExchange * @since 02.00.00 - * @version 02.00.00 + * @version 03.03.00 * @author Ktt Development */ -public abstract class SimpleHttpHandler implements HttpHandler, SimpleHttpExchangeAuthenticator { - +public interface SimpleHttpHandler { +/* /** * Encapsulates the {@link #handle(SimpleHttpExchange)} for the authenticator. Applications do not use this. * @@ -24,16 +24,16 @@ public abstract class SimpleHttpHandler implements HttpHandler, SimpleHttpExchan * * @since 02.00.00 * @author Ktt Development - */ + / @Override - public final void handle(final HttpExchange exchange) throws IOException{ + default final void handle(final HttpExchange exchange) throws IOException{ final SimpleHttpExchange sxe = SimpleHttpExchange.create(exchange); if(authenticate(sxe)) handle(sxe); else sxe.close(); } - +*/ /** * Handlers the given request and generates a response if no exceptions occur. * @@ -43,6 +43,6 @@ public final void handle(final HttpExchange exchange) throws IOException{ * @since 02.00.00 * @author Ktt Development */ - public abstract void handle(final SimpleHttpExchange exchange) throws IOException; + void handle(final SimpleHttpExchange exchange) throws IOException; } diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServer.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServer.java index c6e7d3d..b21eef1 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServer.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServer.java @@ -1,8 +1,6 @@ package com.kttdevelopment.simplehttpserver; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; -import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.*; import java.io.IOException; import java.net.InetSocketAddress; @@ -223,6 +221,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws * * @see HttpContext * @see #createContext(String, HttpHandler) + * @see #createContext(String, SimpleHttpHandler) * @see #removeContext(String) * @see #removeContext(HttpContext) * @since 02.00.00 @@ -242,6 +241,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws * @see HttpContext * @see HttpHandler * @see #createContext(String) + * @see #createContext(String, SimpleHttpHandler) * @see #removeContext(String) * @see #removeContext(HttpContext) * @since 02.00.00 @@ -249,6 +249,92 @@ public static SimpleHttpServer create(final int port, final int backlog) throws */ public abstract HttpContext createContext(final String context, final HttpHandler handler); + /** + * Creates a context mapped to a specific {@link HttpHandler}. + * + * @param context the context + * @param handler the handler + * @return the http context associated with the context + * @throws IllegalArgumentException if the context is invalid or taken + * @throws NullPointerException if the context is null + * + * @see HttpContext + * @see SimpleHttpHandler + * @see #createContext(String) + * @see #createContext(String, HttpHandler) + * @see #removeContext(String) + * @see #removeContext(HttpContext) + * @since 03.03.00 + * @author Ktt Development + */ + public abstract HttpContext createContext(final String context, final SimpleHttpHandler handler); + + // + + /** + * Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}. + * + * @param context the context + * @param authenticator authenticator + * @return the http context associated with the context + * @throws IllegalArgumentException if the context is invalid or taken + * @throws NullPointerException if the context is null + * + * @see HttpContext + * @see Authenticator + * @see #createContext(String, HttpHandler, Authenticator) + * @see #createContext(String, SimpleHttpHandler, Authenticator) + * @see #removeContext(String) + * @see #removeContext(HttpContext) + * @since 03.03.00 + * @author Ktt Development + */ + public abstract HttpContext createContext(final String context, final Authenticator authenticator); + + /** + * Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}. + * + * @param context the context + * @param handler the handler + * @param authenticator authenticator + * @return the http context associated with the context + * @throws IllegalArgumentException if the context is invalid or taken + * @throws NullPointerException if the context is null + * + * @see HttpContext + * @see HttpHandler + * @see Authenticator + * @see #createContext(String, Authenticator) + * @see #createContext(String, SimpleHttpHandler, Authenticator) + * @see #removeContext(String) + * @see #removeContext(HttpContext) + * @since 03.03.00 + * @author Ktt Development + */ + public abstract HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator); + + /** + * Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}. + * + * @param context the context + * @param handler the handler + * @param authenticator authenticator + * @return the http context associated with the context + * @throws IllegalArgumentException if the context is invalid or taken + * @throws NullPointerException if the context is null + * + * @see HttpContext + * @see SimpleHttpHandler + * @see Authenticator + * @see #createContext(String, Authenticator) + * @see #createContext(String, HttpHandler, Authenticator) + * @see #removeContext(String) + * @see #removeContext(HttpContext) + * @since 03.03.00 + * @author Ktt Development + */ + public abstract HttpContext createContext(final String context, final SimpleHttpHandler handler, final Authenticator authenticator); + // /** diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java index 198f53d..79b88bd 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java @@ -116,7 +116,40 @@ public synchronized final HttpContext createContext(final String path, final Htt return context; } - // + @Override + public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler){ + if(!getContext(path).equals("/") && handler instanceof RootHandler) + throw new IllegalArgumentException("RootHandler can only be used at the root '/' context"); + final HttpContext context = server.createContext(getContext(path),(exchange) -> handler.handle(SimpleHttpExchange.create(exchange))); + contexts.put(context,context.getHandler()); + return context; + } + + // + + @Override + public synchronized final HttpContext createContext(final String path, final Authenticator authenticator){ + final HttpContext context = createContext(path); + context.setAuthenticator(authenticator); + return context; + } + + @Override + public synchronized final HttpContext createContext(final String path, final HttpHandler handler, final Authenticator authenticator){ + final HttpContext context = createContext(path,handler); + context.setAuthenticator(authenticator); + return context; + } + + @Override + public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler, final Authenticator authenticator){ + final HttpContext context = createContext(path,handler); + context.setAuthenticator(authenticator); + return context; + } + + + // private String generateRandomContext(){ String targetContext; @@ -151,12 +184,12 @@ public synchronized final HttpContext createTemporaryContext(final HttpHandler h @Override public synchronized final HttpContext createTemporaryContext(final String context){ - return createContext(context, (exchange) -> removeContext(context)); + return createContext(context, (HttpExchange exchange) -> removeContext(context)); } @Override public synchronized final HttpContext createTemporaryContext(final String context, final long maxTime){ - final HttpContext httpContext = createContext(context, (exchange) -> removeContext(context)); + final HttpContext httpContext = createContext(context, (HttpExchange exchange) -> removeContext(context)); new Thread(() -> { try{ @@ -170,7 +203,7 @@ public synchronized final HttpContext createTemporaryContext(final String contex @Override public synchronized final HttpContext createTemporaryContext(final String context, final HttpHandler handler){ - return createContext(context, (exchange) -> { + return createContext(context, (HttpExchange exchange) -> { handler.handle(exchange); removeContext(context); }); @@ -178,7 +211,7 @@ public synchronized final HttpContext createTemporaryContext(final String contex @Override public synchronized final HttpContext createTemporaryContext(final String context, final HttpHandler handler, final long maxTime){ - final HttpContext httpContext = createContext(context, (exchange) -> { + final HttpContext httpContext = createContext(context, (HttpExchange exchange) -> { handler.handle(exchange); removeContext(context); }); diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/handler/FileHandler.java b/src/main/java/com/kttdevelopment/simplehttpserver/handler/FileHandler.java index 4998441..a35245c 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/handler/FileHandler.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/handler/FileHandler.java @@ -24,7 +24,7 @@ * @version 02.00.00 * @author Ktt Development */ -public class FileHandler extends SimpleHttpHandler { +public class FileHandler implements SimpleHttpHandler { private final FileHandlerAdapter adapter; diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/handler/RedirectHandler.java b/src/main/java/com/kttdevelopment/simplehttpserver/handler/RedirectHandler.java index 16303fe..a0c62fe 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/handler/RedirectHandler.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/handler/RedirectHandler.java @@ -15,7 +15,7 @@ * @version 02.00.00 * @author Ktt Development */ -public class RedirectHandler extends SimpleHttpHandler { +public class RedirectHandler implements SimpleHttpHandler { private final String link; diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/handler/SSEHandler.java b/src/main/java/com/kttdevelopment/simplehttpserver/handler/SSEHandler.java index f92b7fc..0634d22 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/handler/SSEHandler.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/handler/SSEHandler.java @@ -12,7 +12,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -public class SSEHandler extends SimpleHttpHandler { +public class SSEHandler implements SimpleHttpHandler { private final List listeners = new ArrayList<>(); private final AtomicInteger eventId = new AtomicInteger(-1);