Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit a93c8d9

Browse files
authored
Adjust HTTP Codes (#18)
1 parent 0e4c1cb commit a93c8d9

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>dev.katsute</groupId>
88
<artifactId>simplehttpserver</artifactId>
9-
<version>5.0.0-RC-2-SNAPSHOT</version>
9+
<version>5.0.0-RC-2</version>
1010

1111
<profiles>
1212
<profile>

src/main/java/dev/katsute/simplehttpserver/handler/RedirectHandler.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.sun.net.httpserver.HttpHandler;
2323

2424
import java.io.IOException;
25-
import java.net.HttpURLConnection;
2625
import java.util.Objects;
2726

2827
/**
@@ -50,7 +49,7 @@ public RedirectHandler(final String link){
5049
@Override
5150
public final void handle(final HttpExchange exchange) throws IOException{
5251
exchange.getResponseHeaders().set("Location", link);
53-
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
52+
exchange.sendResponseHeaders(308, 0); // permanent redirect
5453
exchange.close();
5554
}
5655

src/main/java/dev/katsute/simplehttpserver/handler/TemporaryHandler.java

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import dev.katsute.simplehttpserver.SimpleHttpServer;
2424

2525
import java.io.IOException;
26+
import java.net.HttpURLConnection;
2627
import java.util.Objects;
2728

2829
/**
@@ -69,6 +70,8 @@ public TemporaryHandler(final HttpHandler handler, final long maxTime){
6970
public synchronized final void handle(final HttpExchange exchange) throws IOException{
7071
if(expiry == null || System.currentTimeMillis() < expiry) // expire on first connection or past expiry
7172
handler.handle(exchange);
73+
else
74+
exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, 0);
7275
exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext());
7376
exchange.close();
7477
}

src/main/java/dev/katsute/simplehttpserver/handler/file/FileHandler.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,10 @@ public final void handle(final HttpExchange exchange) throws IOException{
445445
* @since 5.0.0
446446
*/
447447
public void handle(final SimpleHttpExchange exchange, final File source, final byte[] bytes) throws IOException {
448-
exchange.send(bytes, HttpURLConnection.HTTP_OK);
448+
if(source == null)
449+
exchange.send(HttpURLConnection.HTTP_NOT_FOUND);
450+
else
451+
exchange.send(bytes, HttpURLConnection.HTTP_OK);
449452
}
450453

451454
//

src/main/java/dev/katsute/simplehttpserver/handler/throttler/ThrottledHandler.java

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException {
6969
}finally{
7070
throttler.deleteConnection(exchange);
7171
}
72+
else
73+
exchange.send(429); // too many requests
7274
exchange.close();
7375
}
7476

src/test/java/dev/katsute/simplehttpserver/handler/RedirectTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.katsute.simplehttpserver.handler;
22

3+
import dev.katsute.simplehttpserver.Requests;
34
import dev.katsute.simplehttpserver.SimpleHttpServer;
45
import org.junit.jupiter.api.*;
56

