Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit eaea35e

Browse files
committed
Merge branch 'hotfix/121'
Close #121
2 parents 697579b + 7786e3f commit eaea35e

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#121](https://github.com/zendframework/zend-http/pull/121) adds detection for non-numeric connection timeout values as well as
26+
integer casting to ensure the timeout is set properly in both the Curl and
27+
Socket adapters.
2628

2729
## 2.8.2 - 2018-08-13
2830

src/Client/Adapter/Curl.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ public function connect($host, $port = 80, $secure = false)
208208
} else {
209209
$connectTimeout = null;
210210
}
211+
212+
if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) {
213+
throw new AdapterException\InvalidArgumentException(sprintf(
214+
'integer or numeric string expected, got %s',
215+
gettype($connectTimeout)
216+
));
217+
}
218+
219+
if ($connectTimeout !== null) {
220+
$connectTimeout = (int) $connectTimeout;
221+
}
222+
211223
if ($connectTimeout !== null) {
212224
if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
213225
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $connectTimeout * 1000);

src/Client/Adapter/Socket.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ public function connect($host, $port = 80, $secure = false)
262262
} else {
263263
$connectTimeout = $this->config['timeout'];
264264
}
265+
266+
if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) {
267+
throw new AdapterException\InvalidArgumentException(sprintf(
268+
'integer or numeric string expected, got %s',
269+
gettype($connectTimeout)
270+
));
271+
}
272+
265273
ErrorHandler::start();
266274
$this->socket = stream_socket_client(
267275
$host . ':' . $port,

test/Client/CurlTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@ public function testConfigSetAsZendConfig()
9696
$this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
9797
}
9898

99+
public function provideValidTimeoutConfig()
100+
{
101+
return [
102+
'integer' => [10],
103+
'numeric' => ['10'],
104+
];
105+
}
106+
107+
/**
108+
* @dataProvider provideValidTimeoutConfig
109+
*/
110+
public function testPassValidTimeout($timeout)
111+
{
112+
$adapter = new Adapter\Curl();
113+
$adapter->setOptions(['timeout' => $timeout]);
114+
115+
$adapter->connect('http://framework.zend.com');
116+
}
117+
118+
public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout()
119+
{
120+
$adapter = new Adapter\Curl();
121+
$adapter->setOptions(['timeout' => 'timeout']);
122+
123+
$this->expectException(InvalidArgumentException::class);
124+
$this->expectExceptionMessage('integer or numeric string expected, got string');
125+
126+
$adapter->connect('http://framework.zend.com');
127+
}
128+
99129
/**
100130
* Check that an exception is thrown when trying to set invalid config
101131
*

test/Client/SocketTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,36 @@ public function testSetConfigInvalidConfig($config)
171171
$this->_adapter->setOptions($config);
172172
}
173173

174+
public function provideValidTimeoutConfig()
175+
{
176+
return [
177+
'integer' => [10],
178+
'numeric' => ['10'],
179+
];
180+
}
181+
182+
/**
183+
* @dataProvider provideValidTimeoutConfig
184+
*/
185+
public function testPassValidTimeout($timeout)
186+
{
187+
$adapter = new Adapter\Socket();
188+
$adapter->setOptions(['timeout' => $timeout]);
189+
190+
$adapter->connect('http://framework.zend.com');
191+
}
192+
193+
public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout()
194+
{
195+
$adapter = new Adapter\Socket();
196+
$adapter->setOptions(['timeout' => 'timeout']);
197+
198+
$this->expectException(InvalidArgumentException::class);
199+
$this->expectExceptionMessage('integer or numeric string expected, got string');
200+
201+
$adapter->connect('http://framework.zend.com');
202+
}
203+
174204
/**
175205
* Stream context related tests
176206
*/

0 commit comments

Comments
 (0)