Skip to content

Commit 8178edc

Browse files
committed
Consistently use EINVAL for invalid connection attempts
1 parent 6e610d5 commit 8178edc

21 files changed

+209
-50
lines changed

src/Connector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ public function connect($uri)
169169

170170
if (!isset($this->connectors[$scheme])) {
171171
return \React\Promise\reject(new \RuntimeException(
172-
'No connector available for URI scheme "' . $scheme . '"'
172+
'No connector available for URI scheme "' . $scheme . '"',
173+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
173174
));
174175
}
175176

src/DnsConnector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public function connect($uri)
2929
}
3030

3131
if (!$parts || !isset($parts['host'])) {
32-
return Promise\reject(new \InvalidArgumentException('Given URI "' . $original . '" is invalid'));
32+
return Promise\reject(new \InvalidArgumentException(
33+
'Given URI "' . $original . '" is invalid',
34+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
35+
));
3336
}
3437

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

src/FdServer.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ public function __construct($fd, LoopInterface $loop = null)
8181
$fd = (int) $m[1];
8282
}
8383
if (!\is_int($fd) || $fd < 0 || $fd >= \PHP_INT_MAX) {
84-
throw new \InvalidArgumentException('Invalid FD number given');
84+
throw new \InvalidArgumentException(
85+
'Invalid FD number given',
86+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
87+
);
8588
}
8689

8790
$this->loop = $loop ?: Loop::get();
@@ -95,7 +98,10 @@ public function __construct($fd, LoopInterface $loop = null)
9598
$errno = isset($m[1]) ? (int) $m[1] : 0;
9699
$errstr = isset($m[2]) ? $m[2] : $error['message'];
97100

98-
throw new \RuntimeException('Failed to listen on FD ' . $fd . ': ' . $errstr, $errno);
101+
throw new \RuntimeException(
102+
'Failed to listen on FD ' . $fd . ': ' . $errstr,
103+
$errno
104+
);
99105
}
100106

101107
$meta = \stream_get_meta_data($this->master);
@@ -105,7 +111,10 @@ public function __construct($fd, LoopInterface $loop = null)
105111
$errno = \defined('SOCKET_ENOTSOCK') ? \SOCKET_ENOTSOCK : 88;
106112
$errstr = \function_exists('socket_strerror') ? \socket_strerror($errno) : 'Not a socket';
107113

108-
throw new \RuntimeException('Failed to listen on FD ' . $fd . ': ' . $errstr, $errno);
114+
throw new \RuntimeException(
115+
'Failed to listen on FD ' . $fd . ': ' . $errstr,
116+
$errno
117+
);
109118
}
110119

111120
// Socket should not have a peer address if this is a listening socket.
@@ -116,7 +125,10 @@ public function __construct($fd, LoopInterface $loop = null)
116125
$errno = \defined('SOCKET_EISCONN') ? \SOCKET_EISCONN : 106;
117126
$errstr = \function_exists('socket_strerror') ? \socket_strerror($errno) : 'Socket is connected';
118127

119-
throw new \RuntimeException('Failed to listen on FD ' . $fd . ': ' . $errstr, $errno);
128+
throw new \RuntimeException(
129+
'Failed to listen on FD ' . $fd . ': ' . $errstr,
130+
$errno
131+
);
120132
}
121133

122134
// Assume this is a Unix domain socket (UDS) when its listening address doesn't parse as a valid URL with a port.

src/HappyEyeBallsConnector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ public function connect($uri)
4141
}
4242

4343
if (!$parts || !isset($parts['host'])) {
44-
return Promise\reject(new \InvalidArgumentException('Given URI "' . $original . '" is invalid'));
44+
return Promise\reject(new \InvalidArgumentException(
45+
'Given URI "' . $original . '" is invalid',
46+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
47+
));
4548
}
4649

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

src/SecureConnector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function connect($uri)
3434

