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

Check available compression types of curl #2328

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is mostly based on [Keep a Changelog](https://keepachangelog.com/en/1
### Changed
- Drop support for Nextcloud 25, Supported: 26, 27 (#2316)
- Add a new command for occ `./occ news:updater:job` allows to check and reset the update job (#2166)
- Check for available http(s) compression options and use them (gzip, deflate, brotli) (#2328)
### Fixed

# Releases
Expand Down
25 changes: 24 additions & 1 deletion lib/Config/FetcherConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ public function __construct(IConfig $config)
return $this;
}

/**
* Checks for available encoding options
*
* @return String list of supported encoding types
*/
public function checkEncoding()
{
$supportedEncoding = [];

// check curl features
$curl_features = curl_version()["features"];

$bitfields = array('CURL_VERSION_LIBZ' => ['gzip', 'deflate'], 'CURL_VERSION_BROTLI' => ['br']);

foreach ($bitfields as $feature => $header) {
// checking available features via the 'features' bitmask and adding available types to the list
if (defined($feature) && $curl_features & constant($feature)) {
$supportedEncoding = array_merge($supportedEncoding, $header);
}
}
return implode(", ", $supportedEncoding);
}

/**
* Configure a guzzle client
*
Expand All @@ -106,7 +129,7 @@ public function getClient(): ClientInterface
'headers' => [
'User-Agent' => static::DEFAULT_USER_AGENT,
'Accept' => static::DEFAULT_ACCEPT,
'Accept-Encoding' => 'gzip, deflate',
'Accept-Encoding' => $this->checkEncoding()
],
];

Expand Down
12 changes: 10 additions & 2 deletions lib/Fetcher/FeedFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ class FeedFetcher implements IFeedFetcher
*/
private $logger;

/**
* @var FetcherConfig
*/
private $fetcherConfig;

public function __construct(
FeedIo $fetcher,
Favicon $favicon,
Scraper $scraper,
IL10N $l10n,
ITempManager $ITempManager,
Time $time,
LoggerInterface $logger
LoggerInterface $logger,
FetcherConfig $fetcherConfig
) {
$this->reader = $fetcher;
$this->faviconFactory = $favicon;
Expand All @@ -89,6 +95,7 @@ public function __construct(
$this->ITempManager = $ITempManager;
$this->time = $time;
$this->logger = $logger;
$this->fetcherConfig = $fetcherConfig;
}


Expand Down Expand Up @@ -409,7 +416,8 @@ protected function getFavicon(FeedInterface $feed, string $url): ?string
'headers' => [
'User-Agent' => FetcherConfig::DEFAULT_USER_AGENT,
'Accept' => 'image/*',
'If-Modified-Since' => date(DateTime::RFC7231, $last_modified)
'If-Modified-Since' => date(DateTime::RFC7231, $last_modified),
'Accept-Encoding' => $this->fetcherConfig->checkEncoding()
]
]
);
Expand Down
11 changes: 6 additions & 5 deletions tests/Unit/Fetcher/FeedFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
use \OCA\News\Db\Item;
use OCA\News\Scraper\Scraper;
use OCA\News\Fetcher\FeedFetcher;
use GuzzleHttp\Client;
use OCA\News\Config\FetcherConfig;

use OCA\News\Utility\Time;
use OCP\IL10N;
use OCP\ITempManager;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -108,9 +109,9 @@ class FeedFetcherTest extends TestCase
private $scraper;

/**
* @var MockObject|Client
* @var MockObject|FetcherConfig
*/
private $client;
private $fetcherConfig;

//metadata
/**
Expand Down Expand Up @@ -194,7 +195,7 @@ protected function setUp(): void
$this->scraper = $this->getMockBuilder(Scraper::class)
->disableOriginalConstructor()
->getMock();
$this->client = $this->getMockBuilder(Client::class)
$this->fetcherConfig = $this->getMockBuilder(FetcherConfig::class)
->disableOriginalConstructor()
->getMock();
$this->fetcher = new FeedFetcher(
Expand All @@ -205,7 +206,7 @@ protected function setUp(): void
$this->ITempManager,
$timeFactory,
$this->logger,
$this->client
$this->fetcherConfig
);
$this->url = 'http://tests/';

Expand Down
Loading