aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoushik Dutta <koushd@gmail.com>2012-12-27 21:04:00 -0800
committerKoushik Dutta <koushd@gmail.com>2012-12-27 21:04:00 -0800
commit19c781224547ac25ccfc300f0710f6ba8fdad14d (patch)
treedf2175b1284c646efd998a5bef2343967cf42280
parent96856a7ea2090bd88b3f5c4541a10207f614eba5 (diff)
downloadAndroidAsync-19c781224547ac25ccfc300f0710f6ba8fdad14d.tar.gz
AndroidAsync-19c781224547ac25ccfc300f0710f6ba8fdad14d.tar.bz2
AndroidAsync-19c781224547ac25ccfc300f0710f6ba8fdad14d.zip
fixup boundary matches between chunks
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/ByteBufferList.java13
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/Util.java2
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/MultipartFormDataBody.java25
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/UrlEncodedFormBody.java2
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/server/BoundaryEmitter.java32
5 files changed, 47 insertions, 27 deletions
diff --git a/AndroidAsync/src/com/koushikdutta/async/ByteBufferList.java b/AndroidAsync/src/com/koushikdutta/async/ByteBufferList.java
index e6e378c..b2b25fc 100644
--- a/AndroidAsync/src/com/koushikdutta/async/ByteBufferList.java
+++ b/AndroidAsync/src/com/koushikdutta/async/ByteBufferList.java
@@ -166,20 +166,11 @@ public class ByteBufferList implements Iterable<ByteBuffer> {
}
public void spewString() {
- for (ByteBuffer bb: mBuffers) {
- try {
- String s = new String(bb.array(), bb.arrayOffset() + bb.position(), bb.limit());
- System.out.println(s);
- }
- catch (Exception e) {
- e.printStackTrace();
-
- }
- }
+ System.out.println(peekString());
}
// not doing toString as this is really nasty in the debugger...
- public String getString() {
+ public String peekString() {
StringBuilder builder = new StringBuilder();
for (ByteBuffer bb: this) {
builder.append(new String(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining()));
diff --git a/AndroidAsync/src/com/koushikdutta/async/Util.java b/AndroidAsync/src/com/koushikdutta/async/Util.java
index 713b0c1..4e14fc7 100644
--- a/AndroidAsync/src/com/koushikdutta/async/Util.java
+++ b/AndroidAsync/src/com/koushikdutta/async/Util.java
@@ -25,7 +25,7 @@ public class Util {
}
}
if (!(list.remaining() == 0 || (socket != null && socket.isPaused()))) {
- System.out.println("Data: " + list.getString());
+ System.out.println("Data: " + list.peekString());
System.out.println("handler: " + handler);
Assert.fail();
}
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/MultipartFormDataBody.java b/AndroidAsync/src/com/koushikdutta/async/http/MultipartFormDataBody.java
index 35d64a0..2f5cbcb 100644
--- a/AndroidAsync/src/com/koushikdutta/async/http/MultipartFormDataBody.java
+++ b/AndroidAsync/src/com/koushikdutta/async/http/MultipartFormDataBody.java
@@ -33,19 +33,26 @@ public class MultipartFormDataBody extends AsyncHttpRequestBodyBase {
headers.addLine(s);
}
else {
- boundaryEmitter.setDataCallback(new DataCallback() {
- @Override
- public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
- System.out.println(bb.getString());
- bb.clear();
- }
- });
+ DataCallback callback = onPart(headers);
+ if (callback == null)
+ callback = new DataCallback() {
+ int total;
+ @Override
+ public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
+ total += bb.remaining();
+ System.out.println(total);
+ System.out.println(bb.peekString());
+ bb.clear();
+ }
+ };
+ boundaryEmitter.setDataCallback(callback);
}
}
});
}
@Override
protected void onBoundaryEnd() {
+ System.out.println("boundary end");
}
};
return;
@@ -57,4 +64,8 @@ public class MultipartFormDataBody extends AsyncHttpRequestBodyBase {
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
boundaryEmitter.onDataAvailable(emitter, bb);
}
+
+ public DataCallback onPart(RawHeaders headers) {
+ return null;
+ }
}
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/UrlEncodedFormBody.java b/AndroidAsync/src/com/koushikdutta/async/http/UrlEncodedFormBody.java
index 6bcda77..a3a3d70 100644
--- a/AndroidAsync/src/com/koushikdutta/async/http/UrlEncodedFormBody.java
+++ b/AndroidAsync/src/com/koushikdutta/async/http/UrlEncodedFormBody.java
@@ -61,7 +61,7 @@ public class UrlEncodedFormBody implements AsyncHttpRequestBody {
public void onCompleted(Exception ex) {
ArrayList<NameValuePair> params;
mParameters = params = new ArrayList<NameValuePair>();
- String[] pairs = data.getString().split("&");
+ String[] pairs = data.peekString().split("&");
for (String p : pairs) {
String[] pair = p.split("=", 2);
if (pair.length == 0)
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/server/BoundaryEmitter.java b/AndroidAsync/src/com/koushikdutta/async/http/server/BoundaryEmitter.java
index 0662db1..60b7104 100644
--- a/AndroidAsync/src/com/koushikdutta/async/http/server/BoundaryEmitter.java
+++ b/AndroidAsync/src/com/koushikdutta/async/http/server/BoundaryEmitter.java
@@ -1,6 +1,7 @@
package com.koushikdutta.async.http.server;
import java.nio.ByteBuffer;
+import java.util.DuplicateFormatFlagsException;
import junit.framework.Assert;
@@ -9,11 +10,9 @@ import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.FilteredDataCallback;
public class BoundaryEmitter extends FilteredDataCallback {
- private int mNearMatch;
private byte[] boundary;
public BoundaryEmitter(String boundary) {
this.boundary = ("--" + boundary).getBytes();
- mNearMatch = this.boundary.length - 2;
}
protected void onBoundaryStart() {
@@ -27,7 +26,7 @@ public class BoundaryEmitter extends FilteredDataCallback {
Assert.assertTrue(count <= a2.length - o2);
for (int i = 0; i < count; i++, o1++, o2++) {
if (a1[o1] != a2[o2]) {
- System.out.println("match fail at " + i);
+// System.out.println("match fail at " + i);
return i;
}
}
@@ -49,7 +48,19 @@ public class BoundaryEmitter extends FilteredDataCallback {
int state = 0;
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
- System.out.println(bb.getString());
+// System.out.println(bb.getString());
+ System.out.println("chunk: " + bb.remaining());
+
+ System.out.println("state: " + state);
+
+ // if we were in the middle of a potential match, let's throw that
+ // at the beginning of the buffer and process it too.
+ if (state > 0) {
+ ByteBuffer b = ByteBuffer.wrap(boundary, 0, state).duplicate();
+ bb.add(0, b);
+ state = 0;
+ }
+
int last = 0;
byte[] buf = new byte[bb.remaining()];
bb.get(buf);
@@ -60,7 +71,10 @@ public class BoundaryEmitter extends FilteredDataCallback {
if (state == boundary.length)
state = -1;
}
- else {
+ else if (state > 0) {
+ // let's try matching again one byte after the start
+ // of last match occurrence
+ i -= state;
state = 0;
}
}
@@ -78,6 +92,7 @@ public class BoundaryEmitter extends FilteredDataCallback {
// len can be -1 on the first boundary
Assert.assertEquals(-2, len);
}
+ System.out.println("bstart");
onBoundaryStart();
}
else if (buf[i] == '-') {
@@ -104,6 +119,7 @@ public class BoundaryEmitter extends FilteredDataCallback {
ByteBufferList list = new ByteBufferList();
list.add(b);
super.onDataAvailable(emitter, list);
+ System.out.println("bend");
onBoundaryEnd();
}
else {
@@ -136,9 +152,11 @@ public class BoundaryEmitter extends FilteredDataCallback {
}
}
- // TODO: this is derped if it is in the middle of a match
if (last < buf.length) {
- ByteBuffer b = ByteBuffer.wrap(buf, last, buf.length - last);
+ System.out.println("amount left at boundary: " + (buf.length - last));
+ System.out.println(state);
+ int keep = Math.max(state, 0);
+ ByteBuffer b = ByteBuffer.wrap(buf, last, buf.length - last - keep);
ByteBufferList list = new ByteBufferList();
list.add(b);
super.onDataAvailable(emitter, list);