Skip to content

Commit

Permalink
Fix issue 3988 (#3990)
Browse files Browse the repository at this point in the history
* Fix issue 3988

* Add test

* Don't create socket in enableSSL

* Fix test

* Enable ssl in PHP_METHOD(swoole_client_coro, enableSSL)

* Enable ssl before php_swoole_socket_set_ssl
  • Loading branch information
huanghantao committed Jan 10, 2021
1 parent 1f3000b commit 36a1dc2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ext-src/swoole_client_coro.cc
Expand Up @@ -867,9 +867,11 @@ static PHP_METHOD(swoole_client_coro, close) {
#ifdef SW_USE_OPENSSL
static PHP_METHOD(swoole_client_coro, enableSSL) {
Socket *cli = client_get_ptr(ZEND_THIS);

if (!cli) {
RETURN_FALSE;
}

if (cli->get_type() != SW_SOCK_TCP && cli->get_type() != SW_SOCK_TCP6) {
php_swoole_fatal_error(E_WARNING, "cannot use enableSSL");
RETURN_FALSE;
Expand All @@ -878,6 +880,9 @@ static PHP_METHOD(swoole_client_coro, enableSSL) {
php_swoole_fatal_error(E_WARNING, "SSL has been enabled");
RETURN_FALSE;
}

cli->enable_ssl_encrypt();

zval *zset = sw_zend_read_property_ex(swoole_client_coro_ce, ZEND_THIS, SW_ZSTR_KNOWN(SW_ZEND_STR_SETTING), 0);
if (php_swoole_array_length_safe(zset) > 0) {
php_swoole_socket_set_ssl(cli, zset);
Expand Down
35 changes: 35 additions & 0 deletions tests/swoole_client_coro/enableSSL.phpt
@@ -0,0 +1,35 @@
--TEST--
swoole_client_coro: enableSSL
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Coroutine\Client;

use function Swoole\Coroutine\run;

run(function () {
$client = new Client(SWOOLE_SOCK_TCP);
$client->connect('www.baidu.com', 443);
$client->enableSSL();

$http = "GET / HTTP/1.0\r\nAccept: */*User-Agent: Lowell-Agent\r\nHost: www.baidu.com\r\nConnection: Close\r\n\r\n";
if (!$client->send($http)) {
echo "ERROR\n";
}

$content = '';
while (true) {
$read = $client->recv();
if (empty($read)) {
break;
}
$content .= $read;
}
$client->close();
Assert::assert(strpos($content, 'map.baidu.com') !== false);
});
?>
--EXPECT--

0 comments on commit 36a1dc2

Please sign in to comment.