Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 4530812

Browse files
committed
All handlers except file
1 parent 59a17e1 commit 4530812

23 files changed

+477
-376
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.kttdevelopment</groupId>
88
<artifactId>simplehttpserver</artifactId>
9-
<version>03.05.07</version>
9+
<version>03.05.08</version>
1010
<packaging>jar</packaging>
1111

1212
<url>https://github.com/Ktt-Development/simplehttpserver</url>

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @see SimpleHttpExchange
1010
* @see Builder
1111
* @since 02.00.00
12-
* @version 03.05.00
12+
* @version 03.05.08
1313
* @author Ktt Development
1414
*/
1515
public class SimpleHttpCookie {
@@ -61,7 +61,6 @@ private SimpleHttpCookie(final String name, final String value, final String dom
6161
this.httpOnly = httpOnly;
6262
}
6363

64-
@SuppressWarnings("SpellCheckingInspection")
6564
private final SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
6665

6766
/**

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServer.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @see SimpleHttpsServer
1818
* @see SimpleHttpHandler
1919
* @since 02.00.00
20-
* @version 03.05.02
20+
* @version 03.05.08
2121
* @author Ktt Development
2222
*/
2323
@SuppressWarnings("SpellCheckingInspection")
@@ -270,12 +270,15 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
270270

271271
/**
272272
* Creates an empty context.
273+
* <br>
274+
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
273275
*
274276
* @param context the context
275277
* @return the http context associated with the context
276-
* @throws IllegalArgumentException if the context is invalid or taken
278+
* @throws IllegalArgumentException if the context is invalid or taken*
277279
* @throws NullPointerException if the context is null
278280
*
281+
*
279282
* @see HttpContext
280283
* @see #createContext(String, HttpHandler)
281284
* @see #removeContext(String)
@@ -287,11 +290,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
287290

288291
/**
289292
* Creates a context mapped to a specified {@link HttpHandler}.
293+
* <br>
294+
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
290295
*
291296
* @param context the context
292297
* @param handler the handler
293298
* @return the http context associated with the context
294-
* @throws IllegalArgumentException if the context is invalid or taken
299+
* @throws IllegalArgumentException if the context is invalid or taken*
295300
* @throws NullPointerException if the context is null
296301
*
297302
* @see HttpContext
@@ -308,11 +313,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
308313

309314
/**
310315
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
316+
* <br>
317+
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
311318
*
312319
* @param context the context
313320
* @param authenticator authenticator
314321
* @return the http context associated with the context
315-
* @throws IllegalArgumentException if the context is invalid or taken
322+
* @throws IllegalArgumentException if the context is invalid or taken*
316323
* @throws NullPointerException if the context is null
317324
*
318325
* @see HttpContext
@@ -327,12 +334,14 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
327334

328335
/**
329336
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
337+
* <br>
338+
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
330339
*
331340
* @param context the context
332341
* @param handler the handler
333342
* @param authenticator authenticator
334343
* @return the http context associated with the context
335-
* @throws IllegalArgumentException if the context is invalid or taken
344+
* @throws IllegalArgumentException if the context is invalid or taken*
336345
* @throws NullPointerException if the context is null
337346
*
338347
* @see HttpContext

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
import java.net.InetSocketAddress;
88
import java.util.*;
99
import java.util.concurrent.Executor;
10-
import java.util.function.BiConsumer;
1110

1211
/**
1312
* Implementation for {@link SimpleHttpServer}. Applications do not use this class.
1413
*
1514
* @see SimpleHttpServer
1615
* @since 02.00.00
17-
* @version 03.05.03
16+
* @version 03.05.08
1817
* @author Ktt Development
1918
*/
2019
@SuppressWarnings("SpellCheckingInspection")

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpsServerImpl.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @see SimpleHttpsServer
1515
* @since 03.04.00
16-
* @version 03.05.03
16+
* @version 03.05.08
1717
* @author Ktt Development
1818
*/
1919
@SuppressWarnings("SpellCheckingInspection")
@@ -73,7 +73,7 @@ public final HttpsConfigurator getHttpsConfigurator(){
7373
return server.getHttpsConfigurator();
7474
}
7575

76-
// region copySimpleHttpServerImpl
76+
// region copy
7777

7878
@Override
7979
public synchronized final InetSocketAddress bind(final int port) throws IOException{
@@ -159,15 +159,18 @@ public synchronized final HttpContext createContext(final String context, final
159159

160160
@Override
161161
public synchronized final HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator){
162-
if(!ContextUtil.getContext(context,true,false).equals("/") && handler instanceof RootHandler)
162+
final String ct = ContextUtil.getContext(context,true,false);
163+
if(!ct.equals("/") && handler instanceof RootHandler)
163164
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");
164165

165166
final HttpHandler wrapper = exchange -> {
166167
handle(exchange);
167168
handler.handle(exchange);
168169
};
169170

170-
final HttpContext hc = server.createContext(ContextUtil.getContext(context,true,false),wrapper);
171+
final HttpContext hc = server.createContext(ct);
172+
173+
hc.setHandler(wrapper);
171174
contexts.put(hc,handler);
172175

173176
if(authenticator != null)
@@ -264,7 +267,7 @@ public synchronized final void stop(final int delay){
264267
}
265268
}
266269

267-
// endregion
270+
// endregion copy
268271

269272
@Override
270273
public String toString(){

src/main/java/com/kttdevelopment/simplehttpserver/handler/DirectoryEntry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @version 03.05.06
2222
* @author Ktt Development
2323
*/
24+
@SuppressWarnings("SpellCheckingInspection")
2425
class DirectoryEntry {
2526

2627
private final File directory;

src/main/java/com/kttdevelopment/simplehttpserver/var/HttpCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @version 01.00.00
88
* @author Ktt Development
99
*/
10-
@SuppressWarnings({"unused", "SpellCheckingInspection"})
10+
@SuppressWarnings({"unused", "SpellCheckingInspection", "RedundantSuppression"})
1111
public abstract class HttpCode {
1212

1313
public static final int HTTP_Continue = 100;

src/main/java/com/kttdevelopment/simplehttpserver/var/RequestMethod.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* @version 01.00.00
88
* @author Ktt Development
99
*/
10-
@SuppressWarnings("SpellCheckingInspection")
1110
public enum RequestMethod {
1211
GET,
1312
HEAD,

src/test/java/handlers/FileHandlerTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Map;
1515
import java.util.concurrent.ExecutionException;
1616

17+
@SuppressWarnings("SpellCheckingInspection")
1718
public class FileHandlerTests {
1819

1920
@SuppressWarnings("ResultOfMethodCallIgnored")

src/test/java/handlers/PredicateHandlerTests.java

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.kttdevelopment.simplehttpserver.SimpleHttpHandler;
44
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
55
import com.kttdevelopment.simplehttpserver.handler.PredicateHandler;
6-
import com.kttdevelopment.simplehttpserver.handler.RootHandler;
76
import org.junit.Assert;
87
import org.junit.Test;
98

@@ -12,11 +11,11 @@
1211
import java.net.http.*;
1312
import java.util.concurrent.ExecutionException;
1413

15-
public class PredicateHandlerTests {
14+
public final class PredicateHandlerTests {
1615

1716
@Test
1817
public void predicate() throws IOException, ExecutionException, InterruptedException{
19-
final int port = 30004;
18+
final int port = 8080;
2019

2120
final SimpleHttpServer server = SimpleHttpServer.create(port);
2221

@@ -28,52 +27,18 @@ public void predicate() throws IOException, ExecutionException, InterruptedExcep
2827

2928
Assert.assertFalse("Server did not contain a temporary context", server.getContexts().isEmpty());
3029

31-
String url = "http://localhost:" + port + context;
30+
final String url = "http://localhost:" + port + context;
3231

33-
HttpRequest request = HttpRequest.newBuilder()
34-
.uri(URI.create(url))
35-
.build();
32+
final HttpRequest request = HttpRequest.newBuilder()
33+
.uri(URI.create(url))
34+
.build();
3635

37-
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
38-
.thenApply(HttpResponse::body).get();
36+
final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
37+
.thenApply(HttpResponse::body).get();
3938

4039
Assert.assertEquals("Server response did not match client response", condition ? "A" : "B", response);
4140

4241
server.stop();
4342
}
4443

45-
@Test
46-
public void root() throws IOException, ExecutionException, InterruptedException{
47-
final int port = 30005;
48-
49-
final SimpleHttpServer server = SimpleHttpServer.create(port);
50-
String context = "";
51-
server.createContext(context,new RootHandler((SimpleHttpHandler) exchange -> exchange.send("A"), (SimpleHttpHandler) exchange -> exchange.send("B")));
52-
server.start();
53-
54-
Assert.assertFalse("Server did not contain a temporary context", server.getContexts().isEmpty());
55-
56-
String url = "http://localhost:" + port;
57-
58-
HttpRequest request = HttpRequest.newBuilder()
59-
.uri(URI.create(url))
60-
.build();
61-
62-
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
63-
.thenApply(HttpResponse::body).get();
64-
65-
Assert.assertEquals("Server root response did not match client root response","A",response);
66-
67-
request = HttpRequest.newBuilder()
68-
.uri(URI.create(url + server.getRandomContext()))
69-
.build();
70-
71-
response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
72-
.thenApply(HttpResponse::body).get();
73-
74-
Assert.assertEquals("Server else response did not match client else response","B",response);
75-
76-
server.stop();
77-
}
78-
7944
}

src/test/java/handlers/RedirectHandlerTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import java.net.http.*;
1212
import java.util.concurrent.ExecutionException;
1313

14-
public class RedirectHandlerTests {
14+
public final class RedirectHandlerTests {
1515

1616
@Test
17-
public void redirectToGoogle() throws IOException, ExecutionException, InterruptedException{
18-
final int port = 30003;
17+
public final void redirectToGoogle() throws IOException, ExecutionException, InterruptedException{
18+
final int port = 8080;
1919

2020
final SimpleHttpServer server = SimpleHttpServer.create(port);
2121

@@ -25,16 +25,16 @@ public void redirectToGoogle() throws IOException, ExecutionException, Interrupt
2525

2626
Assert.assertFalse("Server did not contain a temporary context", server.getContexts().isEmpty());
2727

28-
String url = "http://localhost:" + port + context;
28+
final String url = "http://localhost:" + port + context;
2929

30-
HttpRequest request = HttpRequest.newBuilder()
31-
.uri(URI.create(url))
32-
.build();
30+
final HttpRequest request = HttpRequest.newBuilder()
31+
.uri(URI.create(url))
32+
.build();
3333

3434
final HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
3535

36-
int response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
37-
.thenApply(HttpResponse::statusCode).get();
36+
final int response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
37+
.thenApply(HttpResponse::statusCode).get();
3838

3939
Assert.assertEquals("Client responded with redirect code (302 HTTP FOUND) not 200 HTTP OK",HttpCode.HTTP_OK, response);
4040

src/test/java/handlers/SSEHandlerTests.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import java.time.Duration;
1111
import java.util.concurrent.*;
1212

13-
public class SSEHandlerTests {
13+
public final class SSEHandlerTests {
1414

1515
@SuppressWarnings({"unchecked", "Convert2Lambda", "rawtypes"})
1616
@Test
17-
public void test() throws IOException, ExecutionException, InterruptedException{
18-
final int port = 30006;
17+
public final void test() throws IOException, ExecutionException, InterruptedException{
18+
final int port = 8080;
1919

2020
final SimpleHttpServer server = SimpleHttpServer.create(port);
2121
String context = "";
@@ -25,38 +25,39 @@ public void test() throws IOException, ExecutionException, InterruptedException{
2525
server.start();
2626

2727
HttpRequest request = HttpRequest.newBuilder()
28-
.uri(URI.create("http://localhost:" + port))
29-
.timeout(Duration.ofSeconds(2))
30-
.build();
28+
.uri(URI.create("http://localhost:" + port))
29+
.timeout(Duration.ofSeconds(2))
30+
.build();
3131

3232
final HttpClient client = HttpClient.newHttpClient();
3333
HttpResponse<InputStream> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()).get();
3434

3535
final String result = "result";
3636
handler.push(result);
3737

38-
BufferedReader IN = new BufferedReader(new InputStreamReader(response.body()));
39-
StringBuilder OUT = new StringBuilder();
38+
final BufferedReader IN = new BufferedReader(new InputStreamReader(response.body()));
39+
final StringBuilder OUT = new StringBuilder();
4040

4141
final Duration dur = Duration.ofSeconds(2);
4242
final ExecutorService executor = Executors.newSingleThreadExecutor();
4343
final Future<String> future = executor.submit(new Callable() {
4444
@Override
45-
public String call(){
45+
public final String call(){
4646
String ln;
4747
try{
4848
while((ln = IN.readLine()) != null)
49-
OUT.append(ln).append('\n');
49+
OUT.append(ln).append('\n');
5050
}catch(final IOException ignored){
5151
Assert.fail("Unable to read input stream from client");
5252
}
5353
return OUT.toString();
5454
}
5555
});
5656

57-
try {
57+
try{
5858
future.get(dur.toMillis(), TimeUnit.MILLISECONDS);
59-
} catch (TimeoutException e) {
59+
}catch(final TimeoutException ignored){
60+
}finally{
6061
future.cancel(true);
6162
}
6263

0 commit comments

Comments
 (0)