/** @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"); }
require __DIR__ . '/../vendor/autoload.php'; $loop = Factory::create(); if (function_exists('posix_isatty') && posix_isatty(STDIN)) { // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead) shell_exec('stty -icanon -echo'); } // process control codes from STDIN $stdin = new Stream(STDIN, $loop); $parser = new ControlCodeParser($stdin); $stdout = new Stream(STDOUT, $loop); $stdout->pause(); // pass all c0 codes through to output $parser->on('c0', array($stdout, 'write')); // replace any color codes (SGR) with a random color $parser->on('csi', function ($code) use($stdout) { // we read any color code (SGR) on the input // assign a new random foreground and background color instead if (substr($code, -1) === 'm') { $code = "[" . mt_rand(30, 37) . ';' . mt_rand(40, 47) . "m"; } $stdout->write($code); }); // reset to default color at the end $stdin->on('close', function () use($stdout) { $stdout->write("[m"); }); // pass plain data to output $parser->pipe($stdout, array('end' => false)); // start with random color $stdin->emit('data', array("[m")); $loop->run();
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); }); }