Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Adds the possibility to set the stream context of the connection #103

Closed
wants to merge 4 commits into from
Closed
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
37 changes: 37 additions & 0 deletions src/Engine/AbstractSocketIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ abstract class AbstractSocketIO implements EngineInterface
/** @var resource Resource to the connected stream */
protected $stream;

/** @var mixed[] Array of php stream context wrappers */
protected $context;

public function __construct($url, array $options = [])
{
$this->url = $this->parseUrl($url);
Expand Down Expand Up @@ -191,6 +194,40 @@ protected function parseUrl($url)
return $server;
}

/** Initializes the stream context according to the options **/
protected function initContext() {
$this->context = [];
if (isset($this->options['context'])) {
if (isset($this->options['context']['http'])) {
$this->context['http'] = $this->options['context']['http'];
}
if (isset($this->options['context']['ssl'])) {
$this->context['ssl'] = $this->options['context']['ssl'];
}
}
}

/**
* Searches the origin in the headers. If not found return '*';
*
* @return string the origin
*/
protected function getOrigin() {
$origin = '*';
$headers = '';
if (isset($this->context['http']['header'])) {
$headers = is_array($this->context['http']['header']) ?
join(';', $this->context['http']['header']) :
$this->context['http']['header'];
}

if (preg_match('/Origin:\s*(.*?)(;|$)/', $headers, $matches)) {
$origin = $matches[1];
}

return $origin;
}

/**
* Get the defaults options
*
Expand Down
13 changes: 9 additions & 4 deletions src/Engine/SocketIO/Version0X.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function connect()
return;
}

$this->initContext();
$this->handshake();

$errors = [null, null];
Expand All @@ -65,7 +66,7 @@ public function connect()
$host = 'ssl://' . $host;
}

$this->stream = stream_socket_client($host, $errors[0], $errors[1], $this->options['timeout'], STREAM_CLIENT_CONNECT, stream_context_create($this->options['context']));
$this->stream = stream_socket_client($host, $errors[0], $errors[1], $this->options['timeout'], STREAM_CLIENT_CONNECT, stream_context_create($this->context));

if (!is_resource($this->stream)) {
throw new SocketException($error[0], $error[1]);
Expand All @@ -83,14 +84,15 @@ public function close()
return;
}

$this->write(static::CLOSE);
fclose($this->stream);
$this->stream = null;
}

/** {@inheritDoc} */
public function emit($event, array $args)
{
$this->write(static::EVENT, json_encode(['name' => $event, 'args' => $args]));
$this->write(static::MESSAGE, json_encode($args));
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -143,7 +145,10 @@ protected function handshake()
$url .= '/?' . http_build_query($this->url['query']);
}

$result = @file_get_contents($url, false, stream_context_create(['http' => ['timeout' => (float) $this->options['timeout']]]));
$context = $this->context;
$context['http']['timeout'] = (float) $this->options['timeout'];

$result = @file_get_contents($url, false, stream_context_create($context));

if (false === $result) {
throw new ServerConnectionFailureException;
Expand Down Expand Up @@ -182,7 +187,7 @@ private function upgradeTransport()
. "Connection: Upgrade\r\n"
. "Sec-WebSocket-Key: {$key}\r\n"
. "Sec-WebSocket-Version: 13\r\n"
. "Origin: *\r\n\r\n";
. "Origin: {$this->getOrigin()}\r\n\r\n";

fwrite($this->stream, $request);
$result = fread($this->stream, 12);
Expand Down
11 changes: 6 additions & 5 deletions src/Engine/SocketIO/Version1X.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function connect()
return;
}

$this->initContext();
$this->handshake();

$errors = [null, null];
Expand All @@ -54,7 +55,7 @@ public function connect()
$host = 'ssl://' . $host;
}

$this->stream = stream_socket_client($host, $errors[0], $errors[1], $this->options['timeout'], STREAM_CLIENT_CONNECT, stream_context_create($this->options['context']));
$this->stream = stream_socket_client($host, $errors[0], $errors[1], $this->options['timeout'], STREAM_CLIENT_CONNECT, stream_context_create($this->context));

if (!is_resource($this->stream)) {
throw new SocketException($errors[0], $errors[1]);
Expand Down Expand Up @@ -137,9 +138,9 @@ protected function handshake()
$query = array_replace($query, $this->url['query']);
}

$context = $this->options['context'];
$context['http'] = ['timeout' => (float) $this->options['timeout']];

$context = $this->context;
$context['http']['timeout'] = (float) $this->options['timeout'];
$url = sprintf('%s://%s:%d/%s/?%s', $this->url['scheme'], $this->url['host'], $this->url['port'], trim($this->url['path'], '/'), http_build_query($query));
$result = @file_get_contents($url, false, stream_context_create($context));

Expand Down Expand Up @@ -173,7 +174,7 @@ private function upgradeTransport()
. "Connection: Upgrade\r\n"
. "Sec-WebSocket-Key: {$key}\r\n"
. "Sec-WebSocket-Version: 13\r\n"
. "Origin: *\r\n\r\n";
. "Origin: {$this->getOrigin()}\r\n\r\n";

fwrite($this->stream, $request);
$result = fread($this->stream, 12);
Expand Down