Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
Merge branch 'fix/#27-fix-memory-leak-in-header-creation-logic' into …
Browse files Browse the repository at this point in the history
…develop

Forward port #27
  • Loading branch information
Ocramius committed Aug 11, 2017
2 parents b6182e7 + 843ea43 commit 992adb0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
22 changes: 21 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 2.6.1 - TBD
## 2.6.2 - TBD

### Added

Expand All @@ -38,6 +38,26 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 2.6.1 - 2017-08-11

### Added

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#27](https://github.com/zendframework/zend-xmlrpc/pull/19) fixed a memory leak
caused by repetitive addition of `Accept` and `Content-Type` headers on subsequent
HTTP requests produced by the `Zend\XmlRpc\Client`.

## 2.6.0 - 2016-06-21

### Added
Expand Down
23 changes: 16 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,15 @@ public function skipSystemLookup()
/**
* Perform an XML-RPC request and return a response.
*
* @param \Zend\XmlRpc\Request $request
* @param \Zend\XmlRpc\Request $request
* @param null|\Zend\XmlRpc\Response $response
* @return void
*
* @throws \Zend\Http\Exception\InvalidArgumentException
* @throws \Zend\Http\Client\Exception\RuntimeException
* @throws \Zend\XmlRpc\Client\Exception\HttpException
* @throws \Zend\XmlRpc\Exception\ValueException
*
* @return void
*/
public function doRequest($request, $response = null)
{
Expand All @@ -205,12 +210,16 @@ public function doRequest($request, $response = null)
}

$headers = $httpRequest->getHeaders();
$headers->addHeaders([
'Content-Type: text/xml; charset=utf-8',
'Accept: text/xml',
]);

if (!$headers->get('user-agent')) {
if (!$headers->has('Content-Type')) {
$headers->addHeaderLine('Content-Type', 'text/xml; charset=utf-8');
}

if (!$headers->has('Accept')) {
$headers->addHeaderLine('Accept', 'text/xml');
}

if (!$headers->has('user-agent')) {
$headers->addHeaderLine('user-agent', 'Zend_XmlRpc_Client');
}

Expand Down
36 changes: 36 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,42 @@ public function testCustomHttpClientUserAgentIsNotOverridden()
$this->assertSame($expectedUserAgent, $this->httpClient->getHeader('user-agent'));
}

/**
* @group #27
*/
public function testContentTypeIsNotReplaced()
{
$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'));
}

/**
* @group #27
*/
public function testAcceptIsNotReplaced()
{
$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
*/
Expand Down

0 comments on commit 992adb0

Please sign in to comment.