Skip to content

Commit

Permalink
refactor: migrate OC_EventSource to dependency injection
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed May 21, 2023
1 parent 96f0118 commit 77fdb97
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion core/ajax/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

$l = \OC::$server->getL10N('core');

$eventSource = \OC::$server->createEventSource();
$eventSource = \OC::$server->get(IEventSource::class);
// need to send an initial message to force-init the event source,
// which will then trigger its own CSRF check and produces its own CSRF error
// message
Expand Down
12 changes: 2 additions & 10 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,8 @@ public function __construct($webRoot, \OC\Config $config) {

$this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class);

$this->registerAlias(\OCP\IEventSource::class, \OC_EventSource::class);

$this->connectDispatcher();
}

Expand Down Expand Up @@ -1922,16 +1924,6 @@ public function getHTTPClientService() {
return $this->get(IClientService::class);
}

/**
* Create a new event source
*
* @return \OCP\IEventSource
* @deprecated 20.0.0
*/
public function createEventSource() {
return new \OC_EventSource();
}

/**
* Get the active event logger
*
Expand Down
13 changes: 11 additions & 2 deletions lib/private/legacy/OC_EventSource.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use OCP\IRequest;

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -42,6 +45,12 @@ class OC_EventSource implements \OCP\IEventSource {
*/
private $started = false;

private IRequest $request;

public function __construct(IRequest $request) {
$this->request = $request;
}

protected function init() {
if ($this->started) {
return;
Expand Down Expand Up @@ -71,11 +80,11 @@ protected function init() {
} else {
header("Content-Type: text/event-stream");
}
if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
if (!$this->request->passesStrictCookieCheck()) {
header('Location: '.\OC::$WEBROOT);
exit();
}
if (!\OC::$server->getRequest()->passesCSRFCheck()) {
if (!$this->request->passesCSRFCheck()) {
$this->send('error', 'Possible CSRF attack. Connection will be closed.');
$this->close();
exit();
Expand Down
9 changes: 0 additions & 9 deletions lib/public/IServerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,6 @@ public function getSearch();
*/
public function getCertificateManager();

/**
* Create a new event source
*
* @return \OCP\IEventSource
* @since 8.0.0
* @deprecated 20.0.0 have it injected or fetch it through \Psr\Container\ContainerInterface::get
*/
public function createEventSource();

/**
* Returns an instance of the HTTP client service
*
Expand Down

0 comments on commit 77fdb97

Please sign in to comment.