Skip to content

Commit 050293e

Browse files
committed
Optimize ssl context code, fix tests, fix memory leak
1 parent 2dd90db commit 050293e

File tree

8 files changed

+36
-36
lines changed

8 files changed

+36
-36
lines changed

ext-src/stubs/php_swoole_http_server_coro.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ final class Server {
44
public function __construct(string $host, int $port = 0, bool $ssl = false, bool $reuse_port = false) {}
55
public function __destruct() {}
66
public function set(array $settings): bool {}
7-
public function handle(string $pattern, callable $callback): void {}
7+
public function handle(string $pattern, callable $callback): bool {}
88
public function start(): bool {}
99
public function shutdown(): void {}
1010
private function onAccept(\Swoole\Coroutine\Socket $conn): void {}

ext-src/stubs/php_swoole_http_server_coro_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 2f5ecf154780c21ccc66ba5e2fd318eb117191b0 */
2+
* Stub hash: 6f65975475013861bdaec52a0fb3fe3b1dc75657 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Coroutine_Http_Server___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, host, IS_STRING, 0)
@@ -15,7 +15,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Coroutine_Http_Serv
1515
ZEND_ARG_TYPE_INFO(0, settings, IS_ARRAY, 0)
1616
ZEND_END_ARG_INFO()
1717

18-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Coroutine_Http_Server_handle, 0, 2, IS_VOID, 0)
18+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Coroutine_Http_Server_handle, 0, 2, _IS_BOOL, 0)
1919
ZEND_ARG_TYPE_INFO(0, pattern, IS_STRING, 0)
2020
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0)
2121
ZEND_END_ARG_INFO()

ext-src/swoole_http_server_coro.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,19 @@ class HttpServer {
105105
delete socket;
106106
}
107107

