Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for http endpoint in addition to the websocket #599

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Communication/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,41 @@ public function __construct($socketClient, LoggerInterface $logger = null, int $

// create socket client
if (\is_string($socketClient)) {

$parsedUrl = parse_url($socketClient);
if (isset($parsedUrl['scheme']) && ($parsedUrl['scheme']==='http' or $parsedUrl['scheme']==='https'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expression could be simpler:

Suggested change
$parsedUrl = parse_url($socketClient);
if (isset($parsedUrl['scheme']) && ($parsedUrl['scheme']==='http' or $parsedUrl['scheme']==='https'))
if (\in_array(\parse_url($socketClient, PHP_URL_SCHEME), ['http', 'https'])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please always use strict mode, unless you have a very good reason to not do it - it's less important in modern php, but in <=7.4: https://3v4l.org/Yc3UU

{

$configURL = $socketClient.'/json/version';

$curl = curl_init($configURL);
curl_setopt($curl, CURLOPT_URL, $configURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);

if ($resp === false) {
$err = curl_error($curl);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why get the error if we are not using it for anything?

curl_close($curl);

throw new \Exception("Unable to request $configURL");
}
else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This else isn't doing anything. The throw in the previous if will stop the execution on this method.

curl_close($curl);
}

$json_resp = json_decode($resp);
if (is_null($json_resp)) {
throw new \Exception("Invalid JSON response from $configURL");
}

if (!property_exists($json_resp, 'webSocketDebuggerUrl')) {
throw new \Exception("Websocket debugger URL not found in response from $configURL");
}

$socketClient = $json_resp->webSocketDebuggerUrl;

}

$socketClient = new Wrench(new WrenchBaseClient($socketClient, 'http://127.0.0.1'), $this->logger);
} elseif (!\is_object($socketClient) && !$socketClient instanceof SocketInterface) {
throw new \InvalidArgumentException('$socketClient param should be either a SockInterface instance or a web socket uri string');
Expand Down