Skip to content

Commit d77d54a

Browse files
committed
Prepend tcp:// scheme for URIs in error messages when missing
1 parent 4e70843 commit d77d54a

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

src/DnsConnector.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@ public function __construct(ConnectorInterface $connector, ResolverInterface $re
1919

2020
public function connect($uri)
2121
{
22+
$original = $uri;
2223
if (\strpos($uri, '://') === false) {
23-
$parts = \parse_url('tcp://' . $uri);
24+
$uri = 'tcp://' . $uri;
25+
$parts = \parse_url($uri);
2426
unset($parts['scheme']);
2527
} else {
2628
$parts = \parse_url($uri);
2729
}
2830

2931
if (!$parts || !isset($parts['host'])) {
30-
return Promise\reject(new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
32+
return Promise\reject(new \InvalidArgumentException('Given URI "' . $original . '" is invalid'));
3133
}
3234

3335
$host = \trim($parts['host'], '[]');
3436
$connector = $this->connector;
3537

3638
// skip DNS lookup / URI manipulation if this URI already contains an IP
3739
if (false !== \filter_var($host, \FILTER_VALIDATE_IP)) {
38-
return $connector->connect($uri);
40+
return $connector->connect($original);
3941
}
4042

4143
$promise = $this->resolver->resolve($host);

src/HappyEyeBallsConnector.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,24 @@ public function __construct(LoopInterface $loop = null, ConnectorInterface $conn
3131

3232
public function connect($uri)
3333
{
34-
34+
$original = $uri;
3535
if (\strpos($uri, '://') === false) {
36-
$parts = \parse_url('tcp://' . $uri);
36+
$uri = 'tcp://' . $uri;
37+
$parts = \parse_url($uri);
3738
unset($parts['scheme']);
3839
} else {
3940
$parts = \parse_url($uri);
4041
}
4142

4243
if (!$parts || !isset($parts['host'])) {
43-
return Promise\reject(new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
44+
return Promise\reject(new \InvalidArgumentException('Given URI "' . $original . '" is invalid'));
4445
}
4546

4647
$host = \trim($parts['host'], '[]');
4748

4849
// skip DNS lookup / URI manipulation if this URI already contains an IP
4950
if (false !== \filter_var($host, \FILTER_VALIDATE_IP)) {
50-
return $this->connector->connect($uri);
51+
return $this->connector->connect($original);
5152
}
5253

5354
$builder = new HappyEyeBallsConnectionBuilder(

tests/DnsConnectorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function testConnectRejectsWithOriginalHostnameInMessageAfterResolvingIfT
116116
$promise = $this->connector->connect('example.com:80');
117117
$promise->cancel();
118118

119-
$this->setExpectedException('RuntimeException', 'Connection to example.com:80 failed: Connection to tcp://1.2.3.4:80 failed: Connection failed', 42);
119+
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.com:80 failed: Connection to tcp://1.2.3.4:80 failed: Connection failed', 42);
120120
$this->throwRejection($promise);
121121
}
122122

@@ -141,7 +141,7 @@ public function testSkipConnectionIfDnsFails()
141141

142142
$promise = $this->connector->connect('example.invalid:80');
143143

144-
$this->setExpectedException('RuntimeException', 'Connection to example.invalid:80 failed during DNS lookup: DNS error');
144+
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error');
145145
$this->throwRejection($promise);
146146
}
147147

@@ -167,7 +167,7 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
167167
$promise = $this->connector->connect('example.com:80');
168168
$promise->cancel();
169169

170-
$this->setExpectedException('RuntimeException', 'Connection to example.com:80 cancelled during DNS lookup');
170+
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.com:80 cancelled during DNS lookup');
171171
$this->throwRejection($promise);
172172
}
173173

tests/HappyEyeBallsConnectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function testSkipConnectionIfDnsFails()
256256
$that->throwRejection($promise);
257257
});
258258

259-
$this->setExpectedException('RuntimeException', 'Connection to example.invalid:80 failed during DNS lookup: DNS error');
259+
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error');
260260
$this->loop->run();
261261
}
262262

@@ -275,7 +275,7 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
275275
$that->throwRejection($promise);
276276
});
277277

278-
$this->setExpectedException('RuntimeException', 'Connection to example.com:80 cancelled during DNS lookup');
278+
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.com:80 cancelled during DNS lookup');
279279
$this->loop->run();
280280
}
281281

0 commit comments

Comments
 (0)