Skip to content

Commit

Permalink
SubprotocolHandler prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Jan 19, 2024
1 parent b59cf5d commit a6718bc
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/echoserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$server
->addMiddleware(new \WebSocket\Middleware\CloseHandler())
->addMiddleware(new \WebSocket\Middleware\PingResponder())
->addMiddleware(new \WebSocket\Middleware\SubprotocolHandler(['soap', 'towel']))
;

// If debug mode and logger is available
Expand Down
1 change: 1 addition & 0 deletions examples/send.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
$client
->addMiddleware(new \WebSocket\Middleware\CloseHandler())
->addMiddleware(new \WebSocket\Middleware\PingResponder())
->addMiddleware(new \WebSocket\Middleware\SubprotocolHandler(['towel', 'soap']))
->onText(function ($client, $connection, $message) {
echo "> Received '{$message->getContent()}' [opcode: {$message->getOpcode()}]\n";
echo "< Closing client\n";
Expand Down
4 changes: 3 additions & 1 deletion src/Http/HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function pull(): MessageInterface
$buffer = $this->stream->readLine(1024);
$data .= $buffer;
} while (substr_count($data, "\r\n\r\n") == 0);
echo "---------- PULL: $data \n";

list ($head, $body) = explode("\r\n\r\n", $data);
$headers = array_filter(explode("\r\n", $head));
Expand Down Expand Up @@ -85,7 +86,7 @@ public function pull(): MessageInterface
foreach ($headers as $header) {
$parts = explode(':', $header, 2);
if (count($parts) == 2) {
$message = $message->withHeader($parts[0], $parts[1]);
$message = $message->withAddedHeader($parts[0], $parts[1]);
}
}
if ($message instanceof Request) {
Expand All @@ -99,6 +100,7 @@ public function pull(): MessageInterface
public function push(MessageInterface $message): MessageInterface
{
$data = implode("\r\n", $message->getAsArray()) . "\r\n\r\n";
echo "---------- PUSH: $data \n";
$this->stream->write($data);
return $message;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
};
use Stringable;
use WebSocket\Connection;
use WebSocket\Http\{
HttpHandler,
Message as HttpMessage
};
use WebSocket\Http\Message as HttpMessage;
use WebSocket\Message\Message;
use WebSocket\Trait\StringableTrait;

Expand Down
71 changes: 71 additions & 0 deletions src/Middleware/SubprotocolHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* Copyright (C) 2014-2023 Textalk and contributors.
*
* This file is part of Websocket PHP and is free software under the ISC License.
* License text: https://raw.githubusercontent.com/sirn-se/websocket-php/master/COPYING.md
*/

namespace WebSocket\Middleware;

use Psr\Log\{
LoggerAwareInterface,
LoggerAwareTrait
};
use WebSocket\Connection;
use WebSocket\Http\Message;
use WebSocket\Trait\StringableTrait;

/**
* WebSocket\Middleware\CloseHandler class.
* Handles close procedure.
*/
class SubprotocolHandler implements LoggerAwareInterface, ProcessHttpOutgoingInterface, ProcessHttpIncomingInterface
{
use LoggerAwareTrait;
use StringableTrait;

private $subprotocols;
private $selected = null;

public function __construct(array $subprotocols)
{
$this->subprotocols = $subprotocols;
}

public function processHttpOutgoing(ProcessHttpStack $stack, Connection $connection, Message $message): Message
{
if ($message instanceof \WebSocket\Http\Request) {
// Outgoing requests on Client
foreach ($this->subprotocols as $subprotocol) {
$message = $message->withAddedHeader('Sec-WebSocket-Protocol', $subprotocol);
}
} elseif ($message instanceof \WebSocket\Http\Response) {
// Outgoing Response
if ($this->selected) {
$message = $message->withHeader('Sec-WebSocket-Protocol', $this->selected);
}
}

return $stack->handleHttpOutgoing($message);
}

public function processHttpIncoming(ProcessHttpStack $stack, Connection $connection): Message
{
$this->selected = null;
$message = $stack->handleHttpIncoming();

if ($message instanceof \WebSocket\Http\ServerRequest) {
// Incoming requests on Server
foreach ($message->getHeader('Sec-WebSocket-Protocol') as $subprotocol) {
if (in_array($subprotocol, $this->subprotocols)) {
$this->selected = $subprotocol;
return $message;
}
}
}

return $message;
}
}

0 comments on commit a6718bc

Please sign in to comment.