Skip to content

Commit 6e446f6

Browse files
authored
* fix swoole#4058 * optimize code * revert, fix tests
1 parent 91bf243 commit 6e446f6

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

ext-src/swoole_runtime.cc

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static int socket_flush(php_stream *stream);
6565
static int socket_close(php_stream *stream, int close_handle);
6666
static int socket_stat(php_stream *stream, php_stream_statbuf *ssb);
6767
static int socket_cast(php_stream *stream, int castas, void **ret);
68-
68+
static bool socket_ssl_set_options(Socket *sock, php_stream_context *context);
6969
// clang-format off
7070

7171
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
@@ -747,11 +747,15 @@ static bool php_openssl_capture_peer_certs(php_stream *stream, Socket *sslsock)
747747
}
748748

749749
static int socket_enable_crypto(php_stream *stream, Socket *sock, php_stream_xport_crypto_param *cparam STREAMS_DC) {
750+
php_stream_context *context = PHP_STREAM_CONTEXT(stream);
750751
if (cparam->inputs.activate && !sock->ssl_is_available()) {
751752
sock->enable_ssl_encrypt();
752753
if (!sock->ssl_check_context()) {
753754
return -1;
754755
}
756+
if (!socket_ssl_set_options(sock, context)) {
757+
return -1;
758+
}
755759
if (!sock->ssl_handshake()) {
756760
return -1;
757761
}
@@ -760,11 +764,10 @@ static int socket_enable_crypto(php_stream *stream, Socket *sock, php_stream_xpo
760764
return sock->ssl_shutdown() ? 0 : -1;
761765
}
762766

763-
php_stream_context *context = PHP_STREAM_CONTEXT(stream);
764767
if (context) {
765768
zval *val = php_stream_context_get_option(context, "ssl", "capture_peer_cert");
766-
if (val && zend_is_true(val)) {
767-
return php_openssl_capture_peer_certs(stream, sock) ? 0 : -1;
769+
if (val && zend_is_true(val) && !php_openssl_capture_peer_certs(stream, sock)) {
770+
return -1;
768771
}
769772
}
770773

@@ -993,6 +996,49 @@ static int socket_set_option(php_stream *stream, int option, int value, void *pt
993996
return PHP_STREAM_OPTION_RETURN_OK;
994997
}
995998

999+
static bool socket_ssl_set_options(Socket *sock, php_stream_context *context) {
1000+
if (context && ZVAL_IS_ARRAY(&context->options)) {
1001+
#ifdef SW_USE_OPENSSL
1002+
zval *ztmp;
1003+
1004+
if (sock->ssl_is_enable() && php_swoole_array_get_value(Z_ARRVAL_P(&context->options), "ssl", ztmp) &&
1005+
ZVAL_IS_ARRAY(ztmp)) {
1006+
1007+
zval zalias;
1008+
array_init(&zalias);
1009+
zend_array *options = Z_ARRVAL_P(ztmp);
1010+
1011+
auto add_alias = [&zalias, options](const char *name, const char *alias) {
1012+
zval *ztmp;
1013+
if (php_swoole_array_get_value_ex(options, name, ztmp)) {
1014+
add_assoc_zval_ex(&zalias, alias, strlen(alias), ztmp);
1015+
zval_add_ref(ztmp);
1016+
}
1017+
};
1018+
1019+
add_alias("peer_name", "ssl_host_name");
1020+
add_alias("verify_peer", "ssl_verify_peer");
1021+
add_alias("allow_self_signed", "ssl_allow_self_signed");
1022+
add_alias("cafile", "ssl_cafile");
1023+
add_alias("capath", "ssl_capath");
1024+
add_alias("local_cert", "ssl_cert_file");
1025+
add_alias("local_pk", "ssl_key_file");
1026+
add_alias("passphrase", "ssl_passphrase");
1027+
add_alias("verify_depth", "ssl_verify_depth");
1028+
add_alias("disable_compression", "ssl_disable_compression");
1029+
1030+
php_swoole_socket_set_ssl(sock, &zalias);
1031+
if (!sock->ssl_check_context()) {
1032+
return false;
1033+
}
1034+
zval_dtor(&zalias);
1035+
}
1036+
#endif
1037+
}
1038+
1039+
return true;
1040+
}
1041+
9961042
static php_stream *socket_create(const char *proto,
9971043
size_t protolen,
9981044
const char *resourcename,
@@ -1055,43 +1101,8 @@ static php_stream *socket_create(const char *proto,
10551101
goto _failed;
10561102
}
10571103

1058-
if (context && ZVAL_IS_ARRAY(&context->options)) {
1059-
#ifdef SW_USE_OPENSSL
1060-
zval *ztmp;
1061-
1062-
if (sock->ssl_is_enable() && php_swoole_array_get_value(Z_ARRVAL_P(&context->options), "ssl", ztmp) &&
1063-
ZVAL_IS_ARRAY(ztmp)) {
1064-
1065-
zval zalias;
1066-
array_init(&zalias);
1067-
zend_array *options = Z_ARRVAL_P(ztmp);
1068-
1069-
auto add_alias = [&zalias, options](const char *name, const char *alias) {
1070-
zval *ztmp;
1071-
if (php_swoole_array_get_value_ex(options, name, ztmp)) {
1072-
add_assoc_zval_ex(&zalias, alias, strlen(alias), ztmp);
1073-
zval_add_ref(ztmp);
1074-
}
1075-
};
1076-
1077-
add_alias("peer_name", "ssl_host_name");
1078-
add_alias("verify_peer", "ssl_verify_peer");
1079-
add_alias("allow_self_signed", "ssl_allow_self_signed");
1080-
add_alias("cafile", "ssl_cafile");
1081-
add_alias("capath", "ssl_capath");
1082-
add_alias("local_cert", "ssl_cert_file");
1083-
add_alias("local_pk", "ssl_key_file");
1084-
add_alias("passphrase", "ssl_passphrase");
1085-
add_alias("verify_depth", "ssl_verify_depth");
1086-
add_alias("disable_compression", "ssl_disable_compression");
1087-
1088-
php_swoole_socket_set_ssl(sock, &zalias);
1089-
if (!sock->ssl_check_context()) {
1090-
goto _failed;
1091-
}
1092-
zval_dtor(&zalias);
1093-
}
1094-
#endif
1104+
if (!socket_ssl_set_options(sock, context)) {
1105+
goto _failed;
10951106
}
10961107

10971108
return stream;

0 commit comments

Comments
 (0)