diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index dde3e10..031c4e5 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -31,6 +31,11 @@ "redhat.vscode-xml" ], "settings": { + "java.import.maven.enabled": false, + "java.import.gradle.enabled": false, + "java.project.referencedLibraries": [ + "dist/libs/*.jar" + ], "files.autoSave": "afterDelay", "files.autoSaveDelay": 250, "editor.formatOnSave": true, diff --git a/.vscode/launch.json b/.vscode/launch.json index 2a44e2b..981f635 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,11 +5,14 @@ "version": "0.2.0", "configurations": [ { + "preLaunchTask": "espresso build", "type": "java", "name": "Main", "request": "launch", - "mainClass": "io.kerosenelabs.kindling.Main", - "projectName": "kindling" + "mainClass": "com.kerosenelabs.kindling.Main", + "classPaths": [ + "${workspaceFolder}/dist/dist.jar" + ] } ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..b8c14a8 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,10 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "espresso build", + "type": "shell", + "command": "espresso b" + } + ] +} \ No newline at end of file diff --git a/build/com/kerosenelabs/kindling/HttpRequest.class b/build/com/kerosenelabs/kindling/HttpRequest.class index d0f1cc8..013c90a 100644 Binary files a/build/com/kerosenelabs/kindling/HttpRequest.class and b/build/com/kerosenelabs/kindling/HttpRequest.class differ diff --git a/build/com/kerosenelabs/kindling/Main$1$1.class b/build/com/kerosenelabs/kindling/Main$1$1.class index 86d31fe..7b45471 100644 Binary files a/build/com/kerosenelabs/kindling/Main$1$1.class and b/build/com/kerosenelabs/kindling/Main$1$1.class differ diff --git a/build/com/kerosenelabs/kindling/Main$1.class b/build/com/kerosenelabs/kindling/Main$1.class index abf1d83..a6dcc77 100644 Binary files a/build/com/kerosenelabs/kindling/Main$1.class and b/build/com/kerosenelabs/kindling/Main$1.class differ diff --git a/build/com/kerosenelabs/kindling/Main.class b/build/com/kerosenelabs/kindling/Main.class index a48c1c7..18909a9 100644 Binary files a/build/com/kerosenelabs/kindling/Main.class and b/build/com/kerosenelabs/kindling/Main.class differ diff --git a/dist/dist.jar b/dist/dist.jar index a4f9c75..20cc499 100644 Binary files a/dist/dist.jar and b/dist/dist.jar differ diff --git a/src/java/com/kerosenelabs/kindling/HttpRequest.java b/src/java/com/kerosenelabs/kindling/HttpRequest.java index 5724668..1816d68 100644 --- a/src/java/com/kerosenelabs/kindling/HttpRequest.java +++ b/src/java/com/kerosenelabs/kindling/HttpRequest.java @@ -2,6 +2,8 @@ import java.io.BufferedReader; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -18,9 +20,12 @@ public class HttpRequest { private HttpMethod httpMethod; private String resource; + private String path; + private HashMap queryParameters; private String protocolVersion; private HashMap headers; private String content; + private String[] x; public HttpRequest(BufferedReader bufferedReader) throws KindlingException { List messageHead; @@ -35,6 +40,8 @@ public HttpRequest(BufferedReader bufferedReader) throws KindlingException { httpMethod = httpRequestLine.getHttpMethod(); resource = httpRequestLine.getResource(); protocolVersion = httpRequestLine.getProtocol(); + path = resource.split("\\?")[0]; + queryParameters = parseQueryParameters(resource); // do headers headers = parseHttpHeaders(messageHead); @@ -50,10 +57,33 @@ public HttpRequest(BufferedReader bufferedReader) throws KindlingException { } } + /** + * Get the Resource (path and query parameters) + * + * @return + */ public String getResource() { return resource; } + /** + * Get the path + * + * @return + */ + public String getPath() { + return path; + } + + /** + * Get the query parameters + * + * @return + */ + public HashMap getQueryParmeters() { + return queryParameters; + } + public HttpMethod getHttpMethod() { return httpMethod; } @@ -121,6 +151,29 @@ private static HttpRequestHead parseRequestLine(List messageHead) { return new HttpRequestHead(HttpMethod.valueOf(splitRequestLine[0]), splitRequestLine[1], splitRequestLine[2]); } + /** + * Parses the query parameters out of the given resource as a map. + * + * @param resource Resource string (ex: /registry?q=lombok) + * @return + * @throws KindlingException + */ + private static HashMap parseQueryParameters(String resource) throws KindlingException { + HashMap params = new HashMap<>(); + String[] split = resource.split("&"); + for (String pair : split) { + int idx = pair.indexOf("="); + try { + String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8"); + String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8"); + params.put(key, value); + } catch (UnsupportedEncodingException e) { + throw new KindlingException(e); + } + } + return params; + } + /** * Parse HTTP headers from the message head * diff --git a/src/java/com/kerosenelabs/kindling/Main.java b/src/java/com/kerosenelabs/kindling/Main.java index 80375c8..d1e07be 100644 --- a/src/java/com/kerosenelabs/kindling/Main.java +++ b/src/java/com/kerosenelabs/kindling/Main.java @@ -28,6 +28,7 @@ public boolean accepts(HttpRequest httpRequest) throws KindlingException { */ @Override public HttpResponse handle(HttpRequest httpRequest) throws KindlingException { + System.out.println(httpRequest.getQueryParmeters().toString()); return new HttpResponse.Builder() .status(HttpStatus.OK) .headers(new HashMap<>() { @@ -41,6 +42,6 @@ public HttpResponse handle(HttpRequest httpRequest) throws KindlingException { }); // serve our server - server.serve(8443, Path.of("mykeystore.p12"), "password"); + server.serve(8443, Path.of("keystore.p12"), "password"); } }