Skip to content
Permalink
Browse files

Protocol: Relax UTF-8 checking when sending.

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 6744da6188c8ba1c553131582e9624db50424908
Showing with 2 additions and 2 deletions.
  1. +2 −2 Protocol/Rfc6455.php
@@ -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.',

0 comments on commit 6744da6

Please sign in to comment.
You can’t perform that action at this time.