Skip to content

Commit

Permalink
Updating
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Nov 14, 2024
1 parent 9a3309e commit 6bc6d35
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
]
}
10 changes: 10 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "espresso build",
"type": "shell",
"command": "espresso b"
}
]
}
Binary file modified build/com/kerosenelabs/kindling/HttpRequest.class
Binary file not shown.
Binary file modified build/com/kerosenelabs/kindling/Main$1$1.class
Binary file not shown.
Binary file modified build/com/kerosenelabs/kindling/Main$1.class
Binary file not shown.
Binary file modified build/com/kerosenelabs/kindling/Main.class
Binary file not shown.
Binary file modified dist/dist.jar
Binary file not shown.
53 changes: 53 additions & 0 deletions src/java/com/kerosenelabs/kindling/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,9 +20,12 @@
public class HttpRequest {
private HttpMethod httpMethod;
private String resource;
private String path;
private HashMap<String, String> queryParameters;
private String protocolVersion;
private HashMap<String, String> headers;
private String content;
private String[] x;

public HttpRequest(BufferedReader bufferedReader) throws KindlingException {
List<String> messageHead;
Expand All @@ -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);
Expand All @@ -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<String, String> getQueryParmeters() {
return queryParameters;
}

public HttpMethod getHttpMethod() {
return httpMethod;
}
Expand Down Expand Up @@ -121,6 +151,29 @@ private static HttpRequestHead parseRequestLine(List<String> 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<String, String> parseQueryParameters(String resource) throws KindlingException {
HashMap<String, String> 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
*
Expand Down
3 changes: 2 additions & 1 deletion src/java/com/kerosenelabs/kindling/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<>() {
Expand All @@ -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");
}
}

0 comments on commit 6bc6d35

Please sign in to comment.