public static function postInstall() { Config::makeBinDirectory(); $loop = Factory::create(); $file = Config::getBinFile(); $url = 'https://yt-dl.org/downloads/latest/youtube-dl'; $readStream = fopen($url, 'r'); $writeStream = fopen($file, 'w'); stream_set_blocking($readStream, 0); stream_set_blocking($writeStream, 0); $read = new Stream($readStream, $loop); $write = new Stream($writeStream, $loop); $read->on('end', function () use($file) { chmod($file, 0777); echo "Finished downloading {$file}\n"; }); $read->pipe($write); $loop->run(); }
/** * Handle http request * * @param HttpRequestEvent $event */ public function onRequest(HttpRequestEvent $event) { $filePath = $event->getRequest()->getPath(); foreach ($this->application->getMetadata() as $name => $metadata) { /* @var $metadata \PhpCollection\Map */ if ($metadata->containsKey('assets')) { $assumedFilePath = $metadata->get('application_path')->get() . DIRECTORY_SEPARATOR . $metadata->get('assets')->get() . DIRECTORY_SEPARATOR . $filePath; $assumedFilePath = realpath($assumedFilePath); if (file_exists($assumedFilePath) && is_file($assumedFilePath)) { $event->stopPropagation(); $fileStream = new Stream(fopen($assumedFilePath, 'r'), $this->serverLoop); $event->getResponse()->getHeaders()->set('Content-Type', MimeTypeGuesser::getMimeType($assumedFilePath)); $event->getResponse()->dispatchHeaders(); $fileStream->pipe($event->getResponse()->getReactResponse()); $fileStream->on('end', 'gc_collect_cycles'); gc_collect_cycles(); } } } }
use Clue\React\Tar\Decoder; use React\Stream\BufferedSink; use Clue\Hexdump\Hexdump; use React\EventLoop\StreamSelectLoop; require __DIR__ . '/../vendor/autoload.php'; $in = isset($argv[1]) ? $argv[1] : __DIR__ . '/../tests/fixtures/alice-bob.tar'; echo 'Reading file "' . $in . '" (pass as argument to example)' . PHP_EOL; // using the default loop does *not* work for file I/O //$loop = Factory::create(); $loop = new StreamSelectLoop(); $stream = new Stream(fopen($in, 'r'), $loop); $decoder = new Decoder(); $decoder->on('entry', function ($header, $file) { static $i = 0; echo 'FILE #' . ++$i . PHP_EOL; echo 'Received entry headers:' . PHP_EOL; var_dump($header); BufferedSink::createPromise($file)->then(function ($contents) { echo 'Received entry contents (' . strlen($contents) . ' bytes)' . PHP_EOL; $d = new Hexdump(); echo $d->dump($contents) . PHP_EOL . PHP_EOL; }); }); $decoder->on('error', function ($error) { echo 'ERROR: ' . $error . PHP_EOL; }); $decoder->on('close', function () { echo 'CLOSED' . PHP_EOL; }); $stream->pipe($decoder); $loop->run();
/** @test */ public function pipeShouldPipeDataIntoTheRequestBody() { $requestData = new RequestData('POST', 'http://www.example.com'); $request = new Request($this->loop, $this->connector, $requestData); $this->successfulConnectionMock(); $this->stream->expects($this->at(4))->method('write')->with($this->matchesRegularExpression("#^POST / HTTP/1\\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\n\$#")); $this->stream->expects($this->at(5))->method('write')->with($this->identicalTo("some")); $this->stream->expects($this->at(6))->method('write')->with($this->identicalTo("post")); $this->stream->expects($this->at(7))->method('write')->with($this->identicalTo("data")); $factory = $this->createCallableMock(); $factory->expects($this->once())->method('__invoke')->will($this->returnValue($this->response)); $request->setResponseFactory($factory); $stream = fopen('php://memory', 'r+'); $stream = new Stream($stream, $this->loop); $stream->pipe($request); $stream->emit('data', array('some')); $stream->emit('data', array('post')); $stream->emit('data', array('data')); $request->handleData("HTTP/1.0 200 OK\r\n"); $request->handleData("Content-Type: text/plain\r\n"); $request->handleData("\r\nbody"); }
public function connectTarget(Stream $stream, array $target) { $stream->emit('target', $target); $that = $this; return $this->connector->create($target[0], $target[1])->then(function (Stream $remote) use($stream, $that) { if (!$stream->isWritable()) { $remote->close(); throw new UnexpectedValueException('Remote connection successfully established after client connection closed'); } $stream->pipe($remote, array('end' => false)); $remote->pipe($stream, array('end' => false)); // remote end closes connection => stop reading from local end, try to flush buffer to local and disconnect local $remote->on('end', function () use($stream, $that) { $stream->emit('shutdown', array('remote', null)); $that->endConnection($stream); }); // local end closes connection => stop reading from remote end, try to flush buffer to remote and disconnect remote $stream->on('end', function () use($remote, $that) { $that->endConnection($remote); }); // set bigger buffer size of 100k to improve performance $stream->bufferSize = $remote->bufferSize = 100 * 1024 * 1024; return $remote; }, function (Exception $error) { throw new UnexpectedValueException('Unable to connect to remote target', 0, $error); }); }
/** * Processing one URL, opens pair of read-write streams and piping them * * @param AMQPClosure $closure A closure of message with another URL * @param EventLoop\LoopInterface $loop A loop, worker is run on * * @return bool Success flag * * @throws \Exception */ private function processURL(AMQPClosure $closure, EventLoop\LoopInterface $loop) { $readStream = @fopen($closure->get(), 'r'); if (!$readStream) { return $this->error($closure); } $tmpdir = \Bot::config('tmpdir'); if (!file_exists($tmpdir)) { if (!mkdir($tmpdir, 0777, true)) { throw new \Exception('Cannot create downloading dirname ' . $tmpdir); } } $fWritePath = $tmpdir . "/" . md5($closure->get()); $writeStream = fopen($fWritePath, 'w'); if (!$writeStream) { throw new \Exception('Cannot open downloading file ' . $fWritePath); } $this->files[$fWritePath] = true; $read = new Stream($readStream, $loop); $write = new Stream($writeStream, $loop); $write->on('end', function () use($closure, $fWritePath) { unset($this->files[$fWritePath]); $this->success($closure); }); $read->on('error', function () use($closure, $fWritePath) { unset($this->files[$fWritePath]); $this->error($closure); }); $read->pipe($write); }
use React\EventLoop; use React\Stream\Stream; const KEY = 'GLADOS'; $loop = EventLoop\Factory::create(); // create stdin/stdout streams $in = new Stream(STDIN, $loop); $out = new Stream(STDOUT, $loop); $err = new Stream(STDERR, $loop); $accessLog = new Stream(fopen('access.log', 'a'), $loop); $debugLog = new Stream(fopen('debug.log', 'a'), $loop); $errorLog = new Stream(fopen('error.log', 'a'), $loop); // create encryption streams $encryptor = new EncryptStream(KEY); $decryptor = new DecryptStream(KEY); // shutdown on "exit" command input $quitter = new QuitterStream($loop); // logs piped input to stdout $logger = new LoggerStream(LoggerStream::DEBUG); // simple socket server (because why not) // access via 'telnet 127.0.0.1 1337' $socket = new Server($loop, new DecryptStream(KEY), $accessLog); $socket->listen(1337); $in->pipe($logger); $in->pipe($quitter); $err->pipe($errorLog); $encryptor->pipe($logger); $decryptor->pipe($logger); $logger->pipe($out); $logger->pipe($debugLog); $in->pipe($encryptor)->pipe($decryptor)->pipe($out); $loop->run();