1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Thomas Müller <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2018, 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 OCA\DAV\JobStatus; |
23
|
|
|
|
24
|
|
|
use Hhxsv5\SSE\SSE; |
25
|
|
|
use Hhxsv5\SSE\Update; |
26
|
|
|
use Sabre\DAV\Server; |
27
|
|
|
use Sabre\DAV\ServerPlugin; |
28
|
|
|
use Sabre\HTTP\RequestInterface; |
29
|
|
|
use Sabre\HTTP\ResponseInterface; |
30
|
|
|
|
31
|
|
|
class EventStreamPlugin extends ServerPlugin { |
32
|
|
|
|
33
|
|
|
/** @var Server */ |
34
|
|
|
private $server; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* This initializes the plugin. |
38
|
|
|
* |
39
|
|
|
* This function is called by Sabre\DAV\Server, after |
40
|
|
|
* addPlugin is called. |
41
|
|
|
* |
42
|
|
|
* This method should set up the required event subscriptions. |
43
|
|
|
* |
44
|
|
|
* @param Server $server |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function initialize(Server $server) { |
48
|
|
|
$this->server = $server; |
49
|
|
|
$server->on('method:GET', [$this, 'httpGet'], 90); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Intercepts GET requests on addressbook urls ending with ?photo. |
54
|
|
|
* |
55
|
|
|
* @param RequestInterface $request |
56
|
|
|
* @param ResponseInterface $response |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function httpGet(RequestInterface $request, ResponseInterface $response) { |
60
|
|
|
$queryParams = $request->getQueryParameters(); |
61
|
|
|
if (!\array_key_exists('sse', $queryParams)) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$path = $request->getPath(); |
66
|
|
|
$node = $this->server->tree->getNodeForPath($path); |
67
|
|
|
|
68
|
|
|
if (!($node instanceof JobStatus)) { |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$response->setHeader('Content-Type', 'text/event-stream'); |
73
|
|
|
$response->setHeader('Connection', 'keep-alive'); |
74
|
|
|
$response->setHeader('Cache-Control', 'no-cache'); |
75
|
|
|
$response->setHeader('X-Accel-Buffering', 'no'); |
76
|
|
|
$response->setStatus(200); |
77
|
|
|
|
78
|
|
|
$this->server->sapi->sendResponse($response); |
79
|
|
|
\ob_flush(); |
80
|
|
|
\flush(); |
81
|
|
|
|
82
|
|
|
$etag = ''; |
83
|
|
|
|
84
|
|
|
(new SSE())->start(new Update(function () use ($node, &$etag) { |
85
|
|
|
$node->refreshStatus(); |
86
|
|
|
if ($node->getETag() === $etag) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
$etag = $node->getETag(); |
90
|
|
|
return $node->get(); |
91
|
|
|
}), 'job-status'); |
92
|
|
|
|
93
|
|
|
// Returning false to break the event chain |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|