Skip to content

Commit

Permalink
Merge pull request #32 from gaobinzhan/master
Browse files Browse the repository at this point in the history
fix: cookie overlay header problem
  • Loading branch information
kiss291323003 authored Nov 5, 2020
2 parents 4cb2e3d + cf8fad2 commit c4ec311
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
27 changes: 22 additions & 5 deletions src/Handler/Swoole/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,21 @@ 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;
}

public function upgrade(bool $mask = true): bool
{
$this->getRequest()->setClientSetting('websocket_mask', $mask);
$request = $this->getRequest();
$request->setClientSetting('websocket_mask', $mask);

$client = $this->getClient();
$request->getCookies() && $client->setCookies($request->getCookies());
$request->getHeader() && $client->setHeaders($request->getHeader());
return $client->upgrade($this->url->getFullPath());
}

Expand Down Expand Up @@ -115,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) {
Expand All @@ -124,7 +136,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]);
}
}
Expand All @@ -141,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) {
Expand Down
31 changes: 21 additions & 10 deletions tests/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPUnit\Framework\TestCase;
use Swoole\WebSocket\Frame;

class Test extends TestCase
class HttpTest extends TestCase
{
/*
* url内容请看 tests/index.php
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -426,40 +437,40 @@ 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('[email protected]', $res);
$this->assertStringContainsString('[email protected]', $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('[email protected]', $res);
$this->assertStringContainsString('[email protected]', $res);
$httpClient->setPath();
$res = $httpClient->get();
$res = $res->getBody();
$this->assertContains('一种愉悦的开发方式', $res);
$this->assertStringContainsString('一种愉悦的开发方式', $res);

}

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);
}
}
10 changes: 10 additions & 0 deletions tests/server/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c4ec311

Please sign in to comment.