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

Open
wants to merge 7 commits into
base: 1.11
Choose a base branch
from

Conversation

vrobert78
Copy link

In order to use Chrome Headless you need to know the webSocketDebuggerUrl.
This address can be retrieved by requesting /json/version as mentioned at https://chromedevtools.github.io/devtools-protocol/.

With this PR the goal is to support both the websocket address and the HTTP endpoint.

@GrahamCampbell GrahamCampbell changed the base branch from 1.10 to 1.11 January 17, 2024 13:08
Comment on lines 110 to 111
$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


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.

$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?

@enricodias
Copy link
Member

enricodias commented Jan 17, 2024

Thanks for the contribution. I left some comments in the code.
It also wouldn't hurt checking if curl is available before attempting to use it since this lib doesn't use it anywhere else and there are some styleci issues to be resolved.

@vrobert78
Copy link
Author

Hi @enricodias, thanks a lot for your feedback. I just committed a few changes from your recommendations.


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

$resp = \file_get_contents($configURL);
Copy link
Member

@GrahamCampbell GrahamCampbell Jan 17, 2024

Choose a reason for hiding this comment

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

We can't call this without doing any error handling (yes, I really do mean "error" in the PHP 5 sense of the word).

@enricodias
Copy link
Member

Some environments will prevent file functions from accessing external urls. I could just check if curl is available and use it.

@vrobert78
Copy link
Author

Some environments will prevent file functions from accessing external urls. I could just check if curl is available and use it.

Hey @enricodias , curl is back and I've added a check for it.

Copy link
Member

@enricodias enricodias left a comment

Choose a reason for hiding this comment

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

Looks good to me now.

@@ -106,6 +106,35 @@ public function __construct($socketClient, LoggerInterface $logger = null, int $

// create socket client
if (\is_string($socketClient)) {
if (\in_array(\parse_url($socketClient, \PHP_URL_SCHEME), ['http', 'https'])) {
Copy link
Contributor

@osbre osbre May 24, 2024

Choose a reason for hiding this comment

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

Wouldn't it be better to extract all this code into a private/protected method? something like

private function retrieveWsDebuggerUrl(string $endpoint): string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants