Skip to content

ext/soap: setting xml namespace in classmap #12411

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

Closed
wants to merge 6 commits into from
Closed
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
195 changes: 107 additions & 88 deletions ext/soap/php_encoding.c

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion ext/soap/php_encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ struct _encodeType {
int type;
char *type_str;
char *ns;
zend_string *clark_notation;
sdlTypePtr sdl_type;
soapMappingPtr map;
};
Expand Down Expand Up @@ -214,7 +215,7 @@ encodePtr get_conversion(int encode);
void delete_encoder(zval *zv);
void delete_encoder_persistent(zval *zv);

extern const encode defaultEncoding[];
extern encode defaultEncoding[];
extern int numDefaultEncodings;

#endif
12 changes: 12 additions & 0 deletions ext/soap/php_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ static encodePtr create_encoder(sdlPtr sdl, sdlTypePtr cur_type, const xmlChar *
if (enc->details.type_str) {
efree(enc->details.type_str);
}
if (enc->details.clark_notation) {
zend_string_release_ex(enc->details.clark_notation, 0);
}
} else {
enc_ptr = NULL;
enc = emalloc(sizeof(encode));
Expand All @@ -73,6 +76,9 @@ static encodePtr create_encoder(sdlPtr sdl, sdlTypePtr cur_type, const xmlChar *
enc->details.ns = estrdup((char*)ns);
enc->details.type_str = estrdup((char*)type);
enc->details.sdl_type = cur_type;
if (enc->details.ns != NULL){
enc->details.clark_notation = zend_strpprintf(0, "{%s}%s", enc->details.ns, enc->details.type_str );
}
enc->to_xml = sdl_guess_convert_xml;
enc->to_zval = sdl_guess_convert_zval;

Expand Down Expand Up @@ -335,6 +341,9 @@ static int schema_simpleType(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr simpleType,
memset(cur_type->encode, 0, sizeof(encode));
cur_type->encode->details.ns = estrdup(newType->namens);
cur_type->encode->details.type_str = estrdup(newType->name);
if (cur_type->encode->details.ns){
cur_type->encode->details.clark_notation = zend_strpprintf(0, "{%s}%s", cur_type->encode->details.ns, cur_type->encode->details.type_str );
}
cur_type->encode->details.sdl_type = ptr;
cur_type->encode->to_xml = sdl_guess_convert_xml;
cur_type->encode->to_zval = sdl_guess_convert_zval;
Expand Down Expand Up @@ -1390,6 +1399,9 @@ static int schema_complexType(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr compType, s
memset(cur_type->encode, 0, sizeof(encode));
cur_type->encode->details.ns = estrdup(newType->namens);
cur_type->encode->details.type_str = estrdup(newType->name);
if (cur_type->encode->details.ns){
cur_type->encode->details.clark_notation = zend_strpprintf(0, "{%s}%s", cur_type->encode->details.ns, cur_type->encode->details.type_str );
}
cur_type->encode->details.sdl_type = ptr;
cur_type->encode->to_xml = sdl_guess_convert_xml;
cur_type->encode->to_zval = sdl_guess_convert_zval;
Expand Down
10 changes: 10 additions & 0 deletions ext/soap/php_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,15 @@ encodePtr get_encoder(sdlPtr sdl, const char *ns, const char *type)
if (sdl->is_persistent) {
new_enc->details.ns = zend_strndup(ns, ns_len);
new_enc->details.type_str = strdup(new_enc->details.type_str);
if(new_enc->details.clark_notation){
new_enc->details.clark_notation = zend_string_dup(new_enc->details.clark_notation, 1);
}
} else {
new_enc->details.ns = estrndup(ns, ns_len);
new_enc->details.type_str = estrdup(new_enc->details.type_str);
if(new_enc->details.clark_notation){
new_enc->details.clark_notation = zend_string_copy(new_enc->details.clark_notation);
}
}
if (sdl->encoders == NULL) {
sdl->encoders = pemalloc(sizeof(HashTable), sdl->is_persistent);
Expand Down Expand Up @@ -1418,6 +1424,9 @@ static void sdl_deserialize_encoder(encodePtr enc, sdlTypePtr *types, char **in)
WSDL_CACHE_GET_INT(enc->details.type, in);
enc->details.type_str = sdl_deserialize_string(in);
enc->details.ns = sdl_deserialize_string(in);
if(enc->details.ns){
enc->details.clark_notation = zend_strpprintf(0, "{%s}%s", enc->details.ns, enc->details.type_str );
}
WSDL_CACHE_GET_INT(i, in);
enc->details.sdl_type = types[i];
enc->to_xml = sdl_guess_convert_xml;
Expand Down Expand Up @@ -2833,6 +2842,7 @@ static encodePtr make_persistent_sdl_encoder(encodePtr enc, HashTable *ptr_map,
}
if (penc->details.ns) {
penc->details.ns = strdup(penc->details.ns);
penc->details.clark_notation = zend_string_dup(penc->details.clark_notation, 1);
}

if (penc->details.sdl_type) {
Expand Down
19 changes: 15 additions & 4 deletions ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ static HashTable defEnc, defEncIndex, defEncNs;
static void php_soap_prepare_globals(void)
{
int i;
const encode* enc;
encode* enc;

zend_hash_init(&defEnc, 0, NULL, NULL, 1);
zend_hash_init(&defEncIndex, 0, NULL, NULL, 1);
Expand All @@ -282,9 +282,12 @@ static void php_soap_prepare_globals(void)
/* If has a ns and a str_type then index it */
if (defaultEncoding[i].details.type_str) {
if (defaultEncoding[i].details.ns != NULL) {
char *ns_type;
spprintf(&ns_type, 0, "%s:%s", defaultEncoding[i].details.ns, defaultEncoding[i].details.type_str);
zend_hash_str_add_ptr(&defEnc, ns_type, strlen(ns_type), (void*)enc);
char *ns_type, *clark_notation;
size_t clark_notation_len = spprintf(&clark_notation, 0, "{%s}%s", enc->details.ns, enc->details.type_str);
enc->details.clark_notation = zend_string_init(clark_notation, clark_notation_len, true);
size_t ns_type_len = spprintf(&ns_type, 0, "%s:%s", enc->details.ns, enc->details.type_str);
zend_hash_str_add_ptr(&defEnc, ns_type, ns_type_len, (void*)enc);
efree(clark_notation);
efree(ns_type);
} else {
zend_hash_str_add_ptr(&defEnc, defaultEncoding[i].details.type_str, strlen(defaultEncoding[i].details.type_str), (void*)enc);
Expand Down Expand Up @@ -324,6 +327,14 @@ static void php_soap_init_globals(zend_soap_globals *soap_globals)

PHP_MSHUTDOWN_FUNCTION(soap)
{
int i;
i = 0;
do {
if(defaultEncoding[i].details.clark_notation){
zend_string_release_ex(defaultEncoding[i].details.clark_notation, 1);
}
i++;
} while (defaultEncoding[i].details.type != END_KNOWN_TYPES);
zend_error_cb = old_error_handler;
zend_hash_destroy(&SOAP_GLOBAL(defEnc));
zend_hash_destroy(&SOAP_GLOBAL(defEncIndex));
Expand Down
46 changes: 46 additions & 0 deletions ext/soap/tests/classmap005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
SOAP Classmap 5: SoapClient support for classmap with namespace
--EXTENSIONS--
soap
--INI--
soap.wsdl_cache_enabled=0
--FILE--
<?php
class TestSoapClient extends SoapClient{
function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
return <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.nothing.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body>
<ns1:dotest2Response><res xsi:type="ns1:book">
<a xsi:type="xsd:string">Blaat</a>
<b xsi:type="xsd:string">aap</b>
</res>
</ns1:dotest2Response></SOAP-ENV:Body></SOAP-ENV:Envelope>
EOF;
}
}

class bookNs{
public $a="a";
public $b="c";

}

$options=Array(
'actor' =>'http://schema.nothing.com',
'classmap' => array('{http://schemas.nothing.com}book'=>'bookNs', 'wsdltype2'=>'classname2')
);

$client = new TestSoapClient(__DIR__."/classmap.wsdl",$options);
$ret = $client->dotest2("???");
var_dump($ret);
echo "ok\n";
?>
--EXPECT--
object(bookNs)#2 (2) {
["a"]=>
string(5) "Blaat"
["b"]=>
string(3) "aap"
}
ok
60 changes: 60 additions & 0 deletions ext/soap/tests/classmap006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
SOAP Classmap 6: encoding of inherited objects with namespace
--EXTENSIONS--
soap
--FILE--
<?php
ini_set("soap.wsdl_cache_enabled",0);

class A {
public $x;
function __construct($a){
$this->x = $a;
}
}
class Attest {
public $x;
function __construct($a){
$this->x = $a;
}
}
class B extends A {
public $y;
function __construct($a){
parent::__construct($a);
$this->y = $a + 1;
}
}

function f($input){
return new B(5);
}

class LocalSoapClient extends SoapClient {
private $server;

function __construct($wsdl, $options) {
parent::__construct($wsdl, $options);
$this->server = new SoapServer($wsdl, $options);
$this->server->addFunction("f");
}

function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
ob_start();
$this->server->handle($request);
$response = ob_get_contents();
ob_end_clean();
return $response;
}
}

$client = new LocalSoapClient(__DIR__."/classmap006.wsdl",
array('classmap'=>array('A'=>'A','{urn:abt}At'=>'Attest','B'=>'B')));
print_r($client->f(new Attest('test')));
?>
--EXPECT--
B Object
(
[x] => 5
[y] => 6
)
70 changes: 70 additions & 0 deletions ext/soap/tests/classmap006.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version='1.0' encoding='UTF-8'?>

<!-- WSDL file generated by Zend Studio. -->

<definitions name="ab" targetNamespace="urn:ab" xmlns:typens="urn:ab" xmlns:typenst="urn:abt" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:ab">
<xsd:complexType name="A">
<xsd:sequence>
<xsd:element name="x" type="xsd:anyType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="B">
<xsd:complexContent>
<xsd:extension base="typens:A">
<xsd:sequence>
<xsd:element name="y" type="xsd:anyType"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:abt">
<xsd:complexType name="At">
<xsd:sequence>
<xsd:element name="x" type="xsd:anyType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Bt">
<xsd:complexContent>
<xsd:extension base="typens:A">
<xsd:sequence>
<xsd:element name="y" type="xsd:anyType"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>

</types>
<message name="f">
<part name="fInput" type="xsd:anyType"/>
</message>
<message name="fResponse">
<part name="fReturn" type="typens:A"/>
</message>
<portType name="abServerPortType">
<operation name="f">
<input message="typens:f"/>
<output message="typens:fResponse"/>
</operation>
</portType>
<binding name="abServerBinding" type="typens:abServerPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="f">
<soap:operation soapAction="urn:abServerAction"/>
<input>
<soap:body namespace="urn:ab" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body namespace="urn:ab" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="abService">
<port name="abServerPort" binding="typens:abServerBinding">
<soap:address location="http://localhost/abServer.php"/>
</port>
</service>
</definitions>
66 changes: 66 additions & 0 deletions ext/soap/tests/classmap007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
--TEST--
SOAP Classmap 7: encoding of inherited objects with namespace and cache wsdl
--EXTENSIONS--
soap
--FILE--
<?php
ini_set("soap.wsdl_cache_enabled",1);

class A {
public $x;
function __construct($a){
$this->x = $a;
}
}
class Attest {
public $x;
function __construct($a){
$this->x = $a;
}
}
class B extends A {
public $y;
function __construct($a){
parent::__construct($a);
$this->y = $a + 1;
}
}

function f($input){
return new B(5);
}

class LocalSoapClient extends SoapClient {
private $server;

function __construct($wsdl, $options) {
parent::__construct($wsdl, $options);
$this->server = new SoapServer($wsdl, $options);
$this->server->addFunction("f");
}

function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
ob_start();
$this->server->handle($request);
$response = ob_get_contents();
ob_end_clean();
return $response;
}
}

$client = new LocalSoapClient(__DIR__."/classmap007.wsdl",
array('classmap'=>array('A'=>'A','{urn:abt}At'=>'Attest','B'=>'B')));
print_r($client->f(new Attest('test')));
print_r($client->f(new Attest('test')));
?>
--EXPECT--
B Object
(
[x] => 5
[y] => 6
)
B Object
(
[x] => 5
[y] => 6
)
Loading