Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #439

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.koushikdutta.async.DataEmitterReader;
import com.koushikdutta.async.callback.DataCallback;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -351,7 +352,8 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in
frame[2] = (byte) (length / 256);
frame[3] = (byte) (length & BYTE);
} else {

// Original codes:
/*
frame[1] = (byte) (masked | 127);
frame[2] = (byte) (( length / _2_TO_56_) & BYTE);
frame[3] = (byte) (( length / _2_TO_48_) & BYTE);
Expand All @@ -361,27 +363,66 @@ 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 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/4/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));
Expand Down