Improving fix for padding lines in replies starting with digits
authorNiklas Gustavsson <ngn@apache.org>
Sun, 7 Mar 2010 17:51:34 +0000 (17:51 +0000)
committerNiklas Gustavsson <ngn@apache.org>
Sun, 7 Mar 2010 17:51:34 +0000 (17:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/mina/ftpserver/trunk@920048 13f79535-47bb-0310-9956-ffa450edef68

ftplet-api/src/main/java/org/apache/ftpserver/ftplet/DefaultFtpReply.java
ftplet-api/src/test/java/org/apache/ftpserver/ftplet/DefaultFtpReplyTest.java [moved from core/src/test/java/org/apache/ftpserver/impl/DefaultFtpReplyTest.java with 77% similarity]

index d1eab0f..b208875 100644 (file)
@@ -19,6 +19,7 @@
 
 package org.apache.ftpserver.ftplet;
 
+
 /**
  * FTP reply object.
  *
@@ -87,6 +88,10 @@ public class DefaultFtpReply implements FtpReply {
        return code < 400;
     }
     
+    private boolean isDigit(char c) {
+       return c >= 48 && c <= 57;
+    }
+    
     /*
      * (non-Javadoc)
      * 
@@ -121,7 +126,15 @@ public class DefaultFtpReply implements FtpReply {
                     sb.append(" ");
                 }
 
-                if(line.length() > 0 && Character.isDigit(line.charAt(0))) {
+                // "If an intermediary line begins with a 3-digit number, the Server
+                // must pad the front  to avoid confusion.
+                if(i > 0 
+                               && i + 1 < lines.length 
+                               && line.length() > 2 
+                               && isDigit(line.charAt(0))
+                               && isDigit(line.charAt(1))
+                               && isDigit(line.charAt(2))
+                       ) {
                        sb.append("  ");
                 }
                 sb.append(line);
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-package org.apache.ftpserver.impl;
+package org.apache.ftpserver.ftplet;
 
 import junit.framework.TestCase;
 
@@ -73,10 +73,28 @@ public class DefaultFtpReplyTest extends TestCase {
         assertEquals("123-foo\r\n bar\r\n123 baz\r\n", response.toString());
     }
 
-    public void testMultipleLinesToStringNumberFirst() {
+    public void testMultipleLinesToStringThreeNumbers() {
         DefaultFtpReply response = new DefaultFtpReply(123, "foo\n234bar\nbaz");
 
         assertEquals("123-foo\r\n  234bar\r\n123 baz\r\n", response.toString());
     }
 
+    public void testMultipleLinesToStringThreeNumbersOnFirstLine() {
+        DefaultFtpReply response = new DefaultFtpReply(123, "234foo\nbar\nbaz");
+
+        assertEquals("123-234foo\r\nbar\r\n123 baz\r\n", response.toString());
+    }
+
+    public void testMultipleLinesToStringThreeNumbersOnLastLine() {
+        DefaultFtpReply response = new DefaultFtpReply(123, "foo\nbar\n234baz");
+
+        assertEquals("123-foo\r\nbar\r\n123 234baz\r\n", response.toString());
+    }
+
+    public void testMultipleLinesToStringSingleNumberOnLine() {
+        DefaultFtpReply response = new DefaultFtpReply(123, "foo\n2bar\nbaz");
+
+        assertEquals("123-foo\r\n2bar\r\n123 baz\r\n", response.toString());
+    }
+
 }