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

Commit 2e99c9d

Browse files
committed
QA on Http Server
1 parent 4689f20 commit 2e99c9d

File tree

7 files changed

+53
-44
lines changed

7 files changed

+53
-44
lines changed

src/ktt/lib/httpserver/SimpleHttpExchange.java

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

1212
/**
1313
* <i>This class is a simplified implementation of {@link HttpExchange}</i>. <br>
14-
* This class provides methods to process requests from the client and send a response back. <br><br>
14+
* This class provides methods to process requests from the client and send a response back. The handler will keep executing until a response is sent.<br><br>
1515
* Get/post type requests for <code>application/x-www-form-urlencoded</code> (default) will be a simple key/value map. <br>
1616
* Get/post type requests for <code>multipart/form-data</code> will use an expanded map. <br>
1717
* Example:

src/ktt/lib/httpserver/SimpleHttpExchangeImpl.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,16 @@ static SimpleHttpExchange createSimpleHttpExchange(final HttpExchange exchange){
123123
}
124124
//
125125
hasGet = (rawGet = URI.getQuery()) != null;
126-
getMap = parseWwwFormEnc.apply(rawGet);
126+
getMap = hasGet ? parseWwwFormEnc.apply(rawGet) : new HashMap<>();
127+
127128
//
128129
String OUT;
129130
final InputStream IN = httpExchange.getRequestBody();
130131

131132
try(final Scanner scanner = new Scanner(IN,StandardCharsets.UTF_8)){
132133
OUT = scanner.useDelimiter("\\A").next();
133134
IN.close();
134-
}catch(IOException e){
135+
}catch(IOException | NoSuchElementException e){
135136
OUT = null;
136137
}
137138

@@ -449,29 +450,29 @@ public synchronized final void setAttribute(final String name, final Object valu
449450
public final String toString(){
450451
final StringBuilder OUT = new StringBuilder();
451452
OUT.append("SimpleHttpExchange").append("{");
452-
OUT.append("httpServer") .append("= ") .append(httpServer.toString()) .append(", ");
453-
OUT.append("httpExchange") .append("= ") .append(httpExchange.toString()) .append(", ");
454-
OUT.append("URI") .append("= ") .append(URI.toString()) .append(", ");
455-
OUT.append("publicAddress") .append("= ") .append(publicAddr.toString()) .append(", ");
456-
OUT.append("localAddress") .append("= ") .append(localAddr.toString()) .append(", ");
457-
OUT.append("httpContext") .append("= ") .append(httpContext.toString()) .append(", ");
458-
OUT.append("httpPrincipal") .append("= ") .append(httpPrincipal.toString()) .append(", ");
459-
OUT.append("protocol") .append("= ") .append(protocol) .append(", ");
460-
OUT.append("authority") .append("= ") .append(authority) .append(", ");
461-
OUT.append("scheme") .append("= ") .append(scheme) .append(", ");
462-
OUT.append("context") .append("= ") .append(context) .append(", ");
463-
OUT.append("fragment") .append("= ") .append(fragment) .append(", ");
464-
OUT.append("requestHeaders") .append("= ") .append(requestHeaders.toString()) .append(", ");
465-
OUT.append("requestMethod") .append("= ") .append(requestMethod.toString()) .append(", ");
466-
OUT.append("responseHeader") .append("= ") .append(getResponseHeaders().toString()) .append(", ");
467-
OUT.append("responseCode") .append("= ") .append(getResponseCode()) .append(", ");
468-
OUT.append("rawGet") .append("= ") .append(rawGet) .append(", ");
469-
OUT.append("getMap") .append("= ") .append(getMap.toString()) .append(", ");
470-
OUT.append("hasGet") .append("= ") .append(hasGet) .append(", ");
471-
OUT.append("rawPost") .append("= ") .append(rawPost) .append(", ");
472-
OUT.append("postMap") .append("= ") .append(postMap.toString()) .append(", ");
473-
OUT.append("hasPost") .append("= ") .append(hasPost) .append(", ");
474-
OUT.append("cookies") .append("= ") .append(cookies.toString());
453+
OUT.append("httpServer") .append("= ") .append(httpServer) .append(", ");
454+
OUT.append("httpExchange") .append("= ") .append(httpExchange) .append(", ");
455+
OUT.append("URI") .append("= ") .append(URI) .append(", ");
456+
OUT.append("publicAddress") .append("= ") .append(publicAddr) .append(", ");
457+
OUT.append("localAddress") .append("= ") .append(localAddr) .append(", ");
458+
OUT.append("httpContext") .append("= ") .append(httpContext) .append(", ");
459+
OUT.append("httpPrincipal") .append("= ") .append(httpPrincipal) .append(", ");
460+
OUT.append("protocol") .append("= ") .append(protocol) .append(", ");
461+
OUT.append("authority") .append("= ") .append(authority) .append(", ");
462+
OUT.append("scheme") .append("= ") .append(scheme) .append(", ");
463+
OUT.append("context") .append("= ") .append(context) .append(", ");
464+
OUT.append("fragment") .append("= ") .append(fragment) .append(", ");
465+
OUT.append("requestHeaders") .append("= ") .append(requestHeaders) .append(", ");
466+
OUT.append("requestMethod") .append("= ") .append(requestMethod) .append(", ");
467+
OUT.append("responseHeader") .append("= ") .append(getResponseHeaders()) .append(", ");
468+
OUT.append("responseCode") .append("= ") .append(getResponseCode()) .append(", ");
469+
OUT.append("rawGet") .append("= ") .append(rawGet) .append(", ");
470+
OUT.append("getMap") .append("= ") .append(getMap) .append(", ");
471+
OUT.append("hasGet") .append("= ") .append(hasGet) .append(", ");
472+
OUT.append("rawPost") .append("= ") .append(rawPost) .append(", ");
473+
OUT.append("postMap") .append("= ") .append(postMap) .append(", ");
474+
OUT.append("hasPost") .append("= ") .append(hasPost) .append(", ");
475+
OUT.append("cookies") .append("= ") .append(cookies);
475476
OUT.append("}");
476477
return OUT.toString();
477478
}

src/ktt/lib/httpserver/SimpleHttpHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.io.IOException;
77

88
/**
9-
* A simplified implementation of {@link HttpHandler}. It is used to process {@link SimpleHttpExchange}s.
9+
* A simplified implementation of {@link HttpHandler}. It is used to process {@link SimpleHttpExchange}s. Each handler is its own thread and will not reveal any exceptions unless a try-catch within the handler is used.
1010
*
1111
* @see HttpHandler
1212
* @see SimpleHttpExchange

src/ktt/lib/httpserver/SimpleHttpServer.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public abstract class SimpleHttpServer {
2626
/**
2727
* Create an empty {@link SimpleHttpServer}. Applications don't use this method.
2828
*
29-
* @see SimpleHttpServerImpl#createSimpleHttpServer(int, int)
29+
* @see SimpleHttpServerImpl#createSimpleHttpServer(Integer, Integer)
3030
* @since 02.00.00
3131
* @author Ktt Development
3232
*/
@@ -35,41 +35,44 @@ public abstract class SimpleHttpServer {
3535
//
3636

3737
/**
38-
* Create a {@link SimpleHttpServer} at port 80 with no backlog.
38+
* Creates a {@link SimpleHttpServer}.
3939
*
4040
* @return a {@link SimpleHttpServer}
41-
* @throws java.net.BindException if server can not bind to port
4241
* @throws IOException uncaught exception
4342
*
4443
* @since 02.00.00
4544
* @author Ktt Development
4645
*/
4746
public static SimpleHttpServer create() throws IOException {
48-
return SimpleHttpServerImpl.createSimpleHttpServer(80,0);
47+
return SimpleHttpServerImpl.createSimpleHttpServer(null,null);
4948
}
5049

5150
/**
52-
* Create a {@link SimpleHttpServer} at with no backlog.
51+
* Creates a {@link SimpleHttpServer} bounded to a port.
5352
*
5453
* @param port port to bind to
5554
* @return a {@link SimpleHttpServer}
5655
* @throws java.net.BindException if server can not bind to port
56+
* @throws NullPointerException if address is <code>null</code>
57+
* @throws IllegalArgumentException if port is out of range
5758
* @throws IOException uncaught exception
5859
*
5960
* @since 02.00.00
6061
* @author Ktt Development
6162
*/
6263
public static SimpleHttpServer create(final int port) throws IOException {
63-
return SimpleHttpServerImpl.createSimpleHttpServer(port,0);
64+
return SimpleHttpServerImpl.createSimpleHttpServer(port,null);
6465
}
6566

6667
/**
67-
* Create a {@link SimpleHttpServer}.
68+
* Creates a {@link SimpleHttpServer} bounded to a port.
6869
*
6970
* @param port port to bind to
7071
* @param backlog request backlog
7172
* @return a {@link SimpleHttpServer}
7273
* @throws java.net.BindException if server can not bind to port
74+
* @throws NullPointerException if address is <code>null</code>
75+
* @throws IllegalArgumentException if port is out of range
7376
* @throws IOException uncaught exception
7477
*
7578
* @since 02.00.00
@@ -100,6 +103,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
100103
* @param port port to bind the server to
101104
* @return address the server is binded to
102105
* @throws java.net.BindException if server could not be bound to port, or if it's already bound
106+
* @throws IllegalArgumentException if port is out of range
103107
* @throws NullPointerException if address is <code>null</code>
104108
* @throws IOException uncaught exception
105109
*
@@ -118,6 +122,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
118122
* @param backlog request backlog
119123
* @return address the server is binded to
120124
* @throws java.net.BindException if server could not be bound to port, or if it's already bound
125+
* @throws IllegalArgumentException if port is out of range
121126
* @throws NullPointerException if address is <code>null</code>
122127
* @throws IOException uncaught exception
123128
*
@@ -134,6 +139,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
134139
*
135140
* @param addr address to bind the server to
136141
* @throws java.net.BindException if server could not be bound to port, or if it's already bound
142+
* @throws IllegalArgumentException if port is out of range
137143
* @throws NullPointerException if address is <code>null</code>
138144
* @throws IOException uncaught exception
139145
*
@@ -448,8 +454,6 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
448454
* @throws IllegalArgumentException if no handler at that context exists
449455
* @throws NullPointerException if the context is null
450456
*
451-
* @see #createContext(String)
452-
* @see #createContext(String, HttpHandler)
453457
* @see #removeContext(HttpContext)
454458
* @since 02.00.00
455459
* @author Ktt Development
@@ -462,8 +466,6 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
462466
* @throws IllegalArgumentException if no handler at that context exists
463467
* @throws NullPointerException if the context is null
464468
*
465-
* @see #createContext(String)
466-
* @see #createContext(String, HttpHandler)
467469
* @see #removeContext(String)
468470
* @since 02.00.00
469471
* @author Ktt Development
@@ -508,6 +510,8 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
508510
/**
509511
* Starts the server.
510512
*
513+
* @throws IllegalStateException if server is not bound to a valid port
514+
*
511515
* @see #stop()
512516
* @see #stop(int)
513517
* @since 2.00.00

src/ktt/lib/httpserver/SimpleHttpServerImpl.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class SimpleHttpServerImpl {
3131
* @since 02.00.00
3232
* @author Ktt Development
3333
*/
34-
static SimpleHttpServer createSimpleHttpServer(final int port, final int backlog) throws IOException {
34+
static SimpleHttpServer createSimpleHttpServer(final Integer port, final Integer backlog) throws IOException {
3535
return new SimpleHttpServer() {
3636

3737
private final HttpServer server = HttpServer.create();
@@ -41,7 +41,8 @@ static SimpleHttpServer createSimpleHttpServer(final int port, final int backlog
4141
private boolean running = false;
4242

4343
{
44-
server.bind(new InetSocketAddress(port),backlog);
44+
if(port != null)
45+
server.bind(new InetSocketAddress(port),backlog != null ? backlog : 0);
4546
}
4647

4748
//
@@ -260,10 +261,10 @@ public synchronized final void stop(final int delay){
260261
public final String toString(){
261262
final StringBuilder OUT = new StringBuilder();
262263
OUT.append("SimpleHttpServer") .append("{");
263-
OUT.append("httpServer") .append("= ") .append(server.toString()) .append(", ");
264-
OUT.append("contexts") .append("= ") .append(contexts.toString()) .append(", ");
265-
OUT.append("address") .append("= ") .append(getAddress().toString()) .append(", ");
266-
OUT.append("executor") .append("= ") .append(getExecutor().toString());
264+
OUT.append("httpServer") .append("= ") .append(server) .append(", ");
265+
OUT.append("contexts") .append("= ") .append(contexts) .append(", ");
266+
OUT.append("address") .append("= ") .append(getAddress()) .append(", ");
267+
OUT.append("executor") .append("= ") .append(getExecutor());
267268
OUT.append("}");
268269
return OUT.toString();
269270
}
@@ -273,6 +274,7 @@ public final String toString(){
273274

274275
private static String getContext(final String path){
275276
final String linSlash = path.toLowerCase().replace("\\","/");
277+
if(linSlash.equalsIgnoreCase("/")) return "/";
276278
final String seSlash = (!linSlash.startsWith("/") ? "/" : "") + linSlash + (!linSlash.endsWith("/") ? "/" : "");
277279
return seSlash.substring(0,seSlash.length()-1);
278280
}

src/ktt/lib/httpserver/handler/DirectoryEntry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public final byte[] getBytes(final String path){
213213

214214
private static String getContext(final String path){
215215
final String linSlash = path.toLowerCase().replace("\\","/");
216+
if(linSlash.equalsIgnoreCase("/")) return "/";
216217
final String seSlash = (!linSlash.startsWith("/") ? "/" : "") + linSlash + (!linSlash.endsWith("/") ? "/" : "");
217218
return seSlash.substring(0,seSlash.length()-1);
218219
}

src/ktt/lib/httpserver/handler/FileHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ public void handle(final SimpleHttpExchange exchange, final File source, final b
684684

685685
private static String getContext(final String path){
686686
final String linSlash = path.toLowerCase().replace("\\","/");
687+
if(linSlash.equalsIgnoreCase("/")) return "/";
687688
final String seSlash = (!linSlash.startsWith("/") ? "/" : "") + linSlash + (!linSlash.endsWith("/") ? "/" : "");
688689
return seSlash.substring(0,seSlash.length()-1);
689690
}

0 commit comments

Comments
 (0)