Skip to content

Commit

Permalink
ignored + while decoding path
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzardo committed Apr 19, 2024
1 parent e98f724 commit 5779063
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/main/java/com/wizzardo/http/FileTreeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import com.wizzardo.http.response.RangeResponseHelper;
import com.wizzardo.http.response.Response;
import com.wizzardo.http.response.Status;
import com.wizzardo.http.utils.PercentEncoding;
import com.wizzardo.tools.misc.DateIso8601;
import com.wizzardo.tools.misc.Unchecked;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
Expand Down Expand Up @@ -263,8 +264,10 @@ protected String encodeName(String name) {
return Unchecked.call(() -> URLEncoder.encode(name, "utf-8").replace("+", "%20"));
}

private String decodePath(String path) {
return Unchecked.call(() -> URLDecoder.decode(VERSION_PATTERN.matcher(path).replaceAll(""), "utf-8"));
protected String decodePath(String path) {
byte[] bytes = VERSION_PATTERN.matcher(path).replaceAll("").getBytes(StandardCharsets.UTF_8);
int decodedLength = PercentEncoding.decode(bytes, 0, bytes.length, true);
return new String(bytes, 0, decodedLength, StandardCharsets.UTF_8);
}

public static class HandlerContext {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/wizzardo/http/utils/PercentEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
* Created by Mikhail Bobrutskov on 20.08.17.
*/
public class PercentEncoding {

static byte[] mapping;

static {
mapping = new byte[128];
Arrays.fill(mapping, (byte) 128);
Arrays.fill(mapping, (byte) 127);
mapping['0'] = 0;
mapping['1'] = 1;
mapping['2'] = 2;
Expand Down Expand Up @@ -40,6 +37,10 @@ public class PercentEncoding {
}

public static int decode(byte[] bytes, int from, int to) {
return decode(bytes, from, to, false);
}

public static int decode(byte[] bytes, int from, int to, boolean ignorePlus) {
int position = from;
int i = from;
try {
Expand All @@ -51,7 +52,7 @@ public static int decode(byte[] bytes, int from, int to) {

byte value = (byte) ((getHexValue(bytes[++i]) << 4) + getHexValue(bytes[++i]));
bytes[position++] = value;
} else if (b == '+') {
} else if (!ignorePlus && b == '+') {
bytes[position++] = ' ';
} else if (position == i) {
position++;
Expand All @@ -72,7 +73,7 @@ public static int getHexValue(int c) {
throw new IllegalStateException("unexpected char for hex value: " + (char) c);

c = mapping[c];
if (c == 128)
if (c == 127)
throw new IllegalStateException("unexpected char for hex value");

return c;
Expand Down

0 comments on commit 5779063

Please sign in to comment.