|
|
|
@@ -24,7 +24,9 @@ public final class Server {
|
|
|
|
|
|
|
|
|
|
|
|
public static interface Response {
|
|
|
|
public static interface Response {
|
|
|
|
public int getCode();
|
|
|
|
public int getCode();
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] getBytes();
|
|
|
|
public byte[] getBytes();
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, List<String>> getResponseHeaders();
|
|
|
|
public Map<String, List<String>> getResponseHeaders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -39,14 +41,21 @@ public final class Server {
|
|
|
|
this.responseHeaders = null;
|
|
|
|
this.responseHeaders = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ByteResponse(final int code, final byte[] bytes, final Map<String, List<String>> responseHeaders) {
|
|
|
|
public ByteResponse(
|
|
|
|
|
|
|
|
final int code, final byte[] bytes, final Map<String, List<String>> responseHeaders) {
|
|
|
|
this.code = code;
|
|
|
|
this.code = code;
|
|
|
|
this.bytes = bytes;
|
|
|
|
this.bytes = bytes;
|
|
|
|
this.responseHeaders = responseHeaders;
|
|
|
|
this.responseHeaders = responseHeaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getCode() { return this.code; }
|
|
|
|
public int getCode() {
|
|
|
|
public byte[] getBytes() { return this.bytes; }
|
|
|
|
return this.code;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] getBytes() {
|
|
|
|
|
|
|
|
return this.bytes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, List<String>> getResponseHeaders() {
|
|
|
|
public Map<String, List<String>> getResponseHeaders() {
|
|
|
|
return this.responseHeaders;
|
|
|
|
return this.responseHeaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -57,24 +66,29 @@ public final class Server {
|
|
|
|
super(code, msg.getBytes());
|
|
|
|
super(code, msg.getBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public StringResponse(final int code, final String msg, final Map<String, List<String>> responseHeaders) {
|
|
|
|
public StringResponse(
|
|
|
|
|
|
|
|
final int code, final String msg, final Map<String, List<String>> responseHeaders) {
|
|
|
|
super(code, msg.getBytes(), responseHeaders);
|
|
|
|
super(code, msg.getBytes(), responseHeaders);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public final class Request {
|
|
|
|
public final class Request {
|
|
|
|
final HttpExchange exchange;
|
|
|
|
final HttpExchange exchange;
|
|
|
|
|
|
|
|
|
|
|
|
Request(final HttpExchange exchange) {
|
|
|
|
Request(final HttpExchange exchange) {
|
|
|
|
this.exchange = exchange;
|
|
|
|
this.exchange = exchange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getMethod() {
|
|
|
|
public String getMethod() {
|
|
|
|
return exchange.getRequestMethod();
|
|
|
|
return exchange.getRequestMethod();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, List<String>> getQueryParams() {
|
|
|
|
public Map<String, List<String>> getQueryParams() {
|
|
|
|
final var query = exchange.getRequestURI().getQuery();
|
|
|
|
final var query = exchange.getRequestURI().getQuery();
|
|
|
|
final var params = parseParams(query);
|
|
|
|
final var params = parseParams(query);
|
|
|
|
return params;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] getBody() {
|
|
|
|
public byte[] getBody() {
|
|
|
|
try (final var bodyIS = exchange.getRequestBody()) {
|
|
|
|
try (final var bodyIS = exchange.getRequestBody()) {
|
|
|
|
final var bytes = bodyIS.readAllBytes();
|
|
|
|
final var bytes = bodyIS.readAllBytes();
|
|
|
|
@@ -84,6 +98,7 @@ public final class Server {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getBodyString() {
|
|
|
|
public String getBodyString() {
|
|
|
|
return new String(getBody());
|
|
|
|
return new String(getBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -98,11 +113,13 @@ public final class Server {
|
|
|
|
public final String path;
|
|
|
|
public final String path;
|
|
|
|
public final Processor processor;
|
|
|
|
public final Processor processor;
|
|
|
|
public final String[] methods;
|
|
|
|
public final String[] methods;
|
|
|
|
|
|
|
|
|
|
|
|
public Handler(final String path, final Processor processor) {
|
|
|
|
public Handler(final String path, final Processor processor) {
|
|
|
|
this.path = path;
|
|
|
|
this.path = path;
|
|
|
|
this.processor = processor;
|
|
|
|
this.processor = processor;
|
|
|
|
this.methods = new String[] {};
|
|
|
|
this.methods = new String[] {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Handler(final String path, final String methods, final Processor processor) {
|
|
|
|
public Handler(final String path, final String methods, final Processor processor) {
|
|
|
|
this.path = path;
|
|
|
|
this.path = path;
|
|
|
|
this.processor = processor;
|
|
|
|
this.processor = processor;
|
|
|
|
@@ -110,12 +127,19 @@ public final class Server {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Server(final InetSocketAddress addr, final int backlog, final List<Handler> handlers, final Executor executor) throws IOException {
|
|
|
|
public Server(
|
|
|
|
|
|
|
|
final InetSocketAddress addr,
|
|
|
|
|
|
|
|
final int backlog,
|
|
|
|
|
|
|
|
final List<Handler> handlers,
|
|
|
|
|
|
|
|
final Executor executor)
|
|
|
|
|
|
|
|
throws IOException {
|
|
|
|
this.server = HttpServer.create(addr, backlog);
|
|
|
|
this.server = HttpServer.create(addr, backlog);
|
|
|
|
this.server.setExecutor(executor);
|
|
|
|
this.server.setExecutor(executor);
|
|
|
|
for (final var handler : handlers) {
|
|
|
|
for (final var handler : handlers) {
|
|
|
|
// System.out.println("Registering handler for " + handler.path);
|
|
|
|
// System.out.println("Registering handler for " + handler.path);
|
|
|
|
this.server.createContext(handler.path, new HttpHandler() {
|
|
|
|
this.server.createContext(
|
|
|
|
|
|
|
|
handler.path,
|
|
|
|
|
|
|
|
new HttpHandler() {
|
|
|
|
public void handle(final HttpExchange exchange) {
|
|
|
|
public void handle(final HttpExchange exchange) {
|
|
|
|
final var method = exchange.getRequestMethod();
|
|
|
|
final var method = exchange.getRequestMethod();
|
|
|
|
final Response errorResponse = checkMethods(handler.methods, method);
|
|
|
|
final Response errorResponse = checkMethods(handler.methods, method);
|
|
|
|
@@ -178,17 +202,21 @@ public final class Server {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Adapted from https://stackoverflow.com/a/37368660
|
|
|
|
// Adapted from https://stackoverflow.com/a/37368660
|
|
|
|
private final static Pattern ampersandPattern = Pattern.compile("&");
|
|
|
|
private static final Pattern ampersandPattern = Pattern.compile("&");
|
|
|
|
private final static Pattern equalPattern = Pattern.compile("=");
|
|
|
|
private static final Pattern equalPattern = Pattern.compile("=");
|
|
|
|
private final static Map<String, List<String>> emptyMap = Map.of();
|
|
|
|
private static final Map<String, List<String>> emptyMap = Map.of();
|
|
|
|
|
|
|
|
|
|
|
|
private static Map<String, List<String>> parseParams(final String query) {
|
|
|
|
private static Map<String, List<String>> parseParams(final String query) {
|
|
|
|
if (query == null) {
|
|
|
|
if (query == null) {
|
|
|
|
return emptyMap;
|
|
|
|
return emptyMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final var params = ampersandPattern
|
|
|
|
final var params =
|
|
|
|
|
|
|
|
ampersandPattern
|
|
|
|
.splitAsStream(query)
|
|
|
|
.splitAsStream(query)
|
|
|
|
.map(s -> Arrays.copyOf(equalPattern.split(s, 2), 2))
|
|
|
|
.map(s -> Arrays.copyOf(equalPattern.split(s, 2), 2))
|
|
|
|
.collect(Collectors.groupingBy(s -> decode(s[0]), Collectors.mapping(s -> decode(s[1]), Collectors.toList())));
|
|
|
|
.collect(
|
|
|
|
|
|
|
|
Collectors.groupingBy(
|
|
|
|
|
|
|
|
s -> decode(s[0]), Collectors.mapping(s -> decode(s[1]), Collectors.toList())));
|
|
|
|
return params;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -208,42 +236,52 @@ public final class Server {
|
|
|
|
mAddress = new InetSocketAddress(port);
|
|
|
|
mAddress = new InetSocketAddress(port);
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder backlog(final int backlog) {
|
|
|
|
public ServerBuilder backlog(final int backlog) {
|
|
|
|
this.backlog = backlog;
|
|
|
|
this.backlog = backlog;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder address(final InetSocketAddress addr) {
|
|
|
|
public ServerBuilder address(final InetSocketAddress addr) {
|
|
|
|
mAddress = addr;
|
|
|
|
mAddress = addr;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder handle(final Handler handler) {
|
|
|
|
public ServerBuilder handle(final Handler handler) {
|
|
|
|
handlers.add(handler);
|
|
|
|
handlers.add(handler);
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder GET(final String path, final Processor processor) {
|
|
|
|
public ServerBuilder GET(final String path, final Processor processor) {
|
|
|
|
handlers.add(new Handler(path, "GET", request -> processor.process(request)));
|
|
|
|
handlers.add(new Handler(path, "GET", request -> processor.process(request)));
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder POST(final String path, final Processor processor) {
|
|
|
|
public ServerBuilder POST(final String path, final Processor processor) {
|
|
|
|
handlers.add(new Handler(path, "POST", request -> processor.process(request)));
|
|
|
|
handlers.add(new Handler(path, "POST", request -> processor.process(request)));
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder PUT(final String path, final Processor processor) {
|
|
|
|
public ServerBuilder PUT(final String path, final Processor processor) {
|
|
|
|
handlers.add(new Handler(path, "PUT", request -> processor.process(request)));
|
|
|
|
handlers.add(new Handler(path, "PUT", request -> processor.process(request)));
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder DELETE(final String path, final Processor processor) {
|
|
|
|
public ServerBuilder DELETE(final String path, final Processor processor) {
|
|
|
|
handlers.add(new Handler(path, "DELETE", request -> processor.process(request)));
|
|
|
|
handlers.add(new Handler(path, "DELETE", request -> processor.process(request)));
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder HEAD(final String path, final Processor processor) {
|
|
|
|
public ServerBuilder HEAD(final String path, final Processor processor) {
|
|
|
|
handlers.add(new Handler(path, "HEAD", request -> processor.process(request)));
|
|
|
|
handlers.add(new Handler(path, "HEAD", request -> processor.process(request)));
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerBuilder executor(final Executor executor) {
|
|
|
|
public ServerBuilder executor(final Executor executor) {
|
|
|
|
this.executor = executor;
|
|
|
|
this.executor = executor;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Server build() throws IOException {
|
|
|
|
public Server build() throws IOException {
|
|
|
|
return new Server(mAddress, backlog, handlers, executor);
|
|
|
|
return new Server(mAddress, backlog, handlers, executor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|