Skip to content

Commit

Permalink
Don't error if zlib is missing and compression is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jul 4, 2020
1 parent 50881ac commit e6e2a47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
90 changes: 44 additions & 46 deletions src/Handshake.php
Expand Up @@ -32,7 +32,6 @@ final class Handshake extends Message
* @param string[]|string[][] $headers
*
* @throws \TypeError If $uri is not a string or an instance of PsrUri.
* @throws \Error If compression is enabled in the options but the zlib extension is not installed.
*/
public function __construct($uri, ?Options $options = null, array $headers = [])
{
Expand Down Expand Up @@ -62,38 +61,6 @@ public function withUri($uri): self
return $clone;
}

/**
* @param string|PsrUri $uri
*
* @return PsrUri
*/
private function makeUri($uri): PsrUri
{
if (\is_string($uri)) {
try {
$uri = Uri\Http::createFromString($uri);
} catch (\Exception $exception) {
throw new \Error('Invalid Websocket URI provided', 0, $exception);
}
}

/** @psalm-suppress DocblockTypeContradiction */
if (!$uri instanceof PsrUri) {
throw new \TypeError(\sprintf('Must provide an instance of %s or a websocket URI as a string', PsrUri::class));
}

switch ($uri->getScheme()) {
case 'ws':
case 'wss':
break;

default:
throw new \Error('The URI scheme must be ws or wss: \'' . $uri->getScheme() . '\'');
}

return $uri;
}

/**
* @return Options
*/
Expand All @@ -115,19 +82,6 @@ public function withOptions(Options $options): self
return $clone;
}

private function checkOptions(?Options $options): Options
{
if ($options === null) {
return Options::createClientDefault();
}

if ($options->isCompressionEnabled() && !\extension_loaded('zlib')) {
throw new \Error('Compression is enabled in options, but the zlib extension is not loaded');
}

return $options;
}

/**
* @return int Timeout in milliseconds for the TCP connection.
*/
Expand Down Expand Up @@ -257,4 +211,48 @@ protected function addHeader(string $name, $value): void

parent::addHeader($name, $value);
}

/**
* @param string|PsrUri $uri
*
* @return PsrUri
*/
private function makeUri($uri): PsrUri
{
if (\is_string($uri)) {
try {
$uri = Uri\Http::createFromString($uri);
} catch (\Exception $exception) {
throw new \Error('Invalid Websocket URI provided', 0, $exception);
}
}

/** @psalm-suppress DocblockTypeContradiction */
if (!$uri instanceof PsrUri) {
throw new \TypeError(\sprintf(
'Must provide an instance of %s or a websocket URI as a string',
PsrUri::class
));
}

switch ($uri->getScheme()) {
case 'ws':
case 'wss':
break;

default:
throw new \Error('The URI scheme must be ws or wss: \'' . $uri->getScheme() . '\'');
}

return $uri;
}

private function checkOptions(?Options $options): Options
{
if ($options === null) {
return Options::createClientDefault();
}

return $options;
}
}
2 changes: 1 addition & 1 deletion src/Rfc6455Connector.php
Expand Up @@ -121,7 +121,7 @@ private function generateRequest(Handshake $handshake, string $key): Request

$extensions = self::splitField($request, 'sec-websocket-extensions');

if ($handshake->getOptions()->isCompressionEnabled()) {
if ($handshake->getOptions()->isCompressionEnabled() && \extension_loaded('zlib')) {
$extensions[] = $this->compressionFactory->createRequestHeader();
}

Expand Down

0 comments on commit e6e2a47

Please sign in to comment.