connection_aborted.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. use Sabre\HTTP;
  4. include '../bootstrap.php';
  5. class DummyStream
  6. {
  7. private $position;
  8. public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
  9. {
  10. $this->position = 0;
  11. return true;
  12. }
  13. public function stream_read(int $count): string
  14. {
  15. $this->position += $count;
  16. return random_bytes($count);
  17. }
  18. public function stream_tell(): int
  19. {
  20. return $this->position;
  21. }
  22. public function stream_eof(): bool
  23. {
  24. return $this->position > 25 * 1024 * 1024;
  25. }
  26. public function stream_close(): void
  27. {
  28. file_put_contents(sys_get_temp_dir().'/dummy_stream_read_counter', $this->position);
  29. }
  30. }
  31. /*
  32. * The DummyStream wrapper has two functions:
  33. * - Provide dummy data.
  34. * - Count how many bytes have been read.
  35. */
  36. stream_wrapper_register('dummy', DummyStream::class);
  37. /*
  38. * Overwrite default connection handling.
  39. * The default behaviour is however for your script to be aborted when the remote client disconnects.
  40. *
  41. * Nextcloud/ownCloud set ignore_user_abort(true) on purpose to work around
  42. * some edge cases where the default behavior would end a script too early.
  43. *
  44. * https://github.com/owncloud/core/issues/22370
  45. * https://github.com/owncloud/core/pull/26775
  46. */
  47. ignore_user_abort(true);
  48. $body = fopen('dummy://hello', 'r');
  49. $response = new HTTP\Response();
  50. $response->setStatus(200);
  51. $response->addHeader('Content-Length', 25 * 1024 * 1024);
  52. $response->setBody($body);
  53. HTTP\Sapi::sendResponse($response);