From a93c8d9923bd956558afb35d75d9922b790b94ae Mon Sep 17 00:00:00 2001 From: Katsute <58778985+Katsute@users.noreply.github.com> Date: Thu, 20 Oct 2022 21:57:55 -0400 Subject: [PATCH] Adjust HTTP Codes (#18) --- pom.xml | 2 +- .../handler/RedirectHandler.java | 3 +-- .../handler/TemporaryHandler.java | 3 +++ .../handler/file/FileHandler.java | 5 +++- .../handler/throttler/ThrottledHandler.java | 2 ++ .../handler/RedirectTests.java | 2 ++ .../handler/ThrottlerTests.java | 25 +++++++++---------- 7 files changed, 25 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index a738a86..267db36 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.katsute simplehttpserver - 5.0.0-RC-2-SNAPSHOT + 5.0.0-RC-2 diff --git a/src/main/java/dev/katsute/simplehttpserver/handler/RedirectHandler.java b/src/main/java/dev/katsute/simplehttpserver/handler/RedirectHandler.java index 3b8cf3f..02ac84c 100644 --- a/src/main/java/dev/katsute/simplehttpserver/handler/RedirectHandler.java +++ b/src/main/java/dev/katsute/simplehttpserver/handler/RedirectHandler.java @@ -22,7 +22,6 @@ import com.sun.net.httpserver.HttpHandler; import java.io.IOException; -import java.net.HttpURLConnection; import java.util.Objects; /** @@ -50,7 +49,7 @@ public RedirectHandler(final String link){ @Override public final void handle(final HttpExchange exchange) throws IOException{ exchange.getResponseHeaders().set("Location", link); - exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0); + exchange.sendResponseHeaders(308, 0); // permanent redirect exchange.close(); } diff --git a/src/main/java/dev/katsute/simplehttpserver/handler/TemporaryHandler.java b/src/main/java/dev/katsute/simplehttpserver/handler/TemporaryHandler.java index c4b3125..afa8e89 100644 --- a/src/main/java/dev/katsute/simplehttpserver/handler/TemporaryHandler.java +++ b/src/main/java/dev/katsute/simplehttpserver/handler/TemporaryHandler.java @@ -23,6 +23,7 @@ import dev.katsute.simplehttpserver.SimpleHttpServer; import java.io.IOException; +import java.net.HttpURLConnection; import java.util.Objects; /** @@ -69,6 +70,8 @@ public TemporaryHandler(final HttpHandler handler, final long maxTime){ public synchronized final void handle(final HttpExchange exchange) throws IOException{ if(expiry == null || System.currentTimeMillis() < expiry) // expire on first connection or past expiry handler.handle(exchange); + else + exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, 0); exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext()); exchange.close(); } diff --git a/src/main/java/dev/katsute/simplehttpserver/handler/file/FileHandler.java b/src/main/java/dev/katsute/simplehttpserver/handler/file/FileHandler.java index 2fec4cb..680d155 100644 --- a/src/main/java/dev/katsute/simplehttpserver/handler/file/FileHandler.java +++ b/src/main/java/dev/katsute/simplehttpserver/handler/file/FileHandler.java @@ -445,7 +445,10 @@ public final void handle(final HttpExchange exchange) throws IOException{ * @since 5.0.0 */ public void handle(final SimpleHttpExchange exchange, final File source, final byte[] bytes) throws IOException { - exchange.send(bytes, HttpURLConnection.HTTP_OK); + if(source == null) + exchange.send(HttpURLConnection.HTTP_NOT_FOUND); + else + exchange.send(bytes, HttpURLConnection.HTTP_OK); } // diff --git a/src/main/java/dev/katsute/simplehttpserver/handler/throttler/ThrottledHandler.java b/src/main/java/dev/katsute/simplehttpserver/handler/throttler/ThrottledHandler.java index 57f1870..fa4e8a6 100644 --- a/src/main/java/dev/katsute/simplehttpserver/handler/throttler/ThrottledHandler.java +++ b/src/main/java/dev/katsute/simplehttpserver/handler/throttler/ThrottledHandler.java @@ -69,6 +69,8 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException { }finally{ throttler.deleteConnection(exchange); } + else + exchange.send(429); // too many requests exchange.close(); } diff --git a/src/test/java/dev/katsute/simplehttpserver/handler/RedirectTests.java b/src/test/java/dev/katsute/simplehttpserver/handler/RedirectTests.java index 9d43735..6f21020 100644 --- a/src/test/java/dev/katsute/simplehttpserver/handler/RedirectTests.java +++ b/src/test/java/dev/katsute/simplehttpserver/handler/RedirectTests.java @@ -1,5 +1,6 @@ package dev.katsute.simplehttpserver.handler; +import dev.katsute.simplehttpserver.Requests; import dev.katsute.simplehttpserver.SimpleHttpServer; import org.junit.jupiter.api.*; @@ -25,6 +26,7 @@ static void afterAll(){ @Test final void testRedirect() throws IOException{ server.createContext("redirect", new RedirectHandler("https://github.com/KatsuteDev/simplehttpserver")); + Assertions.assertEquals(308, Requests.getCode("http://localhost:8080/redirect")); Assertions.assertEquals("https://github.com/KatsuteDev/simplehttpserver", getRedirectedURL("http://localhost:8080/redirect")); } diff --git a/src/test/java/dev/katsute/simplehttpserver/handler/ThrottlerTests.java b/src/test/java/dev/katsute/simplehttpserver/handler/ThrottlerTests.java index f845a8b..492e9f1 100644 --- a/src/test/java/dev/katsute/simplehttpserver/handler/ThrottlerTests.java +++ b/src/test/java/dev/katsute/simplehttpserver/handler/ThrottlerTests.java @@ -5,7 +5,6 @@ import org.junit.jupiter.api.*; import java.io.IOException; -import java.io.UncheckedIOException; import java.util.concurrent.Executors; final class ThrottlerTests { @@ -52,7 +51,7 @@ final class Exchange { @Test final void testExchange0(){ server.createContext("exchange/0", new ThrottledHandler(ThrottlerTests.ExchangeThrottler(0), handler)); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/exchange/0")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/exchange/0")); } @Test @@ -62,7 +61,7 @@ final void testExchange1() throws InterruptedException{ new Thread(() -> Requests.getCode("http://localhost:8080/exchange/1")).start(); Thread.sleep(250); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/exchange/1")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/exchange/1")); } @Test @@ -99,7 +98,7 @@ final class ServerExchange { @Test final void testExchange0(){ server.createContext("server/exchange/0", new ThrottledHandler(ThrottlerTests.ServerExchangeThrottler(0, -1, false), handler)); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange/0")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange/0")); } @Test @@ -109,7 +108,7 @@ final void testExchange1() throws InterruptedException{ new Thread(() -> Requests.getCode("http://localhost:8080/server/exchange/1")).start(); Thread.sleep(250); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange/1")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange/1")); } @Test @@ -125,7 +124,7 @@ final void testExchangeU() throws InterruptedException{ @Test final void testServerExchange0(){ server.createContext("server/exchange-server/0", new ThrottledHandler(ThrottlerTests.ServerExchangeThrottler(-1, 0, false), handler)); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange-server/0")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange-server/0")); } @Test @@ -135,7 +134,7 @@ final void testServerExchange1() throws InterruptedException{ new Thread(() -> Requests.getCode("http://localhost:8080/server/exchange-server/1")).start(); Thread.sleep(250); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange-server/1")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange-server/1")); } @Test @@ -182,7 +181,7 @@ final void afterEach() throws InterruptedException{ @Test final void testSession0(){ server.createContext("session/0", new ThrottledHandler(ThrottlerTests.SessionThrottler(server.getSessionHandler(), 0), handler)); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/session/0")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/session/0")); } @Test @@ -195,7 +194,7 @@ final void testSession1() throws InterruptedException{ new Thread(() -> Requests.getCode("http://localhost:8080/session/1")).start(); Thread.sleep(250); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/session/1")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/session/1")); } @Test @@ -240,7 +239,7 @@ final void afterEach() throws InterruptedException{ @Test final void testSession0(){ server.createContext("server/session/0", new ThrottledHandler(ThrottlerTests.ServerSessionThrottler(server.getSessionHandler(),0, -1, false), handler)); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session/0")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session/0")); } @Test @@ -253,7 +252,7 @@ final void testSession1() throws InterruptedException{ new Thread(() -> Requests.getCode("http://localhost:8080/server/session/1")).start(); Thread.sleep(250); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session/1")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session/1")); } @Test @@ -272,7 +271,7 @@ final void testSessionU() throws InterruptedException{ @Test final void testServerSession0(){ server.createContext("server/session-server/0", new ThrottledHandler(ThrottlerTests.ServerSessionThrottler(server.getSessionHandler(),-1, 0, false), handler)); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session-server/0")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session-server/0")); } @Test @@ -285,7 +284,7 @@ final void testServerSession1() throws InterruptedException{ new Thread(() -> Requests.getCode("http://localhost:8080/server/session-server/1")).start(); Thread.sleep(250); - Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session-server/1")); + Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session-server/1")); } @Test