Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFreeman committed Dec 8, 2024
2 parents f0696c0 + 709a5cd commit 7dae97d
Show file tree
Hide file tree
Showing 35 changed files with 590 additions and 106 deletions.
12 changes: 12 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ PHP_ARG_WITH([brotli_dir],
[AS_HELP_STRING([[--with-brotli-dir[=DIR]]],
[Include Brotli support])], [no], [no])

PHP_ARG_ENABLE([zstd],
[enable zstd support],
[AS_HELP_STRING([[--enable-zstd]],
[Use zstd])], [no], [no])

PHP_ARG_WITH([nghttp2_dir],
[dir of nghttp2],
[AS_HELP_STRING([[--with-nghttp2-dir[=DIR]]],
Expand Down Expand Up @@ -873,6 +878,13 @@ EOF
fi
fi

if test "$PHP_ZSTD" = "yes"; then
PKG_CHECK_MODULES([ZSTD], [libzstd >= 1.4.0])
AC_DEFINE(SW_HAVE_ZSTD, 1, [have zstd])
PHP_EVAL_LIBLINE($ZSTD_LIBS, SWOOLE_SHARED_LIBADD)
PHP_EVAL_INCLINE($ZSTD_CFLAGS)
fi

PHP_ADD_LIBRARY(pthread)
PHP_SUBST(SWOOLE_SHARED_LIBADD)

Expand Down
4 changes: 2 additions & 2 deletions core-tests/src/coroutine/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ TEST(coroutine_system, wait_signal) {
System::sleep(0.002);
kill(getpid(), SIGUSR1);
});
ASSERT_TRUE(System::wait_signal(SIGUSR1, 1.0));
ASSERT_FALSE(System::wait_signal(SIGUSR2, 0.1));
ASSERT_EQ(System::wait_signal(SIGUSR1, 1.0), SIGUSR1);
ASSERT_EQ(System::wait_signal(SIGUSR2, 0.1), -1);
});
}

Expand Down
10 changes: 10 additions & 0 deletions ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ END_EXTERN_C()
#include <brotli/encode.h>
#include <brotli/decode.h>
#endif
#ifdef SW_HAVE_ZSTD
#include <zstd.h>
#endif

