Skip to content

Commit

Permalink
updates for Swoole 4.4.17
Browse files Browse the repository at this point in the history
Signed-off-by: Demin Yin <[email protected]>
  • Loading branch information
deminy committed Apr 2, 2020
1 parent 740331c commit fceaf61
Show file tree
Hide file tree
Showing 32 changed files with 2,278 additions and 7 deletions.
6 changes: 3 additions & 3 deletions output/swoole/constants.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

define('SWOOLE_VERSION', '4.4.16');
define('SWOOLE_VERSION_ID', 40416);
define('SWOOLE_VERSION', '4.4.17');
define('SWOOLE_VERSION_ID', 40417);
define('SWOOLE_MAJOR_VERSION', 4);
define('SWOOLE_MINOR_VERSION', 4);
define('SWOOLE_RELEASE_VERSION', 16);
define('SWOOLE_RELEASE_VERSION', 17);
define('SWOOLE_EXTRA_VERSION', '');
define('SWOOLE_DEBUG', '');
define('SWOOLE_HAVE_COMPRESSION', '1');
Expand Down
1 change: 1 addition & 0 deletions output/swoole_library/src/alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
if (SWOOLE_USE_SHORTNAME) {
class_alias(Swoole\Coroutine\WaitGroup::class, Co\WaitGroup::class, true);
class_alias(Swoole\Coroutine\Server::class, Co\Server::class, true);
class_alias(Swoole\Coroutine\FastCGI\Client::class, Co\FastCGI\Client::class, true);
}
10 changes: 10 additions & 0 deletions output/swoole_library/src/core/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class Constant

public const OPTION_STACK_SIZE = 'stack_size';

public const OPTION_SOCKET_DNS_TIMEOUT = 'socket_dns_timeout';

public const OPTION_SOCKET_CONNECT_TIMEOUT = 'socket_connect_timeout';

public const OPTION_SOCKET_TIMEOUT = 'socket_timeout';
Expand Down Expand Up @@ -246,6 +248,8 @@ class Constant

public const OPTION_MAX_WAIT_TIME = 'max_wait_time';

public const OPTION_MAX_QUEUED_BYTES = 'max_queued_bytes';

public const OPTION_MAX_CORO_NUM = 'max_coro_num';

public const OPTION_SEND_TIMEOUT = 'send_timeout';
Expand Down Expand Up @@ -316,8 +320,12 @@ class Constant

public const OPTION_STATIC_HANDLER_LOCATIONS = 'static_handler_locations';

public const OPTION_INPUT_BUFFER_SIZE = 'input_buffer_size';

public const OPTION_BUFFER_INPUT_SIZE = 'buffer_input_size';

public const OPTION_OUTPUT_BUFFER_SIZE = 'output_buffer_size';

public const OPTION_BUFFER_OUTPUT_SIZE = 'buffer_output_size';

public const OPTION_MESSAGE_QUEUE_KEY = 'message_queue_key';
Expand Down Expand Up @@ -366,5 +374,7 @@ class Constant

public const OPTION_OPEN_SSL = 'open_ssl';

public const OPTION_OPEN_FASTCGI_PROTOCOL = 'open_fastcgi_protocol';

/* }}} OPTION */
}
178 changes: 178 additions & 0 deletions output/swoole_library/src/core/Coroutine/FastCGI/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole\Coroutine\FastCGI;

use InvalidArgumentException;
use Swoole\Coroutine\FastCGI\Client\Exception;
use Swoole\Coroutine\Socket;
use Swoole\FastCGI\FrameParser;
use Swoole\FastCGI\HttpRequest;
use Swoole\FastCGI\HttpResponse;
use Swoole\FastCGI\Record\EndRequest;
use Swoole\FastCGI\Request;
use Swoole\FastCGI\Response;

