Compare commits

..

No commits in common. "master" and "0.0.1" have entirely different histories.

6 changed files with 75 additions and 109 deletions

View File

@ -30,7 +30,7 @@ public class HttpServerApi {
throw new HttpHandlingException(404);
}
public static HttpServer create(int port, String rootLocation, Executor exec, Class<?>... apiDefinitions) throws IOException {
public static HttpServer create(int port, String rootLocation, Class<?> apiDefinition, Executor exec) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress( port), 0);
server.setExecutor(exec);
@ -38,7 +38,6 @@ public class HttpServerApi {
HashMap<String, EndpointContainer>[] endpoints = new HashMap[3];
HashSet<String> locations = new HashSet<>();
for (int i = 0; i < endpoints.length; endpoints[i++] = new HashMap<>());
for (var apiDefinition : apiDefinitions) {
endpointLoop:
for (Method endpoint : apiDefinition.getDeclaredMethods()) {
HttpEndpoint spec = endpoint.getAnnotation(HttpEndpoint.class);
@ -100,7 +99,6 @@ public class HttpServerApi {
epMap.put(location, new EndpointContainer(endpoint, auth, pTypes, pNames, pDefaults));
locations.add(location);
}
}
// create appropriate contexts and headers
EndpointContainer default404;

View File

@ -5,13 +5,10 @@ import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpPrincipal;
import dev.asdf00.lib.httpserver.exceptions.HttpHandlingException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.net.URI;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
@ -86,18 +83,4 @@ public class HttpServerApiUtils {
}
throw new IllegalArgumentException(type.getSimpleName() + " is not a primitive");
}
public static String readCookie(String key, List<String> cookies) {
String value = null;
outer:
for (var cookie : cookies) {
for (String s : cookie.split("; ")) {
if (s.startsWith(key + "=")) {
value = s.substring(key.length() + 1);
break outer;
}
}
}
return value;
}
}

View File

@ -2,8 +2,8 @@ package dev.asdf00.lib.httpserver.internal;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import dev.asdf00.lib.httpserver.HttpServerApiUtils;
import dev.asdf00.lib.httpserver.exceptions.HttpHandlingException;
import dev.asdf00.lib.httpserver.utils.ContentType;
import dev.asdf00.lib.httpserver.utils.RequestType;
import dev.asdf00.lib.httpserver.utils.Response;
@ -28,7 +28,7 @@ public class HttpHandlerImpl implements HttpHandler {
EndpointContainer target = endpointContainer[type.id];
if (!target.auth.isAuthenticated(exchange)) {
throw new HttpHandlingException(498);
throw new HttpHandlingException(402);
}
String query = exchange.getRequestURI().getQuery();
@ -47,8 +47,9 @@ public class HttpHandlerImpl implements HttpHandler {
} else if (e instanceof IndexOutOfBoundsException) {
errorCode = 400;
}
Response r = new Response(exchange).status(errorCode).contentType(ContentType.HTML);
r.append(String.format("<p><img src=\"https://http.cat/%s\" /></p>\n<h3>&nbsp;</h3><h3>Error message:</h3>\n<p>\n%s\n</p>", errorCode, e.getMessage()));
Response r = new Response(exchange).status(errorCode).contentType(Response.ContentType.HTML);
r.append(String.format("<h1>ERROR %s</h1>\n", errorCode));
r.append(String.format("<p>something fucky is a foot!\n%s</p>", e.getMessage()));
r.send();
}
}

View File

@ -1,12 +0,0 @@
package dev.asdf00.lib.httpserver.utils;
public enum ContentType {
PLAIN("text/plain"),
HTML("text/html"),
JS("application/javascript"),
JSON("application/json");
public final String type;
ContentType(String type) {
this.type = type;
}
}

View File

@ -9,7 +9,15 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Response {
public enum ContentType {
PLAIN("text/plain"),
HTML("text/html"),
JS("text/javascript");
private final String type;
ContentType(String type) {
this.type = type;
}
}
private int statusCode = 500;
private ContentType cType = ContentType.PLAIN;
@ -31,11 +39,6 @@ public class Response {
return this;
}
public Response append(String format, Object... params) {
response.append(String.format(format, params));
return this;
}
public Response append(String line) {
response.append(line);
return this;

View File

@ -1,7 +0,0 @@
module asdf00.lib.httpserver {
requires jdk.httpserver;
exports dev.asdf00.lib.httpserver;
exports dev.asdf00.lib.httpserver.annotations;
exports dev.asdf00.lib.httpserver.exceptions;
exports dev.asdf00.lib.httpserver.utils;
}