Troubleshooting multipart

This commit is contained in:
Ziver Koc 2016-07-10 22:09:56 +02:00
parent 077963ae28
commit a700cf2863
4 changed files with 34 additions and 11 deletions

View file

@ -36,7 +36,7 @@ import java.io.InputStream;
*/ */
public class BufferedBoundaryInputStream extends FilterInputStream{ public class BufferedBoundaryInputStream extends FilterInputStream{
/** The size of the buffer in bytes */ /** The size of the buffer in bytes */
public static final int DEFAULT_BUF_SIZE = 64*1024; public static final int DEFAULT_BUF_SIZE = 8192;
/** The raw buffer */ /** The raw buffer */
protected byte buffer[]; protected byte buffer[];
@ -133,16 +133,15 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
* @return the amount of bytes read or -1 if EOF * @return the amount of bytes read or -1 if EOF
*/ */
public int read(byte b[], int off, int len) throws IOException { public int read(byte b[], int off, int len) throws IOException {
if(bound_pos == buf_pos)
return -1; // boundary
if(buf_pos >= buf_end-boundary.length) { if(buf_pos >= buf_end-boundary.length) {
if(fillBuffer() <= 0) if(fillBuffer() <= 0)
return -1; // EOF return -1; // EOF
} }
int leftover = buf_end - buf_pos; if(bound_pos == buf_pos)
return -1; // boundary
// The request is larger then the buffer size // The request is larger then the buffer size
int leftover = buf_end - buf_pos;
if(len > leftover){ if(len > leftover){
len = leftover; len = leftover;
} }
@ -159,11 +158,13 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
/** /**
* Skips over the boundary at the current position in the buffer * Skips over the boundary at the current position in the buffer
*/ */
public void next(){ public boolean next(){
if(bound_pos == buf_pos){ if(bound_pos == buf_pos){
buf_pos += boundary.length; buf_pos += boundary.length;
searchNextBoundary(); searchNextBoundary();
return buf_end >= buf_pos;
} }
return true;
} }
/** /**
@ -218,7 +219,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
/** /**
* @return if there is more data to read * @return if there is more data to read
*/ */
public boolean hasNext(){ public boolean isOnBoundary(){
return bound_pos == buf_pos; return bound_pos == buf_pos;
} }

View file

@ -43,7 +43,7 @@ public class IOUtil {
*/ */
public static byte[] getContent(InputStream stream) throws IOException{ public static byte[] getContent(InputStream stream) throws IOException{
DynamicByteArrayStream dyn_buff = new DynamicByteArrayStream(); DynamicByteArrayStream dyn_buff = new DynamicByteArrayStream();
byte[] buff = new byte[256]; byte[] buff = new byte[8192];
int len = 0; int len = 0;
while((len = stream.read(buff)) != -1){ while((len = stream.read(buff)) != -1){
dyn_buff.append(buff, 0, len); dyn_buff.append(buff, 0, len);

View file

@ -120,7 +120,7 @@ public class MultipartParser implements Iterable<MultipartField>{
public boolean hasNext() { public boolean hasNext() {
try { try {
IOUtil.copyStream(buffIn, new NullWriter()); IOUtil.copyStream(buffIn, new NullWriter());
return boundaryIn.hasNext(); return boundaryIn.next();
} catch (IOException e){ } catch (IOException e){
logger.log(Level.SEVERE, null, e); logger.log(Level.SEVERE, null, e);
} }
@ -133,8 +133,11 @@ public class MultipartParser implements Iterable<MultipartField>{
if (!hasNext()) if (!hasNext())
return null; return null;
try { try {
boundaryIn.next(); // Skip current boundary
HttpHeader header = parser.read(); HttpHeader header = parser.read();
String tmp = buffIn.readLine(); // read the new line after the delimiter
if (tmp == null && tmp.equals("--"))
return null;
String disposition = header.getHeader(HEADER_CONTENT_DISPOSITION); String disposition = header.getHeader(HEADER_CONTENT_DISPOSITION);
if (disposition != null){ if (disposition != null){
HashMap<String,String> map = new HashMap<>(); HashMap<String,String> map = new HashMap<>();

View file

@ -68,6 +68,12 @@ public class BufferedBoundaryInputStreamTest {
in.setBoundary("#"); in.setBoundary("#");
assertEquals(-1, in.read()); assertEquals(-1, in.read());
inin = new StringInputStream("#aaa");
in = new BufferedBoundaryInputStream(inin);
in.setBoundary("#");
assertEquals(-1, in.read(new byte[10], 0, 10));
} }
@Test @Test
@ -82,7 +88,7 @@ public class BufferedBoundaryInputStreamTest {
assertEquals(-1, in.read()); assertEquals(-1, in.read());
assertEquals(-1, in.read()); assertEquals(-1, in.read());
in.next(); in.next();
if(!in.hasNext()) if(!in.isOnBoundary())
break; break;
} }
assertEquals(35, n); assertEquals(35, n);
@ -103,4 +109,17 @@ public class BufferedBoundaryInputStreamTest {
assertEquals(data, output.toString()); assertEquals(data, output.toString());
} }
@Test
public void next() throws IOException {
StringInputStream inin = new StringInputStream("a#a#");
BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
in.setBoundary("#");
assertEquals('a', in.read());
assertEquals(-1, in.read());
assertEquals(true, in.next());
assertEquals('a', in.read());
assertEquals(-1, in.read());
assertEquals(false, in.next());
}
} }