1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-17 08:42:25 +03:00

Merge pull request #1465 from square/jw/unsigned

Payload length continuations are unsigned values.
This commit is contained in:
Jake Wharton
2015-03-06 03:07:46 -05:00
5 changed files with 37 additions and 6 deletions

View File

@@ -123,6 +123,28 @@ public class WebSocketReaderTest {
callback.assertTextMessage("Hello");
}
@Test public void clientFramePayloadShort() throws IOException {
data.write(ByteString.decodeHex("817E000548656c6c6f")); // Hello
clientReader.processNextFrame();
callback.assertTextMessage("Hello");
}
@Test public void clientFramePayloadLong() throws IOException {
data.write(ByteString.decodeHex("817f000000000000000548656c6c6f")); // Hello
clientReader.processNextFrame();
callback.assertTextMessage("Hello");
}
@Test public void clientFramePayloadTooLongThrows() throws IOException {
data.write(ByteString.decodeHex("817f8000000000000000"));
try {
clientReader.processNextFrame();
fail();
} catch (ProtocolException e) {
assertEquals("Frame length 0x8000000000000000 > 0x7FFFFFFFFFFFFFFF", e.getMessage());
}
}
@Test public void serverHelloTwoChunks() throws IOException {
data.write(ByteString.decodeHex("818537fa213d7f9f4d")); // Hel

View File

@@ -100,9 +100,9 @@ public class WebSocketWriterTest {
}
@Test public void serverSendBinaryShort() throws IOException {
byte[] payload = binaryData(1000);
byte[] payload = binaryData(0xffff);
serverWriter.sendMessage(BINARY, new Buffer().write(payload));
assertData("827e03e8");
assertData("827effff");
assertData(payload);
}

View File

@@ -72,9 +72,14 @@ public final class WebSocketProtocol {
* {@link #PAYLOAD_SHORT} or {@link #PAYLOAD_LONG}.
*/
static final int PAYLOAD_MAX = 125;
/** Value for {@link #B1_MASK_LENGTH} which indicates the next two bytes are the length. */
/**
* Value for {@link #B1_MASK_LENGTH} which indicates the next two bytes are the unsigned length.
*/
static final int PAYLOAD_SHORT = 126;
/** Value for {@link #B1_MASK_LENGTH} which indicates the next eight bytes are the length. */
/**
* Value for {@link #B1_MASK_LENGTH} which indicates the next eight bytes are the unsigned
* length.
*/
static final int PAYLOAD_LONG = 127;
static void toggleMask(byte[] buffer, long byteCount, byte[] key, long frameBytesRead) {

View File

@@ -135,9 +135,13 @@ public final class WebSocketReader {
// Get frame length, optionally reading from follow-up bytes if indicated by special values.
frameLength = b1 & B1_MASK_LENGTH;
if (frameLength == PAYLOAD_SHORT) {
frameLength = source.readShort();
frameLength = source.readShort() & 0xffffL; // Value is unsigned.
} else if (frameLength == PAYLOAD_LONG) {
frameLength = source.readLong();
if (frameLength < 0) {
throw new ProtocolException(
"Frame length 0x" + Long.toHexString(frameLength) + " > 0x7FFFFFFFFFFFFFFF");
}
}
frameBytesRead = 0;

View File

@@ -215,7 +215,7 @@ public final class WebSocketWriter {
if (byteCount <= PAYLOAD_MAX) {
b1 |= (int) byteCount;
sink.writeByte(b1);
} else if (byteCount <= Short.MAX_VALUE) {
} else if (byteCount <= 0xffffL) { // Unsigned short.
b1 |= PAYLOAD_SHORT;
sink.writeByte(b1);
sink.writeShort((int) byteCount);