#ifdef SW_USE_CARES
#include <ares.h>
Expand Down Expand Up @@ -928,6 +931,13 @@ PHP_MINFO_FUNCTION(swoole) {
snprintf(buf, sizeof(buf), "E%u/D%u", BrotliEncoderVersion(), BrotliDecoderVersion());
php_info_print_table_row(2, "brotli", buf);
#endif
#ifdef SW_HAVE_ZSTD
#ifdef ZSTD_VERSION_NUMBER
php_info_print_table_row(2, "zstd", ZSTD_VERSION_STRING);
#else
php_info_print_table_row(2, "zstd", "enabled");
#endif
#endif
#ifdef HAVE_MUTEX_TIMEDLOCK
php_info_print_table_row(2, "mutex_timedlock", "enabled");
#endif
Expand Down
6 changes: 6 additions & 0 deletions ext-src/php_swoole_cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,12 @@ static inline void array_unset(zval *arg, const char *key, size_t l_key) {
zend_hash_str_del(Z_ARRVAL_P(arg), key, l_key);
}

static inline zend_long object_get_long(zval *obj, zend_string *key) {
static zval rv;
zval *property = zend_read_property_ex(Z_OBJCE_P(obj), Z_OBJ_P(obj), key, 1, &rv);
return property ? zval_get_long(property) : 0;
}

static inline zend_long object_get_long(zval *obj, const char *key, size_t l_key) {
static zval rv;
zval *property = zend_read_property(Z_OBJCE_P(obj), Z_OBJ_P(obj), key, l_key, 1, &rv);
Expand Down
10 changes: 10 additions & 0 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
#define SW_ZLIB_ENCODING_ANY 0x2f
#endif

#ifdef SW_HAVE_BROTLI
#include <brotli/encode.h>
#include <brotli/decode.h>
#endif

#ifdef SW_HAVE_ZSTD
#include <zstd.h>
#endif

#include <nghttp2/nghttp2.h>

enum swHttpHeaderFlag {
Expand All @@ -52,6 +61,7 @@ enum swHttpCompressMethod {
HTTP_COMPRESS_GZIP,
HTTP_COMPRESS_DEFLATE,
HTTP_COMPRESS_BR,
HTTP_COMPRESS_ZSTD,
};

namespace swoole {
Expand Down
2 changes: 1 addition & 1 deletion ext-src/stubs/php_swoole_coroutine_system.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static function readFile(string $filename, int $flag = 0): false|string {
public static function writeFile(string $filename, string $fileContent, int $flags = 0): false|int {}
public static function wait(float $timeout = -1): array|false {}
public static function waitPid(int $pid, float $timeout = -1): array|false {}
public static function waitSignal(int|array $signals, float $timeout = -1): bool {}
public static function waitSignal(int|array $signals, float $timeout = -1): int|false {}
public static function waitEvent(mixed $socket, int $events = SWOOLE_EVENT_READ, float $timeout = -1): int|false {}
}
}
4 changes: 2 additions & 2 deletions ext-src/stubs/php_swoole_coroutine_system_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 59e0a163279f6b7bf53de6f8f50a8ba4820ccf3b */
* Stub hash: 3c270ea28b44ea9ae57763943f8e0188d2fbcc03 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_gethostbyname, 0, 1, MAY_BE_FALSE|MAY_BE_STRING)
ZEND_ARG_TYPE_INFO(0, domain_name, IS_STRING, 0)
Expand Down Expand Up @@ -55,7 +55,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_wa
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, timeout, IS_DOUBLE, 0, "-1")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Coroutine_System_waitSignal, 0, 1, _IS_BOOL, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_waitSignal, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_TYPE_MASK(0, signals, MAY_BE_LONG|MAY_BE_ARRAY, NULL)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, timeout, IS_DOUBLE, 0, "-1")
ZEND_END_ARG_INFO()
Expand Down
37 changes: 24 additions & 13 deletions ext-src/swoole_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ static zend_object *client_create_object(zend_class_entry *ce) {
return &client->std;
}


SW_EXTERN_C_BEGIN
static PHP_METHOD(swoole_client, __construct);
static PHP_METHOD(swoole_client, __destruct);
Expand Down Expand Up @@ -166,8 +165,7 @@ void php_swoole_client_minit(int module_number) {
SW_SET_CLASS_NOT_SERIALIZABLE(swoole_client);
SW_SET_CLASS_CLONEABLE(swoole_client, sw_zend_class_clone_deny);
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_client, sw_zend_class_unset_property_deny);
SW_SET_CLASS_CUSTOM_OBJECT(
swoole_client, client_create_object, client_free_object, ClientObject, std);
SW_SET_CLASS_CUSTOM_OBJECT(swoole_client, client_create_object, client_free_object, ClientObject, std);

SW_INIT_CLASS_ENTRY_EX(swoole_client_exception, "Swoole\\Client\\Exception", nullptr, nullptr, swoole_exception);

Expand Down Expand Up @@ -720,11 +718,11 @@ static PHP_METHOD(swoole_client, connect) {
RETURN_TRUE;
}
php_swoole_core_error(E_WARNING,
"connect to server[%s:%d] failed. Error: %s[%d]",
host,
(int) port,
swoole_strerror(swoole_get_last_error()),
swoole_get_last_error());
"connect to server[%s:%d] failed. Error: %s[%d]",
host,
(int) port,
swoole_strerror(swoole_get_last_error()),
swoole_get_last_error());
php_swoole_client_free(ZEND_THIS, cli);
RETURN_FALSE;
}
Expand Down Expand Up @@ -1234,6 +1232,19 @@ bool php_swoole_client_enable_ssl_encryption(Client *cli, zval *zobject) {
}