3535
$parts = \parse_url($uri);
3636
if (!$parts || !isset($parts['scheme']) || $parts['scheme'] !== 'tls') {
37-
return Promise\reject(new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
37+
return Promise\reject(new \InvalidArgumentException(
38+
'Given URI "' . $uri . '" is invalid',
39+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
40+
));
3841
}
3942

4043
$context = $this->context;

src/SocketServer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public function __construct($uri, array $context = array(), LoopInterface $loop
5252
$server = new FdServer($uri, $loop);
5353
} else {
5454
if (preg_match('#^(?:\w+://)?\d+$#', $uri)) {
55-
throw new \InvalidArgumentException('Invalid URI given');
55+
throw new \InvalidArgumentException(
56+
'Invalid URI given',
57+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
58+
);
5659
}
5760

5861
$server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']);

src/TcpConnector.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ public function connect($uri)
2727

2828
$parts = \parse_url($uri);
2929
if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['port']) || $parts['scheme'] !== 'tcp') {
30-
return Promise\reject(new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
30+
return Promise\reject(new \InvalidArgumentException(
31+
'Given URI "' . $uri . '" is invalid',
32+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
33+
));
3134
}
3235

3336
$ip = \trim($parts['host'], '[]');
3437
if (false === \filter_var($ip, \FILTER_VALIDATE_IP)) {
35-
return Promise\reject(new \InvalidArgumentException('Given URI "' . $ip . '" does not contain a valid host IP'));
38+
return Promise\reject(new \InvalidArgumentException(
39+
'Given URI "' . $uri . '" does not contain a valid host IP',
40+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
41+
));
3642
}
3743

3844
// use context given in constructor
@@ -125,7 +131,10 @@ public function connect($uri)
125131
// @codeCoverageIgnoreEnd
126132

127133
\fclose($stream);
128-
$reject(new \RuntimeException('Connection to ' . $uri . ' failed: ' . $errstr, $errno));
134+
$reject(new \RuntimeException(
135+
'Connection to ' . $uri . ' failed: ' . $errstr,
136+
$errno
137+
));
129138
} else {
130139
$resolve(new Connection($stream, $loop));
131140
}

src/TcpServer.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,17 @@ public function __construct($uri, LoopInterface $loop = null, array $context = a
154154

155155
// ensure URI contains TCP scheme, host and port
156156
if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['port']) || $parts['scheme'] !== 'tcp') {
157-
throw new \InvalidArgumentException('Invalid URI "' . $uri . '" given');
157+
throw new \InvalidArgumentException(
158+
'Invalid URI "' . $uri . '" given',
159+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
160+
);
158161
}
159162

160163
if (false === \filter_var(\trim($parts['host'], '[]'), \FILTER_VALIDATE_IP)) {
161-
throw new \InvalidArgumentException('Given URI "' . $uri . '" does not contain a valid host IP');
164+
throw new \InvalidArgumentException(
165+
'Given URI "' . $uri . '" does not contain a valid host IP',
166+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
167+
);
162168
}
163169

164170
$this->master = @\stream_socket_server(
@@ -169,7 +175,10 @@ public function __construct($uri, LoopInterface $loop = null, array $context = a
169175
\stream_context_create(array('socket' => $context + array('backlog' => 511)))
170176
);
171177
if (false === $this->master) {
172-
throw new \RuntimeException('Failed to listen on "' . $uri . '": ' . $errstr, $errno);
178+
throw new \RuntimeException(
179+
'Failed to listen on "' . $uri . '": ' . $errstr,
180+
$errno
181+
);
173182
}
174183
\stream_set_blocking($this->master, false);
175184

src/UnixConnector.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ public function connect($path)
2828
if (\strpos($path, '://') === false) {
2929
$path = 'unix://' . $path;
3030
} elseif (\substr($path, 0, 7) !== 'unix://') {
31-
return Promise\reject(new \InvalidArgumentException('Given URI "' . $path . '" is invalid'));
31+
return Promise\reject(new \InvalidArgumentException(
32+
'Given URI "' . $path . '" is invalid',
33+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
34+
));
3235
}
3336

3437
$resource = @\stream_socket_client($path, $errno, $errstr, 1.0);
3538

3639
if (!$resource) {
37-
return Promise\reject(new \RuntimeException('Unable to connect to unix domain socket "' . $path . '": ' . $errstr, $errno));
40+
return Promise\reject(new \RuntimeException(
41+
'Unable to connect to unix domain socket "' . $path . '": ' . $errstr,
42+
$errno
43+
));
3844
}
3945

4046
$connection = new Connection($resource, $this->loop);

src/UnixServer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public function __construct($path, LoopInterface $loop = null, array $context =
5757
if (\strpos($path, '://') === false) {
5858
$path = 'unix://' . $path;
5959
} elseif (\substr($path, 0, 7) !== 'unix://') {
60-
throw new \InvalidArgumentException('Given URI "' . $path . '" is invalid');
60+
throw new \InvalidArgumentException(
61+
'Given URI "' . $path . '" is invalid',
62+
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
63+
);
6164
}
6265

6366
$this->master = @\stream_socket_server(
@@ -79,7 +82,10 @@ public function __construct($path, LoopInterface $loop = null, array $context =
7982
}
8083
}
8184

82-
throw new \RuntimeException('Failed to listen on Unix domain socket "' . $path . '": ' . $errstr, $errno);
85+
throw new \RuntimeException(
86+
'Failed to listen on Unix domain socket "' . $path . '": ' . $errstr,
87+
$errno
88+
);
8389
}
8490
\stream_set_blocking($this->master, 0);
8591

0 commit comments

Comments
 (0)