aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Hawke <paul.hawke@gmail.com>2014-04-24 00:01:36 -0500
committerPaul Hawke <paul.hawke@gmail.com>2014-04-24 00:01:36 -0500
commit79cd900bd6099d688c5374691a750bc94a8dd464 (patch)
treeb71294deb600fb645c74a0ac163798cc06e4a8ad
parent9168877f82d99c156a25dbab85059f17b5042625 (diff)
parenta42baa1b602b7d2d7bbfdad9238b0125725da9a3 (diff)
downloadnanohttpd-79cd900bd6099d688c5374691a750bc94a8dd464.tar.gz
Merge pull request #100 from gazazello/master
Fix for issue with spaces in file names
-rw-r--r--core/src/main/java/fi/iki/elonen/NanoHTTPD.java4
-rw-r--r--core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java47
2 files changed, 39 insertions, 12 deletions
diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
index 4be5983..b1d59c3 100644
--- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
+++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
@@ -1112,10 +1112,10 @@ public abstract class NanoHTTPD {
if (contentDisposition == null) {
throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but no content-disposition info found. Usage: GET /example/file.html");
}
- StringTokenizer st = new StringTokenizer(contentDisposition, "; ");
+ StringTokenizer st = new StringTokenizer(contentDisposition, ";");
Map<String, String> disposition = new HashMap<String, String>();
while (st.hasMoreTokens()) {
- String token = st.nextToken();
+ String token = st.nextToken().trim();
int p = token.indexOf('=');
if (p != -1) {
disposition.put(token.substring(0, p).trim().toLowerCase(Locale.US), token.substring(p + 1).trim());
diff --git a/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java b/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java
index ca6707e..522a8c9 100644
--- a/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java
+++ b/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java
@@ -4,6 +4,7 @@ import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileReader;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@@ -85,16 +86,46 @@ public class HttpPostRequestTest extends HttpServerTest {
assertEquals(VALUE, testServer.parms.get(FIELD));
assertEquals(VALUE2, testServer.parms.get(FIELD2));
}
-
+
@Test
public void testPostWithMultipartFormUpload() throws Exception {
- String divider = UUID.randomUUID().toString();
String filename = "GrandCanyon.txt";
String fileContent = VALUE;
+ String input = preparePostWithMultipartForm(filename, fileContent);
+
+ invokeServer(input);
+
+ assertEquals(1, testServer.parms.size());
+ BufferedReader reader = new BufferedReader(new FileReader(testServer.files.get(FIELD)));
+ List<String> lines = readLinesFromFile(reader);
+ assertLinesOfText(new String[]{fileContent}, lines);
+ }
+
+ @Test
+ public void testPostWithMultipartFormUploadFilenameHasSpaces() throws Exception {
+ String fileNameWithSpace = "Grand Canyon.txt";
+ String fileContent = VALUE;
+ String input = preparePostWithMultipartForm(fileNameWithSpace, fileContent);
+
+ invokeServer(input);
+
+ String fileNameAfter = new ArrayList<String>(testServer.parms.values()).get(0);
+
+ assertEquals(fileNameWithSpace, fileNameAfter);
+ }
+
+ /**
+ * contains common preparation steps for testing POST with Multipart Form
+ * @param fileName Name of file to be uploaded
+ * @param fileContent Content of file to be uploaded
+ * @return input String with POST request complete information including header, length and content
+ */
+ private String preparePostWithMultipartForm(String fileName, String fileContent) {
+ String divider = UUID.randomUUID().toString();
String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
"multipart/form-data, boundary=" + divider + "\n";
String content = "--" + divider + "\n" +
- "Content-Disposition: form-data; name=\""+FIELD+"\"; filename=\""+filename+"\"\n" +
+ "Content-Disposition: form-data; name=\""+FIELD+"\"; filename=\""+fileName+"\"\n" +
"Content-Type: image/jpeg\r\n"+
"\r\n" +
fileContent +"\r\n" +
@@ -103,12 +134,8 @@ public class HttpPostRequestTest extends HttpServerTest {
int contentLengthHeaderValueSize = String.valueOf(size).length();
int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
String input = header + CONTENT_LENGTH + (contentLength+5) + "\r\n\r\n" + content;
-
- invokeServer(input);
-
- assertEquals(1, testServer.parms.size());
- BufferedReader reader = new BufferedReader(new FileReader(testServer.files.get(FIELD)));
- List<String> lines = readLinesFromFile(reader);
- assertLinesOfText(new String[]{fileContent}, lines);
+
+ return input;
}
+
}