Merge remote-tracking branch 'origin/jetty-8'

Conflicts:
	jetty-server/src/main/java/org/eclipse/jetty/server/Response.java
	jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java
	jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java
	test-jetty-servlet/src/main/java/org/eclipse/jetty/testing/HttpTester.java
This commit is contained in:
Jan Bartel 2013-06-24 17:28:34 +10:00
commit 7ce05f2c49
3 changed files with 96 additions and 8 deletions

View File

@ -91,6 +91,19 @@ public class HttpTester
{
_version=version;
}
public void setContent(byte[] bytes)
{
try
{
_content=new ByteArrayOutputStream();
_content.write(bytes);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void setContent(String content)
{

View File

@ -45,10 +45,12 @@ 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.util.LazyList;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.MultiPartInputStreamParser;
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;
@ -259,11 +261,11 @@ public class MultiPartFilter implements Filter
{
try
{
return new String((byte[])o,_encoding);
return getParameterBytesAsString(name, (byte[])o);
}
catch(Exception e)
{
e.printStackTrace();
LOG.warn(e);
}
}
else if (o!=null)
@ -282,9 +284,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);
@ -318,7 +318,7 @@ public class MultiPartFilter implements Filter
{
try
{
v[i]=new String((byte[])o,_encoding);
v[i]=getParameterBytesAsString(name, (byte[])o);
}
catch(Exception e)
{
@ -341,5 +341,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.getValue(name+CONTENT_TYPE_SUFFIX,0);
//use default if not
String contentType = _encoding;
if (ct != null)
{
String tmp = MimeTypes.getCharsetFromContentType((String)ct);
contentType = (tmp == null?_encoding:tmp);
}
return new String(bytes,contentType);
}
}
}

View File

@ -26,11 +26,13 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.EnumSet;
import java.util.Map;
import javax.servlet.DispatcherType;
import javax.servlet.ServletException;
@ -38,10 +40,12 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletTester;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -105,6 +109,7 @@ public class MultipartFilterTest
public void tearDown() throws Exception
{
tester.stop();
tester=null;
}
@Test
@ -698,7 +703,6 @@ public class MultipartFilterTest
assertTrue(response.getContent().contains("aaaa,bbbbb"));
}
@Test
public void testContentTypeWithCharSet() throws Exception
{
@ -729,7 +733,7 @@ public class MultipartFilterTest
assertTrue(response.getContent().indexOf("brown cow")>=0);
}
/*
* see the testParameterMap test
*
@ -786,6 +790,59 @@ 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 request = HttpTester.newRequest();
HttpTester.Response response;
tester.addServlet(TestServletCharSet.class,"/test3");
// test GET
request.setMethod("POST");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setURI("/context/test3");
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.setContent(baos.toByteArray());
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
}
public static class DumpServlet extends HttpServlet
{
private static final long serialVersionUID = 201012011130L;