Skip to content

Commit

Permalink
Issue CollaboraOnline#56: Use Guzzle to fetch discovery.xml.
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Nov 12, 2024
1 parent b9953c1 commit 3d5391f
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/Cool/CoolRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Drupal\collabora_online\Exception\CoolRequestException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -32,12 +35,15 @@ class CoolRequest {
* Config factory.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
* @param \GuzzleHttp\ClientInterface $client
* Http client.
*/
public function __construct(
#[Autowire(service: 'logger.channel.collabora_online')]
protected readonly LoggerChannelInterface $logger,
protected readonly ConfigFactoryInterface $configFactory,
protected readonly Request $request,
protected readonly ClientInterface $client,
) {}

/**
Expand Down Expand Up @@ -124,24 +130,13 @@ protected function getDiscovery(): string {
}
$disable_checks = (bool) $default_config->get('cool')['disable_cert_check'];

// 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 Find an equivalent to 'verify_peer_name' for curl.
CURLOPT_SSL_VERIFYPEER => !$disable_checks,
]);
$res = curl_exec($curl);

if ($res === FALSE) {
try {
$response = $this->client->get($discovery_url, [
RequestOptions::VERIFY => !$disable_checks,
]);
$res = $response->getBody()->getContents();
}
catch (GuzzleException) {
$this->logger->error('Cannot fetch from @url.', ['@url' => $discovery_url]);
throw new CoolRequestException(
'Not able to retrieve the discovery.xml file from the Collabora Online server.',
Expand Down

0 comments on commit 3d5391f

Please sign in to comment.