]> source.dussan.org Git - vaadin-framework.git/commitdiff
Merge of (#9548) to Vaadin 7.
authorAnna Koskinen <anna@vaadin.com>
Fri, 1 Feb 2013 08:57:41 +0000 (10:57 +0200)
committerVaadin Code Review <review@vaadin.com>
Tue, 5 Feb 2013 14:26:33 +0000 (14:26 +0000)
Calculate upload file size correctly when there are multibyte characters
in the file name.

Change-Id: I85f1053ce6ad29140d0a37fe8ab4861b0f717fc9

server/src/com/vaadin/server/AbstractCommunicationManager.java
uitest/src/com/vaadin/tests/components/upload/TestFileUploadSize.java [new file with mode: 0644]

index c676cb2ce38983181f936a690041c7b906928441..c1c18901b4db7740f5a82cdcb01cf1c910782601 100644 (file)
@@ -186,7 +186,7 @@ public abstract class AbstractCommunicationManager implements Serializable {
 
     private static final String CRLF = "\r\n";
 
-    private static final String UTF8 = "UTF8";
+    private static final String UTF8 = "UTF-8";
 
     private static String readLine(InputStream stream) throws IOException {
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
@@ -234,14 +234,14 @@ public abstract class AbstractCommunicationManager implements Serializable {
          */
         while (!atStart) {
             String readLine = readLine(inputStream);
-            contentLength -= (readLine.length() + 2);
+            contentLength -= (readLine.getBytes(UTF8).length + CRLF.length());
             if (readLine.startsWith("Content-Disposition:")
                     && readLine.indexOf("filename=") > 0) {
                 rawfilename = readLine.replaceAll(".*filename=", "");
-                String parenthesis = rawfilename.substring(0, 1);
+                char quote = rawfilename.charAt(0);
                 rawfilename = rawfilename.substring(1);
                 rawfilename = rawfilename.substring(0,
-                        rawfilename.indexOf(parenthesis));
+                        rawfilename.indexOf(quote));
                 firstFileFieldFound = true;
             } else if (firstFileFieldFound && readLine.equals("")) {
                 atStart = true;
@@ -251,7 +251,7 @@ public abstract class AbstractCommunicationManager implements Serializable {
         }
 
         contentLength -= (boundary.length() + CRLF.length() + 2
-                * DASHDASH.length() + 2); // 2 == CRLF
+                * DASHDASH.length() + CRLF.length());
 
         /*
          * Reads bytes from the underlying stream. Compares the read bytes to
diff --git a/uitest/src/com/vaadin/tests/components/upload/TestFileUploadSize.java b/uitest/src/com/vaadin/tests/components/upload/TestFileUploadSize.java
new file mode 100644 (file)
index 0000000..32f5c93
--- /dev/null
@@ -0,0 +1,80 @@
+package com.vaadin.tests.components.upload;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+
+import com.vaadin.shared.ui.MarginInfo;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.GridLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Notification;
+import com.vaadin.ui.Upload;
+import com.vaadin.ui.Upload.FinishedEvent;
+import com.vaadin.ui.Upload.Receiver;
+import com.vaadin.ui.Upload.StartedEvent;
+
+public class TestFileUploadSize extends TestBase implements Receiver {
+
+    private Label label = new Label("No finished uploads.");
+    private Label receivedSize = new Label("-");
+    private Label expectedSize = new Label("-");
+
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+    @Override
+    protected void setup() {
+        getLayout().setMargin(new MarginInfo(true, false, false, false));
+        getLayout().setSpacing(true);
+
+        Upload u = new Upload("Upload", new Upload.Receiver() {
+
+            public OutputStream receiveUpload(String filename, String mimeType) {
+                return baos;
+            }
+        });
+        u.setId("UPL");
+        u.addStartedListener(new Upload.StartedListener() {
+
+            public void uploadStarted(StartedEvent event) {
+                expectedSize.setValue(String.valueOf(event.getContentLength()));
+            }
+        });
+        u.addFinishedListener(new Upload.FinishedListener() {
+
+            public void uploadFinished(FinishedEvent event) {
+                label.setValue("Upload finished. Name: " + event.getFilename());
+                receivedSize.setValue(String.valueOf(baos.size()));
+                baos.reset();
+            }
+        });
+
+        expectedSize.setId("expected");
+        receivedSize.setId("received");
+
+        GridLayout grid = new GridLayout(2, 2);
+        grid.addComponent(new Label("Expected size:"), 0, 0);
+        grid.addComponent(new Label("Received size:"), 0, 1);
+        grid.addComponent(expectedSize, 1, 0);
+        grid.addComponent(receivedSize, 1, 1);
+
+        addComponent(label);
+        addComponent(grid);
+        addComponent(u);
+    }
+
+    public OutputStream receiveUpload(String filename, String MIMEType) {
+        Notification.show("Receiving upload");
+        return new ByteArrayOutputStream();
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 9548;
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Multibyte characters in filenames should not cause the upload size to be computed incorrectly";
+    }
+
+}