class Client
{
/** @var int */
protected $af;

/** @var string */
protected $host;

/** @var int */
protected $port;

/** @var bool */
protected $ssl;

/** @var Socket */
protected $socket;

public function __construct(string $host, int $port = 0, bool $ssl = false)
{
if (stripos($host, 'unix:/') === 0) {
$this->af = AF_UNIX;
$host = '/' . ltrim(substr($host, strlen('unix:/')), '/');
$port = 0;
} elseif (strpos($host, ':') !== false) {
$this->af = AF_INET6;
} else {
$this->af = AF_INET;
}
$this->host = $host;
$this->port = $port;
$this->ssl = $ssl;
}

/**
* @throws Exception
* @return HttpResponse|Response
*/
public function execute(Request $request, float $timeout = -1): Response
{
if (!$this->socket) {
$socket = new Socket($this->af, SOCK_STREAM, IPPROTO_IP);
$socket->setProtocol([
'open_ssl' => $this->ssl,
'open_fastcgi_protocol' => true,
]);
if (!$socket->connect($this->host, $this->port, $timeout)) {
$this->ioException();
}
$this->socket = $socket;
} else {
$socket = $this->socket;
}
$sendData = (string) $request;
if ($socket->sendAll($sendData) !== strlen($sendData)) {
$this->ioException();
}
$records = [];
while (true) {
if (SWOOLE_VERSION_ID < 40500) {
$recvData = '';
while (true) {
$tmp = $socket->recv(8192, $timeout);
if (!$tmp) {
if ($tmp === '') {
$this->ioException(SOCKET_ECONNRESET);
}
$this->ioException();
}
$recvData .= $tmp;
if (FrameParser::hasFrame($recvData)) {
break;
}
}
} else {
$recvData = $socket->recvPacket($timeout);
if (!$recvData) {
if ($recvData === '') {
$this->ioException(SOCKET_ECONNRESET);
}
$this->ioException();
}
if (!FrameParser::hasFrame($recvData)) {
$this->ioException(SOCKET_EPROTO);
}
}
do {
$records[] = $record = FrameParser::parseFrame($recvData);
} while (strlen($recvData) !== 0);
if ($record instanceof EndRequest) {
if (!$request->getKeepConn()) {
$this->socket->close();
$this->socket = null;
}
switch (true) {
case $request instanceof HttpRequest:
return new HttpResponse($records);
default:
return new Response($records);
}
}
}
/* never here */
exit(1);
}

public static function parseUrl(string $url): array
{
$url = parse_url($url);
$host = $url['host'] ?? '';
$port = $url['port'] ?? 0;
if (empty($host)) {
$host = $url['path'] ?? '';
if (empty($host)) {
throw new InvalidArgumentException('Invalid url');
}
$host = "unix:/{$host}";
}
return [$host, $port];
}

public static function call(string $url, string $path, $data = '', float $timeout = -1): string
{
$client = new Client(...static::parseUrl($url));
$pathInfo = parse_url($path);
$path = $pathInfo['path'] ?? '';
$root = dirname($path);
$scriptName = '/' . basename($path);
$documentUri = $scriptName;
$query = $pathInfo['query'] ?? '';
$requestUri = $query ? "{$documentUri}?{$query}" : $documentUri;
$request = new HttpRequest();
$request->withDocumentRoot($root)
->withScriptFilename($path)
->withScriptName($documentUri)
->withDocumentUri($documentUri)
->withRequestUri($requestUri)
->withQueryString($query)
->withBody($data)
->withMethod($request->getContentLength() === 0 ? 'GET' : 'POST');
$response = $client->execute($request, $timeout);
return $response->getBody();
}

protected function ioException(?int $errno = null): void
{
$socket = $this->socket;
if ($errno !== null) {
$socket->errCode = $errno;
$socket->errMsg = swoole_strerror($errno);
}
$socket->close();
$this->socket = null;
throw new Exception($socket->errMsg, $socket->errCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole\Coroutine\FastCGI\Client;

class Exception extends \Swoole\Exception
{
}
Loading

0 comments on commit fceaf61

Please sign in to comment.