You are here

private static function Utility::transportTestUrl in Rocket.Chat 8.2

Will test the host for connectivity (ony t* transports are used).

Parameters

string $host:

int $port:

string $path:

Return value

string

1 call to Utility::transportTestUrl()
Utility::serverRun in src/Utility.php
ServerRun.

File

src/Utility.php, line 48
Contains \Drupal\rocket_chat\FormManager.

Class

Utility
Check the form values.

Namespace

Drupal\rocket_chat

Code

private static function transportTestUrl($host = "localhost", $port = 80, $path = "") {
  $transports = stream_get_transports();
  foreach ($transports as $index => $transport) {
    if (strtolower(substr($transport, 0, 1)) !== strtolower("t")) {
      unset($transports[$index]);
    }
  }
  $connections = [];
  $results = [];
  $meta = [];
  $working = [];
  $processed = [];
  $returnCode = [];
  $errCode = [];
  $errStr = [];
  foreach ($transports as $index => $transport) {
    $conUrl = $transport . "://" . $host;

    // . $path . "/api/info";
    try {
      $connections[$index] = fsockopen($conUrl, $port, $errCode[$index], $errStr[$index], 15);
      if ($connections[$index]) {
        $state = stream_set_blocking($connections[$index], 1);
        $bits = fwrite($connections[$index], "GET {$path}/api/info HTTP/1.1\r\nHost: {$host}\r\n\r\n");
        $meta[$index] = stream_get_meta_data($connections[$index]);
        $results[$index] = "";
        while (!($oef = feof($connections[$index]))) {
          $meta[$index] = stream_get_meta_data($connections[$index]);
          $buf = fread($connections[$index], 100);
          if ($buf !== FALSE) {
            $results[$index] = $results[$index] . $buf;
          }
          break;
        }
        $meta[$index] = stream_get_meta_data($connections[$index]);
        $sections = explode("\r\n\r\n", $results[$index]);
        foreach ($sections as $subIndex => $section) {
          $processed[$index][$subIndex] = explode("\r\n", rtrim($section));
        }
        $returnCode[$index] = explode(" ", $processed[$index][0][0])[1];
        $meta[$index] = stream_get_meta_data($connections[$index]);
      }
    } catch (Exception $exception) {
      $connections[$index] = FALSE;

      //Error?
    }
    if ($connections[$index]) {
      $working[] = $transport;
    }
    if ($connections[$index] !== FALSE) {
      fclose($connections[$index]);
    }
  }
  $selected = "";
  foreach ($returnCode as $offset => $code) {
    if ($code == 200) {
      $selected = $transports[$offset];
    }
  }
  return $selected;
}