108-
void set_handler(std::string pattern, zend::Callable *cb) {
108+
bool set_handler(std::string pattern, zval *zfn) {
109+
auto cb = sw_callable_create(zfn);
110+
if (!cb) {
111+
return false;
112+
}
113+
if (handlers.find(pattern) != handlers.end()) {
114+
sw_callable_free(handlers[pattern]);
115+
}
109116
handlers[pattern] = cb;
110117
if (pattern == "/") {
111118
default_handler = cb;
112119
}
120+
return true;
113121
}
114122

115123
zend::Callable *get_handler(HttpContext *ctx) {
@@ -388,13 +396,8 @@ static PHP_METHOD(swoole_http_server_coro, handle) {
388396
Z_PARAM_ZVAL(zfn)
389397
ZEND_PARSE_PARAMETERS_END();
390398

391-
auto cb = sw_callable_create(zfn);
392-
if (!cb) {
393-
RETURN_FALSE;
394-
}
395-
396399
std::string key(pattern, pattern_len);
397-
hs->set_handler(key, cb);
400+
RETURN_BOOL(hs->set_handler(key, zfn));
398401
}
399402

400403
static PHP_METHOD(swoole_http_server_coro, set) {
@@ -421,10 +424,9 @@ static PHP_METHOD(swoole_http_server_coro, start) {
421424
/* get callback fci cache */
422425
char *func_name = nullptr;
423426
zend_fcall_info_cache fci_cache;
424-
zval zcallback;
425-
ZVAL_STRING(&zcallback, "onAccept");
427+
zend::Variable zcallback("onAccept");
426428
if (!sw_zend_is_callable_at_frame(
427-
&zcallback, ZEND_THIS, execute_data, 0, &func_name, nullptr, &fci_cache, nullptr)) {
429+
zcallback.ptr(), ZEND_THIS, execute_data, 0, &func_name, nullptr, &fci_cache, nullptr)) {
428430
php_swoole_fatal_error(E_CORE_ERROR, "function '%s' is not callable", func_name);
429431
return;
430432
}
@@ -510,7 +512,7 @@ static PHP_METHOD(swoole_http_server_coro, start) {
510512
if (conn) {
511513
zval zsocket;
512514
php_swoole_init_socket_object(&zsocket, conn);
513-
long cid = PHPCoroutine::create(&fci_cache, 1, &zsocket, &zcallback);
515+
long cid = PHPCoroutine::create(&fci_cache, 1, &zsocket, zcallback.ptr());
514516
zval_dtor(&zsocket);
515517
if (cid < 0) {
516518
goto _wait_1s;
@@ -535,8 +537,6 @@ static PHP_METHOD(swoole_http_server_coro, start) {
535537
}
536538
}
537539

538-
zval_dtor(&zcallback);
539-
540540
RETURN_TRUE;
541541
}
542542

ext-src/swoole_runtime.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,10 +786,13 @@ static int socket_enable_crypto(php_stream *stream, Socket *sock, php_stream_xpo
786786
return -1;
787787
}
788788

789-
zval *val = php_stream_context_get_option(context, "ssl", "capture_peer_cert");
790-
if (val && zend_is_true(val) && !php_openssl_capture_peer_certs(stream, sock)) {
791-
return -1;
789+
if (context && sock->ssl_is_available()) {
790+
zval *val = php_stream_context_get_option(context, "ssl", "capture_peer_cert");
791+
if (val && zend_is_true(val) && !php_openssl_capture_peer_certs(stream, sock)) {
792+
return -1;
793+
}
792794
}
795+
793796
return 1;
794797
}
795798
#endif

include/swoole_coroutine_socket.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ class Socket {
129129
* Operation sequence:
130130
* 1. enable_ssl_encrypt()
131131
* 2. Set SSL parameters, such as certificate file, key file
132-
* 3. ssl_check_context()
133-
* 4. ssl_accept()/ssl_connect()/ssl_handshake()
132+
* 3. ssl_handshake(), to be executed after connect or accept
134133
*/
135134
bool enable_ssl_encrypt() {
136135
if (ssl_context.get()) {
@@ -442,7 +441,6 @@ class Socket {
442441
std::string ssl_host_name;
443442
bool ssl_context_create();
444443
bool ssl_create(SSLContext *ssl_context);
445-
bool ssl_listen();
446444
#endif
447445

448446
bool connected = false;

src/core/base.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ void swoole_clean(void) {
255255
delete SwooleTG.buffer_stack;
256256
SwooleTG.buffer_stack = nullptr;
257257
}
258+
SW_LOOP_N(SW_MAX_HOOK_TYPE) {
259+
if (SwooleG.hooks[i]) {
260+
auto hooks = static_cast<std::list<swoole::Callback> *>(SwooleG.hooks[i]);
261+
delete hooks;
262+
}
263+
}
258264
swoole_signal_clear();
259265
SwooleG = {};
260266
}

src/coroutine/socket.cc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,10 +1137,7 @@ bool Socket::listen(int backlog) {
11371137
return false;
11381138
}
11391139
#ifdef SW_USE_OPENSSL
1140-
if (ssl_is_enable() && !ssl_listen()) {
1141-
set_err(SW_ERROR_SSL_CREATE_CONTEXT_FAILED);
1142-
return false;
1143-
}
1140+
ssl_is_server = true;
11441141
#endif
11451142
return true;
11461143
}
@@ -1149,6 +1146,11 @@ Socket *Socket::accept(double timeout) {
11491146
if (sw_unlikely(!is_available(SW_EVENT_READ))) {
11501147
return nullptr;
11511148
}
1149+
#ifdef SW_USE_OPENSSL
1150+
if (ssl_is_enable() && sw_unlikely(ssl_context->context == nullptr) && !ssl_context_create()) {
1151+
return nullptr;
1152+
}
1153+
#endif
11521154
network::Socket *conn = socket->accept();
11531155
if (conn == nullptr && errno == EAGAIN) {
11541156
TimerController timer(&read_timer, timeout == 0 ? read_timeout : timeout, this, timer_callback);
@@ -1213,14 +1215,6 @@ bool Socket::ssl_create(SSLContext *ssl_context) {
12131215
return true;
12141216
}
12151217

1216-
bool Socket::ssl_listen() {
1217-
ssl_is_server = true;
1218-
if (ssl_context->context == nullptr && !ssl_context_create()) {
1219-
return false;
1220-
}
1221-
return true;
1222-
}
1223-
12241218
bool Socket::ssl_handshake() {
12251219
if (ssl_handshaked) {
12261220
return false;

tests/swoole_server_coro/ssl.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ $pm = new ProcessManager;
1313

1414
$pm->parentFunc = function ($pid) use ($pm) {
1515
$client = new Swoole\Client(SWOOLE_SOCK_TCP | SWOOLE_SSL, SWOOLE_SOCK_SYNC); //同步阻塞
16-
if (!$client->connect('127.0.0.1', $pm->getFreePort()))
17-
{
16+
if (!$client->connect('127.0.0.1', $pm->getFreePort())) {
1817
exit("connect failed\n");
1918
}
2019
$client->send("hello world");

0 commit comments

Comments
 (0)