static PHP_METHOD(swoole_client, enableSSL) {
zval *zcallback = nullptr;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(zcallback)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (zcallback) {
zend_throw_exception(
swoole_exception_ce, "sync client does not support `onSslReady` callback", SW_ERROR_INVALID_PARAMS);
RETURN_FALSE;
}

Client *cli = php_swoole_client_get_cli_safe(ZEND_THIS);
if (!cli) {
RETURN_FALSE;
Expand Down Expand Up @@ -1296,11 +1307,11 @@ PHP_FUNCTION(swoole_client_select) {
double timeout = SW_CLIENT_CONNECT_TIMEOUT;

ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_ARRAY_EX2(r_array, 1, 1, 0)
Z_PARAM_ARRAY_EX2(w_array, 1, 1, 0)
Z_PARAM_ARRAY_EX2(e_array, 1, 1, 0)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE(timeout)
Z_PARAM_ARRAY_EX2(r_array, 1, 1, 0)
Z_PARAM_ARRAY_EX2(w_array, 1, 1, 0)
Z_PARAM_ARRAY_EX2(e_array, 1, 1, 0)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END();

int maxevents = SW_MAX(SW_MAX(php_swoole_array_length_safe(r_array), php_swoole_array_length_safe(w_array)),
Expand Down
23 changes: 15 additions & 8 deletions ext-src/swoole_client_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ static sw_inline void client_execute_callback(zval *zobject, enum php_swoole_cli
}
}


// clang-format off
static const zend_function_entry swoole_client_async_methods[] = {
PHP_ME(swoole_client_async, __construct, arginfo_class_Swoole_Async_Client___construct, ZEND_ACC_PUBLIC)
Expand All @@ -144,7 +143,8 @@ static const zend_function_entry swoole_client_async_methods[] = {
// clang-format on

void php_swoole_client_async_minit(int module_number) {
SW_INIT_CLASS_ENTRY_EX(swoole_client_async, "Swoole\\Async\\Client", nullptr, swoole_client_async_methods, swoole_client);
SW_INIT_CLASS_ENTRY_EX(
swoole_client_async, "Swoole\\Async\\Client", nullptr, swoole_client_async_methods, swoole_client);
SW_SET_CLASS_NOT_SERIALIZABLE(swoole_client_async);
SW_SET_CLASS_CLONEABLE(swoole_client_async, sw_zend_class_clone_deny);
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_client_async, sw_zend_class_unset_property_deny);
Expand Down Expand Up @@ -504,6 +504,18 @@ static PHP_METHOD(swoole_client_async, wakeup) {

#ifdef SW_USE_OPENSSL
static PHP_METHOD(swoole_client_async, enableSSL) {
zval *zcallback = nullptr;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(zcallback)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (zcallback == nullptr) {
zend_throw_exception(swoole_exception_ce, "require `onSslReady` callback", SW_ERROR_INVALID_PARAMS);
RETURN_FALSE;
}

Client *cli = php_swoole_client_get_cli_safe(ZEND_THIS);
if (!cli) {
RETURN_FALSE;
Expand All @@ -512,11 +524,6 @@ static PHP_METHOD(swoole_client_async, enableSSL) {
RETURN_FALSE;
}

zval *zcallback;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zcallback) == FAILURE) {
RETURN_FALSE;
}

auto client_obj = php_swoole_client_fetch_object(ZEND_THIS);
if (swoole_event_set(cli->socket, SW_EVENT_WRITE) < 0) {
RETURN_FALSE;
Expand All @@ -528,7 +535,7 @@ static PHP_METHOD(swoole_client_async, enableSSL) {

auto cb = sw_callable_create(zcallback);
if (!cb) {
return;
RETURN_FALSE;
}
zend_update_property(swoole_client_async_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("onSSLReady"), zcallback);
client_obj->async->onSSLReady = cb;
Expand Down
5 changes: 3 additions & 2 deletions ext-src/swoole_coroutine_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ PHP_METHOD(swoole_coroutine_system, waitSignal) {
signals.push_back(zval_get_long(zsignals));
}

if (!System::wait_signal(signals, timeout)) {
int signo = System::wait_signal(signals, timeout);
if (signo == -1) {
if (swoole_get_last_error() == EBUSY) {
php_swoole_fatal_error(E_WARNING, "Unable to wait signal, async signal listener has been registered");
} else if (swoole_get_last_error() == EINVAL) {
Expand All @@ -366,7 +367,7 @@ PHP_METHOD(swoole_coroutine_system, waitSignal) {
RETURN_FALSE;
}

RETURN_TRUE;
RETURN_LONG(signo);
}

PHP_METHOD(swoole_coroutine_system, waitEvent) {
Expand Down
71 changes: 60 additions & 11 deletions ext-src/swoole_http_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,7 @@ SW_EXTERN_C_BEGIN

#include "ext/standard/base64.h"

#ifdef SW_HAVE_ZLIB
#include <zlib.h>
#endif

#include "stubs/php_swoole_http_client_coro_arginfo.h"

SW_EXTERN_C_END

#ifdef SW_HAVE_BROTLI
#include <brotli/decode.h>
#endif

using swoole::File;
using swoole::String;
using swoole::coroutine::Socket;
Expand Down Expand Up @@ -175,6 +164,9 @@ class Client {
#endif
#ifdef SW_HAVE_BROTLI
BrotliDecoderState *brotli_decoder_state = nullptr;
#endif
#ifdef SW_HAVE_ZSTD
ZSTD_DStream *zstd_stream = nullptr;
#endif
bool bind(std::string address, int port = 0);
bool connect();
Expand Down Expand Up @@ -475,6 +467,11 @@ static int http_parser_on_header_value(llhttp_t *parser, const char *at, size_t
} else if (SW_STR_ISTARTS_WITH(at, length, "deflate")) {
http->compress_method = HTTP_COMPRESS_DEFLATE;
}
#endif
#ifdef SW_HAVE_ZSTD
else if (SW_STR_ISTARTS_WITH(at, length, "zstd")) {
http->compress_method = HTTP_COMPRESS_ZSTD;
}
#endif
}
#endif
Expand Down Expand Up @@ -708,6 +705,48 @@ bool Client::decompress_response(const char *in, size_t in_len) {
body->length = reserved_body_length;
return false;
}
#endif
#ifdef SW_HAVE_ZSTD
case HTTP_COMPRESS_ZSTD: {
size_t zstd_result = 0;
if (zstd_stream == nullptr) {
zstd_stream = ZSTD_createDStream();
if (!zstd_stream) {
swoole_warning("ZSTD_createDStream() failed, can not create ZSTD stream");
return false;
}

zstd_result = ZSTD_initDStream(zstd_stream);
if (ZSTD_isError(zstd_result)) {
swoole_warning("ZSTD_initDStream() failed, Error: [%s]", ZSTD_getErrorName(zstd_result));
return false;
}
}

size_t recommended_size = ZSTD_DStreamOutSize();
ZSTD_inBuffer in_buffer = {in, in_len, 0};
ZSTD_outBuffer out_buffer = {body->str + body->length, body->size - body->length, 0};
while (in_buffer.pos < in_buffer.size) {
if (sw_unlikely(out_buffer.pos == out_buffer.size)) {
if (!body->extend(recommended_size + body->size)) {
swoole_warning("ZSTD_decompressStream() failed, no memory is available");
return false;
}

body->length += out_buffer.pos;
out_buffer = {body->str + body->length, body->size - body->length, 0};
}

zstd_result = ZSTD_decompressStream(zstd_stream, &out_buffer, &in_buffer);
if (ZSTD_isError(zstd_result)) {
swoole_warning("ZSTD_decompressStream() failed, Error: [%s]", ZSTD_getErrorName(zstd_result));
return false;
}
}

body->length += out_buffer.pos;
return true;
}
#endif
default:
break;
Expand Down Expand Up @@ -1131,6 +1170,10 @@ bool Client::send_request() {
#else
#ifdef SW_HAVE_BROTLI
ZEND_STRL("br")
#else
#ifdef SW_HAVE_ZSTD
ZEND_STRL("zstd")
#endif
#endif
#endif
#endif
Expand Down Expand Up @@ -1660,6 +1703,12 @@ void Client::reset() {
BrotliDecoderDestroyInstance(brotli_decoder_state);
brotli_decoder_state = nullptr;
}
#endif
#ifdef SW_HAVE_ZSTD
if (zstd_stream) {
ZSTD_freeDStream(zstd_stream);
zstd_stream = nullptr;
}
#endif
if (has_upload_files) {
zend_update_property_null(swoole_http_client_coro_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("uploadFiles"));
Expand Down
Loading

0 comments on commit 7dae97d

Please sign in to comment.