ghedipunk / PHP-Websockets

A Websockets server written in PHP.
BSD 3-Clause "New" or "Revised" License
913 stars 376 forks source link

opcode extracted wrong #2

Closed torleif closed 12 years ago

torleif commented 12 years ago

In extractHeaders() on line 420:

'opcode' => ord($message[0]) & 8 + ord($message[0]) & 4 + ord($message[0]) & 2 + ord($message[0]) & 1,

this should be

'opcode' => ord($message[0]) & 15,

Now opcodes are interpreted by the server correctly. However now disconnect messages are handled incorrectly, when it receives a opcode 8 (disconnect) $willClose gets set to true, and the function quits prematurely (according to the spec, disconnect message MAY can contain a body and therefore should be interpreted by the server. You can do this by changing line 350 in deframe():

            case 8:
                $user->hasSentClose = true;
                break;

and on line 67 in __construct:

                                        if ($message = $this->deframe($buffer, $user)) {
                                            $this->process($user, utf8_encode ($message));
                                            if($user->hasSentClose) {
                                                $this->disconnect($user->socket);
                                            }
                                        }

( there's also another process() function above that needs the if statement). Now the server will correctly handle disconnection events.

The problem is now process() gets called on a disconnect, which is what we want, but the deframe process buffers it with some funny characters. It maybe another bug in the deframe() function, I simply fixed this with if($user->hasSentClose) return;, but again that's a hack.

Also I tested the mbstring fix: it works great :)

ghedipunk commented 12 years ago

Instead of the final if($user->hasSentClose) return;, I set it to return an empty string, and to return as soon as it detects that the client's connection is closing.

Other than that, I'm making the changes.

I'm glad that mbstring works great.

Sorry for the very long delay: I just got a new job and had to take time to ramp up with them.