friendship ended with social-app. php is my new best friend
1<?php
2
3namespace React\Http\Io;
4
5use Evenement\EventEmitter;
6use React\Stream\ReadableStreamInterface;
7use React\Stream\Util;
8use React\Stream\WritableStreamInterface;
9
10/**
11 * [Internal] Encodes given payload stream with "Transfer-Encoding: chunked" and emits encoded data
12 *
13 * This is used internally to encode outgoing requests with this encoding.
14 *
15 * @internal
16 */
17class ChunkedEncoder extends EventEmitter implements ReadableStreamInterface
18{
19 private $input;
20 private $closed = false;
21
22 public function __construct(ReadableStreamInterface $input)
23 {
24 $this->input = $input;
25
26 $this->input->on('data', array($this, 'handleData'));
27 $this->input->on('end', array($this, 'handleEnd'));
28 $this->input->on('error', array($this, 'handleError'));
29 $this->input->on('close', array($this, 'close'));
30 }
31
32 public function isReadable()
33 {
34 return !$this->closed && $this->input->isReadable();
35 }
36
37 public function pause()
38 {
39 $this->input->pause();
40 }
41
42 public function resume()
43 {
44 $this->input->resume();
45 }
46
47 public function pipe(WritableStreamInterface $dest, array $options = array())
48 {
49 return Util::pipe($this, $dest, $options);
50 }
51
52 public function close()
53 {
54 if ($this->closed) {
55 return;
56 }
57
58 $this->closed = true;
59 $this->input->close();
60
61 $this->emit('close');
62 $this->removeAllListeners();
63 }
64
65 /** @internal */
66 public function handleData($data)
67 {
68 if ($data !== '') {
69 $this->emit('data', array(
70 \dechex(\strlen($data)) . "\r\n" . $data . "\r\n"
71 ));
72 }
73 }
74
75 /** @internal */
76 public function handleError(\Exception $e)
77 {
78 $this->emit('error', array($e));
79 $this->close();
80 }
81
82 /** @internal */
83 public function handleEnd()
84 {
85 $this->emit('data', array("0\r\n\r\n"));
86
87 if (!$this->closed) {
88 $this->emit('end');
89 $this->close();
90 }
91 }
92}