Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/main/php/websocket/protocol/Connection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Connection {
const MAXLENGTH= 0x8000000;

private $socket, $id, $listener, $path, $headers;
private $socket, $id, $listener, $path, $params, $headers;

/**
* Creates a new connection
Expand All @@ -27,7 +27,16 @@ public function __construct($socket, $id, $listener, $path= '/', $headers= []) {
$this->socket= $socket;
$this->id= $id;
$this->listener= $listener;
$this->path= $path;

$p= strpos($path, '?');
if (false === $p) {
$this->path= $path;
$this->params= [];
} else {
$this->path= substr($path, 0, $p);
parse_str(substr($path, $p + 1), $this->params);
}

$this->headers= $headers;
}

Expand All @@ -40,9 +49,23 @@ public function listener() { return $this->listener; }
/** @return string */
public function path() { return $this->path; }

/** @return [:var] */
public function params() { return $this->params; }

/** @return [:var] */
public function headers() { return $this->headers; }

/**
* Returns a named parameter, or NULL if it does not exist.
*
* @param string $name
* @param var $default
* @return var
*/
public function param($name, $default= null) {
return $this->params[$name] ?? $default;
}

/**
* Opens connection
*
Expand Down
31 changes: 30 additions & 1 deletion src/test/php/websocket/unittest/ConnectionTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private function listener($callable= null) {
* @return []
*/
private function receive($channel) {
$conn= new Connection($channel->connect(), self::ID, $this->listener(), []);
$conn= new Connection($channel->connect(), self::ID, $this->listener());
$r= [];
foreach ($conn->receive() as $type => $message) {
$r[]= [$type => $message];
Expand Down Expand Up @@ -54,6 +54,35 @@ public function path($value) {
Assert::equals($value, (new Connection(new Channel(), self::ID, $this->listener(), $value))->path());
}

#[Test]
public function path_does_not_contain_params() {
Assert::equals('/', (new Connection(new Channel(), self::ID, $this->listener(), '/?for=test'))->path());
}

#[Test, Values(['/?for=test', '/ws?for=test', '/feed/6100?for=test'])]
public function params($value) {
$conn= new Connection(new Channel(), self::ID, $this->listener(), $value);

Assert::equals('test', $conn->param('for'));
Assert::equals(['for' => 'test'], $conn->params());
}

#[Test]
public function non_existant_param() {
$conn= new Connection(new Channel(), self::ID, $this->listener(), '?for=test');

Assert::null($conn->param('non-existant'));
Assert::equals('default', $conn->param('non-existant', 'default'));
}

#[Test]
public function array_param() {
$conn= new Connection(new Channel(), self::ID, $this->listener(), '?for[]=a&for[]=b');

Assert::equals(['a', 'b'], $conn->param('for'));
Assert::equals(['for' => ['a', 'b']], $conn->params());
}

#[Test, Values([[[]], [['User-Agent' => 'Test', 'Accept' => '*/*']]])]
public function headers($value) {
Assert::equals($value, (new Connection(new Channel(), self::ID, $this->listener(), '/', $value))->headers());
Expand Down
2 changes: 1 addition & 1 deletion src/test/php/websocket/unittest/MessagesTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private function fixture($channel, $listener= null) {

// Simulate handshake
$channel->connect();
$listeners->connections[self::ID]= new Connection($channel, self::ID, $listeners->listener('/ws'), []);
$listeners->connections[self::ID]= new Connection($channel, self::ID, $listeners->listener('/ws'));

return new Messages($listeners, $this->log);
}
Expand Down
Loading