411458 MultiPartFilter getParameterMap doesn't preserve multivalued parameters

411459  MultiPartFilter.Wrapper getParameter should use charset encoding of part
This commit is contained in:
Jan Bartel 2013-06-24 16:20:04 +10:00
parent 0bca1974b7
commit 0daddd1a3a
3 changed files with 103 additions and 10 deletions

View File

@ -48,12 +48,15 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.ReadLineInputStream;
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;
@ -513,12 +516,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)
@ -533,11 +535,11 @@ public class MultiPartFilter implements Filter
@Override
public Map getParameterMap()
{
Map<String, String> cmap = new HashMap<String,String>();
Map<String, String[]> cmap = new HashMap<String,String[]>();
for ( Object key : _params.keySet() )
{
cmap.put((String)key,getParameter((String)key));
cmap.put((String)key,getParameterValues((String)key));
}
return Collections.unmodifiableMap(cmap);
@ -571,7 +573,7 @@ public class MultiPartFilter implements Filter
{
try
{
v[i]=new String((byte[])o,_encoding);
v[i]=getParameterBytesAsString(name, (byte[])o);
}
catch(Exception e)
{
@ -594,6 +596,24 @@ 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);
}
}
private static class Base64InputStream extends InputStream

View File

@ -21,21 +21,26 @@ package org.eclipse.jetty.servlets;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.Map;
import javax.servlet.ServletException;
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.StringUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -747,7 +752,7 @@ public class MultipartFilterTest
assertTrue(response.getContent().contains("aaaa,bbbbb"));
}
/*
* see the testParameterMap test
*
@ -758,13 +763,12 @@ public class MultipartFilterTest
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String content = (String)req.getParameterMap().get("\"strup\"Content-Type: application/octet-stream");
String content = (String)req.getParameter("\"strup\"Content-Type: application/octet-stream");
assertThat(content, containsString("How now brown cow."));
super.doPost(req, resp);
}
}
}
/**
@ -809,6 +813,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;

View File

@ -531,6 +531,15 @@ public class HttpTester
_genContent=null;
}
}
/* ------------------------------------------------------------ */
public void setContentBytes(byte[] bytes)
{
_parsedContent = null;
_genContent = bytes;
setLongHeader(HttpHeaders.CONTENT_LENGTH, bytes.length);
}
/* ------------------------------------------------------------ */
private class PH extends HttpParser.EventHandler