-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll.php
More file actions
50 lines (45 loc) · 1.92 KB
/
Copy pathpoll.php
File metadata and controls
50 lines (45 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
use NoSocket\Auth\SubscriptionSigner;
use NoSocket\Config;
use NoSocket\Http\PollService;
use NoSocket\Http\RateLimitExceeded;
use NoSocket\RateLimit\PdoRateLimiter;
use NoSocket\Store\PdoEventStore;
require dirname(__DIR__) . '/vendor/autoload.php';
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');
try {
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
http_response_code(405);
header('Allow: POST');
echo json_encode(['error' => 'NoSocket poll requires POST.']);
exit;
}
$pdo = new PDO((string) getenv('NOSOCKET_DSN'), (string) getenv('NOSOCKET_DB_USER'), (string) getenv('NOSOCKET_DB_PASSWORD'), [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$body = json_decode((string) file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR);
$token = preg_replace('/^Bearer\s+/i', '', (string) ($_SERVER['HTTP_AUTHORIZATION'] ?? ''));
$clientKey = ($_SERVER['REMOTE_ADDR'] ?? 'unknown') . ':' . hash('sha256', $token);
$service = new PollService(
new PdoEventStore($pdo),
new SubscriptionSigner((string) getenv('NOSOCKET_SECRET')),
new PdoRateLimiter($pdo),
new Config(),
);
echo json_encode($service->poll((array) ($body['subscriptions'] ?? []), $token, $clientKey), JSON_THROW_ON_ERROR);
} catch (RateLimitExceeded $exception) {
http_response_code(429);
header('Retry-After: 120');
echo json_encode(['error' => $exception->getMessage()]);
} catch (JsonException $exception) {
http_response_code(400);
echo json_encode(['error' => 'NoSocket poll requires a valid JSON body.']);
} catch (InvalidArgumentException $exception) {
http_response_code(403);
echo json_encode(['error' => $exception->getMessage()]);
} catch (Throwable $exception) {
http_response_code(500);
echo json_encode(['error' => 'NoSocket poll failed.']);
}