Skip to content

Commit aa43ed7

Browse files
Error check if target ip port is http
1 parent 38a4e57 commit aa43ed7

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Bolt.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ private function handshake(): string
102102
$this->connection->write(chr(0x60) . chr(0x60) . chr(0xb0) . chr(0x17));
103103
$this->connection->write($this->packProtocolVersions());
104104

105-
$version = $this->unpackProtocolVersion();
105+
$bytes = $this->connection->read(4);
106+
if ($bytes == 'HTTP')
107+
throw new ConnectException('Cannot to connect to Bolt service on ' . $this->connection->getIp() . ':' . $this->connection->getPort() . ' (looks like HTTP)');
108+
$version = $this->unpackProtocolVersion($bytes);
106109
if (empty($version))
107110
throw new ConnectException('Wrong version');
108111

@@ -111,13 +114,14 @@ private function handshake(): string
111114

112115
/**
113116
* Read and compose selected protocol version
117+
* @param string $bytes
114118
* @return string|null
115119
*/
116-
private function unpackProtocolVersion(): ?string
120+
private function unpackProtocolVersion(string $bytes): ?string
117121
{
118122
$result = [];
119123

120-
foreach (mb_str_split($this->connection->read(4), 1, '8bit') as $ch) {
124+
foreach (mb_str_split($bytes, 1, '8bit') as $ch) {
121125
$result[] = unpack('C', $ch)[1] ?? 0;
122126
}
123127

0 commit comments

Comments
 (0)