moved to http.cat
This commit is contained in:
parent
d40ca8cc56
commit
8860abf8aa
|
|
@ -30,7 +30,7 @@ public class HttpServerApi {
|
||||||
throw new HttpHandlingException(404);
|
throw new HttpHandlingException(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpServer create(int port, String rootLocation, Class<?> apiDefinition, Executor exec) throws IOException {
|
public static HttpServer create(int port, String rootLocation, Executor exec, Class<?>... apiDefinitions) throws IOException {
|
||||||
HttpServer server = HttpServer.create(new InetSocketAddress( port), 0);
|
HttpServer server = HttpServer.create(new InetSocketAddress( port), 0);
|
||||||
server.setExecutor(exec);
|
server.setExecutor(exec);
|
||||||
|
|
||||||
|
|
@ -38,66 +38,68 @@ public class HttpServerApi {
|
||||||
HashMap<String, EndpointContainer>[] endpoints = new HashMap[3];
|
HashMap<String, EndpointContainer>[] endpoints = new HashMap[3];
|
||||||
HashSet<String> locations = new HashSet<>();
|
HashSet<String> locations = new HashSet<>();
|
||||||
for (int i = 0; i < endpoints.length; endpoints[i++] = new HashMap<>());
|
for (int i = 0; i < endpoints.length; endpoints[i++] = new HashMap<>());
|
||||||
endpointLoop:
|
for (var apiDefinition : apiDefinitions) {
|
||||||
for (Method endpoint : apiDefinition.getDeclaredMethods()) {
|
endpointLoop:
|
||||||
HttpEndpoint spec = endpoint.getAnnotation(HttpEndpoint.class);
|
for (Method endpoint : apiDefinition.getDeclaredMethods()) {
|
||||||
if (spec == null || spec.type() == INVALID) {
|
HttpEndpoint spec = endpoint.getAnnotation(HttpEndpoint.class);
|
||||||
continue;
|
if (spec == null || spec.type() == INVALID) {
|
||||||
}
|
continue;
|
||||||
if (!endpoint.getReturnType().equals(Response.class)) {
|
|
||||||
System.err.printf("invalid return type %s!\nskipping %s ...\n", endpoint.getReturnType().getSimpleName(), endpoint.getName());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
RequestType type = spec.type();
|
|
||||||
String location = spec.location();
|
|
||||||
HashMap<String, EndpointContainer> epMap = endpoints[type.id];
|
|
||||||
if (epMap.containsKey(location)) {
|
|
||||||
System.err.printf("HttpEndpoint for type %s and location \"%s\" is defined multiple times!\nskipping %s ...\n",
|
|
||||||
type.name(), location, endpoint.getName());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Parameter[] params = endpoint.getParameters();
|
|
||||||
if (params.length < 1 || !HttpExchange.class.equals(params[0].getType())) {
|
|
||||||
System.err.printf("HttpEndpoint must have HttpExchange as first parameter!\nskipping %s ...\n",
|
|
||||||
endpoint.getReturnType().getSimpleName(), endpoint.getName());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
params = Arrays.stream(params).skip(1).toArray(Parameter[]::new);
|
|
||||||
|
|
||||||
Class<?>[] pTypes = Arrays.stream(params).map(p -> p.getType()).toArray(Class[]::new);
|
|
||||||
String[] pNames = new String[params.length];
|
|
||||||
String[] stringVals = new String[params.length];
|
|
||||||
for (int i = 0; i < params.length; i++) {
|
|
||||||
QParam name = params[i].getAnnotation(QParam.class);
|
|
||||||
if (name == null) {
|
|
||||||
System.err.printf("missing QParam annotation!\nskipping %s ...\n", endpoint.getName());
|
|
||||||
continue endpointLoop;
|
|
||||||
}
|
}
|
||||||
pNames[i] = name.name();
|
if (!endpoint.getReturnType().equals(Response.class)) {
|
||||||
stringVals[i] = name.empty();
|
System.err.printf("invalid return type %s!\nskipping %s ...\n", endpoint.getReturnType().getSimpleName(), endpoint.getName());
|
||||||
}
|
continue;
|
||||||
Object[] pDefaults = new Object[pTypes.length];
|
|
||||||
try {
|
|
||||||
for (int i = 0; i < pTypes.length; i++) {
|
|
||||||
pDefaults[i] = parseValue(pTypes[i], stringVals[i]);
|
|
||||||
}
|
}
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
System.err.printf("encountered error setting up HttpEndpoint (%s)!\nskipping %s ...\n", e.getMessage(), endpoint.getName());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Authorizer auth;
|
RequestType type = spec.type();
|
||||||
try {
|
String location = spec.location();
|
||||||
auth = spec.auth().getConstructor().newInstance();
|
HashMap<String, EndpointContainer> epMap = endpoints[type.id];
|
||||||
} catch (ReflectiveOperationException e) {
|
if (epMap.containsKey(location)) {
|
||||||
System.err.printf("encountered error setting up HttpEndpoint (%s)!\nskipping %s ...\n", e.getMessage(), endpoint.getName());
|
System.err.printf("HttpEndpoint for type %s and location \"%s\" is defined multiple times!\nskipping %s ...\n",
|
||||||
continue;
|
type.name(), location, endpoint.getName());
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
epMap.put(location, new EndpointContainer(endpoint, auth, pTypes, pNames, pDefaults));
|
Parameter[] params = endpoint.getParameters();
|
||||||
locations.add(location);
|
if (params.length < 1 || !HttpExchange.class.equals(params[0].getType())) {
|
||||||
|
System.err.printf("HttpEndpoint must have HttpExchange as first parameter!\nskipping %s ...\n",
|
||||||
|
endpoint.getReturnType().getSimpleName(), endpoint.getName());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
params = Arrays.stream(params).skip(1).toArray(Parameter[]::new);
|
||||||
|
|
||||||
|
Class<?>[] pTypes = Arrays.stream(params).map(p -> p.getType()).toArray(Class[]::new);
|
||||||
|
String[] pNames = new String[params.length];
|
||||||
|
String[] stringVals = new String[params.length];
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
QParam name = params[i].getAnnotation(QParam.class);
|
||||||
|
if (name == null) {
|
||||||
|
System.err.printf("missing QParam annotation!\nskipping %s ...\n", endpoint.getName());
|
||||||
|
continue endpointLoop;
|
||||||
|
}
|
||||||
|
pNames[i] = name.name();
|
||||||
|
stringVals[i] = name.empty();
|
||||||
|
}
|
||||||
|
Object[] pDefaults = new Object[pTypes.length];
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < pTypes.length; i++) {
|
||||||
|
pDefaults[i] = parseValue(pTypes[i], stringVals[i]);
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
System.err.printf("encountered error setting up HttpEndpoint (%s)!\nskipping %s ...\n", e.getMessage(), endpoint.getName());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Authorizer auth;
|
||||||
|
try {
|
||||||
|
auth = spec.auth().getConstructor().newInstance();
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
System.err.printf("encountered error setting up HttpEndpoint (%s)!\nskipping %s ...\n", e.getMessage(), endpoint.getName());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
epMap.put(location, new EndpointContainer(endpoint, auth, pTypes, pNames, pDefaults));
|
||||||
|
locations.add(location);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create appropriate contexts and headers
|
// create appropriate contexts and headers
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,13 @@ import com.sun.net.httpserver.HttpExchange;
|
||||||
import com.sun.net.httpserver.HttpPrincipal;
|
import com.sun.net.httpserver.HttpPrincipal;
|
||||||
import dev.asdf00.lib.httpserver.exceptions.HttpHandlingException;
|
import dev.asdf00.lib.httpserver.exceptions.HttpHandlingException;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.file.InvalidPathException;
|
import java.nio.file.InvalidPathException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
|
@ -83,4 +86,18 @@ public class HttpServerApiUtils {
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException(type.getSimpleName() + " is not a primitive");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ package dev.asdf00.lib.httpserver.internal;
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
import com.sun.net.httpserver.HttpHandler;
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
import dev.asdf00.lib.httpserver.HttpServerApiUtils;
|
|
||||||
import dev.asdf00.lib.httpserver.exceptions.HttpHandlingException;
|
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.RequestType;
|
||||||
import dev.asdf00.lib.httpserver.utils.Response;
|
import dev.asdf00.lib.httpserver.utils.Response;
|
||||||
|
|
||||||
|
|
@ -28,7 +28,7 @@ public class HttpHandlerImpl implements HttpHandler {
|
||||||
|
|
||||||
EndpointContainer target = endpointContainer[type.id];
|
EndpointContainer target = endpointContainer[type.id];
|
||||||
if (!target.auth.isAuthenticated(exchange)) {
|
if (!target.auth.isAuthenticated(exchange)) {
|
||||||
throw new HttpHandlingException(402);
|
throw new HttpHandlingException(498);
|
||||||
}
|
}
|
||||||
|
|
||||||
String query = exchange.getRequestURI().getQuery();
|
String query = exchange.getRequestURI().getQuery();
|
||||||
|
|
@ -47,9 +47,8 @@ public class HttpHandlerImpl implements HttpHandler {
|
||||||
} else if (e instanceof IndexOutOfBoundsException) {
|
} else if (e instanceof IndexOutOfBoundsException) {
|
||||||
errorCode = 400;
|
errorCode = 400;
|
||||||
}
|
}
|
||||||
Response r = new Response(exchange).status(errorCode).contentType(Response.ContentType.HTML);
|
Response r = new Response(exchange).status(errorCode).contentType(ContentType.HTML);
|
||||||
r.append(String.format("<h1>ERROR %s</h1>\n", errorCode));
|
r.append(String.format("<p><img src=\"https://http.cat/%s\" /></p>\n<h3> </h3><h3>Error message:</h3>\n<p>\n%s\n</p>", errorCode, e.getMessage()));
|
||||||
r.append(String.format("<p>something fucky is a foot!\n%s</p>", e.getMessage()));
|
|
||||||
r.send();
|
r.send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
src/dev/asdf00/lib/httpserver/utils/ContentType.java
Normal file
12
src/dev/asdf00/lib/httpserver/utils/ContentType.java
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,15 +9,7 @@ import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class Response {
|
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 int statusCode = 500;
|
||||||
private ContentType cType = ContentType.PLAIN;
|
private ContentType cType = ContentType.PLAIN;
|
||||||
|
|
@ -39,6 +31,11 @@ public class Response {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Response append(String format, Object... params) {
|
||||||
|
response.append(String.format(format, params));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Response append(String line) {
|
public Response append(String line) {
|
||||||
response.append(line);
|
response.append(line);
|
||||||
return this;
|
return this;
|
||||||
|
|
|
||||||
7
src/module-info.java
Normal file
7
src/module-info.java
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user