From 455cec02f52e75c292719fe03f8f00d1d9fc15d3 Mon Sep 17 00:00:00 2001 From: Nick Dekker Date: Sun, 6 Aug 2017 12:35:20 +0200 Subject: [PATCH 1/3] Fix memory leak in Client.php Headers are no longer added every time. They used to be added regardless, which caused the headers array to grow infinitely. --- src/Client.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Client.php b/src/Client.php index 443e88f..34c5676 100644 --- a/src/Client.php +++ b/src/Client.php @@ -205,10 +205,14 @@ public function doRequest($request, $response = null) } $headers = $httpRequest->getHeaders(); - $headers->addHeaders([ - 'Content-Type: text/xml; charset=utf-8', - 'Accept: text/xml', - ]); + + if (!$headers->get('Content-Type')) { + $headers->addHeaderLine('Content-Type', 'text/xml; charset=utf-8'); + } + + if (!$headers->get('Accept')) { + $headers->addHeaderLine('Accept', 'text/xml'); + } if (!$headers->get('user-agent')) { $headers->addHeaderLine('user-agent', 'Zend_XmlRpc_Client'); From e532b8db16946a44eff9f39a67ceaaa371adf6b1 Mon Sep 17 00:00:00 2001 From: Nick Dekker Date: Sun, 6 Aug 2017 23:14:40 +0200 Subject: [PATCH 2/3] [test] Add headers check for content-type & accept [cs] Remove whitespace on empty line --- src/Client.php | 4 ++-- test/ClientTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 34c5676..23e92e3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -205,11 +205,11 @@ public function doRequest($request, $response = null) } $headers = $httpRequest->getHeaders(); - + if (!$headers->get('Content-Type')) { $headers->addHeaderLine('Content-Type', 'text/xml; charset=utf-8'); } - + if (!$headers->get('Accept')) { $headers->addHeaderLine('Accept', 'text/xml'); } diff --git a/test/ClientTest.php b/test/ClientTest.php index cf53363..c9aa4df 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -603,6 +603,36 @@ public function testCustomHttpClientUserAgentIsNotOverridden() $this->assertSame($expectedUserAgent, $this->httpClient->getHeader('user-agent')); } + public function testContentTypeAutomaticallySet() + { + $this->assertFalse( + $this->httpClient->getHeader('Content-Type'), + 'Content-Type is null if no request was made' + ); + + $expectedContentType = 'text/xml; charset=utf-8'; + $this->httpClient->setHeaders(['Content-Type' => $expectedContentType]); + + $this->setServerResponseTo(true); + $this->assertTrue($this->xmlrpcClient->call('method')); + $this->assertSame($expectedContentType, $this->httpClient->getHeader('')); + } + + public function testAcceptAutomaticallySet() + { + $this->assertFalse( + $this->httpClient->getHeader('Accept'), + 'Accept header is null if no request was made' + ); + + $expectedAccept = 'text/xml'; + $this->httpClient->setHeaders(['Accept' => $expectedAccept]); + + $this->setServerResponseTo(true); + $this->assertTrue($this->xmlrpcClient->call('method')); + $this->assertSame($expectedAccept, $this->httpClient->getHeader('')); + } + /** * @group ZF-8478 */ From f99695b1cf0bc5ba0cf8a0f482af69ac28f9efbc Mon Sep 17 00:00:00 2001 From: Nick Dekker Date: Sun, 6 Aug 2017 12:35:20 +0200 Subject: [PATCH 3/3] Fix memory leak in Client.php Headers are no longer added every time. They used to be added regardless, which caused the headers array to grow infinitely. [test] Add headers check for content-type & accept [cs] Remove whitespace on empty line --- src/Client.php | 12 ++++++++---- test/ClientTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Client.php b/src/Client.php index 443e88f..23e92e3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -205,10 +205,14 @@ public function doRequest($request, $response = null) } $headers = $httpRequest->getHeaders(); - $headers->addHeaders([ - 'Content-Type: text/xml; charset=utf-8', - 'Accept: text/xml', - ]); + + if (!$headers->get('Content-Type')) { + $headers->addHeaderLine('Content-Type', 'text/xml; charset=utf-8'); + } + + if (!$headers->get('Accept')) { + $headers->addHeaderLine('Accept', 'text/xml'); + } if (!$headers->get('user-agent')) { $headers->addHeaderLine('user-agent', 'Zend_XmlRpc_Client'); diff --git a/test/ClientTest.php b/test/ClientTest.php index cf53363..f8e311d 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -603,6 +603,36 @@ public function testCustomHttpClientUserAgentIsNotOverridden() $this->assertSame($expectedUserAgent, $this->httpClient->getHeader('user-agent')); } + public function testContentTypeAutomaticallySet() + { + $this->assertFalse( + $this->httpClient->getHeader('Content-Type'), + 'Content-Type is null if no request was made' + ); + + $expectedContentType = 'text/xml; charset=utf-8'; + $this->httpClient->setHeaders(['Content-Type' => $expectedContentType]); + + $this->setServerResponseTo(true); + $this->assertTrue($this->xmlrpcClient->call('method')); + $this->assertSame($expectedContentType, $this->httpClient->getHeader('Content-Type')); + } + + public function testAcceptAutomaticallySet() + { + $this->assertFalse( + $this->httpClient->getHeader('Accept'), + 'Accept header is null if no request was made' + ); + + $expectedAccept = 'text/xml'; + $this->httpClient->setHeaders(['Accept' => $expectedAccept]); + + $this->setServerResponseTo(true); + $this->assertTrue($this->xmlrpcClient->call('method')); + $this->assertSame($expectedAccept, $this->httpClient->getHeader('Accept')); + } + /** * @group ZF-8478 */