Skip to content

Implement request #47317: SoapServer::__getLastResponse() #15792

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 1 commit 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
3 changes: 3 additions & 0 deletions ext/soap/php_soap.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ struct _soapService {
int features;
int send_errors;
struct _soapHeader **soap_headers_ptr;

bool trace;
zend_string *last_response_body;
};

#define SOAP_CLASS 1
Expand Down
24 changes: 24 additions & 0 deletions ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,11 @@ PHP_METHOD(SoapServer, __construct)
}
}

if ((tmp = zend_hash_find(ht, ZSTR_KNOWN(ZEND_STR_TRACE))) != NULL &&
(Z_TYPE_P(tmp) == IS_TRUE ||
(Z_TYPE_P(tmp) == IS_LONG && Z_LVAL_P(tmp) == 1))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you do not want to treat as string literals eventually ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the question.
In any case, this check was copied over from SoapClient so that the behaviour is consistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant considering "trace" => "true" but if this is consistent then it s fine :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. Some people may be tempted to do this, and the annoying thing is that passing wrong types in the options array does not cause an exception, the option just gets ignored in that case... however, instead of allowing "true" I rather start throwing exceptions on wrong options.

service->trace = true;
}
} else if (!wsdl) {
php_error_docref(NULL, E_ERROR, "'uri' option is required in nonWSDL mode");
}
Expand Down Expand Up @@ -1644,6 +1649,12 @@ PHP_METHOD(SoapServer, handle)
sapi_add_header(cont_len, strlen(cont_len), 1);
}
php_write(buf, size);
if (service->trace) {
if (service->last_response_body) {
zend_string_release_ex(service->last_response_body, false);
}
service->last_response_body = zend_string_init((const char *) buf, size, false);
}
xmlFree(buf);
} else {
sapi_add_header("HTTP/1.1 202 Accepted", sizeof("HTTP/1.1 202 Accepted")-1, 1);
Expand Down Expand Up @@ -1752,6 +1763,16 @@ PHP_METHOD(SoapServer, addSoapHeader)
}
/* }}} */

PHP_METHOD(SoapServer, __getLastResponse)
{
soapServicePtr service;
ZEND_PARSE_PARAMETERS_NONE();
FETCH_THIS_SERVICE_NO_BAILOUT(service);
if (service->last_response_body) {
RETURN_STR_COPY(service->last_response_body);
}
}

static void soap_server_fault_ex(sdlFunctionPtr function, zval* fault, soapHeader *hdr) /* {{{ */
{
int soap_version;
Expand Down Expand Up @@ -4533,6 +4554,9 @@ static void delete_service(soapServicePtr service) /* {{{ */
zend_hash_destroy(service->class_map);
FREE_HASHTABLE(service->class_map);
}
if (service->last_response_body) {
zend_string_release_ex(service->last_response_body, false);
}
zval_ptr_dtor(&service->soap_object);
efree(service);
}
Expand Down
2 changes: 2 additions & 0 deletions ext/soap/soap.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ public function addFunction($functions): void {}

/** @tentative-return-type */
public function handle(?string $request = null): void {}

public function __getLastResponse(): ?string {}
}

class SoapClient
Expand Down
7 changes: 6 additions & 1 deletion ext/soap/soap_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions ext/soap/tests/SoapServer/__getLastResponse.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
Request #47317 (SoapServer::__getLastResponse)
--EXTENSIONS--
soap
--INI--
soap.wsdl_cache_enabled=0
--FILE--
<?php
function f() {
}

class LocalSoapClient extends SoapClient {
public $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__."/../classmap003.wsdl", ["trace" => false]);
$client->f();
var_dump($client->__getLastResponse());
var_dump($client->server->__getLastResponse());
var_dump($client->__getLastResponse() === $client->server->__getLastResponse());

echo "---\n";

$client = new LocalSoapClient(__DIR__."/../classmap003.wsdl", ["trace" => true]);
var_dump($client->__getLastResponse());
var_dump($client->server->__getLastResponse());
var_dump($client->__getLastResponse() === $client->server->__getLastResponse());

echo "---\n";

$client->f();
echo $client->__getLastResponse(), "\n";
echo $client->server->__getLastResponse(), "\n";
var_dump($client->__getLastResponse() === $client->server->__getLastResponse());
?>
--EXPECT--
NULL
NULL
bool(true)
---
NULL
NULL
bool(true)
---
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:ab" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:fResponse><fReturn xsi:nil="true" xsi:type="ns1:A"/></ns1:fResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:ab" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:fResponse><fReturn xsi:nil="true" xsi:type="ns1:A"/></ns1:fResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

bool(true)
Loading