IT
apps/winzik / tests / fixtures / stream-server.php · commit 6576d6f
<?php// The test server of YouTube's streams (php -S ... tests/fixtures/stream-server.php), as googlevideo.com answers:// /videoplayback?...&range=a-b gives the bytes a..b of the stream, byte i being chr(i % 251) (so a range in the wrong// place shows); each address asked is written to the file named by the env WZ_STREAM_LOG.$path = parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH);if ($path !== '/videoplayback') {    http_response_code(404);    return true;}$log = getenv('WZ_STREAM_LOG');if (is_string($log) && $log !== '') {    file_put_contents($log, $_SERVER['REQUEST_URI'] . "\n", FILE_APPEND);}if (!preg_match('/^(\d+)-(\d+)$/', (string)($_GET['range'] ?? ''), $m) || (int)$m[2] < (int)$m[1]) {    http_response_code(403);    // a whole stream in one go: refused here (YouTube throttles it)    return true;}[$from, $to] = [(int)$m[1], (int)$m[2]];$pattern = '';for ($i = 0; $i < 251; $i++) {    $pattern .= chr($i);}$length = $to - $from + 1;header('Content-Type: application/octet-stream');header('Content-Length: ' . $length);echo substr(str_repeat($pattern, intdiv($length, 251) + 2), $from % 251, $length);return true;