From 300e546182d52ff580ecb9061f191aa325aa6a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB=E5=BA=B7?= Date: Thu, 14 Apr 2016 18:17:10 +0800 Subject: [PATCH 1/2] Fixed #437 --- .../koushikdutta/async/http/HybiParser.java | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java b/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java index 071fe6e96..374d95de5 100644 --- a/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java +++ b/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java @@ -30,12 +30,20 @@ package com.koushikdutta.async.http; +import android.app.Application; +import android.os.Environment; +import android.widget.Toast; + import com.koushikdutta.async.ByteBufferList; import com.koushikdutta.async.DataEmitter; import com.koushikdutta.async.DataEmitterReader; import com.koushikdutta.async.callback.DataCallback; +import com.tpad.common.utils.TimeUtils; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Arrays; @@ -351,8 +359,9 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in frame[2] = (byte) (length / 256); frame[3] = (byte) (length & BYTE); } else { - - frame[1] = (byte) (masked | 127); + // Original codes: + /* + frame[1] = (byte) (masked | 127); frame[2] = (byte) (( length / _2_TO_56_) & BYTE); frame[3] = (byte) (( length / _2_TO_48_) & BYTE); frame[4] = (byte) (( length / _2_TO_40_) & BYTE); @@ -361,27 +370,65 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in frame[7] = (byte) (( length / _2_TO_16_) & BYTE); frame[8] = (byte) (( length / _2_TO_8_) & BYTE); frame[9] = (byte) (length & BYTE); + */ + + // Fixed #437. I guess there are some code rendering issues that + // probably affect the arraycopy process of the frame byte array. + // So I wrote S32_TO_8BYTES function to bypass it. It doesn't force + // to cast int to byte and assigns byte values that return from the + // function so that no code rendering will occur around this frame. + // Therefore, we will have a correct byte array of frames. + // Rogerus Rex scripsit 14/Apr/2016. + byte[] len=S32_TO_8BYTES(length); + frame[1] = (byte) (masked | 127); + frame[2] = len[0]; + frame[3] = len[1]; + frame[4] = len[2]; + frame[5] = len[3]; + frame[6] = len[4]; + frame[7] = len[5]; + frame[8] = len[6]; + frame[9] = len[7]; + + } if (errorCode > 0) { frame[offset] = (byte) ((errorCode / 256) & BYTE); frame[offset+1] = (byte) (errorCode & BYTE); } - - System.arraycopy(buffer, dataOffset, frame, offset + insert, dataLength - dataOffset); + if (mMasking) { byte[] mask = { (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256) }; + + System.arraycopy(mask, 0, frame, header, mask.length); mask(frame, mask, offset); + + } + return frame; } + private static byte[] S32_TO_8BYTES(int s32){ + byte[] ret=new byte[8]; + ret[0]=0; + ret[1]=0; + ret[2]=0; + ret[3]=0; + ret[4]=Integer.valueOf(((s32&0xFF000000) >>> 24) & 0xFF).byteValue(); + ret[5]=Integer.valueOf(((s32&0x00FF0000) >>> 16) & 0xFF).byteValue(); + ret[6]=Integer.valueOf(((s32&0x0000FF00) >>> 8 ) & 0xFF).byteValue(); + ret[7]=Integer.valueOf( s32&0x000000FF ).byteValue(); + return ret; + } + public void close(int code, String reason) { if (mClosed) return; sendFrame(frame(OP_CLOSE, reason, code)); From 6b2784e82fcb00bf4d6df86769091cec8a1b6c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB=E5=BA=B7?= Date: Thu, 14 Apr 2016 18:26:39 +0800 Subject: [PATCH 2/2] Deleted some useless codes. --- .../koushikdutta/async/http/HybiParser.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java b/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java index 374d95de5..6fcab23c7 100644 --- a/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java +++ b/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java @@ -30,20 +30,13 @@ package com.koushikdutta.async.http; -import android.app.Application; -import android.os.Environment; -import android.widget.Toast; - import com.koushikdutta.async.ByteBufferList; import com.koushikdutta.async.DataEmitter; import com.koushikdutta.async.DataEmitterReader; import com.koushikdutta.async.callback.DataCallback; -import com.tpad.common.utils.TimeUtils; + import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Arrays; @@ -361,7 +354,7 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in } else { // Original codes: /* - frame[1] = (byte) (masked | 127); + frame[1] = (byte) (masked | 127); frame[2] = (byte) (( length / _2_TO_56_) & BYTE); frame[3] = (byte) (( length / _2_TO_48_) & BYTE); frame[4] = (byte) (( length / _2_TO_40_) & BYTE); @@ -375,10 +368,10 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in // Fixed #437. I guess there are some code rendering issues that // probably affect the arraycopy process of the frame byte array. // So I wrote S32_TO_8BYTES function to bypass it. It doesn't force - // to cast int to byte and assigns byte values that return from the - // function so that no code rendering will occur around this frame. + // to cast int typ to byte and assigns byte values return from the + // function so that no code rendering will occur around frame. // Therefore, we will have a correct byte array of frames. - // Rogerus Rex scripsit 14/Apr/2016. + // Rogerus Rex scripsit. 14/4/2016 byte[] len=S32_TO_8BYTES(length); frame[1] = (byte) (masked | 127); frame[2] = len[0]; @@ -398,7 +391,9 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in frame[offset+1] = (byte) (errorCode & BYTE); } - + + System.arraycopy(buffer, dataOffset, frame, offset + insert, dataLength - dataOffset); + if (mMasking) { byte[] mask = { (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256), @@ -408,11 +403,10 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in System.arraycopy(mask, 0, frame, header, mask.length); mask(frame, mask, offset); - } - + return frame; }