Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
Protocol: Relax UTF-8 checking when sending.
Browse files Browse the repository at this point in the history
All messages sent with `Protocol\Rfc6455::send` must be UTF-8 encoded.
This is a hard constraint. All messages received by the server must be
UTF-8 encoded, so we ease the work of the server and avoid sending
invalid messages by adding this constraint.

This constraint was checked when the opcode was either
`OPCODE_TEXT_FRAME` or `OPCODE_CONTINUATION_FRAME`. This is wrong
because in some cases a valid UTF-8 message can be send byte after byte
as fragments. Each fragment is not necessarily a valid UTF-8 string.

So now, this constraint is checked only when the opcode is
`OPCODE_TEXT_FRAME` and when the message is not fragmented (so `end` is
set to `true`). And so, this is possible to send a valid UTF-8 message
as many invalid UTF-8 fragments.
  • Loading branch information
Hywan committed Jul 5, 2016
1 parent 2ecdda7 commit 6744da6
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Protocol/Rfc6455.php
Expand Up @@ -317,8 +317,8 @@ public function send(
$end = true,
$mask = false
) {
if ((Websocket\Connection::OPCODE_TEXT_FRAME === $opcode ||
Websocket\Connection::OPCODE_CONTINUATION_FRAME === $opcode) &&
if (Websocket\Connection::OPCODE_TEXT_FRAME === $opcode &&
true === $end &&
false === (bool) preg_match('//u', $message)) {
throw new Websocket\Exception\InvalidMessage(
'Message “%s” is not in UTF-8, cannot send it.',
Expand Down

0 comments on commit 6744da6

Please sign in to comment.