From 6ed63bdedd859f6eb4e5ea4a992867a295a200fd Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Thu, 14 Mar 2019 14:14:57 +0100 Subject: Set nextcloud user agent --- AUTHORS.md | 4 +-- lib/Config/FetcherConfig.php | 72 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 8ab801778..0d476bce6 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -5,16 +5,16 @@ * [Gregor Tätzner](mailto:gregor@freenet.de) * [Morris Jobke](mailto:hey@morrisjobke.de) * [Jan-Christoph Borchardt](mailto:hey@jancborchardt.net) -* [Daniel Schaal](mailto:daniel@schaal.email) * [Sean Molenaar](mailto:SMillerDev@users.noreply.github.com) +* [Daniel Schaal](mailto:daniel@schaal.email) * [Davide Saurino](mailto:davide.saurino@alcacoop.it) * [raghunayyar](mailto:me@iraghu.com) * [bastei](mailto:bastei@users.noreply.github.com) * [Bernhard Posselt](mailto:bep@foryouandyourcustomers.com) * [Sean Molenaar](mailto:sean@seanmolenaar.eu) * [Thomas Müller](mailto:thomas.mueller@tmit.eu) -* [Hoàng Đức Hiếu](mailto:hdhoang@zahe.me) * [Benjamin Brahmer](mailto:info@b-brahmer.de) +* [Hoàng Đức Hiếu](mailto:hdhoang@zahe.me) * [Daniel Opitz](mailto:git@copynpaste.de) * [rakekniven](mailto:mark.ziegler@rakekniven.de) * [David-Development](mailto:david-dev@live.de) diff --git a/lib/Config/FetcherConfig.php b/lib/Config/FetcherConfig.php index 1459ff032..3110b0018 100644 --- a/lib/Config/FetcherConfig.php +++ b/lib/Config/FetcherConfig.php @@ -25,11 +25,37 @@ use OCP\IConfig; */ class FetcherConfig { + /** + * Timeout before the client should abort. + * @var string + */ protected $client_timeout; + + /** + * Configuration for an HTTP proxy. + * @var string + */ protected $proxy; + + /** + * Amount of allowed redirects. + * @var string + */ protected $redirects; + + /** + * Max size of the recieved data. + * @deprecated guzzle can't handle this + * @var string + */ protected $max_size; + /** + * User agent for the client. + * @var string + */ + const DEFAULT_USER_AGENT = 'NextCloud-News/1.0'; + /** * Configure a guzzle client * @@ -38,27 +64,43 @@ class FetcherConfig public function getClient() { if (!class_exists('GuzzleHttp\Collection')) { - $config = [ - 'timeout' => $this->client_timeout, - ]; - - if (!empty($this->proxy)) { - $config['proxy'] = $this->proxy; - } - - if (!empty($this->redirects)) { - $config['redirect.max'] = $this->redirects; - } + return new FeedIoClient($this->getConfig()); + } - $guzzle = new Client($config); - $client = new FeedIoClient($guzzle); + return new LegacyGuzzleClient($this->getOldConfig()); + } + /** + * Get configuration for modern guzzle. + * @return Client Guzzle client. + */ + private function getConfig() + { + $config = [ + 'timeout' => $this->client_timeout, + 'headers' => ['User-Agent' => static::DEFAULT_USER_AGENT], + ]; - return $client; + if (!empty($this->proxy)) { + $config['proxy'] = $this->proxy; } + if (!empty($this->redirects)) { + $config['redirect.max'] = $this->redirects; + } + + $guzzle = new Client($config); + return $guzzle; + } + /** + * Get configuration for old guzzle. + * @return Client Guzzle client. + */ + private function getOldConfig() + { $config = [ 'request.options' => [ 'timeout' => $this->client_timeout, + 'headers' => ['User-Agent' => static::DEFAULT_USER_AGENT], ], ]; @@ -71,7 +113,7 @@ class FetcherConfig } $guzzle = new Client($config); - return new LegacyGuzzleClient($guzzle); + return $guzzle; } /** -- cgit v1.2.3