Skip to content

Commit

Permalink
Extract slash as a constant to reuse in FilterUtil (alibaba#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
shannon312 authored and sczyh30 committed Oct 29, 2018
1 parent d726e62 commit 4198d21
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
*/
public final class FilterUtil {

private static final String PATH_SPLIT = "/";

public static String filterTarget(HttpServletRequest request) {
String pathInfo = getResourcePath(request);
if (!pathInfo.startsWith("/")) {
pathInfo = "/" + pathInfo;
if (!pathInfo.startsWith(PATH_SPLIT)) {
pathInfo = PATH_SPLIT + pathInfo;
}

if ("/".equals(pathInfo)) {
if (PATH_SPLIT.equals(pathInfo)) {
return pathInfo;
}

Expand All @@ -49,7 +51,7 @@ public static String filterTarget(HttpServletRequest request) {
pathInfo = pathInfo.substring(0, lastSlashIndex) + "/"
+ StringUtil.trim(pathInfo.substring(lastSlashIndex + 1));
} else {
pathInfo = "/" + StringUtil.trim(pathInfo);
pathInfo = PATH_SPLIT + StringUtil.trim(pathInfo);
}

return pathInfo;
Expand Down Expand Up @@ -102,8 +104,8 @@ private static String normalizePath(String path, boolean forceAbsolute, boolean
char firstChar = pathChars[0];
char lastChar = pathChars[length - 1];

startsWithSlash = firstChar == '/' || firstChar == '\\';
endsWithSlash = lastChar == '/' || lastChar == '\\';
startsWithSlash = firstChar == PATH_SPLIT.charAt(0) || firstChar == '\\';
endsWithSlash = lastChar == PATH_SPLIT.charAt(0) || lastChar == '\\';
}

StringBuilder buf = new StringBuilder(length);
Expand All @@ -112,7 +114,7 @@ private static String normalizePath(String path, boolean forceAbsolute, boolean
int level = 0;

if (isAbsolutePath) {
buf.append("/");
buf.append(PATH_SPLIT);
}

while (index < length) {
Expand All @@ -138,7 +140,7 @@ private static String normalizePath(String path, boolean forceAbsolute, boolean
if (isAbsolutePath) {
throw new IllegalStateException(path);
} else {
buf.append("../");
buf.append("..").append(PATH_SPLIT);
}
} else {
buf.setLength(pathChars[--level]);
Expand All @@ -148,7 +150,7 @@ private static String normalizePath(String path, boolean forceAbsolute, boolean
}

pathChars[level++] = (char)buf.length();
buf.append(element).append('/');
buf.append(element).append(PATH_SPLIT);
}

// remove the last "/"
Expand All @@ -168,11 +170,11 @@ private static int indexOfSlash(char[] chars, int beginIndex, boolean slash) {
char ch = chars[i];

if (slash) {
if (ch == '/' || ch == '\\') {
if (ch == PATH_SPLIT.charAt(0) || ch == '\\') {
break; // if a slash
}
} else {
if (ch != '/' && ch != '\\') {
if (ch != PATH_SPLIT.charAt(0) && ch != '\\') {
break; // if not a slash
}
}
Expand Down

0 comments on commit 4198d21

Please sign in to comment.