|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\React\Docker\Io; |
| 4 | + |
| 5 | +use React\Stream\ReadableStreamInterface; |
| 6 | +use Evenement\EventEmitter; |
| 7 | +use React\Stream\WritableStreamInterface; |
| 8 | +use React\Stream\Util; |
| 9 | +/** |
| 10 | + * Parser for Docker's own frame format used for bidrectional frames |
| 11 | + * |
| 12 | + * Each frame consists of a simple header containing the stream identifier and the payload length |
| 13 | + * plus the actual payload string. |
| 14 | + * |
| 15 | + * @internal |
| 16 | + * @link https://docs.docker.com/engine/reference/api/docker_remote_api_v1.15/#attach-to-a-container |
| 17 | + */ |
| 18 | +class ReadableDemultiplexStream extends EventEmitter implements ReadableStreamInterface |
| 19 | +{ |
| 20 | + private $buffer = ''; |
| 21 | + private $closed = false; |
| 22 | + private $multiplexed; |
| 23 | + private $stderrEvent; |
| 24 | + |
| 25 | + public function __construct(ReadableStreamInterface $multiplexed, $stderrEvent = null) |
| 26 | + { |
| 27 | + $this->multiplexed = $multiplexed; |
| 28 | + |
| 29 | + if ($stderrEvent === null) { |
| 30 | + $stderrEvent = 'data'; |
| 31 | + } |
| 32 | + |
| 33 | + $this->stderrEvent = $stderrEvent; |
| 34 | + |
| 35 | + $out = $this; |
| 36 | + $buffer =& $this->buffer; |
| 37 | + $closed =& $this->closed; |
| 38 | + |
| 39 | + // pass all input data chunks through the parser |
| 40 | + $multiplexed->on('data', array($out, 'push')); |
| 41 | + |
| 42 | + // forward end event to output (unless parsing is still in progress) |
| 43 | + $multiplexed->on('end', function () use (&$buffer, $out, &$closed) { |
| 44 | + // ignore duplicate end events |
| 45 | + if ($closed) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // buffer must be empty on end, otherwise this is an error situation |
| 50 | + if ($buffer === '') { |
| 51 | + $out->emit('end', array()); |
| 52 | + } else { |
| 53 | + $out->emit('error', array(new \RuntimeException('Stream ended within incomplete multiplexed chunk'))); |
| 54 | + } |
| 55 | + $out->close(); |
| 56 | + }); |
| 57 | + |
| 58 | + // forward error event to output |
| 59 | + $multiplexed->on('error', function ($error) use ($out) { |
| 60 | + $out->emit('error', array($error)); |
| 61 | + $out->close(); |
| 62 | + }); |
| 63 | + |
| 64 | + // forward close event to output |
| 65 | + $multiplexed->on('close', function ($error) use ($out) { |
| 66 | + $out->close(); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * push the given stream chunk into the parser buffer and try to extract all frames |
| 72 | + * |
| 73 | + * @internal |
| 74 | + * @param string $chunk |
| 75 | + */ |
| 76 | + public function push($chunk) |
| 77 | + { |
| 78 | + $this->buffer .= $chunk; |
| 79 | + |
| 80 | + while ($this->buffer !== '') { |
| 81 | + if (!isset($this->buffer[7])) { |
| 82 | + // last header byte not set => no complete header in buffer |
| 83 | + break; |
| 84 | + } |
| 85 | + |
| 86 | + $header = unpack('Cstream/x/x/x/Nlength', substr($this->buffer, 0, 8)); |
| 87 | + |
| 88 | + if (!isset($this->buffer[7 + $header['length']])) { |
| 89 | + // last payload byte not set => message payload is incomplete |
| 90 | + break; |
| 91 | + } |
| 92 | + |
| 93 | + $payload = substr($this->buffer, 8, $header['length']); |
| 94 | + $this->buffer = (string)substr($this->buffer, 8 + $header['length']); |
| 95 | + |
| 96 | + $this->emit( |
| 97 | + ($header['stream'] === 2) ? $this->stderrEvent : 'data', |
| 98 | + array($payload) |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public function pause() |
| 104 | + { |
| 105 | + $this->multiplexed->pause(); |
| 106 | + } |
| 107 | + |
| 108 | + public function resume() |
| 109 | + { |
| 110 | + $this->multiplexed->resume(); |
| 111 | + } |
| 112 | + |
| 113 | + public function isReadable() |
| 114 | + { |
| 115 | + return $this->multiplexed->isReadable(); |
| 116 | + } |
| 117 | + |
| 118 | + public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 119 | + { |
| 120 | + return Util::pipe($this, $dest, $options); |
| 121 | + } |
| 122 | + |
| 123 | + public function close() |
| 124 | + { |
| 125 | + if ($this->closed) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + $this->closed = true; |
| 130 | + |
| 131 | + // closing output stream closes input stream |
| 132 | + $this->multiplexed->close(); |
| 133 | + |
| 134 | + $this->emit('close', array()); |
| 135 | + } |
| 136 | +} |
0 commit comments