Skip to content

Commit

Permalink
Merge pull request dotnet#10510 from anurse/anurse/10490-websocket-se…
Browse files Browse the repository at this point in the history
…nd-payload-when-server

fixes for ManagedWebSocket server mode
  • Loading branch information
mellinoe authored Aug 2, 2016
2 parents 3f64d33 + f520d02 commit 745c2f6
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ private int WriteFrameToSendBuffer(MessageOpcode opcode, bool endOfMessage, Arra

// Write the message header data to the buffer.
int headerLength;
int? maskOffset = null;
if (_isServer)
{
// The server doesn't send a mask, so the mask offset returned by WriteHeader
Expand All @@ -463,15 +464,20 @@ private int WriteFrameToSendBuffer(MessageOpcode opcode, bool endOfMessage, Arra
{
// We need to know where the mask starts so that we can use the mask to manipulate the payload data,
// and we need to know the total length for sending it on the wire.
int maskOffset = WriteHeader(opcode, _sendBuffer, payloadBuffer, endOfMessage, useMask: true);
headerLength = maskOffset + MaskLength;
maskOffset = WriteHeader(opcode, _sendBuffer, payloadBuffer, endOfMessage, useMask: true);
headerLength = maskOffset.GetValueOrDefault() + MaskLength;
}

// Write the payload
if (payloadBuffer.Count > 0)
{
Buffer.BlockCopy(payloadBuffer.Array, payloadBuffer.Offset, _sendBuffer, headerLength, payloadBuffer.Count);

// If there is payload data, XOR it with the mask. We do the manipulation in the send buffer so as to avoid
// If we added a mask to the header, XOR the payload with the mask. We do the manipulation in the send buffer so as to avoid
// changing the data in the caller-supplied payload buffer.
if (payloadBuffer.Count > 0)
if (maskOffset.HasValue)
{
Buffer.BlockCopy(payloadBuffer.Array, payloadBuffer.Offset, _sendBuffer, headerLength, payloadBuffer.Count);
ApplyMask(_sendBuffer, headerLength, _sendBuffer, maskOffset, 0, payloadBuffer.Count);
ApplyMask(_sendBuffer, headerLength, _sendBuffer, maskOffset.Value, 0, payloadBuffer.Count);
}
}

Expand Down Expand Up @@ -914,6 +920,9 @@ private bool TryParseMessageHeaderFromReceiveBuffer(out MessageHeader resultHead
shouldFail = true;
}
header.Mask = CombineMaskBytes(_receiveBuffer, _receiveBufferOffset);

// Consume the mask bytes
ConsumeFromBuffer(4);
}

// Do basic validation of the header
Expand Down

0 comments on commit 745c2f6

Please sign in to comment.