Skip to content

Commit 9d8bab0

Browse files
authored
Merge pull request #59141 from nextcloud/jtr/chore-EventSource-drop-legacy-IE-inline-fallback
chore(EventSource): drop no longer needed legacy fallback
2 parents 753e6ee + 6346b9c commit 9d8bab0

File tree

11 files changed

+49
-149
lines changed

11 files changed

+49
-149
lines changed

core/src/OC/eventsource.js

Lines changed: 16 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -20,88 +20,31 @@ function OCEventSource(src, data) {
2020
let joinChar
2121
this.typelessListeners = []
2222
this.closed = false
23-
this.listeners = {}
2423
if (data) {
2524
for (name in data) {
2625
dataStr += name + '=' + encodeURIComponent(data[name]) + '&'
2726
}
2827
}
2928
dataStr += 'requesttoken=' + encodeURIComponent(getRequestToken())
30-
if (!this.useFallBack && typeof EventSource !== 'undefined') {
31-
joinChar = '&'
32-
if (src.indexOf('?') === -1) {
33-
joinChar = '?'
34-
}
35-
this.source = new EventSource(src + joinChar + dataStr)
36-
this.source.onmessage = function(e) {
37-
for (let i = 0; i < this.typelessListeners.length; i++) {
38-
this.typelessListeners[i](JSON.parse(e.data))
39-
}
40-
}.bind(this)
41-
} else {
42-
const iframeId = 'oc_eventsource_iframe_' + OCEventSource.iframeCount
43-
OCEventSource.fallBackSources[OCEventSource.iframeCount] = this
44-
const iframe = document.createElement('iframe')
45-
iframe.id = iframeId
46-
iframe.style.display = 'none'
47-
48-
joinChar = '&'
49-
if (src.indexOf('?') === -1) {
50-
joinChar = '?'
51-
}
52-
iframe.src = src + joinChar + 'fallback=true&fallback_id=' + OCEventSource.iframeCount + '&' + dataStr
53-
54-
this.iframe = iframe
55-
document.body.appendChild(this.iframe)
56-
this.useFallBack = true
57-
OCEventSource.iframeCount++
29+
joinChar = '&'
30+
if (src.indexOf('?') === -1) {
31+
joinChar = '?'
5832
}
33+
this.source = new EventSource(src + joinChar + dataStr)
34+
this.source.onmessage = function(e) {
35+
for (let i = 0; i < this.typelessListeners.length; i++) {
36+
this.typelessListeners[i](JSON.parse(e.data))
37+
}
38+
}.bind(this)
5939
// add close listener
6040
this.listen('__internal__', function(data) {
6141
if (data === 'close') {
6242
this.close()
6343
}
6444
}.bind(this))
6545
}
66-
OCEventSource.fallBackSources = []
67-
OCEventSource.iframeCount = 0// number of fallback iframes
68-
OCEventSource.fallBackCallBack = function(id, type, data) {
69-
OCEventSource.fallBackSources[id].fallBackCallBack(type, data)
70-
}
7146
OCEventSource.prototype = {
7247
typelessListeners: [],
73-
iframe: null,
74-
listeners: {}, // only for fallback
75-
useFallBack: false,
76-
/**
77-
* Fallback callback for browsers that don't have the
78-
* native EventSource object.
79-
*
80-
* Calls the registered listeners.
81-
*
82-
* @private
83-
* @param {string} type event type
84-
* @param {object} data received data
85-
*/
86-
fallBackCallBack: function(type, data) {
87-
let i
88-
// ignore messages that might appear after closing
89-
if (this.closed) {
90-
return
91-
}
92-
if (type) {
93-
if (typeof this.listeners.done !== 'undefined') {
94-
for (i = 0; i < this.listeners[type].length; i++) {
95-
this.listeners[type][i](data)
96-
}
97-
}
98-
} else {
99-
for (i = 0; i < this.typelessListeners.length; i++) {
100-
this.typelessListeners[i](data)
101-
}
102-
}
103-
},
104-
lastLength: 0, // for fallback
10548
/**
10649
* Listen to a given type of events.
10750
*
@@ -111,20 +54,13 @@ OCEventSource.prototype = {
11154
listen: function(type, callback) {
11255
if (callback && callback.call) {
11356
if (type) {
114-
if (this.useFallBack) {
115-
if (!this.listeners[type]) {
116-
this.listeners[type] = []
57+
this.source.addEventListener(type, function(e) {
58+
if (typeof e.data !== 'undefined') {
59+
callback(JSON.parse(e.data))
60+
} else {
61+
callback('')
11762
}
118-
this.listeners[type].push(callback)
119-
} else {
120-
this.source.addEventListener(type, function(e) {
121-
if (typeof e.data !== 'undefined') {
122-
callback(JSON.parse(e.data))
123-
} else {
124-
callback('')
125-
}
126-
}, false)
127-
}
63+
}, false)
12864
} else {
12965
this.typelessListeners.push(callback)
13066
}
@@ -135,9 +71,7 @@ OCEventSource.prototype = {
13571
*/
13672
close: function() {
13773
this.closed = true
138-
if (typeof this.source !== 'undefined') {
139-
this.source.close()
140-
}
74+
this.source.close()
14175
},
14276
}
14377

dist/9396-9396.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/9396-9396.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-login.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-login.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-update.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-update.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/private/EventSource.php

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44
/**
5-
* SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2020-2026 Nextcloud GmbH and Nextcloud contributors
66
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
77
* SPDX-License-Identifier: AGPL-3.0-only
88
*/
@@ -12,8 +12,6 @@
1212
use OCP\IRequest;
1313

1414
class EventSource implements IEventSource {
15-
private bool $fallback = false;
16-
private int $fallBackId = 0;
1715
private bool $started = false;
1816

1917
public function __construct(
@@ -31,25 +29,7 @@ protected function init(): void {
3129
\OC_Util::obEnd();
3230
header('Cache-Control: no-cache');
3331
header('X-Accel-Buffering: no');
34-
$this->fallback = isset($_GET['fallback']) && $_GET['fallback'] == 'true';
35-
if ($this->fallback) {
36-
$this->fallBackId = (int)$_GET['fallback_id'];
37-
/**
38-
* FIXME: The default content-security-policy of ownCloud forbids inline
39-
* JavaScript for security reasons. IE starting on Windows 10 will
40-
* however also obey the CSP which will break the event source fallback.
41-
*
42-
* As a workaround thus we set a custom policy which allows the execution
43-
* of inline JavaScript.
44-
*
45-
* @link https://github.com/owncloud/core/issues/14286
46-
*/
47-
header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
48-
header('Content-Type: text/html');
49-
echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
50-
} else {
51-
header('Content-Type: text/event-stream');
52-
}
32+
header('Content-Type: text/event-stream');
5333
if (!$this->request->passesStrictCookieCheck()) {
5434
header('Location: ' . \OC::$WEBROOT);
5535
exit();
@@ -63,16 +43,10 @@ protected function init(): void {
6343
}
6444

6545
/**
66-
* send a message to the client
67-
*
68-
* @param string $type
69-
* @param mixed $data
70-
*
7146
* @throws \BadMethodCallException
72-
* if only one parameter is given, a typeless message will be send with that parameter as data
7347
* @suppress PhanDeprecatedFunction
7448
*/
75-
public function send($type, $data = null) {
49+
public function send(string $type, mixed $data = null): void {
7650
if ($data && !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
7751
throw new \BadMethodCallException('Type needs to be alphanumeric (' . $type . ')');
7852
}
@@ -81,24 +55,15 @@ public function send($type, $data = null) {
8155
$data = $type;
8256
$type = null;
8357
}
84-
if ($this->fallback) {
85-
$response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
86-
. $this->fallBackId . ',"' . ($type ?? '') . '",' . json_encode($data, JSON_HEX_TAG) . ')</script>' . PHP_EOL;
87-
echo $response;
88-
} else {
89-
if ($type) {
90-
echo 'event: ' . $type . PHP_EOL;
91-
}
92-
echo 'data: ' . json_encode($data, JSON_HEX_TAG) . PHP_EOL;
58+
if ($type) {
59+
echo 'event: ' . $type . PHP_EOL;
9360
}
61+
echo 'data: ' . json_encode($data, JSON_HEX_TAG) . PHP_EOL;
9462
echo PHP_EOL;
9563
flush();
9664
}
9765

98-
/**
99-
* close the connection of the event source
100-
*/
101-
public function close() {
66+
public function close(): void {
10267
$this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it
10368
}
10469
}

0 commit comments

Comments
 (0)