From a3246a927de542e1b3ab403359bfd3c08705b6a7 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Wed, 30 Jan 2019 20:36:40 +0100 Subject: Parser: Switch to feedIO for parsing instead of picoFeed --- lib/Config/Config.php | 4 +- lib/Config/FetcherConfig.php | 118 ++++++++++++++++++++++++++++++++++++ lib/Config/LegacyGuzzleClient.php | 65 ++++++++++++++++++++ lib/Config/LegacyGuzzleResponse.php | 86 ++++++++++++++++++++++++++ 4 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 lib/Config/FetcherConfig.php create mode 100644 lib/Config/LegacyGuzzleClient.php create mode 100644 lib/Config/LegacyGuzzleResponse.php (limited to 'lib/Config') diff --git a/lib/Config/Config.php b/lib/Config/Config.php index 7c5cee74a..dea1f5814 100644 --- a/lib/Config/Config.php +++ b/lib/Config/Config.php @@ -13,7 +13,7 @@ namespace OCA\News\Config; -use OCP\ILogger; +use OCA\News\Utility\PsrLogger; use OCP\Files\Folder; class Config @@ -35,7 +35,7 @@ class Config public function __construct( Folder $fileSystem, - ILogger $logger, + PsrLogger $logger, $LoggerParameters ) { $this->fileSystem = $fileSystem; diff --git a/lib/Config/FetcherConfig.php b/lib/Config/FetcherConfig.php new file mode 100644 index 000000000..55603c47c --- /dev/null +++ b/lib/Config/FetcherConfig.php @@ -0,0 +1,118 @@ + + * @author Bernhard Posselt + * @copyright 2012 Alessandro Cosentino + * @copyright 2012-2014 Bernhard Posselt + */ + +namespace OCA\News\Config; + +use FeedIo\Adapter\ClientInterface; +use \GuzzleHttp\Client; +use \FeedIo\Adapter\Guzzle\Client as FeedIoClient; + +/** + * Class FetcherConfig + * + * @package OCA\News\Config + */ +class FetcherConfig +{ + protected $client_timeout; + protected $proxy; + + /** + * Configure a guzzle client + * + * @return ClientInterface Legacy client to guzzle. + */ + public function getClient() + { + if (!class_exists('GuzzleHttp\Collection')) { + $config = [ + 'timeout' => $this->getClientTimeout(), + ]; + + if (!empty($this->proxy)) { + $config['proxy'] = $this->proxy; + } + + $guzzle = new Client(); + $client = new FeedIoClient($guzzle); + + return $client; + } + + $config = [ + 'request.options' => [ + 'timeout' => $this->getClientTimeout(), + ], + ]; + + if (!empty($this->proxy)) { + $config['request.options']['proxy'] = $this->proxy; + } + + $guzzle = new Client($config); + return new LegacyGuzzleClient($guzzle); + } + + /** + * Set a timeout for the client + * + * @param int $timeout The timeout + * + * @return self + */ + public function setClientTimeout($timeout) + { + $this->client_timeout = $timeout; + + return $this; + } + + /** + * Get the client timeout. + * + * @return mixed + */ + public function getClientTimeout() + { + return $this->client_timeout; + } + + /** + * Set the proxy + * + * @param \OCA\News\Utility\ProxyConfigParser $proxy The proxy to set. + * + * @return self + */ + public function setProxy($proxy) + { + // proxy settings + $proxySettings = $proxy->parse(); + $host = $proxySettings['host']; + $port = $proxySettings['port']; + $user = $proxySettings['user']; + $password = $proxySettings['password']; + + $proxy_string = 'https://'; + if (!empty($user)) { + $proxy_string .= $user . ':' . $password . '@'; + } + $proxy_string .= $host; + if (!empty($port)) { + $proxy_string .= ':' . $port; + } + $this->proxy = $proxy_string; + + return $this; + } +} diff --git a/lib/Config/LegacyGuzzleClient.php b/lib/Config/LegacyGuzzleClient.php new file mode 100644 index 000000000..bc1364c30 --- /dev/null +++ b/lib/Config/LegacyGuzzleClient.php @@ -0,0 +1,65 @@ + + * @copyright 2018 Sean Molenaar + */ + +namespace OCA\News\Config; + +use FeedIo\Adapter\ClientInterface as FeedIoClientInterface; +use FeedIo\Adapter\NotFoundException; +use FeedIo\Adapter\ServerErrorException; +use Guzzle\Service\ClientInterface; +use GuzzleHttp\Exception\BadResponseException; + +/** + * Guzzle dependent HTTP client + */ +class LegacyGuzzleClient implements FeedIoClientInterface +{ + /** + * @var ClientInterface + */ + protected $guzzleClient; + + /** + * @param ClientInterface $guzzleClient + */ + public function __construct(ClientInterface $guzzleClient) + { + $this->guzzleClient = $guzzleClient; + } + + /** + * @param string $url + * @param \DateTime $modifiedSince + * @throws \FeedIo\Adapter\NotFoundException + * @throws \FeedIo\Adapter\ServerErrorException + * @return \FeedIo\Adapter\ResponseInterface + */ + public function getResponse($url, \DateTime $modifiedSince) + { + try { + $options = [ + 'headers' => [ + 'User-Agent' => 'NextCloud-News/1.0', + 'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822) + ] + ]; + + return new LegacyGuzzleResponse($this->guzzleClient->get($url, $options)); + } catch (BadResponseException $e) { + switch ((int) $e->getResponse()->getStatusCode()) { + case 404: + throw new NotFoundException($e->getMessage()); + default: + throw new ServerErrorException($e->getMessage()); + } + } + } +} diff --git a/lib/Config/LegacyGuzzleResponse.php b/lib/Config/LegacyGuzzleResponse.php new file mode 100644 index 000000000..d9f6102ee --- /dev/null +++ b/lib/Config/LegacyGuzzleResponse.php @@ -0,0 +1,86 @@ + + * @copyright 2018 Sean Molenaar + */ + +namespace OCA\News\Config; + +use FeedIo\Adapter\ResponseInterface; +use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface; + +/** + * Guzzle dependent HTTP Response + */ +class LegacyGuzzleResponse implements ResponseInterface +{ + const HTTP_LAST_MODIFIED = 'Last-Modified'; + + /** + * @var \GuzzleHttp\Message\ResponseInterface + */ + protected $response; + + /** + * @param \GuzzleHttp\Message\ResponseInterface + */ + public function __construct(GuzzleResponseInterface $psrResponse) + { + $this->response = $psrResponse; + } + + /** + * @return boolean + */ + public function isModified() + { + return $this->response->getStatusCode() !== 304 && $this->response->getBody()->getSize() > 0; + } + + /** + * @return \Psr\Http\Message\StreamInterface + */ + public function getBody() + { + return $this->response->getBody(); + } + + /** + * @return \DateTime|null + */ + public function getLastModified() + { + if ($this->response->hasHeader(static::HTTP_LAST_MODIFIED)) { + $lastModified = \DateTime::createFromFormat( + \DateTime::RFC2822, + $this->getHeader(static::HTTP_LAST_MODIFIED) + ); + + return false === $lastModified ? null : $lastModified; + } + + return; + } + + /** + * @return array + */ + public function getHeaders() + { + return $this->response->getHeaders(); + } + + /** + * @param string $name + * @return string[] + */ + public function getHeader($name) + { + return $this->response->getHeader($name); + } +} -- cgit v1.2.3