Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Bartel2013-06-24 02:44:45 -0400
committerJan Bartel2013-06-24 02:44:45 -0400
commita7436bc774506be99e80bf0a88c367ce276ba42a (patch)
tree2101c65e8e54483130c7b7c5d44c098b50b6d329 /jetty-servlets/src
parent068e67a8df4d65d1c3645659ffb8e2582171b7a1 (diff)
parent0daddd1a3acf1609169de229b1f33aa2307ce981 (diff)
downloadorg.eclipse.jetty.project-a7436bc774506be99e80bf0a88c367ce276ba42a.tar.gz
org.eclipse.jetty.project-a7436bc774506be99e80bf0a88c367ce276ba42a.tar.xz
org.eclipse.jetty.project-a7436bc774506be99e80bf0a88c367ce276ba42a.zip
Merge remote-tracking branch 'origin/jetty-7' into jetty-8
Conflicts: jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java
Diffstat (limited to 'jetty-servlets/src')
-rw-r--r--jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java32
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java68
2 files changed, 90 insertions, 10 deletions
diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java
index 976fc5aa6d..8277966b9b 100644
--- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java
+++ b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java
@@ -49,13 +49,15 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.Part;
-
import org.eclipse.jetty.util.IO;
+import org.eclipse.jetty.http.MimeTypes;
+import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.MultiPartInputStream;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.StringUtil;
+import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@@ -268,12 +270,11 @@ public class MultiPartFilter implements Filter
{
try
{
- String s=new String((byte[])o,_encoding);
- return s;
+ return getParameterBytesAsString(name, (byte[])o);
}
catch(Exception e)
{
- e.printStackTrace();
+ LOG.warn(e);
}
}
else if (o!=null)
@@ -292,8 +293,7 @@ public class MultiPartFilter implements Filter
for ( Object key : _params.keySet() )
{
- String[] a = LazyList.toStringArray(getParameter((String)key));
- cmap.put((String)key,a);
+ cmap.put((String)key,getParameterValues((String)key));
}
return Collections.unmodifiableMap(cmap);
@@ -327,7 +327,7 @@ public class MultiPartFilter implements Filter
{
try
{
- v[i]=new String((byte[])o,_encoding);
+ v[i]=getParameterBytesAsString(name, (byte[])o);
}
catch(Exception e)
{
@@ -350,5 +350,23 @@ public class MultiPartFilter implements Filter
{
_encoding=enc;
}
+
+
+ /* ------------------------------------------------------------------------------- */
+ private String getParameterBytesAsString (String name, byte[] bytes)
+ throws UnsupportedEncodingException
+ {
+ //check if there is a specific encoding for the parameter
+ Object ct = _params.get(name+CONTENT_TYPE_SUFFIX);
+ //use default if not
+ String contentType = _encoding;
+ if (ct != null)
+ {
+ String tmp = MimeTypes.getCharsetFromContentType(new ByteArrayBuffer((String)ct));
+ contentType = (tmp == null?_encoding:tmp);
+ }
+
+ return new String(bytes,contentType);
+ }
}
}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java
index 046b464624..d79fa4809e 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java
@@ -21,6 +21,7 @@ package org.eclipse.jetty.servlets;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -38,12 +39,13 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.testing.HttpTester;
import org.eclipse.jetty.testing.ServletTester;
import org.eclipse.jetty.util.IO;
-import org.eclipse.jetty.util.QuotedStringTokenizer;
+import org.eclipse.jetty.util.StringUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -755,7 +757,7 @@ public class MultipartFilterTest
assertTrue(response.getContent().contains("aaaa,bbbbb"));
}
-
+
/*
* see the testParameterMap test
*
@@ -768,7 +770,7 @@ public class MultipartFilterTest
String[] content = req.getParameterMap().get("\"strup\"Content-Type: application/octet-stream");
assertThat (content[0], containsString("How now brown cow."));
super.doPost(req, resp);
- }
+ }
}
/**
@@ -813,6 +815,66 @@ public class MultipartFilterTest
assertTrue(response.getContent().indexOf("brown cow")>=0);
}
+
+ public static class TestServletCharSet extends HttpServlet
+ {
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ //test that the multipart content bytes were converted correctly from their charset to unicode
+ String content = (String)req.getParameter("ttt");
+ assertNotNull(content);
+ assertEquals("ttt\u01FCzzz",content);
+ assertEquals("application/octet-stream; charset=UTF-8",req.getParameter("ttt"+MultiPartFilter.CONTENT_TYPE_SUFFIX));
+
+
+ //test that the parameter map retrieves values as String[]
+ Map map = req.getParameterMap();
+ Object o = map.get("ttt");
+ assertTrue(o.getClass().isArray());
+ super.doPost(req, resp);
+ }
+ }
+
+
+ @Test
+ public void testWithCharSet()
+ throws Exception
+ {
+ // generated and parsed test
+ HttpTester request = new HttpTester() {
+
+ };
+ HttpTester response = new HttpTester();
+
+ tester.addServlet(TestServletCharSet.class,"/test2");
+
+ // test GET
+ request.setMethod("POST");
+ request.setVersion("HTTP/1.0");
+ request.setHeader("Host","tester");
+ request.setURI("/context/test2");
+
+ String boundary="XyXyXy";
+ request.setHeader("Content-Type","multipart/form-data; boundary="+boundary);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ baos.write(("--" + boundary + "\r\n"+
+ "Content-Disposition: form-data; name=\"ttt\"\r\n"+
+ "Content-Type: application/octet-stream; charset=UTF-8\r\n\r\n").getBytes());
+ baos.write("ttt\u01FCzzz".getBytes(StringUtil.__UTF8));
+ baos.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes());
+
+ request.setContentBytes(baos.toByteArray());
+
+ response.parse(tester.getResponses(new ByteArrayBuffer(request.generate().getBytes(StringUtil.__UTF8))).toString());
+ }
+
+
+
+
public static class DumpServlet extends HttpServlet
{
private static final long serialVersionUID = 201012011130L;

Back to the top