Author: sebb
Date: Wed Dec 12 13:14:33 2012
New Revision: 1420655
URL: http://svn.apache.org/viewvc?rev=1420655&view=rev
Log:
NET-492 FTPClient.printWorkingDirectory() incorrectly parses certain valid PWD
command results
Rework to look for lone trailing quote
Added:
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/FTPClientTest.java
(with props)
Modified:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
Modified:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
URL:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java?rev=1420655&r1=1420654&r2=1420655&view=diff
==============================================================================
---
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
(original)
+++
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
Wed Dec 12 13:14:33 2012
@@ -496,18 +496,33 @@ implements Configurable
* @param reply
* @return
*/
- private static String __parsePathname(String reply)
+ // package protected for access by test cases
+ static String __parsePathname(String reply)
{
String param = reply.substring(REPLY_CODE_LEN + 1);
if (param.startsWith("\"")) {
- int end;
- if (param.endsWith("\"")) {
- end = param.length()-1;
- } else { // perhaps there's a trailing comment
- end=param.lastIndexOf("\" "); // find start of comment (assume
it does not contain ")
+ StringBuilder sb = new StringBuilder();
+ boolean quoteSeen = false;
+ // start after initial quote
+ for(int i=1; i < param.length(); i++) {
+ char ch = param.charAt(i);
+ if (ch=='"') {
+ if (quoteSeen) {
+ sb.append(ch);
+ quoteSeen=false;
+ } else {
+ // don't output yet, in case doubled
+ quoteSeen=true;
+ }
+ } else {
+ if (quoteSeen) { // found lone trailing quote within string
+ return sb.toString();
+ }
+ sb.append(ch); // just another character
+ }
}
- if (end != -1) { // It was a match
- return param.substring(1, end).replace("\"\"", "\"");
+ if (quoteSeen) { // found lone trailing quote at end of string
+ return sb.toString();
}
}
// malformed reply, return all after reply code and space
Added:
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/FTPClientTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/FTPClientTest.java?rev=1420655&view=auto
==============================================================================
---
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/FTPClientTest.java
(added)
+++
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/FTPClientTest.java
Wed Dec 12 13:14:33 2012
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.commons.net.ftp;
+
+import junit.framework.TestCase;
+
+public class FTPClientTest extends TestCase {
+
+ private static final String[] TESTS = {
+ "257 /path/without/quotes",
+ "/path/without/quotes",
+
+ "257 \"/path/with/delimiting/quotes/without/commentary\"",
+ "/path/with/delimiting/quotes/without/commentary",
+
+ "257 \"/path/with/quotes\"\" /inside/but/without/commentary\"",
+ "/path/with/quotes\" /inside/but/without/commentary",
+
+ "257 \"/path/with/quotes\"\" /inside/string\" and with commentary",
+ "/path/with/quotes\" /inside/string",
+
+ "257 \"/path/with/quotes\"\" /inside/string\" and with commentary that
also \"contains quotes\"",
+ "/path/with/quotes\" /inside/string",
+
+ "257 \"/path/without/trailing/quote", // invalid syntax, return all
after reply code prefix
+ "\"/path/without/trailing/quote",
+
+ "257 root is current directory.", // NET-442
+ "root is current directory."
+
+ };
+ public FTPClientTest(String name) {
+ super(name);
+ }
+
+ public void testParseClient() {
+ for(int i=0; i<TESTS.length; i+=2) {
+ assertEquals("Failed to parse",TESTS[i+1],
FTPClient.__parsePathname(TESTS[i]));
+ }
+ }
+
+}
Propchange:
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/FTPClientTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/FTPClientTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision