Skip to content

Commit

Permalink
fix(client): Use shared default options for HTTP client requests
Browse files Browse the repository at this point in the history
Makes sure we always set a sane timeout and `allow_local_address`.

Fixes: #3255
Fixes: #3435
Fixes: nextcloud/server#44190

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Jul 24, 2024
1 parent 0980182 commit 9532339
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
29 changes: 17 additions & 12 deletions lib/Preview/Office.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@

use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Capabilities;
use OCA\Richdocuments\Service\RemoteOptionsService;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\Http\Client\IClientService;
use OCP\IImage;
use OCP\Image;
use OCP\Preview\IProviderV2;
use Psr\Log\LoggerInterface;

abstract class Office implements IProviderV2 {
private array $capabilities = [];

public function __construct(private IClientService $clientService, private AppConfig $config, Capabilities $capabilities, private LoggerInterface $logger) {
$this->capabilitites = $capabilities->getCapabilities()['richdocuments'] ?? [];
private array $capabilities;

public function __construct(
private IClientService $clientService,
private AppConfig $config,
Capabilities $capabilities,
private LoggerInterface $logger,
) {
$this->capabilities = $capabilities->getCapabilities()['richdocuments'] ?? [];
}

public function isAvailable(\OCP\Files\FileInfo $file): bool {
if (isset($this->capabilitites['collabora']['convert-to']['available'])) {
return (bool)$this->capabilitites['collabora']['convert-to']['available'];
public function isAvailable(FileInfo $file): bool {
if (isset($this->capabilities['collabora']['convert-to']['available'])) {
return (bool)$this->capabilities['collabora']['convert-to']['available'];
}
return false;
}
Expand All @@ -45,11 +52,9 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
}

$client = $this->clientService->newClient();
$options = [
'timeout' => 25,
// FIXME: Can be removed once https://github.com/CollaboraOnline/online/issues/6983 is fixed upstream
'expect' => false,
];
$options = RemoteOptionsService::getDefaultOptions();
// FIXME: can be removed once https://github.com/CollaboraOnline/online/issues/6983 is fixed upstream
$options['expect'] = false;

if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
$options['verify'] = false;
Expand Down
8 changes: 1 addition & 7 deletions lib/Service/CachedRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Psr\Log\LoggerInterface;

abstract class CachedRequestService {

public function __construct(
private IClientService $clientService,
private ICacheFactory $cacheFactory,
Expand Down Expand Up @@ -91,12 +90,7 @@ public function resetCache(): void {
}

protected function getDefaultRequestOptions(): array {
$options = [
'timeout' => 5,
'nextcloud' => [
'allow_local_address' => true
]
];
$options = RemoteOptionsService::getDefaultOptions();

if ($this->appConfig->getValueString('richdocuments', 'disable_certificate_verification') === 'yes') {
$options['verify'] = false;
Expand Down
17 changes: 17 additions & 0 deletions lib/Service/RemoteOptionsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace OCA\Richdocuments\Service;

class RemoteOptionsService {
public const REMOTE_TIMEOUT_DEFAULT = 5;

public static function getDefaultOptions(int $timeout = self::REMOTE_TIMEOUT_DEFAULT): array {
return [
'timeout' => $timeout,
'nextcloud' => [
'allow_local_address' => true,
]
];
}

}
13 changes: 4 additions & 9 deletions lib/Service/RemoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
use Psr\Log\LoggerInterface;

class RemoteService {

public const REMOTE_TIMEOUT_DEFAULT = 25;

public function __construct(
private AppConfig $appConfig,
private IClientService $clientService,
Expand Down Expand Up @@ -69,12 +66,10 @@ private function getRequestOptionsForFile(File $file, ?string $target = null): a
$stream = $file->fopen('rb');
}

$options = [
'timeout' => self::REMOTE_TIMEOUT_DEFAULT,
'multipart' => [
['name' => $file->getName(), 'contents' => $stream],
['name' => 'target', 'contents' => $target]
]
$options = RemoteOptionsService::getDefaultOptions(25);
$options['multipart'] = [
['name' => $file->getName(), 'contents' => $stream],
['name' => 'target', 'contents' => $target]
];

if ($this->appConfig->getDisableCertificateValidation()) {
Expand Down

0 comments on commit 9532339

Please sign in to comment.