Skip to content

Commit 5cad1d6

Browse files
author
Vincent Petry
authored
Merge pull request #28523 from karakayasemi/dispathEventFor404
dispatch event for 404 errors
2 parents ae2451c + c1cf05a commit 5cad1d6

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

lib/base.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,11 @@ public static function handleRequest() {
937937
throw $e;
938938
} catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
939939
//header('HTTP/1.0 404 Not Found');
940+
$dispatcher = \OC::$server->getEventDispatcher();
941+
$dispatcher->dispatch(\OCP\Http\HttpEvents::EVENT_404, new OCP\Http\HttpEvents(
942+
\OCP\Http\HttpEvents::EVENT_404,
943+
OC::$server->getRequest()
944+
));
940945
} catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
941946
OC_Response::setStatus(405);
942947
return;

lib/public/Http/HttpEvents.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* @author Semih Serhat Karakaya <karakayasemi@itu.edu.tr>
4+
*
5+
* @copyright Copyright (c) 2017, ownCloud GmbH
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
22+
namespace OCP\Http;
23+
24+
use OCP\IRequest;
25+
use Symfony\Component\EventDispatcher\Event;
26+
27+
/**
28+
* @since 10.0.3
29+
*/
30+
class HttpEvents extends Event {
31+
/** @var string */
32+
const EVENT_404 = 'resource.not_found';
33+
34+
/** @var string */
35+
protected $event;
36+
/** @var IRequest */
37+
protected $request;
38+
39+
/**
40+
* @param string $event
41+
* @param IRequest $request
42+
* @since 10.0.3
43+
*/
44+
public function __construct($event, $request) {
45+
$this->event = $event;
46+
$this->request = $request;
47+
}
48+
49+
/**
50+
* @return string
51+
* @since 10.0.3
52+
*/
53+
public function getEvent() {
54+
return $this->event;
55+
}
56+
57+
58+
/**
59+
* @return IRequest
60+
* @since 10.0.3
61+
*/
62+
public function getRequest() {
63+
return $this->request;
64+
}
65+
}

ocs/v1.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @author Thomas Müller <thomas.mueller@tmit.eu>
1212
* @author Tom Needham <tom@owncloud.com>
1313
* @author Vincent Petry <pvince81@owncloud.com>
14+
* @author Semih Serhat Karakaya <karakayasemi@itu.edu.tr>
1415
*
1516
* @copyright Copyright (c) 2017, ownCloud GmbH
1617
* @license AGPL-3.0
@@ -34,6 +35,7 @@
3435
use OC\User\LoginException;
3536
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
3637
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
38+
use OCP\Http\HttpEvents;
3739

3840
require_once __DIR__ . '/../lib/base.php';
3941

@@ -63,6 +65,11 @@
6365
OC::$server->getRouter()->match('/ocs'.\OC::$server->getRequest()->getRawPathInfo());
6466
return;
6567
} catch (ResourceNotFoundException $e) {
68+
$dispatcher = \OC::$server->getEventDispatcher();
69+
$dispatcher->dispatch(\OCP\Http\HttpEvents::EVENT_404, new OCP\Http\HttpEvents(
70+
\OCP\Http\HttpEvents::EVENT_404,
71+
OC::$server->getRequest()
72+
));
6673
// Fall through the not found
6774
} catch (MethodNotAllowedException $e) {
6875
OC_API::setContentType();
@@ -85,6 +92,11 @@
8592
} catch (LoginException $e) {
8693
OC_API::respond(new Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'Unauthorised'), OC_API::requestedFormat());
8794
} catch (ResourceNotFoundException $e) {
95+
$dispatcher = \OC::$server->getEventDispatcher();
96+
$dispatcher->dispatch(\OCP\Http\HttpEvents::EVENT_404, new OCP\Http\HttpEvents(
97+
\OCP\Http\HttpEvents::EVENT_404,
98+
OC::$server->getRequest()
99+
));
88100
OC_API::setContentType();
89101
OC_API::notFound();
90102
} catch (MethodNotAllowedException $e) {

public.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444

4545
if (!$pathInfo && $request->getParam('service', '') === '') {
4646
header('HTTP/1.0 404 Not Found');
47+
$dispatcher = \OC::$server->getEventDispatcher();
48+
$dispatcher->dispatch(\OCP\Http\HttpEvents::EVENT_404, new OCP\Http\HttpEvents(
49+
\OCP\Http\HttpEvents::EVENT_404,
50+
OC::$server->getRequest()
51+
));
4752
exit;
4853
} elseif ($request->getParam('service', '')) {
4954
$service = $request->getParam('service', '');
@@ -54,6 +59,11 @@
5459
$file = OCP\Config::getAppValue('core', 'public_' . strip_tags($service));
5560
if (is_null($file)) {
5661
header('HTTP/1.0 404 Not Found');
62+
$dispatcher = \OC::$server->getEventDispatcher();
63+
$dispatcher->dispatch(\OCP\Http\HttpEvents::EVENT_404, new OCP\Http\HttpEvents(
64+
\OCP\Http\HttpEvents::EVENT_404,
65+
OC::$server->getRequest()
66+
));
5767
exit;
5868
}
5969

remote.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ function resolveService($service) {
123123
$request = \OC::$server->getRequest();
124124
$pathInfo = $request->getPathInfo();
125125
if ($pathInfo === false || $pathInfo === '') {
126+
$dispatcher = \OC::$server->getEventDispatcher();
127+
$dispatcher->dispatch(\OCP\Http\HttpEvents::EVENT_404, new OCP\Http\HttpEvents(
128+
\OCP\Http\HttpEvents::EVENT_404,
129+
OC::$server->getRequest()
130+
));
126131
throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
127132
}
128133
if (!$pos = strpos($pathInfo, '/', 1)) {
@@ -133,6 +138,11 @@ function resolveService($service) {
133138
$file = resolveService($service);
134139

135140
if(is_null($file)) {
141+
$dispatcher = \OC::$server->getEventDispatcher();
142+
$dispatcher->dispatch(\OCP\Http\HttpEvents::EVENT_404, new OCP\Http\HttpEvents(
143+
\OCP\Http\HttpEvents::EVENT_404,
144+
OC::$server->getRequest()
145+
));
136146
throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
137147
}
138148

0 commit comments

Comments
 (0)