From 0e146dec1c507dd3b93c69c74b6be474712e8335 Mon Sep 17 00:00:00 2001 From: gaobinzhan Date: Wed, 14 Oct 2020 10:18:43 +0800 Subject: [PATCH 1/4] fix: upgrade --- src/Handler/Swoole/Client.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Handler/Swoole/Client.php b/src/Handler/Swoole/Client.php index 303ba59..2abb877 100644 --- a/src/Handler/Swoole/Client.php +++ b/src/Handler/Swoole/Client.php @@ -49,8 +49,12 @@ public function closeClient(): bool public function upgrade(bool $mask = true): bool { - $this->getRequest()->setClientSetting('websocket_mask', $mask); + $request = $this->getRequest(); + $request->setClientSetting('websocket_mask', $mask); + $client = $this->getClient(); + $client->setCookies($request->getCookies()); + $client->setHeaders($request->getHeader()); return $client->upgrade($this->url->getFullPath()); } From 256932ff6a3ed4a3f713dbfab0c013a8dd6c0fba Mon Sep 17 00:00:00 2001 From: gaobinzhan Date: Fri, 16 Oct 2020 10:51:58 +0800 Subject: [PATCH 2/4] fix --- src/Handler/Swoole/Client.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Handler/Swoole/Client.php b/src/Handler/Swoole/Client.php index 2abb877..583d1a7 100644 --- a/src/Handler/Swoole/Client.php +++ b/src/Handler/Swoole/Client.php @@ -42,7 +42,9 @@ public function getClient(): SwooleHttpClient public function closeClient(): bool { if ($this->client instanceof SwooleHttpClient) { - return $this->client->close(); + $result = $this->client->close(); + $this->client = null; + return $result; } return false; } @@ -128,7 +130,7 @@ public function rawRequest($httpMethod = HttpClient::METHOD_GET, $rawData = null unset($rawData[$key]); } if ($item instanceof CURLFile) { - $client->addFile($item->getPath(), $item->getName(), $item->getType(), $item->getFilename(),$item->getOffset(),$item->getLength()); + $client->addFile($item->getPath(), $item->getName(), $item->getType(), $item->getFilename(), $item->getOffset(), $item->getLength()); unset($rawData[$key]); } } From c56507f57ab84280efe12119bbcd6be102d102af Mon Sep 17 00:00:00 2001 From: gaobinzhan Date: Thu, 5 Nov 2020 11:34:25 +0800 Subject: [PATCH 3/4] fix: cookie overlay header problem --- src/Handler/Swoole/Client.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Handler/Swoole/Client.php b/src/Handler/Swoole/Client.php index 583d1a7..433eb2d 100644 --- a/src/Handler/Swoole/Client.php +++ b/src/Handler/Swoole/Client.php @@ -55,8 +55,8 @@ public function upgrade(bool $mask = true): bool $request->setClientSetting('websocket_mask', $mask); $client = $this->getClient(); - $client->setCookies($request->getCookies()); - $client->setHeaders($request->getHeader()); + $request->getCookies() && $client->setCookies($request->getCookies()); + $request->getHeader() && $client->setHeaders($request->getHeader()); return $client->upgrade($this->url->getFullPath()); } @@ -121,7 +121,13 @@ public function rawRequest($httpMethod = HttpClient::METHOD_GET, $rawData = null //预处理。合并cookie 和header $request->setMethod($httpMethod); $client->setMethod($httpMethod); - $client->setCookies((array)$request->getCookies() + (array)$client->cookies); + + $cookies = (array)$request->getCookies() + (array)$client->cookies; + if ($cookies) { + $client->setCookies($cookies); + } + + if ($httpMethod == HttpClient::METHOD_POST) { if (is_array($rawData)) { foreach ($rawData as $key => $item) { @@ -147,7 +153,12 @@ public function rawRequest($httpMethod = HttpClient::METHOD_GET, $rawData = null if (!empty($contentType)) { $request->setContentType($contentType); } - $client->setHeaders($request->getHeader()); + + $headers = $request->getHeader(); + if ($headers){ + $client->setHeaders($headers); + } + $client->execute($this->url->getFullPath()); // 如果不设置保持长连接则直接关闭当前链接 if (!isset($request->getClientSetting()['keep_alive']) || $request->getClientSetting()['keep_alive'] !== true) { From cf8fad2e891310fa8098c917b64208b10df1ca1f Mon Sep 17 00:00:00 2001 From: gaobinzhan Date: Thu, 5 Nov 2020 11:35:17 +0800 Subject: [PATCH 4/4] feat: testing --- tests/HttpTest.php | 31 +++++++++++++++++++++---------- tests/server/server.php | 10 ++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/tests/HttpTest.php b/tests/HttpTest.php index 795f2c5..314bf53 100644 --- a/tests/HttpTest.php +++ b/tests/HttpTest.php @@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase; use Swoole\WebSocket\Frame; -class Test extends TestCase +class HttpTest extends TestCase { /* * url内容请看 tests/index.php @@ -342,20 +342,31 @@ function testAddCookie() function testWebsocket() { $client = new HttpClient('127.0.0.1:9510'); + $client->setHeader('aaa','bbb'); $upgradeResult = $client->upgrade('cookie1', 'cook'); $this->assertIsBool(true, $upgradeResult); + $recvFrame = $client->recv(); + $this->assertEquals('bbb', json_decode($recvFrame->data,true)['aaa'] ?? ''); + $frame = new Frame(); $frame->data = json_encode(['action' => 'hello', 'content' => ['a' => 1]]); $pushResult = $client->push($frame); $this->assertIsBool(true, $pushResult); + $recvFrame = $client->recv(); $this->assertIsBool(true, !!$recvFrame); $this->assertEquals('call hello with arg:{"a":1}', $recvFrame->data); $client = new HttpClient(); $client->setUrl('127.0.0.1:9510'); + $client->addCookie('ca-1','cookie1'); $upgradeResult = $client->upgrade('cookie1', 'cook'); $this->assertIsBool(true, $upgradeResult); + + + $recvFrame = $client->recv(); + $this->assertEquals('cookie1', json_decode($recvFrame->data,true)['ca-1'] ?? ''); + $frame = new Frame(); $frame->data = json_encode(['action' => 'hello', 'content' => ['a' => 1]]); $pushResult = $client->push($frame); @@ -426,27 +437,27 @@ public function testSetPath() $httpClient = new HttpClient('https://www.easyswoole.com/Cn/demo.html'); $res = $httpClient->get(); $res = $res->getBody(); - $this->assertContains('基于EasySwoole V3 实现的聊天室', $res); + $this->assertStringContainsString('基于EasySwoole V3 实现的聊天室', $res); $httpClient->setPath('/Cn/Preface/introduction.html'); $res = $httpClient->get(); $res = $res->getBody(); - $this->assertContains('admin@fosuss.com', $res); + $this->assertStringContainsString('admin@fosuss.com', $res); $httpClient = new HttpClient(); $httpClient->setUrl('https://www.easyswoole.com/Cn/demo.html'); $res = $httpClient->get(); $res = $res->getBody(); - $this->assertContains('基于EasySwoole V3 实现的聊天室', $res); + $this->assertStringContainsString('基于EasySwoole V3 实现的聊天室', $res); $httpClient->setPath('/Cn/Preface/introduction.html'); $httpClient->setQuery(['a' => 2]); $res = $httpClient->get(); $res = $res->getBody(); - $this->assertContains('admin@fosuss.com', $res); + $this->assertStringContainsString('admin@fosuss.com', $res); $httpClient->setPath(); $res = $httpClient->get(); $res = $res->getBody(); - $this->assertContains('一种愉悦的开发方式', $res); + $this->assertStringContainsString('一种愉悦的开发方式', $res); } @@ -454,12 +465,12 @@ public function testSetMethod() { $httpClient = new HttpClient('https://www.easyswoole.com/Cn/demo.html'); $httpClient->setMethod('get'); - $this->assertEquals('get',$httpClient->getClient()->requestMethod); + $this->assertEquals('get', $httpClient->getClient()->requestMethod); $httpClient->setMethod('post'); - $this->assertEquals('post',$httpClient->getClient()->requestMethod); + $this->assertEquals('post', $httpClient->getClient()->requestMethod); $httpClient->setMethod('put'); - $this->assertEquals('put',$httpClient->getClient()->requestMethod); + $this->assertEquals('put', $httpClient->getClient()->requestMethod); $httpClient->setMethod('delete'); - $this->assertEquals('delete',$httpClient->getClient()->requestMethod); + $this->assertEquals('delete', $httpClient->getClient()->requestMethod); } } diff --git a/tests/server/server.php b/tests/server/server.php index cb1d620..8fe0a49 100644 --- a/tests/server/server.php +++ b/tests/server/server.php @@ -54,6 +54,16 @@ }); +$server->on('open', function (Swoole\WebSocket\Server $server, Swoole\Http\Request $request) { + if (isset($request->header['aaa'])) { + $server->push($request->fd, json_encode($request->header)); + } + + if (isset($request->cookie['ca-1'])) { + $server->push($request->fd, json_encode($request->cookie)); + } +}); + $server->on('message', function ($server, $frame) { echo "received message: {$frame->data}\n"; $data = json_decode($frame->data, true);