Skip to content

Commit 8bcfc8c

Browse files
committed
Implement request #47317: SoapServer::__getLastResponse()
Convenient for debugging. Closes GH-15792.
1 parent 5048a06 commit 8bcfc8c

File tree

7 files changed

+106
-1
lines changed

7 files changed

+106
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ PHP NEWS
5353
- SOAP:
5454
. Fixed bug #61525 (SOAP functions require at least one space after HTTP
5555
header colon). (nielsdos)
56+
. Implement request #47317 (SoapServer::__getLastResponse()). (nielsdos)
5657

5758
- Standard:
5859
. Fixed bug GH-15552 (Signed integer overflow in ext/standard/scanf.c). (cmb)

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,11 @@ PHP 8.4 UPGRADE NOTES
845845
. Added seek() method to SplObjectStorage, now it implements
846846
SeekableIterator.
847847

848+
- SOAP:
849+
. Added SoapServer::__getLastResponse(). This only tracks the last response
850+
if the trace option is set to true in the SoapServer constructor's $options
851+
argument.
852+
848853
- Standard:
849854
. Added the http_get_last_response_headers() and
850855
http_clear_last_response_headers() that allows retrieving the same content

ext/soap/php_soap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ struct _soapService {
9898
int features;
9999
int send_errors;
100100
struct _soapHeader **soap_headers_ptr;
101+
102+
bool trace;
103+
zend_string *last_response_body;
101104
};
102105

103106
#define SOAP_CLASS 1

ext/soap/soap.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,11 @@ PHP_METHOD(SoapServer, __construct)
983983
}
984984
}
985985

986+
if ((tmp = zend_hash_find(ht, ZSTR_KNOWN(ZEND_STR_TRACE))) != NULL &&
987+
(Z_TYPE_P(tmp) == IS_TRUE ||
988+
(Z_TYPE_P(tmp) == IS_LONG && Z_LVAL_P(tmp) == 1))) {
989+
service->trace = true;
990+
}
986991
} else if (!wsdl) {
987992
php_error_docref(NULL, E_ERROR, "'uri' option is required in nonWSDL mode");
988993
}
@@ -1644,6 +1649,12 @@ PHP_METHOD(SoapServer, handle)
16441649
sapi_add_header(cont_len, strlen(cont_len), 1);
16451650
}
16461651
php_write(buf, size);
1652+
if (service->trace) {
1653+
if (service->last_response_body) {
1654+
zend_string_release_ex(service->last_response_body, false);
1655+
}
1656+
service->last_response_body = zend_string_init((const char *) buf, size, false);
1657+
}
16471658
xmlFree(buf);
16481659
} else {
16491660
sapi_add_header("HTTP/1.1 202 Accepted", sizeof("HTTP/1.1 202 Accepted")-1, 1);
@@ -1752,6 +1763,16 @@ PHP_METHOD(SoapServer, addSoapHeader)
17521763
}
17531764
/* }}} */
17541765

1766+
PHP_METHOD(SoapServer, __getLastResponse)
1767+
{
1768+
soapServicePtr service;
1769+
ZEND_PARSE_PARAMETERS_NONE();
1770+
FETCH_THIS_SERVICE_NO_BAILOUT(service);
1771+
if (service->last_response_body) {
1772+
RETURN_STR_COPY(service->last_response_body);
1773+
}
1774+
}
1775+
17551776
static void soap_server_fault_ex(sdlFunctionPtr function, zval* fault, soapHeader *hdr) /* {{{ */
17561777
{
17571778
int soap_version;
@@ -4533,6 +4554,9 @@ static void delete_service(soapServicePtr service) /* {{{ */
45334554
zend_hash_destroy(service->class_map);
45344555
FREE_HASHTABLE(service->class_map);
45354556
}
4557+
if (service->last_response_body) {
4558+
zend_string_release_ex(service->last_response_body, false);
4559+
}
45364560
zval_ptr_dtor(&service->soap_object);
45374561
efree(service);
45384562
}

ext/soap/soap.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ public function addFunction($functions): void {}
527527

528528
/** @tentative-return-type */
529529
public function handle(?string $request = null): void {}
530+
531+
public function __getLastResponse(): ?string {}
530532
}
531533

532534
class SoapClient

ext/soap/soap_arginfo.h

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
Request #47317 (SoapServer::__getLastResponse)
3+
--EXTENSIONS--
4+
soap
5+
--INI--
6+
soap.wsdl_cache_enabled=0
7+
--FILE--
8+
<?php
9+
function f() {
10+
}
11+
12+
class LocalSoapClient extends SoapClient {
13+
public $server;
14+
15+
function __construct($wsdl, $options) {
16+
parent::__construct($wsdl, $options);
17+
$this->server = new SoapServer($wsdl, $options);
18+
$this->server->addFunction("f");
19+
}
20+
21+
function __doRequest($request, $location, $action, $version, $one_way = 0): string {
22+
ob_start();
23+
$this->server->handle($request);
24+
$response = ob_get_contents();
25+
ob_end_clean();
26+
return $response;
27+
}
28+
}
29+
30+
$client = new LocalSoapClient(__DIR__."/../classmap003.wsdl", ["trace" => false]);
31+
$client->f();
32+
var_dump($client->__getLastResponse());
33+
var_dump($client->server->__getLastResponse());
34+
var_dump($client->__getLastResponse() === $client->server->__getLastResponse());
35+
36+
echo "---\n";
37+
38+
$client = new LocalSoapClient(__DIR__."/../classmap003.wsdl", ["trace" => true]);
39+
var_dump($client->__getLastResponse());
40+
var_dump($client->server->__getLastResponse());
41+
var_dump($client->__getLastResponse() === $client->server->__getLastResponse());
42+
43+
echo "---\n";
44+
45+
$client->f();
46+
echo $client->__getLastResponse(), "\n";
47+
echo $client->server->__getLastResponse(), "\n";
48+
var_dump($client->__getLastResponse() === $client->server->__getLastResponse());
49+
?>
50+
--EXPECT--
51+
NULL
52+
NULL
53+
bool(true)
54+
---
55+
NULL
56+
NULL
57+
bool(true)
58+
---
59+
<?xml version="1.0" encoding="UTF-8"?>
60+
<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>
61+
62+
<?xml version="1.0" encoding="UTF-8"?>
63+
<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>
64+
65+
bool(true)

0 commit comments

Comments
 (0)