This commit is contained in:
Ziver Koc 2016-07-13 21:29:38 +02:00
parent 5606f57514
commit dc91d5e992
4 changed files with 44 additions and 28 deletions

View file

@ -162,7 +162,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
searchNextBoundary(); searchNextBoundary();
} }
else { // read data until we find the next boundary or get to the end of the stream else { // read data until we find the next boundary or get to the end of the stream
while (buf_bound_pos >= 0 || fillBuffer() >= 0) while (buf_bound_pos < 0 && fillBuffer() >= 0)
buf_pos = buf_end; buf_pos = buf_end;
} }
} }
@ -207,7 +207,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
* over) from this buffered input stream without blocking. * over) from this buffered input stream without blocking.
* @exception IOException if an I/O error occurs. * @exception IOException if an I/O error occurs.
*/ */
public int available() throws IOException { public int available() {
return buf_end - buf_pos; return buf_end - buf_pos;
} }
@ -259,6 +259,8 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
* Searches for the nearest boundary from the current buffer position * Searches for the nearest boundary from the current buffer position
*/ */
private void searchNextBoundary(){ private void searchNextBoundary(){
// No need to check for boundary if buffer is smaller than the boundary length
if (this.available() >= boundary.length) {
for (int i = buf_pos; i < buf_end; i++) { for (int i = buf_pos; i < buf_end; i++) {
for (int b = 0; b < boundary.length; b++) { for (int b = 0; b < boundary.length; b++) {
if (buffer[i + b] != boundary[b]) if (buffer[i + b] != boundary[b])
@ -269,6 +271,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
} }
} }
} }
}
buf_bound_pos = -1; buf_bound_pos = -1;
} }
} }

View file

@ -83,6 +83,7 @@ public class IOUtil {
while((line = in.readLine()) != null){ while((line = in.readLine()) != null){
str.append(line).append("\n"); str.append(line).append("\n");
} }
str.delete(str.length()-1, str.length()); // remove last new line
in.close(); in.close();
return str.toString(); return str.toString();

View file

@ -117,11 +117,17 @@ public class BufferedBoundaryInputStreamTest {
} }
@Test @Test
public void readArr_multiCharBoundary() throws IOException { public void readArr_multiCharBoundary() throws IOException {
BufferedBoundaryInputStream in = getBufferedBoundaryInputStream("aaa1234", "1234"); BufferedBoundaryInputStream in = getBufferedBoundaryInputStream(
"------------------------------83ff53821b7chello------------------------------83ff53821b7c--",
"------------------------------83ff53821b7c");
assertEquals('a', in.read()); assertEquals(-1, in.read());
assertEquals('a', in.read()); assertTrue(in.hasNext()); in.next();
assertEquals('a', in.read()); assertEquals("hello", IOUtil.readContentAsString(in));
assertEquals(-1, in.read());
assertTrue(in.hasNext()); in.next();
assertEquals('-', in.read());
assertEquals('-', in.read());
assertEquals(-1, in.read()); assertEquals(-1, in.read());
assertFalse(in.hasNext()); assertFalse(in.hasNext());
@ -194,12 +200,14 @@ public class BufferedBoundaryInputStreamTest {
@Test @Test
public void read_largeData() throws IOException { public void read_largeData() throws IOException {
String data = "aaaaaaaaaaaa#aa#aaaaaaaaaaaaaaa#"; String data = "#aaaaaaaaaaaa#aa#aaaaaaaaaaaaaaa#";
StringInputStream inin = new StringInputStream(data); StringInputStream inin = new StringInputStream(data);
BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin, 10); BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin, 10);
in.setBoundary("#"); in.setBoundary("#");
assertEquals(-1, in.read());
assertTrue(in.hasNext()); assertTrue(in.hasNext());
in.next();
for (int i=0; i<12; ++i) for (int i=0; i<12; ++i)
assertEquals('a', in.read()); assertEquals('a', in.read());
assertEquals(-1, in.read()); assertEquals(-1, in.read());

View file

@ -1,8 +1,10 @@
package zutil.net.http.multipart; package zutil.net.http.multipart;
import org.junit.Test; import org.junit.Test;
import zutil.io.IOUtil;
import zutil.io.StringInputStream; import zutil.io.StringInputStream;
import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertFalse;
@ -43,24 +45,25 @@ public class MultipartParserTest {
} }
@Test @Test
public void singleFileUpload() { public void singleFileUpload() throws IOException {
String input = String input_start =
"------------------------------83ff53821b7c\n" + "------------------------------83ff53821b7c\n" +
"Content-Disposition: form-data; name=\"img\"; filename=\"a.png\"\n" + "Content-Disposition: form-data; name=\"img\"; filename=\"a.png\"\n" +
"Content-Type: application/octet-stream\n" + "Content-Type: application/octet-stream\n" +
"\n" + "\n";
String input_data =
"?PNG\n" + "?PNG\n" +
"\n" + "\n" +
"IHD?wS??iCCPICC Profilex?T?kA?6n??Zk?x?\"IY?hE?6?bk\n" + "lkuytrewacvbnmloiuytrewrtyuiol,mnbvdc xswertyuioplm cdsertyuioölkjgf\n" +
"Y?<ߡ)??????9Nyx?+=?Y\"|@5-?\u007FM?S?%?@?H8??qR>?\u05CB??inf???O?????b??N?????~N??>?!?\n" + "kgkfdgfhgfhgkhgvytvjgxslkyöiyfödgjdjhföjhdlgdgjfhföjhföiyföudlögföudöfö\n" +
"??V?J?p?8?da?sZHO?Ln?}&???wVQ?y?g????E??0\n" + "ögdljgdjhöfjhfdfsgdfg ryrt dtrd ytfc uhöiugljfkdkhdjgd\n" +
" ??\n" + " xx\n" +
" IDAc????????-IEND?B`?\n" + " kuykutfytdh ytrd trutrd trxxx";
"------------------------------83ff53821b7c--"; String input_end = "\n------------------------------83ff53821b7c--";
MultipartParser parser = new MultipartParser( MultipartParser parser = new MultipartParser(
new StringInputStream(input), new StringInputStream(input_start+input_data+input_end),
"----------------------------83ff53821b7c", "----------------------------83ff53821b7c",
input.length()); 0);
// Assertions // Assertions
Iterator<MultipartField> it = parser.iterator(); Iterator<MultipartField> it = parser.iterator();
@ -71,6 +74,7 @@ public class MultipartParserTest {
assertEquals("img", fileField.getName()); assertEquals("img", fileField.getName());
assertEquals("a.png", fileField.getFilename()); assertEquals("a.png", fileField.getFilename());
assertEquals("application/octet-stream", fileField.getContentType()); assertEquals("application/octet-stream", fileField.getContentType());
assertEquals(input_data, new String(fileField.getContent()));
//assertFalse(it.hasNext()); //TODO: does not work, how to solve this? //assertFalse(it.hasNext()); //TODO: does not work, how to solve this?
} }