Catch guzzle error in proxy
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
This commit is contained in:
Родитель
a0d7207720
Коммит
1d2da1b2c5
|
@ -34,6 +34,8 @@ use OCP\Http\Client\IClientService;
|
|||
use OCP\IRequest;
|
||||
use OCP\ISession;
|
||||
use OCP\IURLGenerator;
|
||||
use Psr\Http\Client\ClientExceptionInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class ProxyController extends Controller {
|
||||
|
||||
|
@ -46,29 +48,21 @@ class ProxyController extends Controller {
|
|||
/** @var IClientService */
|
||||
private $clientService;
|
||||
|
||||
/** @var string */
|
||||
private $hostname;
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @param string $appName
|
||||
* @param IRequest $request
|
||||
* @param IURLGenerator $urlGenerator
|
||||
* @param ISession $session
|
||||
* @param IClientService $clientService
|
||||
* @param string $hostname
|
||||
*/
|
||||
public function __construct(string $appName,
|
||||
IRequest $request,
|
||||
IURLGenerator $urlGenerator,
|
||||
ISession $session,
|
||||
IClientService $clientService,
|
||||
$hostname) {
|
||||
LoggerInterface $logger) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->request = $request;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->session = $session;
|
||||
$this->clientService = $clientService;
|
||||
$this->hostname = $hostname;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,8 +114,14 @@ class ProxyController extends Controller {
|
|||
$this->session->close();
|
||||
|
||||
$client = $this->clientService->newClient();
|
||||
$response = $client->get($src);
|
||||
$content = $response->getBody();
|
||||
try {
|
||||
$response = $client->get($src);
|
||||
$content = $response->getBody();
|
||||
} catch (ClientExceptionInterface $e) {
|
||||
$this->logger->notice('Unable to proxy image', ['exception' => $e]);
|
||||
$content = file_get_contents(__DIR__ . '/../../img/blocked-image.png');
|
||||
}
|
||||
|
||||
return new ProxyDownloadResponse($content, $src, 'application/octet-stream');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,13 +23,14 @@
|
|||
<errorLevel type="suppress">
|
||||
<referencedClass name="OC" />
|
||||
<referencedClass name="OC\Security\CSP\ContentSecurityPolicyNonceManager" />
|
||||
<referencedClass name="Psr\Http\Client\ClientExceptionInterface" />
|
||||
</errorLevel>
|
||||
</UndefinedClass>
|
||||
<UndefinedDocblockClass>
|
||||
<errorLevel type="suppress">
|
||||
<referencedClass name="Doctrine\DBAL\Driver\Statement" />
|
||||
<referencedClass name="Doctrine\DBAL\Schema\Schema" />
|
||||
<referencedClass name="Doctrine\DBAL\Schema\SchemaException" />
|
||||
<referencedClass name="Doctrine\DBAL\Driver\Statement" />
|
||||
<referencedClass name="Doctrine\DBAL\Schema\Table" />
|
||||
<referencedClass name="OC\Security\CSP\ContentSecurityPolicyNonceManager" />
|
||||
</errorLevel>
|
||||
|
|
|
@ -33,6 +33,8 @@ use OCP\IRequest;
|
|||
use OCP\ISession;
|
||||
use OCP\IURLGenerator;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
class ProxyControllerTest extends TestCase {
|
||||
|
||||
|
@ -51,8 +53,8 @@ class ProxyControllerTest extends TestCase {
|
|||
/** @var IClientService|MockObject */
|
||||
private $clientService;
|
||||
|
||||
/** @var string */
|
||||
private $hostname;
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
||||
/** @var ProxyController */
|
||||
private $controller;
|
||||
|
@ -65,7 +67,7 @@ class ProxyControllerTest extends TestCase {
|
|||
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
||||
$this->session = $this->createMock(ISession::class);
|
||||
$this->clientService = $this->createMock(IClientService::class);
|
||||
$this->hostname = 'example.com';
|
||||
$this->logger = new NullLogger();
|
||||
}
|
||||
|
||||
public function redirectDataProvider() {
|
||||
|
@ -122,7 +124,7 @@ class ProxyControllerTest extends TestCase {
|
|||
$this->urlGenerator,
|
||||
$this->session,
|
||||
$this->clientService,
|
||||
'example.com'
|
||||
$this->logger
|
||||
);
|
||||
$expected = new TemplateResponse(
|
||||
$this->appName,
|
||||
|
@ -148,7 +150,7 @@ class ProxyControllerTest extends TestCase {
|
|||
$this->urlGenerator,
|
||||
$this->session,
|
||||
$this->clientService,
|
||||
''
|
||||
$this->logger
|
||||
);
|
||||
$this->expectException(Exception::class);
|
||||
|
||||
|
@ -165,14 +167,14 @@ class ProxyControllerTest extends TestCase {
|
|||
$client = $this->getMockBuilder(IClient::class)->getMock();
|
||||
$this->clientService->expects($this->once())
|
||||
->method('newClient')
|
||||
->will($this->returnValue($client));
|
||||
->willReturn($client);
|
||||
$client->expects($this->once())
|
||||
->method('get')
|
||||
->with($src)
|
||||
->will($this->returnValue($httpResponse));
|
||||
->willReturn($httpResponse);
|
||||
$httpResponse->expects($this->once())
|
||||
->method('getBody')
|
||||
->will($this->returnValue($content));
|
||||
->willReturn($content);
|
||||
|
||||
$expected = new ProxyDownloadResponse(
|
||||
$content,
|
||||
|
@ -185,7 +187,7 @@ class ProxyControllerTest extends TestCase {
|
|||
$this->urlGenerator,
|
||||
$this->session,
|
||||
$this->clientService,
|
||||
''
|
||||
$this->logger
|
||||
);
|
||||
|
||||
$response = $this->controller->proxy($src);
|
||||
|
|
Загрузка…
Ссылка в новой задаче