Skip to content

Commit

Permalink
Issue CollaboraOnline#56: Use curl instead of file_get_contents() to …
Browse files Browse the repository at this point in the history
…get discovery.

It can happen that file_get_contents() hangs at the end of a stream, waiting for more data.
With curl it seems this does not happen.

Even better would be to use e.g. Guzzle http client, but that's too big a change for now.
  • Loading branch information
donquixote committed Nov 12, 2024
1 parent 1a8c95c commit a4092b3
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/Cool/CoolRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@ function getDiscovery($server) {
}
$disable_checks = (bool) $default_config->get('cool')['disable_cert_check'];

$stream_context = stream_context_create([
'ssl' => [
'verify_peer' => !$disable_checks,
'verify_peer_name' => !$disable_checks,
],
// Previously, file_get_contents() was used to fetch the discovery xml data.
// Depending on the environment, it can happen that file_get_contents() will
// hang at the end of a stream, expecting more data.
// With curl, this does not happen.
// @todo Refactor this and use e.g. Guzzle http client.
$curl = curl_init($discovery_url);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => TRUE,
// Previously, when this request was done with file_get_contents() and
// stream_context_create(), the 'verify_peer' and 'verify_peer_name'
// options were set.
// @todo Check if an equivalent to 'verify_peer_name' exists for curl.
CURLOPT_SSL_VERIFYPEER => !$disable_checks,
]);
$res = file_get_contents($discovery_url, FALSE, $stream_context);
$res = curl_exec($curl);

if ($res === FALSE) {
\Drupal::logger('cool')->error('Cannot fetch from @url.', ['@url' => $discovery_url]);
}
return $res;
}

Expand Down

0 comments on commit a4092b3

Please sign in to comment.