Commit 923125ae authored by Carsten  Rose's avatar Carsten Rose

Refs #11076. Reports a bit more error messages.

parent 505c4e35
Pipeline #3758 passed with stages
in 4 minutes and 7 seconds
......@@ -537,12 +537,13 @@ class Link {
}
$urlParts = parse_url($param[TOKEN_WEBSOCKET]);
if (empty($urlParts['host']) || empty($urlParts['port']) || empty($urlParts['path'])) {
$urlParts = array_merge(['scheme' => 'ws', 'host' => '', 'port' => 80, 'path' => ''], $urlParts);
if (empty($urlParts['host'])) {
throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Target URL incomplete',
ERROR_MESSAGE_TO_DEVELOPER =>
'host:' . $urlParts['host'] . ', ' .
'port:' . $urlParts['port'] . ', ' .
'path:' . $urlParts['path']])
'host: ' . $urlParts['host'] . ', ' .
'port: ' . $urlParts['port'] . ', ' .
'path: ' . $urlParts['path']])
, ERROR_MISSING_VALUE);
}
......@@ -555,12 +556,13 @@ class Link {
}
// Open Socket
if (false === $websocket->connect($urlParts['host'], $urlParts['port'], $urlParts['path'])) {
throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Failed connect websocket',
$errorMsg = '';
if (false === $websocket->connect($urlParts['host'], $urlParts['port'], $urlParts['path'], '', $errorMsg)) {
throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Failed connect websocket: ' . $errorMsg,
ERROR_MESSAGE_TO_DEVELOPER =>
'host:' . $urlParts['host'] . ', ' .
'port:' . $urlParts['port'] . ', ' .
'path:' . $urlParts['path']])
'host: ' . $urlParts['host'] . ', ' .
'port: ' . $urlParts['port'] . ', ' .
'path: ' . $urlParts['path']])
, ERROR_MISSING_VALUE);
}
......
......@@ -143,9 +143,10 @@ class WebSocket {
* @param int $port
* @param string $path
* @param string $origin
* @param string $errorMsg # In case of an error, return error msg.
* @return bool
*/
public function connect(string $host, int $port, string $path, string $origin = '') {
public function connect(string $host, int $port, string $path, string $origin = '', &$errorMsg = '') {
$this->host = $host;
$this->port = $port;
$this->path = $path;
......@@ -171,12 +172,18 @@ class WebSocket {
@fwrite($this->socket, $header);
$response = @fread($this->socket, 1500);
$errorMsg = $response;
$this->connected = false;
preg_match('#Sec-WebSocket-Accept:\s(.*)$#mU', $response, $matches);
if ($matches) {
$keyAccept = trim($matches[1]);
$expectedResponse = base64_encode(pack('H*', sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$this->connected = ($keyAccept === $expectedResponse) ? true : false;
if ($keyAccept === $expectedResponse) {
$this->connected = true;
$errorMsg = '';
}
}
return $this->connected;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment