Skip to content

Commit

Permalink
refactor: Use Http framework where possible
Browse files Browse the repository at this point in the history
Signed-off-by: Git'Fellow <[email protected]>
  • Loading branch information
solracsf committed Dec 14, 2024
1 parent afc4b0a commit 36d6b0f
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 47 deletions.
7 changes: 4 additions & 3 deletions apps/dav/lib/CalDAV/BirthdayCalendar/EnablePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalendarHome;
use OCP\AppFramework\Http;
use OCP\IConfig;
use OCP\IUser;
use Sabre\DAV\Server;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function initialize(Server $server) {
*/
public function httpPost(RequestInterface $request, ResponseInterface $response) {
$node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
if (!($node instanceof CalendarHome)) {
if (!$node instanceof CalendarHome) {
return;
}

Expand All @@ -104,14 +105,14 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)

$owner = substr($node->getOwner(), 17);
if ($owner !== $this->user->getUID()) {
$this->server->httpResponse->setStatus(403);
$this->server->httpResponse->setStatus(Http::STATUS_FORBIDDEN);
return false;
}

$this->config->setUserValue($this->user->getUID(), 'dav', 'generateBirthdayCalendar', 'yes');
$this->birthdayService->syncUser($this->user->getUID());

$this->server->httpResponse->setStatus(204);
$this->server->httpResponse->setStatus(Http::STATUS_NO_CONTENT);

return false;
}
Expand Down
5 changes: 3 additions & 2 deletions apps/dav/lib/CalDAV/Publishing/PublishPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\Publishing\Xml\Publisher;
use OCP\AppFramework\Http;
use OCP\IConfig;
use OCP\IURLGenerator;
use Sabre\CalDAV\Xml\Property\AllowedSharingModes;
Expand Down Expand Up @@ -179,7 +180,7 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
$node->setPublishStatus(true);

// iCloud sends back the 202, so we will too.
$response->setStatus(202);
$response->setStatus(Http::STATUS_ACCEPTED);

// Adding this because sending a response body may cause issues,
// and I wanted some type of indicator the response was handled.
Expand Down Expand Up @@ -213,7 +214,7 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)

$node->setPublishStatus(false);

$response->setStatus(200);
$response->setStatus(Http::STATUS_OK);

// Adding this because sending a response body may cause issues,
// and I wanted some type of indicator the response was handled.
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/CalDAV/Search/SearchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use OCA\DAV\CalDAV\CalendarHome;
use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport;
use OCP\AppFramework\Http;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;

Expand Down Expand Up @@ -133,7 +134,7 @@ private function calendarSearch($report) {

$prefer = $this->server->getHTTPPrefer();

$this->server->httpResponse->setStatus(207);
$this->server->httpResponse->setStatus(Http::STATUS_MULTI_STATUS);
$this->server->httpResponse->setHeader('Content-Type',
'application/xml; charset=utf-8');
$this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
Expand Down
7 changes: 4 additions & 3 deletions apps/dav/lib/CardDAV/ImageExportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\DAV\CardDAV;

use OCP\AppFramework\Http;
use OCP\Files\NotFoundException;
use Sabre\CardDAV\Card;
use Sabre\DAV\Server;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function httpGet(RequestInterface $request, ResponseInterface $response)
$path = $request->getPath();
$node = $this->server->tree->getNodeForPath($path);

if (!($node instanceof Card)) {
if (!$node instanceof Card) {
return true;
}

Expand All @@ -86,11 +87,11 @@ public function httpGet(RequestInterface $request, ResponseInterface $response)
$response->setHeader('Content-Type', $file->getMimeType());
$fileName = $node->getName() . '.' . PhotoCache::ALLOWED_CONTENT_TYPES[$file->getMimeType()];
$response->setHeader('Content-Disposition', "attachment; filename=$fileName");
$response->setStatus(200);
$response->setStatus(Http::STATUS_OK);

$response->setBody($file->getContent());
} catch (NotFoundException $e) {
$response->setStatus(\OCP\AppFramework\Http::STATUS_NO_CONTENT);
$response->setStatus(Http::STATUS_NO_CONTENT);
}

return false;
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/CardDAV/MultiGetExportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace OCA\DAV\CardDAV;

use OCP\AppFramework\Http;
use Sabre\DAV;
use Sabre\DAV\Server;
use Sabre\HTTP\RequestInterface;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function httpReport(RequestInterface $request, ResponseInterface $respons
$response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
$response->setHeader('Content-Type', 'text/vcard');

$response->setStatus(200);
$response->setStatus(Http::STATUS_OK);
$response->setBody($output);

return true;
Expand Down
5 changes: 3 additions & 2 deletions apps/dav/lib/Comments/CommentsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\DAV\Comments;

use OCP\AppFramework\Http;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\Comments\MessageTooLongException;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
$response->setHeader('Content-Location', $url);

// created
$response->setStatus(201);
$response->setStatus(Http::STATUS_CREATED);
return false;
}

Expand Down Expand Up @@ -177,7 +178,7 @@ public function onReport($reportName, $report, $uri) {
new MultiStatus($responses)
);

$this->server->httpResponse->setStatus(207);
$this->server->httpResponse->setStatus(Http::STATUS_MULTI_STATUS);
$this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
$this->server->httpResponse->setBody($xml);

Expand Down
15 changes: 6 additions & 9 deletions apps/dav/lib/Connector/Sabre/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OC\User\Session;
use OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden;
use OCA\DAV\Connector\Sabre\Exception\TooManyRequests;
use OCP\AppFramework\Http;
use OCP\Defaults;
use OCP\IRequest;
use OCP\ISession;
Expand All @@ -27,20 +28,16 @@

class Auth extends AbstractBasic {
public const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND';
private Session $userSession;
private ?string $currentUser = null;
private Manager $twoFactorManager;

public function __construct(
private ISession $session,
Session $userSession,
private Session $userSession,
private IRequest $request,
Manager $twoFactorManager,
private Manager $twoFactorManager,
private IThrottler $throttler,
string $principalPrefix = 'principals/users/',
) {
$this->userSession = $userSession;
$this->twoFactorManager = $twoFactorManager;
$this->principalPrefix = $principalPrefix;

// setup realm
Expand Down Expand Up @@ -111,7 +108,7 @@ public function check(RequestInterface $request, ResponseInterface $response) {
} catch (Exception $e) {
$class = get_class($e);
$msg = $e->getMessage();
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
\OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
throw new ServiceUnavailable("$class: $msg");
}
}
Expand Down Expand Up @@ -166,7 +163,7 @@ private function auth(RequestInterface $request, ResponseInterface $response): a
if ($this->request->getMethod() === 'POST') {
$forcedLogout = true;
} else {
$response->setStatus(401);
$response->setStatus(Http::STATUS_UNAUTHORIZED);
throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.');
}
}
Expand Down Expand Up @@ -199,7 +196,7 @@ private function auth(RequestInterface $request, ResponseInterface $response): a
} elseif (in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With') ?? ''))) {
// For ajax requests use dummy auth name to prevent browser popup in case of invalid creditials
$response->addHeader('WWW-Authenticate', 'DummyBasic realm="' . $this->realm . '"');
$response->setStatus(401);
$response->setStatus(Http::STATUS_UNAUTHORIZED);
throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
}
return $data;
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Connector/Sabre/BearerAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace OCA\DAV\Connector\Sabre;

use OCP\AppFramework\Http;
use OCP\Defaults;
use OCP\IRequest;
use OCP\ISession;
Expand Down Expand Up @@ -56,6 +57,6 @@ public function validateBearerToken($bearerToken) {
* @param ResponseInterface $response
*/
public function challenge(RequestInterface $request, ResponseInterface $response): void {
$response->setStatus(401);
$response->setStatus(Http::STATUS_UNAUTHORIZED);
}
}
3 changes: 2 additions & 1 deletion apps/dav/lib/Connector/Sabre/ChecksumUpdatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\DAV\Connector\Sabre;

