Skip to content

Commit d7b0de5

Browse files
authored
Use SoapServer headers
While debugging the istio error `Error dispatching received data: http/1.1 protocol error: HPE_UNEXPECTED_CONTENT_LENGTH` we noticed that the error was [triggered](https://github.com/nodejs/http-parser/blob/e13b274/http_parser.c#L1442) because of a duplicated Content-Length headers being send by the PHP application. After review we noticed that the header was send by both the SoapServer and the Symfony Response for this reason I decided to open this PR.
1 parent 605826b commit d7b0de5

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

controller/soap_web_service.rst

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ can be retrieved via ``/soap?wsdl``::
6161

6262
use App\Service\HelloService;
6363
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
64+
use Symfony\Component\HttpFoundation\Request;
6465
use Symfony\Component\HttpFoundation\Response;
6566
use Symfony\Component\Routing\Annotation\Route;
6667

@@ -69,18 +70,23 @@ can be retrieved via ``/soap?wsdl``::
6970
/**
7071
* @Route("/soap")
7172
*/
72-
public function index(HelloService $helloService)
73+
public function index(HelloService $helloService, Request $request)
7374
{
7475
$soapServer = new \SoapServer('/path/to/hello.wsdl');
7576
$soapServer->setObject($helloService);
7677

7778
$response = new Response();
78-
$response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');
7979

8080
ob_start();
81-
$soapServer->handle();
81+
$soapServer->handle($request->getContent());
8282
$response->setContent(ob_get_clean());
8383

84+
foreach (headers_list() as $header) {
85+
$header = explode(':', $header, 2);
86+
$response->headers->set($header[0], $header[1]);
87+
}
88+
header_remove();
89+
8490
return $response;
8591
}
8692
}
@@ -89,11 +95,13 @@ Take note of the calls to ``ob_start()`` and ``ob_get_clean()``. These
8995
methods control `output buffering`_ which allows you to "trap" the echoed
9096
output of ``$server->handle()``. This is necessary because Symfony expects
9197
your controller to return a ``Response`` object with the output as its "content".
92-
You must also remember to set the ``"Content-Type"`` header to ``"text/xml"``, as
93-
this is what the client will expect. So, you use ``ob_start()`` to start
94-
buffering the STDOUT and use ``ob_get_clean()`` to dump the echoed output
95-
into the content of the Response and clear the output buffer. Finally, you're
96-
ready to return the ``Response``.
98+
So, you use ``ob_start()`` to start buffering the STDOUT and use
99+
``ob_get_clean()`` to dump the echoed output into the content of the Response
100+
and clear the output buffer. Since ``$server->handle()`` can set headers it is
101+
also necessary to "trap" these. For this we use ``headers_list`` which provides
102+
the set headers, these are then parsed and added into the Response after which
103+
``header_remove`` is used to remove the headers and to avoid duplicates.
104+
Finally, you're ready to return the ``Response``.
97105

98106
Below is an example of calling the service using a native `SoapClient`_ client. This example
99107
assumes that the ``index()`` method in the controller above is accessible via

0 commit comments

Comments
 (0)