@@ -25,6 +26,7 @@ static void afterAll(){
2526
@Test
2627
final void testRedirect() throws IOException{
2728
server.createContext("redirect", new RedirectHandler("https://github.com/KatsuteDev/simplehttpserver"));
29+
Assertions.assertEquals(308, Requests.getCode("http://localhost:8080/redirect"));
2830
Assertions.assertEquals("https://github.com/KatsuteDev/simplehttpserver", getRedirectedURL("http://localhost:8080/redirect"));
2931
}
3032

src/test/java/dev/katsute/simplehttpserver/handler/ThrottlerTests.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.junit.jupiter.api.*;
66

77
import java.io.IOException;
8-
import java.io.UncheckedIOException;
98
import java.util.concurrent.Executors;
109

1110
final class ThrottlerTests {
@@ -52,7 +51,7 @@ final class Exchange {
5251
@Test
5352
final void testExchange0(){
5453
server.createContext("exchange/0", new ThrottledHandler(ThrottlerTests.ExchangeThrottler(0), handler));
55-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/exchange/0"));
54+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/exchange/0"));
5655
}
5756

5857
@Test
@@ -62,7 +61,7 @@ final void testExchange1() throws InterruptedException{
6261
new Thread(() -> Requests.getCode("http://localhost:8080/exchange/1")).start();
6362
Thread.sleep(250);
6463

65-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/exchange/1"));
64+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/exchange/1"));
6665
}
6766

6867
@Test
@@ -99,7 +98,7 @@ final class ServerExchange {
9998
@Test
10099
final void testExchange0(){
101100
server.createContext("server/exchange/0", new ThrottledHandler(ThrottlerTests.ServerExchangeThrottler(0, -1, false), handler));
102-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange/0"));
101+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange/0"));
103102
}
104103

105104
@Test
@@ -109,7 +108,7 @@ final void testExchange1() throws InterruptedException{
109108
new Thread(() -> Requests.getCode("http://localhost:8080/server/exchange/1")).start();
110109
Thread.sleep(250);
111110

112-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange/1"));
111+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange/1"));
113112
}
114113

115114
@Test
@@ -125,7 +124,7 @@ final void testExchangeU() throws InterruptedException{
125124
@Test
126125
final void testServerExchange0(){
127126
server.createContext("server/exchange-server/0", new ThrottledHandler(ThrottlerTests.ServerExchangeThrottler(-1, 0, false), handler));
128-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange-server/0"));
127+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange-server/0"));
129128
}
130129

131130
@Test
@@ -135,7 +134,7 @@ final void testServerExchange1() throws InterruptedException{
135134
new Thread(() -> Requests.getCode("http://localhost:8080/server/exchange-server/1")).start();
136135
Thread.sleep(250);
137136

138-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange-server/1"));
137+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange-server/1"));
139138
}
140139

141140
@Test
@@ -182,7 +181,7 @@ final void afterEach() throws InterruptedException{
182181
@Test
183182
final void testSession0(){
184183
server.createContext("session/0", new ThrottledHandler(ThrottlerTests.SessionThrottler(server.getSessionHandler(), 0), handler));
185-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/session/0"));
184+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/session/0"));
186185
}
187186

188187
@Test
@@ -195,7 +194,7 @@ final void testSession1() throws InterruptedException{
195194
new Thread(() -> Requests.getCode("http://localhost:8080/session/1")).start();
196195
Thread.sleep(250);
197196

198-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/session/1"));
197+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/session/1"));
199198
}
200199

201200
@Test
@@ -240,7 +239,7 @@ final void afterEach() throws InterruptedException{
240239
@Test
241240
final void testSession0(){
242241
server.createContext("server/session/0", new ThrottledHandler(ThrottlerTests.ServerSessionThrottler(server.getSessionHandler(),0, -1, false), handler));
243-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session/0"));
242+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session/0"));
244243
}
245244

246245
@Test
@@ -253,7 +252,7 @@ final void testSession1() throws InterruptedException{
253252
new Thread(() -> Requests.getCode("http://localhost:8080/server/session/1")).start();
254253
Thread.sleep(250);
255254

256-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session/1"));
255+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session/1"));
257256
}
258257

259258
@Test
@@ -272,7 +271,7 @@ final void testSessionU() throws InterruptedException{
272271
@Test
273272
final void testServerSession0(){
274273
server.createContext("server/session-server/0", new ThrottledHandler(ThrottlerTests.ServerSessionThrottler(server.getSessionHandler(),-1, 0, false), handler));
275-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session-server/0"));
274+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session-server/0"));
276275
}
277276

278277
@Test
@@ -285,7 +284,7 @@ final void testServerSession1() throws InterruptedException{
285284
new Thread(() -> Requests.getCode("http://localhost:8080/server/session-server/1")).start();
286285
Thread.sleep(250);
287286

288-
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session-server/1"));
287+
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session-server/1"));
289288
}
290289

291290
@Test

0 commit comments

Comments
 (0)