Fixing Parsing of masking bytes on slow testcase (would read in reverse order under certain conditions)

This commit is contained in:
Joakim Erdfelt 2012-08-02 10:31:37 -07:00
parent e2dd62a3af
commit 01c40b5290
4 changed files with 23 additions and 11 deletions

View File

@ -156,7 +156,7 @@ public class BufferUtil
/* ------------------------------------------------------------ */
/** Flip the buffer to Flush mode.
* The limit is set to the first unused byte(the old position) amd
* The limit is set to the first unused byte(the old position) and
* the position is set to the passed position.
* <p>
* This method is used as a replacement of {@link Buffer#flip()}.

View File

@ -392,8 +392,8 @@ public class Parser
case MASK_BYTES:
{
byte b = buffer.get();
frame.getMask()[4 - cursor] = b;
--cursor;
frame.getMask()[cursor] = b;
if (cursor == 0)
{
// special case for empty payloads (no more bytes left in buffer)
@ -449,6 +449,7 @@ public class Parser
getPolicy().assertValidPayloadLength(payloadLength);
frame.assertValid();
payload = ByteBuffer.allocate(payloadLength);
BufferUtil.clearToFill(payload);
}
BufferUtil.put(buffer,payload);
@ -457,14 +458,18 @@ public class Parser
{
BufferUtil.flipToFlush(payload,0);
LOG.debug("PreMask: {}",BufferUtil.toDetailString(payload));
// demask (if needed)
if (frame.isMasked())
{
byte mask[] = frame.getMask();
int offset;
int start = payload.position();
int end = payload.limit();
for (int i = payload.position(); i < end; i++)
for (int i = start; i < end; i++)
{
payload.put(i,(byte)(payload.get(i) ^ mask[i % 4]));
offset = (i - start);
payload.put(i,(byte)(payload.get(i) ^ mask[offset % 4]));
}
}

View File

@ -17,6 +17,14 @@ import org.junit.Test;
public class GeneratorTest
{
private void parsePartial(Parser parser, ByteBuffer buf, int numBytes)
{
int len = Math.min(numBytes,buf.remaining());
byte arr[] = new byte[len];
buf.get(arr,0,len);
parser.parse(ByteBuffer.wrap(arr));
}
/**
* Prevent regression of masking of many packets.
*/
@ -25,7 +33,7 @@ public class GeneratorTest
{
byte[] MASK =
{ 0x11, 0x22, 0x33, 0x44 };
int pingCount = 1000;
int pingCount = 10;
// the generator
Generator generator = new UnitGenerator();
@ -67,13 +75,11 @@ public class GeneratorTest
int segmentSize = 5;
while (completeBuf.remaining() > 0)
{
ByteBuffer part = completeBuf.slice();
int len = Math.min(segmentSize,part.remaining());
part.limit(part.position() + len);
parser.parse(part);
completeBuf.position(completeBuf.position() + len);
parsePartial(parser,completeBuf,segmentSize);
}
capture.dump();
// Assert validity of frame
int frameCount = send.size();
capture.assertFrameCount(frameCount);

View File

@ -1,2 +1,3 @@
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
org.eclipse.jetty.websocket.LEVEL=WARN
org.eclipse.jetty.websocket.LEVEL=WARN
# org.eclipse.jetty.websocket.protocol.Parser.LEVEL=DEBUG