use OCP\AppFramework\Http;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function httpPatch(RequestInterface $request, ResponseInterface $response
$node->setChecksum($checksum);
$response->addHeader('OC-Checksum', $checksum);
$response->setHeader('Content-Length', '0');
$response->setStatus(204);
$response->setStatus(Http::STATUS_NO_CONTENT);

return false;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Connector/Sabre/DummyGetResponsePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\DAV\Connector\Sabre;

use OCP\AppFramework\Http;
use Sabre\DAV\Server;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function httpGet(RequestInterface $request, ResponseInterface $response)
fwrite($stream, $string);
rewind($stream);

$response->setStatus(200);
$response->setStatus(Http::STATUS_OK);
$response->setBody($stream);

return false;
Expand Down
12 changes: 7 additions & 5 deletions apps/dav/lib/Connector/Sabre/FakeLockerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
*/
namespace OCA\DAV\Connector\Sabre;

use OCP\AppFramework\Http;
use Sabre\DAV\INode;
use Sabre\DAV\Locks\LockInfo;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\DAV\Xml\Property\LockDiscovery;
use Sabre\DAV\Xml\Property\SupportedLock;
Expand All @@ -29,11 +31,11 @@
* @package OCA\DAV\Connector\Sabre
*/
class FakeLockerPlugin extends ServerPlugin {
/** @var \Sabre\DAV\Server */
/** @var Server */
private $server;

/** {@inheritDoc} */
public function initialize(\Sabre\DAV\Server $server) {
public function initialize(Server $server) {
$this->server = $server;
$this->server->on('method:LOCK', [$this, 'fakeLockProvider'], 1);
$this->server->on('method:UNLOCK', [$this, 'fakeUnlockProvider'], 1);
Expand Down Expand Up @@ -111,15 +113,15 @@ public function fakeLockProvider(RequestInterface $request,
$lockInfo = new LockInfo();
$lockInfo->token = md5($request->getPath());
$lockInfo->uri = $request->getPath();
$lockInfo->depth = \Sabre\DAV\Server::DEPTH_INFINITY;
$lockInfo->depth = Server::DEPTH_INFINITY;
$lockInfo->timeout = 1800;

$body = $this->server->xml->write('{DAV:}prop', [
'{DAV:}lockdiscovery' =>
new LockDiscovery([$lockInfo])
]);

$response->setStatus(200);
$response->setStatus(Http::STATUS_OK);
$response->setBody($body);

return false;
Expand All @@ -134,7 +136,7 @@ public function fakeLockProvider(RequestInterface $request,
*/
public function fakeUnlockProvider(RequestInterface $request,
ResponseInterface $response) {
$response->setStatus(204);
$response->setStatus(Http::STATUS_NO_CONTENT);
$response->setHeader('Content-Length', '0');
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Connector/Sabre/FilesReportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OC\Files\View;
use OCA\Circles\Api\v1\Circles;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
use OCP\Files\Folder;
use OCP\Files\Node as INode;
use OCP\IGroupManager;
Expand Down Expand Up @@ -183,7 +184,7 @@ public function onReport($reportName, $report, $uri) {
new MultiStatus($responses)
);

$this->server->httpResponse->setStatus(207);
$this->server->httpResponse->setStatus(Http::STATUS_MULTI_STATUS);
$this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
$this->server->httpResponse->setBody($xml);

Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/DAV/Sharing/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\DAV\Connector\Sabre\Auth;
use OCA\DAV\DAV\Sharing\Xml\Invite;
use OCA\DAV\DAV\Sharing\Xml\ShareRequest;
use OCP\AppFramework\Http;
use OCP\IConfig;
use OCP\IRequest;
use Sabre\DAV\Exception\NotFound;
Expand Down Expand Up @@ -157,7 +158,7 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)

$node->updateShares($message->set, $message->remove);

$response->setStatus(200);
$response->setStatus(Http::STATUS_OK);
// Adding this because sending a response body may cause issues,
// and I wanted some type of indicator the response was handled.
$response->setHeader('X-Sabre-Status', 'everything-went-well');
Expand Down
11 changes: 4 additions & 7 deletions apps/dav/lib/Provisioning/Apple/AppleProvisioningPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace OCA\DAV\Provisioning\Apple;

use OCP\AppFramework\Http;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
Expand Down Expand Up @@ -66,7 +67,7 @@ public function httpGet(RequestInterface $request, ResponseInterface $response):
$useSSL = ($serverProtocol === 'https');

if (!$useSSL) {
$response->setStatus(200);
$response->setStatus(Http::STATUS_OK);
$response->setHeader('Content-Type', 'text/plain; charset=utf-8');
$response->setBody($this->l10n->t('Your %s needs to be configured to use HTTPS in order to use CalDAV and CardDAV with iOS/macOS.', [$this->themingDefaults->getName()]));

Expand All @@ -75,11 +76,7 @@ public function httpGet(RequestInterface $request, ResponseInterface $response):

$absoluteURL = $this->urlGenerator->getBaseUrl();
$parsedUrl = parse_url($absoluteURL);
if (isset($parsedUrl['port'])) {
$serverPort = $parsedUrl['port'];
} else {
$serverPort = 443;
}
$serverPort = $parsedUrl['port'] ?? 443;
$server_url = $parsedUrl['host'];

$description = $this->themingDefaults->getName();
Expand Down Expand Up @@ -128,7 +125,7 @@ public function httpGet(RequestInterface $request, ResponseInterface $response):
]
));

$response->setStatus(200);
$response->setStatus(Http::STATUS_OK);
$response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
$response->setHeader('Content-Type', 'application/xml; charset=utf-8');
$response->setBody($body);
Expand Down
Loading

0 comments on commit 36d6b0f

Please sign in to comment.