From b9249d422d012066367251c882b6131d597d83d9 Mon Sep 17 00:00:00 2001 From: Rodrigo Aguilera Date: Thu, 18 Jun 2020 15:12:46 +0200 Subject: [PATCH 1/3] Use guzzle for requests. --- composer.json | 1 + src/Engine/SocketIO/Version1X.php | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index f37dc89e..44687c32 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "require": { "php": ">=7.1.0", + "guzzlehttp/guzzle": "^6", "psr/log": "~1.0" }, diff --git a/src/Engine/SocketIO/Version1X.php b/src/Engine/SocketIO/Version1X.php index ea2d7ef5..4005f9b1 100644 --- a/src/Engine/SocketIO/Version1X.php +++ b/src/Engine/SocketIO/Version1X.php @@ -14,6 +14,9 @@ use InvalidArgumentException; use UnexpectedValueException; +use Psr\Log\LoggerInterface; +use GuzzleHttp\Client; + use ElephantIO\EngineInterface; use ElephantIO\Payload\Encoder; use ElephantIO\Engine\AbstractSocketIO; @@ -179,12 +182,19 @@ protected function handshake() } // add customer headers + $request_headers = []; if (isset($this->options['headers'])) { - $headers = isset($context['http']['header']) ? $context['http']['header'] : []; - $context['http']['header'] = array_merge($headers, $this->options['headers']); + $headers = isset($context[$protocol]['header']) ? $context[$protocol]['header'] : []; + $context[$protocol]['header'] = \array_merge($headers, $this->options['headers']); + } + + foreach ($context[$protocol]['header'] as $item) { + if (\preg_match('/^([^:]*):\s*([^;]*)/i', $item, $matches)) { + $request_headers[$matches[1]] = $matches[2]; + } } - $url = \sprintf( + $url = \sprintf( '%s://%s:%d/%s/?%s', $this->url['scheme'], $this->url['host'], @@ -193,7 +203,10 @@ protected function handshake() \http_build_query($query) ); - $result = @\file_get_contents($url, false, \stream_context_create($context)); + $client = new Client([]); + + $response = $client->request('GET', $url, ['headers' => $request_headers]); + $result = $response->getBody()->getContents(); if (false === $result) { $message = null; @@ -214,13 +227,7 @@ protected function handshake() throw new UnsupportedTransportException('websocket'); } - $cookies = []; - foreach ($http_response_header as $header) { - if (\preg_match('/^Set-Cookie:\s*([^;]*)/i', $header, $matches)) { - $cookies[] = $matches[1]; - } - } - $this->cookies = $cookies; + $this->cookies = $response->getHeader('Set-Cookie'); $this->session = new Session( $decoded['sid'], From 99380024ab974e619f47f75829a5858833ff89f7 Mon Sep 17 00:00:00 2001 From: Rodrigo Aguilera Date: Thu, 18 Jun 2020 15:19:46 +0200 Subject: [PATCH 2/3] Remove unused interface. --- src/Engine/SocketIO/Version1X.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Engine/SocketIO/Version1X.php b/src/Engine/SocketIO/Version1X.php index 4005f9b1..48100f9d 100644 --- a/src/Engine/SocketIO/Version1X.php +++ b/src/Engine/SocketIO/Version1X.php @@ -13,8 +13,6 @@ use InvalidArgumentException; use UnexpectedValueException; - -use Psr\Log\LoggerInterface; use GuzzleHttp\Client; use ElephantIO\EngineInterface; From 8c7113f99e255ae33a894df4d376174adfe77e33 Mon Sep 17 00:00:00 2001 From: Rodrigo Aguilera Date: Thu, 18 Jun 2020 15:52:05 +0200 Subject: [PATCH 3/3] Fix warning when there is no optional headers. --- src/Engine/SocketIO/Version1X.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Engine/SocketIO/Version1X.php b/src/Engine/SocketIO/Version1X.php index 48100f9d..1a87182f 100644 --- a/src/Engine/SocketIO/Version1X.php +++ b/src/Engine/SocketIO/Version1X.php @@ -181,9 +181,9 @@ protected function handshake() // add customer headers $request_headers = []; + $context[$protocol]['header'] = isset($context[$protocol]['header']) ? $context[$protocol]['header'] : []; if (isset($this->options['headers'])) { - $headers = isset($context[$protocol]['header']) ? $context[$protocol]['header'] : []; - $context[$protocol]['header'] = \array_merge($headers, $this->options['headers']); + $context[$protocol]['header'] = \array_merge($context[$protocol]['header'], $this->options['headers']); } foreach ($context[$protocol]['header'] as $item) {