Skip to content

Migrate ext/soap resources to objects #14121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ PHP 8.4 UPGRADE NOTES
. Calling simplexml_import_dom() with a non-XML object now throws a TypeError
instead of a ValueError.

- SOAP:
. SoapClient::$httpurl is now a Soap\Url object rather than a resource.
Checks using is_resource() (i.e. is_resource($client->httpurl)) should be
replaced with checks for null (i.e. $client->httpurl !== null).
. SoapClient::$sdl is now a Soap\Sdl object rather than a resource.
Checks using is_resource() (i.e. is_resource($client->sdl)) should be
replaced with checks for null (i.e. $client->sdl !== null).

- SPL:
. Out of bounds accesses in SplFixedArray now throw an exception of type
OutOfBoundsException instead of RuntimeException. As OutOfBoundsException
Expand Down
16 changes: 11 additions & 5 deletions ext/soap/php_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ int make_http_soap_request(zval *this_ptr,
if (stream != NULL) {
php_url *orig;
tmp = Z_CLIENT_HTTPURL_P(this_ptr);
if (Z_TYPE_P(tmp) == IS_RESOURCE &&
(orig = (php_url *) zend_fetch_resource_ex(tmp, "httpurl", le_url)) != NULL &&
if (Z_TYPE_P(tmp) == IS_OBJECT && instanceof_function(Z_OBJCE_P(tmp), soap_url_class_entry) &&
(orig = Z_SOAP_URL_P(tmp)->url) != NULL &&
((use_proxy && !use_ssl) ||
(((use_ssl && orig->scheme != NULL && zend_string_equals_literal(orig->scheme, "https")) ||
(!use_ssl && orig->scheme == NULL) ||
Expand Down Expand Up @@ -536,9 +536,15 @@ int make_http_soap_request(zval *this_ptr,

if (stream) {
zval *cookies, *login, *password;
zend_resource *ret = zend_register_resource(phpurl, le_url);
ZVAL_RES(Z_CLIENT_HTTPURL_P(this_ptr), ret);
GC_ADDREF(ret);

zval *url_zval = Z_CLIENT_HTTPURL_P(this_ptr);
if (Z_TYPE_P(url_zval) == IS_OBJECT) {
zval_ptr_dtor(url_zval);
}

object_init_ex(url_zval, soap_url_class_entry);
soap_url_object *url_obj = Z_SOAP_URL_P(url_zval);
url_obj->url = phpurl;

if (context &&
(tmp = php_stream_context_get_option(context, "http", "protocol_version")) != NULL &&
Expand Down
8 changes: 3 additions & 5 deletions ext/soap/php_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3449,12 +3449,10 @@ void delete_sdl_impl(void *handle)
efree(tmp);
}

void delete_sdl(void *handle)
void delete_sdl(sdl *handle)
{
sdlPtr tmp = (sdlPtr)handle;

if (!tmp->is_persistent) {
delete_sdl_impl(tmp);
if (!handle->is_persistent) {
delete_sdl_impl(handle);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/soap/php_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ encodePtr get_encoder_ex(sdlPtr sdl, const char *nscat, int len);
sdlBindingPtr get_binding_from_type(sdlPtr sdl, sdlBindingType type);
sdlBindingPtr get_binding_from_name(sdlPtr sdl, char *name, char *ns);

void delete_sdl(void *handle);
void delete_sdl(sdl *handle);
void delete_sdl_impl(void *handle);

void sdl_set_uri_credentials(sdlCtx *ctx, char *uri);
Expand Down
21 changes: 19 additions & 2 deletions ext/soap/php_soap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
# define stricmp strcasecmp
#endif

extern int le_url;

typedef struct _encodeType encodeType, *encodeTypePtr;
typedef struct _encode encode, *encodePtr;

Expand Down Expand Up @@ -194,6 +192,8 @@ ZEND_TSRMLS_CACHE_EXTERN()

extern zend_class_entry* soap_class_entry;
extern zend_class_entry* soap_var_class_entry;
extern zend_class_entry* soap_url_class_entry;
extern zend_class_entry* soap_sdl_class_entry;

void add_soap_fault(zval *obj, char *fault_code, char *fault_string, char *fault_actor, zval *fault_detail);

Expand Down Expand Up @@ -253,4 +253,21 @@ static zend_always_inline zval *php_soap_deref(zval *zv) {
#define Z_CLIENT_LAST_REQUEST_HEADERS_P(zv) php_soap_deref(OBJ_PROP_NUM(Z_OBJ_P(zv), 34))
#define Z_CLIENT_LAST_RESPONSE_HEADERS_P(zv) php_soap_deref(OBJ_PROP_NUM(Z_OBJ_P(zv), 35))

typedef struct soap_url_object {
php_url *url;
zend_object std;
} soap_url_object;

static inline soap_url_object *soap_url_object_fetch(zend_object *obj)
{
return (soap_url_object *) ((char *) obj - XtOffsetOf(soap_url_object, std));
}

#define Z_SOAP_URL_P(zv) soap_url_object_fetch(Z_OBJ_P(zv))

typedef struct soap_sdl_object {
sdl *sdl;
zend_object std;
} soap_sdl_object;

#endif
150 changes: 112 additions & 38 deletions ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#include "ext/standard/php_incomplete_class.h"


static int le_sdl = 0;
int le_url = 0;
static int le_typemap = 0;

typedef struct _soapHeader {
Expand Down Expand Up @@ -65,7 +63,6 @@ static xmlNodePtr serialize_parameter(sdlParamPtr param,zval *param_val,int inde
static xmlNodePtr serialize_zval(zval *val, sdlParamPtr param, char *paramName, int style, xmlNodePtr parent);

static void delete_service(void *service);
static void delete_url(void *handle);
static void delete_hashtable(void *hashtable);

static void soap_error_handler(int error_num, zend_string *error_filename, const uint32_t error_lineno, zend_string *message);
Expand Down Expand Up @@ -130,15 +127,13 @@ static void soap_error_handler(int error_num, zend_string *error_filename, const
#define FETCH_THIS_SDL(ss) \
{ \
zval *__tmp = Z_CLIENT_SDL_P(ZEND_THIS); \
if (Z_TYPE_P(__tmp) == IS_RESOURCE) { \
FETCH_SDL_RES(ss,__tmp); \
if (Z_TYPE_P(__tmp) == IS_OBJECT && instanceof_function(Z_OBJCE_P(__tmp), soap_sdl_class_entry)) { \
ss = Z_SOAP_SDL_P(__tmp)->sdl; \
} else { \
ss = NULL; \
} \
}

#define FETCH_SDL_RES(ss,tmp) ss = (sdlPtr) zend_fetch_resource_ex(tmp, "sdl", le_sdl)

#define FETCH_TYPEMAP_RES(ss,tmp) ss = (HashTable*) zend_fetch_resource_ex(tmp, "typemap", le_typemap)

#define Z_PARAM_NAME_P(zv) php_soap_deref(OBJ_PROP_NUM(Z_OBJ_P(zv), 0))
Expand Down Expand Up @@ -178,8 +173,12 @@ static zend_class_entry* soap_fault_class_entry;
static zend_class_entry* soap_header_class_entry;
static zend_class_entry* soap_param_class_entry;
zend_class_entry* soap_var_class_entry;
zend_class_entry *soap_url_class_entry;
zend_class_entry *soap_sdl_class_entry;

static zend_object_handlers soap_server_object_handlers;
static zend_object_handlers soap_url_object_handlers;
static zend_object_handlers soap_sdl_object_handlers;

typedef struct {
soapServicePtr service;
Expand All @@ -206,6 +205,71 @@ static void soap_server_object_free(zend_object *obj) {
zend_object_std_dtor(obj);
}

static zend_object *soap_url_object_create(zend_class_entry *ce)
{
soap_url_object *url_obj = zend_object_alloc(sizeof(soap_url_object), ce);

zend_object_std_init(&url_obj->std, ce);
object_properties_init(&url_obj->std, ce);

return &url_obj->std;
}

static void soap_url_object_free(zend_object *obj)
{
soap_url_object *url_obj = soap_url_object_fetch(obj);

if (url_obj->url) {
php_url_free(url_obj->url);
url_obj->url = NULL;
}

zend_object_std_dtor(&url_obj->std);
}

static zend_function *soap_url_object_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "Cannot directly construct Soap\\Url");

return NULL;
}

static inline soap_sdl_object *soap_sdl_object_fetch(zend_object *obj)
{
return (soap_sdl_object *) ((char *) obj - XtOffsetOf(soap_sdl_object, std));
}

#define Z_SOAP_SDL_P(zv) soap_sdl_object_fetch(Z_OBJ_P(zv))

static zend_object *soap_sdl_object_create(zend_class_entry *ce)
{
soap_sdl_object *sdl_obj = zend_object_alloc(sizeof(soap_sdl_object), ce);

zend_object_std_init(&sdl_obj->std, ce);
object_properties_init(&sdl_obj->std, ce);

return &sdl_obj->std;
}

static void soap_sdl_object_free(zend_object *obj)
{
soap_sdl_object *sdl_obj = soap_sdl_object_fetch(obj);

if (sdl_obj->sdl) {
delete_sdl(sdl_obj->sdl);
sdl_obj->sdl = NULL;
}

zend_object_std_dtor(&sdl_obj->std);
}

static zend_function *soap_sdl_object_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "Cannot directly construct Soap\\Sdl");

return NULL;
}

ZEND_DECLARE_MODULE_GLOBALS(soap)

static void (*old_error_handler)(int, zend_string *, const uint32_t, zend_string *);
Expand Down Expand Up @@ -390,16 +454,6 @@ PHP_RINIT_FUNCTION(soap)
return SUCCESS;
}

static void delete_sdl_res(zend_resource *res)
{
delete_sdl(res->ptr);
}

static void delete_url_res(zend_resource *res)
{
delete_url(res->ptr);
}

static void delete_hashtable_res(zend_resource *res)
{
delete_hashtable(res->ptr);
Expand Down Expand Up @@ -435,10 +489,30 @@ PHP_MINIT_FUNCTION(soap)

soap_header_class_entry = register_class_SoapHeader();

le_sdl = zend_register_list_destructors_ex(delete_sdl_res, NULL, "SOAP SDL", module_number);
le_url = zend_register_list_destructors_ex(delete_url_res, NULL, "SOAP URL", module_number);
le_typemap = zend_register_list_destructors_ex(delete_hashtable_res, NULL, "SOAP table", module_number);

soap_url_class_entry = register_class_Soap_Url();
soap_url_class_entry->create_object = soap_url_object_create;
soap_url_class_entry->default_object_handlers = &soap_url_object_handlers;

memcpy(&soap_url_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
soap_url_object_handlers.offset = XtOffsetOf(soap_url_object, std);
soap_url_object_handlers.free_obj = soap_url_object_free;
soap_url_object_handlers.get_constructor = soap_url_object_get_constructor;
soap_url_object_handlers.clone_obj = NULL;
soap_url_object_handlers.compare = zend_objects_not_comparable;

soap_sdl_class_entry = register_class_Soap_Sdl();
soap_sdl_class_entry->create_object = soap_sdl_object_create;
soap_sdl_class_entry->default_object_handlers = &soap_sdl_object_handlers;

memcpy(&soap_sdl_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
soap_sdl_object_handlers.offset = XtOffsetOf(soap_sdl_object, std);
soap_sdl_object_handlers.free_obj = soap_sdl_object_free;
soap_sdl_object_handlers.get_constructor = soap_sdl_object_get_constructor;
soap_sdl_object_handlers.clone_obj = NULL;
soap_sdl_object_handlers.compare = zend_objects_not_comparable;

register_soap_symbols(module_number);

old_error_handler = zend_error_cb;
Expand Down Expand Up @@ -2053,15 +2127,20 @@ PHP_METHOD(SoapClient, __construct)

if (wsdl) {
int old_soap_version;
zend_resource *res;

old_soap_version = SOAP_GLOBAL(soap_version);
SOAP_GLOBAL(soap_version) = soap_version;

sdl = get_sdl(this_ptr, ZSTR_VAL(wsdl), cache_wsdl);
res = zend_register_resource(sdl, le_sdl);

ZVAL_RES(Z_CLIENT_SDL_P(this_ptr), res);
zval *sdl_zval = Z_CLIENT_SDL_P(this_ptr);
if (Z_TYPE_P(sdl_zval) == IS_OBJECT) {
zval_ptr_dtor(sdl_zval);
}

object_init_ex(sdl_zval, soap_sdl_class_entry);
soap_sdl_object *sdl_object = Z_SOAP_SDL_P(sdl_zval);
sdl_object->sdl = sdl;

SOAP_GLOBAL(soap_version) = old_soap_version;
}
Expand Down Expand Up @@ -2194,8 +2273,11 @@ static void do_soap_call(zend_execute_data *execute_data,
}

tmp = Z_CLIENT_SDL_P(this_ptr);
if (Z_TYPE_P(tmp) == IS_RESOURCE) {
FETCH_SDL_RES(sdl,tmp);
if (Z_TYPE_P(tmp) == IS_OBJECT) {
#ifdef ZEND_DEBUG
ZEND_ASSERT(instanceof_function(Z_OBJCE_P(tmp), soap_sdl_class_entry));
#endif
sdl = Z_SOAP_SDL_P(tmp)->sdl;
}

tmp = Z_CLIENT_TYPEMAP_P(this_ptr);
Expand Down Expand Up @@ -2503,14 +2585,13 @@ PHP_METHOD(SoapClient, __soapCall)
/* {{{ Returns list of SOAP functions */
PHP_METHOD(SoapClient, __getFunctions)
{
sdlPtr sdl;

FETCH_THIS_SDL(sdl);

if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}

sdl *sdl;
FETCH_THIS_SDL(sdl);

if (sdl) {
smart_str buf = {0};
sdlFunctionPtr function;
Expand All @@ -2529,14 +2610,13 @@ PHP_METHOD(SoapClient, __getFunctions)
/* {{{ Returns list of SOAP types */
PHP_METHOD(SoapClient, __getTypes)
{
sdlPtr sdl;

FETCH_THIS_SDL(sdl);

if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}

sdl *sdl;
FETCH_THIS_SDL(sdl);

if (sdl) {
sdlTypePtr type;
smart_str buf = {0};
Expand Down Expand Up @@ -4355,12 +4435,6 @@ static void type_to_string(sdlTypePtr type, smart_str *buf, int level) /* {{{ */
}
/* }}} */

static void delete_url(void *handle) /* {{{ */
{
php_url_free((php_url*)handle);
}
/* }}} */

static void delete_service(void *data) /* {{{ */
{
soapServicePtr service = (soapServicePtr)data;
Expand Down
Loading
Loading