Skip to content

Commit 7b7ae87

Browse files
committed
Fixed %-codes in URL
1 parent 14f1805 commit 7b7ae87

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/main/java/dev/latvian/apps/tinyserver/HTTPServer.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.latvian.apps.tinyserver;
22

33
import dev.latvian.apps.tinyserver.error.BindFailedException;
4+
import dev.latvian.apps.tinyserver.error.InvalidPathException;
45
import dev.latvian.apps.tinyserver.http.HTTPHandler;
56
import dev.latvian.apps.tinyserver.http.HTTPMethod;
67
import dev.latvian.apps.tinyserver.http.HTTPPathHandler;
@@ -251,6 +252,16 @@ private void handleClient(Socket socket) {
251252
} else {
252253
var pathParts = path.split("/");
253254

255+
for (int i = 0; i < pathParts.length; i++) {
256+
try {
257+
if (pathParts[i].indexOf('%') != -1) {
258+
pathParts[i] = URLDecoder.decode(pathParts[i], StandardCharsets.UTF_8);
259+
}
260+
} catch (Exception ex) {
261+
throw new InvalidPathException(ex.getMessage());
262+
}
263+
}
264+
254265
for (var handler : handlers.entrySet()) {
255266
if (handler.getValue().staticHandlers().containsKey(path)) {
256267
allowed.add(handler.getKey());
@@ -293,6 +304,17 @@ private void handleClient(Socket socket) {
293304

294305
if (hl != null) {
295306
var pathParts = path.split("/");
307+
308+
for (int i = 0; i < pathParts.length; i++) {
309+
try {
310+
if (pathParts[i].indexOf('%') != -1) {
311+
pathParts[i] = URLDecoder.decode(pathParts[i], StandardCharsets.UTF_8);
312+
}
313+
} catch (Exception ex) {
314+
throw new InvalidPathException(ex.getMessage());
315+
}
316+
}
317+
296318
var h = hl.staticHandlers().get(path);
297319

298320
if (h != null) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dev.latvian.apps.tinyserver.error;
2+
3+
public class InvalidPathException extends RuntimeException {
4+
public InvalidPathException(String path) {
5+
super("Invalid path: " + path);
6+
}
7+
}

src/main/java/dev/latvian/apps/tinyserver/http/HTTPRequest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.latvian.apps.tinyserver.CompiledPath;
44
import dev.latvian.apps.tinyserver.HTTPServer;
5+
import dev.latvian.apps.tinyserver.error.InvalidPathException;
56

67
import java.io.IOException;
78
import java.io.InputStream;
@@ -47,6 +48,16 @@ public Map<String, String> variables() {
4748
return variables;
4849
}
4950

51+
public String variable(String name) {
52+
var s = variables.get(name);
53+
54+
if (s == null || s.isEmpty()) {
55+
throw new InvalidPathException("Variable " + name + " not found");
56+
}
57+
58+
return s;
59+
}
60+
5061
public Map<String, String> query() {
5162
return query;
5263
}

src/test/java/dev/latvian/apps/tinyserver/test/TinyServerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ private static HTTPResponse test(HTTPRequest req) {
4141
}
4242

4343
private static HTTPResponse variable(HTTPRequest req) {
44-
return HTTPResponse.ok().text("Test: " + req.variables().get("test")).header("X-ABC", "Def");
44+
return HTTPResponse.ok().text("Test: " + req.variable("test")).header("X-ABC", "Def");
4545
}
4646

4747
private static HTTPResponse varpath(HTTPRequest req) {
48-
return HTTPResponse.ok().text("Test: " + req.variables().get("test"));
48+
return HTTPResponse.ok().text("Test: " + req.variable("test"));
4949
}
5050

5151
private static HTTPResponse console(HTTPRequest req) throws IOException {

0 commit comments

Comments
 (0)