Skip to content

Commit 5b9ca0e

Browse files
ericnorrisAppla
authored andcommitted
backport from 8.5 to 8.3
RFC: Add `CurlSharePersistentHandle` objects (php#16937) see https://wiki.php.net/rfc/curl_share_persistence_improvement cherry-pick from php@d20880c
1 parent 27f17c3 commit 5b9ca0e

14 files changed

+416
-10
lines changed

Zend/Optimizer/zend_func_infos.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static const func_info_t func_infos[] = {
4242
F1("curl_multi_strerror", MAY_BE_STRING|MAY_BE_NULL),
4343
F1("curl_share_init", MAY_BE_OBJECT),
4444
F1("curl_share_strerror", MAY_BE_STRING|MAY_BE_NULL),
45+
F1("curl_share_init_persistent", MAY_BE_OBJECT),
4546
F1("curl_strerror", MAY_BE_STRING|MAY_BE_NULL),
4647
F1("curl_version", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_FALSE),
4748
F1("date", MAY_BE_STRING),

ext/curl/curl.stub.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,6 +3644,15 @@ final class CurlShareHandle
36443644
{
36453645
}
36463646

3647+
/**
3648+
* @strict-properties
3649+
* @not-serializable
3650+
*/
3651+
final class CurlSharePersistentHandle
3652+
{
3653+
public readonly array $options;
3654+
}
3655+
36473656
function curl_close(CurlHandle $handle): void {}
36483657

36493658
/** @refcount 1 */
@@ -3724,6 +3733,9 @@ function curl_share_setopt(CurlShareHandle $share_handle, int $option, mixed $va
37243733
/** @refcount 1 */
37253734
function curl_share_strerror(int $error_code): ?string {}
37263735

3736+
/** @refcount 1 */
3737+
function curl_share_init_persistent(array $share_options): CurlSharePersistentHandle {}
3738+
37273739
/** @refcount 1 */
37283740
function curl_strerror(int $error_code): ?string {}
37293741

ext/curl/curl_arginfo.h

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/curl/curl_private.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@
4040
#define SAVE_CURL_ERROR(__handle, __err) \
4141
do { (__handle)->err.no = (int) __err; } while (0)
4242

43+
44+
ZEND_BEGIN_MODULE_GLOBALS(curl)
45+
HashTable persistent_curlsh;
46+
ZEND_END_MODULE_GLOBALS(curl)
47+
48+
ZEND_EXTERN_MODULE_GLOBALS(curl)
49+
50+
#define CURL_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(curl, v)
51+
4352
PHP_MINIT_FUNCTION(curl);
4453
PHP_MSHUTDOWN_FUNCTION(curl);
4554
PHP_MINFO_FUNCTION(curl);
55+
PHP_GINIT_FUNCTION(curl);
56+
PHP_GSHUTDOWN_FUNCTION(curl);
4657

4758
typedef struct {
4859
zval func_name;
@@ -164,6 +175,8 @@ static inline php_curlsh *curl_share_from_obj(zend_object *obj) {
164175

165176
void curl_multi_register_handlers(void);
166177
void curl_share_register_handlers(void);
178+
void curl_share_persistent_register_handlers(void);
179+
void curl_share_free_persistent_curlsh(zval *data);
167180
void curlfile_register_class(void);
168181
zend_result curl_cast_object(zend_object *obj, zval *result, int type);
169182

ext/curl/interface.c

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969

7070
#include "curl_arginfo.h"
7171

72+
ZEND_DECLARE_MODULE_GLOBALS(curl)
73+
7274
#ifdef PHP_CURL_NEED_OPENSSL_TSL /* {{{ */
7375
static MUTEX_T *php_curl_openssl_tsl = NULL;
7476

@@ -217,18 +219,34 @@ zend_module_entry curl_module_entry = {
217219
NULL,
218220
PHP_MINFO(curl),
219221
PHP_CURL_VERSION,
220-
STANDARD_MODULE_PROPERTIES
222+
PHP_MODULE_GLOBALS(curl),
223+
PHP_GINIT(curl),
224+
PHP_GSHUTDOWN(curl),
225+
NULL,
226+
STANDARD_MODULE_PROPERTIES_EX
221227
};
222228
/* }}} */
223229

224230
#ifdef COMPILE_DL_CURL
225231
ZEND_GET_MODULE (curl)
226232
#endif
227233

234+
PHP_GINIT_FUNCTION(curl)
235+
{
236+
zend_hash_init(&curl_globals->persistent_curlsh, 0, NULL, curl_share_free_persistent_curlsh, true);
237+
GC_MAKE_PERSISTENT_LOCAL(&curl_globals->persistent_curlsh);
238+
}
239+
240+
PHP_GSHUTDOWN_FUNCTION(curl)
241+
{
242+
zend_hash_destroy(&curl_globals->persistent_curlsh);
243+
}
244+
228245
/* CurlHandle class */
229246

230247
zend_class_entry *curl_ce;
231248
zend_class_entry *curl_share_ce;
249+
zend_class_entry *curl_share_persistent_ce;
232250
static zend_object_handlers curl_object_handlers;
233251

234252
static zend_object *curl_create_object(zend_class_entry *class_type);
@@ -425,6 +443,10 @@ PHP_MINIT_FUNCTION(curl)
425443

426444
curl_share_ce = register_class_CurlShareHandle();
427445
curl_share_register_handlers();
446+
447+
curl_share_persistent_ce = register_class_CurlSharePersistentHandle();
448+
curl_share_persistent_register_handlers();
449+
428450
curlfile_register_class();
429451

430452
return SUCCESS;
@@ -2416,16 +2438,24 @@ static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue
24162438

24172439
case CURLOPT_SHARE:
24182440
{
2419-
if (Z_TYPE_P(zvalue) == IS_OBJECT && Z_OBJCE_P(zvalue) == curl_share_ce) {
2420-
php_curlsh *sh = Z_CURL_SHARE_P(zvalue);
2421-
curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
2441+
if (Z_TYPE_P(zvalue) != IS_OBJECT) {
2442+
break;
2443+
}
24222444

2423-
if (ch->share) {
2424-
OBJ_RELEASE(&ch->share->std);
2425-
}
2426-
GC_ADDREF(&sh->std);
2427-
ch->share = sh;
2445+
if (Z_OBJCE_P(zvalue) != curl_share_ce && Z_OBJCE_P(zvalue) != curl_share_persistent_ce) {
2446+
break;
24282447
}
2448+
2449+
php_curlsh *sh = Z_CURL_SHARE_P(zvalue);
2450+
2451+
curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
2452+
2453+
if (ch->share) {
2454+
OBJ_RELEASE(&ch->share->std);
2455+
}
2456+
2457+
GC_ADDREF(&sh->std);
2458+
ch->share = sh;
24292459
}
24302460
break;
24312461

ext/curl/php_curl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern zend_module_entry curl_module_entry;
3737

3838
PHP_CURL_API extern zend_class_entry *curl_ce;
3939
PHP_CURL_API extern zend_class_entry *curl_share_ce;
40+
PHP_CURL_API extern zend_class_entry *curl_share_persistent_ce;
4041
PHP_CURL_API extern zend_class_entry *curl_multi_ce;
4142
PHP_CURL_API extern zend_class_entry *curl_CURLFile_class;
4243
PHP_CURL_API extern zend_class_entry *curl_CURLStringFile_class;

0 commit comments

Comments
 (0)