garydgregory commented on code in PR #396:
URL: https://github.com/apache/commons-vfs/pull/396#discussion_r1444666817


##########
commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java:
##########
@@ -455,6 +455,156 @@ public static boolean fixSeparators(final StringBuilder 
name) {
         return changed;
     }
 
+    private static class PathNormalizer {
+        private final StringBuilder path;
+        private int cursor = 0;
+        private int lastSeparator;
+        private int end;
+        PathNormalizer(StringBuilder path) {
+            this.path = path;
+            this.end = path.length();
+        }
+        void run() throws FileSystemException {
+            lastSeparator = cursor;
+            readSeparator();
+            while (cursor < end) {
+                consumeSeparators();
+                if (readDot()) {
+                    if (readDot()) {
+                        int beforeNextSeparator = cursor;
+                        if (readSeparator() || cursor == end) {
+                            // '/../'
+                            removePreviousElement(beforeNextSeparator);
+                        } else {
+                            // '/..other'
+                            readNonSeparators();
+                            lastSeparator = cursor;
+                            readSeparator();
+                        }
+                    } else {
+                        int beforeNextSeparator = cursor;
+                        if (readSeparator() || cursor == end) {
+                            // '/./'
+                            path.delete(lastSeparator, beforeNextSeparator);
+                            cursor = lastSeparator + (cursor - 
beforeNextSeparator);
+                            this.end = path.length();
+                        } else {
+                            // '/.other'
+                            readNonSeparators();
+                            lastSeparator = cursor;
+                            readSeparator();
+                        }
+                    }
+                } else {
+                    readToNextSeparator();
+                    lastSeparator = cursor;
+                    readSeparator();
+                }
+            }
+        }
+        private void consumeSeparators() {
+            boolean consuming = true;
+            while (consuming) {
+                consuming = consumeSeparator();
+            }
+        }
+        private void readNonSeparators() {
+            boolean reading = true;
+            while (reading) {
+                reading = readNonSeparator();
+            }

Review Comment:
   Simpler to say `while (readNonSeparator()); ?



##########
commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java:
##########
@@ -455,6 +455,156 @@ public static boolean fixSeparators(final StringBuilder 
name) {
         return changed;
     }
 
+    private static class PathNormalizer {
+        private final StringBuilder path;
+        private int cursor = 0;
+        private int lastSeparator;
+        private int end;
+        PathNormalizer(StringBuilder path) {
+            this.path = path;
+            this.end = path.length();
+        }
+        void run() throws FileSystemException {
+            lastSeparator = cursor;
+            readSeparator();
+            while (cursor < end) {
+                consumeSeparators();
+                if (readDot()) {
+                    if (readDot()) {
+                        int beforeNextSeparator = cursor;
+                        if (readSeparator() || cursor == end) {
+                            // '/../'
+                            removePreviousElement(beforeNextSeparator);
+                        } else {
+                            // '/..other'
+                            readNonSeparators();
+                            lastSeparator = cursor;
+                            readSeparator();
+                        }
+                    } else {
+                        int beforeNextSeparator = cursor;
+                        if (readSeparator() || cursor == end) {
+                            // '/./'
+                            path.delete(lastSeparator, beforeNextSeparator);
+                            cursor = lastSeparator + (cursor - 
beforeNextSeparator);
+                            this.end = path.length();
+                        } else {
+                            // '/.other'
+                            readNonSeparators();
+                            lastSeparator = cursor;
+                            readSeparator();
+                        }
+                    }
+                } else {
+                    readToNextSeparator();
+                    lastSeparator = cursor;
+                    readSeparator();
+                }
+            }
+        }
+        private void consumeSeparators() {
+            boolean consuming = true;
+            while (consuming) {
+                consuming = consumeSeparator();
+            }
+        }
+        private void readNonSeparators() {
+            boolean reading = true;
+            while (reading) {
+                reading = readNonSeparator();
+            }
+        }
+        private void removePreviousElement(int to) throws FileSystemException {
+            if (lastSeparator == 0) {
+                // Previous element is missing
+                throw new 
FileSystemException("vfs.provider/invalid-relative-path.error");
+            }
+            cursor = lastSeparator - 1;
+            while (readNonSeparator()) {
+                cursor = cursor - 2;
+                if (cursor < 0) {
+                    // Previous element is missing
+                    throw new 
FileSystemException("vfs.provider/invalid-relative-path.error");
+                }
+            }
+            path.delete(cursor, to);
+            lastSeparator = cursor;
+            this.end = path.length();
+            readSeparator();
+        }
+        private void readToNextSeparator() {
+            boolean reading = true;
+            while (reading) {
+                reading = readNonSeparator();
+            }
+        }
+        private boolean readSeparator() {
+            if (cursor == end) {
+                return false;
+            }
+            if (path.charAt(cursor) == SEPARATOR_CHAR) {
+                cursor++;
+                return true;
+            }
+            if (cursor + 2 >= end) {
+                return false;
+            }
+            String sub = path.substring(cursor, cursor + 3);
+            if (sub.equals("%2f") || sub.equals("%2F")) {
+                cursor = cursor + 3;
+                return true;
+            }
+            return false;
+        }
+        private boolean readDot() {
+            if (cursor == end) {
+                return false;
+            }
+            if (path.charAt(cursor) == '.') {
+                cursor++;
+                return true;
+            }
+            if (cursor + 2 >= end) {
+                return false;
+            }
+            String sub = path.substring(cursor, cursor + 3);
+            if (sub.equals("%2e") || sub.equals("%2E")) {
+                cursor = cursor + 3;
+                return true;
+            }
+            return false;
+        }
+        private boolean consumeSeparator() {
+            int from = cursor;
+            if (readSeparator()) {
+                path.delete(from, cursor);
+                cursor = from;
+                this.end = path.length();
+                return true;
+            }
+            return false;
+        }
+        private boolean readNonSeparator() {
+            if (cursor == end) {
+                return false;
+            }
+            if (path.charAt(cursor) == SEPARATOR_CHAR) {
+                return false;
+            }
+            if (cursor + 2 >= end) {
+                cursor++;
+                return true;
+            }
+            String sub = path.substring(cursor + 1, cursor + 3);
+            if (sub.equals("%2f") || sub.equals("%2F")) {

Review Comment:
   Refactor magic string into a constant with a comment containing the fact 
that 2f is a forward slash.



##########
commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java:
##########
@@ -455,6 +455,156 @@ public static boolean fixSeparators(final StringBuilder 
name) {
         return changed;
     }
 
+    private static class PathNormalizer {
+        private final StringBuilder path;
+        private int cursor = 0;
+        private int lastSeparator;
+        private int end;
+        PathNormalizer(StringBuilder path) {
+            this.path = path;
+            this.end = path.length();
+        }
+        void run() throws FileSystemException {
+            lastSeparator = cursor;
+            readSeparator();
+            while (cursor < end) {
+                consumeSeparators();
+                if (readDot()) {
+                    if (readDot()) {
+                        int beforeNextSeparator = cursor;
+                        if (readSeparator() || cursor == end) {
+                            // '/../'
+                            removePreviousElement(beforeNextSeparator);
+                        } else {
+                            // '/..other'
+                            readNonSeparators();
+                            lastSeparator = cursor;
+                            readSeparator();
+                        }
+                    } else {
+                        int beforeNextSeparator = cursor;
+                        if (readSeparator() || cursor == end) {
+                            // '/./'
+                            path.delete(lastSeparator, beforeNextSeparator);
+                            cursor = lastSeparator + (cursor - 
beforeNextSeparator);
+                            this.end = path.length();
+                        } else {
+                            // '/.other'
+                            readNonSeparators();
+                            lastSeparator = cursor;
+                            readSeparator();
+                        }
+                    }
+                } else {
+                    readToNextSeparator();
+                    lastSeparator = cursor;
+                    readSeparator();
+                }
+            }
+        }
+        private void consumeSeparators() {
+            boolean consuming = true;
+            while (consuming) {
+                consuming = consumeSeparator();
+            }
+        }
+        private void readNonSeparators() {
+            boolean reading = true;
+            while (reading) {
+                reading = readNonSeparator();
+            }
+        }
+        private void removePreviousElement(int to) throws FileSystemException {
+            if (lastSeparator == 0) {
+                // Previous element is missing
+                throw new 
FileSystemException("vfs.provider/invalid-relative-path.error");
+            }
+            cursor = lastSeparator - 1;
+            while (readNonSeparator()) {
+                cursor = cursor - 2;
+                if (cursor < 0) {
+                    // Previous element is missing
+                    throw new 
FileSystemException("vfs.provider/invalid-relative-path.error");
+                }
+            }
+            path.delete(cursor, to);
+            lastSeparator = cursor;
+            this.end = path.length();
+            readSeparator();
+        }
+        private void readToNextSeparator() {
+            boolean reading = true;
+            while (reading) {
+                reading = readNonSeparator();
+            }
+        }
+        private boolean readSeparator() {
+            if (cursor == end) {
+                return false;
+            }
+            if (path.charAt(cursor) == SEPARATOR_CHAR) {
+                cursor++;
+                return true;
+            }
+            if (cursor + 2 >= end) {
+                return false;
+            }
+            String sub = path.substring(cursor, cursor + 3);
+            if (sub.equals("%2f") || sub.equals("%2F")) {
+                cursor = cursor + 3;
+                return true;
+            }
+            return false;
+        }
+        private boolean readDot() {

Review Comment:
   Nit: Add a blank line b/w methods.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to