Skip to content

Commit 9315c32

Browse files
committed
minor #16859 Use SoapServer headers (boekkooi)
This PR was merged into the 6.1 branch. Discussion ---------- 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. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `6.x` for features of unreleased versions). --> Commits ------- 4717f04 Use SoapServer headers
2 parents a551cf7 + 4717f04 commit 9315c32

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
@@ -66,24 +66,30 @@ can be retrieved via ``/soap?wsdl``::
6666

6767
use App\Service\HelloService;
6868
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
69+
use Symfony\Component\HttpFoundation\Request;
6970
use Symfony\Component\HttpFoundation\Response;
7071
use Symfony\Component\Routing\Annotation\Route;
7172

7273
class HelloServiceController extends AbstractController
7374
{
7475
#[Route('/soap')]
75-
public function index(HelloService $helloService)
76+
public function index(HelloService $helloService, Request $request)
7677
{
7778
$soapServer = new \SoapServer('/path/to/hello.wsdl');
7879
$soapServer->setObject($helloService);
7980

8081
$response = new Response();
81-
$response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');
8282

8383
ob_start();
84-
$soapServer->handle();
84+
$soapServer->handle($request->getContent());
8585
$response->setContent(ob_get_clean());
8686

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

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

0 commit comments

Comments
 (0)