diff --git a/src/main/java/dev/latvian/apps/tinyserver/HTTPServer.java b/src/main/java/dev/latvian/apps/tinyserver/HTTPServer.java index a570efc..6d63d76 100644 --- a/src/main/java/dev/latvian/apps/tinyserver/HTTPServer.java +++ b/src/main/java/dev/latvian/apps/tinyserver/HTTPServer.java @@ -1,6 +1,7 @@ package dev.latvian.apps.tinyserver; import dev.latvian.apps.tinyserver.error.BindFailedException; +import dev.latvian.apps.tinyserver.error.InvalidPathException; import dev.latvian.apps.tinyserver.http.HTTPHandler; import dev.latvian.apps.tinyserver.http.HTTPMethod; import dev.latvian.apps.tinyserver.http.HTTPPathHandler; @@ -251,6 +252,16 @@ private void handleClient(Socket socket) { } else { var pathParts = path.split("/"); + for (int i = 0; i < pathParts.length; i++) { + try { + if (pathParts[i].indexOf('%') != -1) { + pathParts[i] = URLDecoder.decode(pathParts[i], StandardCharsets.UTF_8); + } + } catch (Exception ex) { + throw new InvalidPathException(ex.getMessage()); + } + } + for (var handler : handlers.entrySet()) { if (handler.getValue().staticHandlers().containsKey(path)) { allowed.add(handler.getKey()); @@ -293,6 +304,17 @@ private void handleClient(Socket socket) { if (hl != null) { var pathParts = path.split("/"); + + for (int i = 0; i < pathParts.length; i++) { + try { + if (pathParts[i].indexOf('%') != -1) { + pathParts[i] = URLDecoder.decode(pathParts[i], StandardCharsets.UTF_8); + } + } catch (Exception ex) { + throw new InvalidPathException(ex.getMessage()); + } + } + var h = hl.staticHandlers().get(path); if (h != null) { diff --git a/src/main/java/dev/latvian/apps/tinyserver/error/InvalidPathException.java b/src/main/java/dev/latvian/apps/tinyserver/error/InvalidPathException.java new file mode 100644 index 0000000..5d2dbb1 --- /dev/null +++ b/src/main/java/dev/latvian/apps/tinyserver/error/InvalidPathException.java @@ -0,0 +1,7 @@ +package dev.latvian.apps.tinyserver.error; + +public class InvalidPathException extends RuntimeException { + public InvalidPathException(String path) { + super("Invalid path: " + path); + } +} diff --git a/src/main/java/dev/latvian/apps/tinyserver/http/HTTPRequest.java b/src/main/java/dev/latvian/apps/tinyserver/http/HTTPRequest.java index a1c4ccc..8e31085 100644 --- a/src/main/java/dev/latvian/apps/tinyserver/http/HTTPRequest.java +++ b/src/main/java/dev/latvian/apps/tinyserver/http/HTTPRequest.java @@ -2,6 +2,7 @@ import dev.latvian.apps.tinyserver.CompiledPath; import dev.latvian.apps.tinyserver.HTTPServer; +import dev.latvian.apps.tinyserver.error.InvalidPathException; import java.io.IOException; import java.io.InputStream; @@ -47,6 +48,16 @@ public Map variables() { return variables; } + public String variable(String name) { + var s = variables.get(name); + + if (s == null || s.isEmpty()) { + throw new InvalidPathException("Variable " + name + " not found"); + } + + return s; + } + public Map query() { return query; } diff --git a/src/test/java/dev/latvian/apps/tinyserver/test/TinyServerTest.java b/src/test/java/dev/latvian/apps/tinyserver/test/TinyServerTest.java index 73ac261..bf920ea 100644 --- a/src/test/java/dev/latvian/apps/tinyserver/test/TinyServerTest.java +++ b/src/test/java/dev/latvian/apps/tinyserver/test/TinyServerTest.java @@ -41,11 +41,11 @@ private static HTTPResponse test(HTTPRequest req) { } private static HTTPResponse variable(HTTPRequest req) { - return HTTPResponse.ok().text("Test: " + req.variables().get("test")).header("X-ABC", "Def"); + return HTTPResponse.ok().text("Test: " + req.variable("test")).header("X-ABC", "Def"); } private static HTTPResponse varpath(HTTPRequest req) { - return HTTPResponse.ok().text("Test: " + req.variables().get("test")); + return HTTPResponse.ok().text("Test: " + req.variable("test")); } private static HTTPResponse console(HTTPRequest req) throws IOException {