Skip to content

Commit

Permalink
Fixed %-codes in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Aug 20, 2024
1 parent 14f1805 commit 7b7ae87
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/main/java/dev/latvian/apps/tinyserver/HTTPServer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.latvian.apps.tinyserver.error;

public class InvalidPathException extends RuntimeException {
public InvalidPathException(String path) {
super("Invalid path: " + path);
}
}
11 changes: 11 additions & 0 deletions src/main/java/dev/latvian/apps/tinyserver/http/HTTPRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,6 +48,16 @@ public Map<String, String> 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<String, String> query() {
return query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7b7ae87

Please sign in to comment.