1 | <?php |
||
8 | class Request extends EventEmitter |
||
9 | { |
||
10 | /** @var \React\Http\Request */ |
||
11 | public $httpRequest; |
||
12 | |||
13 | /** @var string */ |
||
14 | private $content; |
||
15 | |||
16 | /** @var array */ |
||
17 | private $data = []; |
||
18 | |||
19 | /** |
||
20 | * @param ReactHttpRequest $httpRequest |
||
21 | */ |
||
22 | public function __construct(ReactHttpRequest $httpRequest) |
||
26 | |||
27 | /** |
||
28 | * Set the raw data of the request |
||
29 | * @param string $content |
||
30 | */ |
||
31 | public function setContent($content) |
||
35 | |||
36 | /** |
||
37 | * Set the raw data of the request |
||
38 | * @return string |
||
39 | */ |
||
40 | public function getContent() |
||
44 | |||
45 | /** |
||
46 | * Get formated headers |
||
47 | * @return array |
||
48 | */ |
||
49 | public function getHeaders() |
||
61 | |||
62 | /** |
||
63 | * Parse request data |
||
64 | * @return void |
||
65 | */ |
||
66 | public function parseData() |
||
67 | { |
||
68 | $headers = $this->getHeaders(); |
||
69 | |||
70 | if (!in_array($this->httpRequest->getMethod(), ['PUT', 'POST'])) { |
||
71 | return $this->emit('end'); |
||
72 | } |
||
73 | |||
74 | $this->httpRequest->on('data', function($data) use ($headers, &$dataResult) { |
||
75 | $dataResult .= $data; |
||
76 | |||
77 | if (isset($headers["content-length"])) { |
||
78 | if (strlen($dataResult) == $headers["content-length"][0]) { |
||
79 | $this->httpRequest->close(); |
||
80 | } |
||
81 | } else { |
||
82 | $this->httpRequest->close(); |
||
83 | } |
||
84 | }); |
||
85 | |||
86 | $this->httpRequest->on('end', function() use (&$dataResult) { |
||
87 | $this->onEnd($dataResult); |
||
88 | }); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * On request end |
||
93 | * @param string $dataResult |
||
94 | * @return void |
||
95 | */ |
||
96 | private function onEnd($dataResult) |
||
97 | { |
||
98 | if ($dataResult === null) return $this->emit('end'); |
||
99 | |||
100 | if ($this->isJson()) $this->parseJson($dataResult); |
||
101 | else $this->parseStr($dataResult); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Parse querystring |
||
106 | * @param string $dataString |
||
107 | * @return void |
||
108 | */ |
||
109 | private function parseStr($dataString) |
||
110 | { |
||
111 | $data = []; |
||
112 | parse_str($dataString, $data); |
||
113 | |||
114 | $this->setContent($dataString); |
||
115 | if(is_array($data)) $this->setData($data); |
||
116 | |||
117 | $this->emit('end'); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Parse json string |
||
122 | * @param string $jsonString |
||
123 | * @return void |
||
124 | */ |
||
125 | private function parseJson($jsonString) |
||
138 | |||
139 | /** |
||
140 | * Check if current request is a json request |
||
141 | * @return boolean |
||
142 | */ |
||
143 | public function isJson() |
||
144 | { |
||
145 | $headers = $this->getHeaders(); |
||
146 | |||
147 | return isset($headers['content-type']) && $headers['content-type'][0] == 'application/json'; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Set the data array |
||
152 | * @param array $data array of data |
||
153 | */ |
||
154 | public function setData($data) |
||
158 | |||
159 | /** |
||
160 | * Get the data array |
||
161 | * @return array |
||
162 | */ |
||
163 | public function getData() |
||
167 | |||
168 | public function __get($name) |
||
172 | |||
173 | public function __set($name, $value) |
||
177 | } |
||
178 |