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

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 { ...@@ -537,12 +537,13 @@ class Link {
} }
$urlParts = parse_url($param[TOKEN_WEBSOCKET]); $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', throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Target URL incomplete',
ERROR_MESSAGE_TO_DEVELOPER => ERROR_MESSAGE_TO_DEVELOPER =>
'host:' . $urlParts['host'] . ', ' . 'host: ' . $urlParts['host'] . ', ' .
'port:' . $urlParts['port'] . ', ' . 'port: ' . $urlParts['port'] . ', ' .
'path:' . $urlParts['path']]) 'path: ' . $urlParts['path']])
, ERROR_MISSING_VALUE); , ERROR_MISSING_VALUE);
} }
...@@ -555,12 +556,13 @@ class Link { ...@@ -555,12 +556,13 @@ class Link {
} }
// Open Socket // Open Socket
if (false === $websocket->connect($urlParts['host'], $urlParts['port'], $urlParts['path'])) { $errorMsg = '';
throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Failed connect websocket', 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 => ERROR_MESSAGE_TO_DEVELOPER =>
'host:' . $urlParts['host'] . ', ' . 'host: ' . $urlParts['host'] . ', ' .
'port:' . $urlParts['port'] . ', ' . 'port: ' . $urlParts['port'] . ', ' .
'path:' . $urlParts['path']]) 'path: ' . $urlParts['path']])
, ERROR_MISSING_VALUE); , ERROR_MISSING_VALUE);
} }
......
...@@ -143,9 +143,10 @@ class WebSocket { ...@@ -143,9 +143,10 @@ class WebSocket {
* @param int $port * @param int $port
* @param string $path * @param string $path
* @param string $origin * @param string $origin
* @param string $errorMsg # In case of an error, return error msg.
* @return bool * @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->host = $host;
$this->port = $port; $this->port = $port;
$this->path = $path; $this->path = $path;
...@@ -171,12 +172,18 @@ class WebSocket { ...@@ -171,12 +172,18 @@ class WebSocket {
@fwrite($this->socket, $header); @fwrite($this->socket, $header);
$response = @fread($this->socket, 1500); $response = @fread($this->socket, 1500);
$errorMsg = $response;
$this->connected = false;
preg_match('#Sec-WebSocket-Accept:\s(.*)$#mU', $response, $matches); preg_match('#Sec-WebSocket-Accept:\s(.*)$#mU', $response, $matches);
if ($matches) { if ($matches) {
$keyAccept = trim($matches[1]); $keyAccept = trim($matches[1]);
$expectedResponse = base64_encode(pack('H*', sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'))); $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; return $this->connected;
......
Supports Markdown
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