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() |
||
58 | |||
59 | /** |
||
60 | * Parse request data |
||
61 | * @return void |
||
62 | */ |
||
63 | public function parseData() |
||
64 | { |
||
65 | $headers = $this->getHeaders(); |
||
66 | |||
67 | if (!in_array($this->httpRequest->getMethod(), ['PUT', 'POST'])) { |
||
68 | return $this->emit('end'); |
||
69 | } |
||
70 | |||
71 | $this->httpRequest->on('data', function($data) use ($headers, &$dataResult) { |
||
72 | $dataResult .= $data; |
||
73 | |||
74 | if (isset($headers["Content-Length"])) { |
||
75 | if (strlen($dataResult) == $headers["Content-Length"]) { |
||
76 | $this->httpRequest->close(); |
||
77 | } |
||
78 | } else { |
||
79 | $this->httpRequest->close(); |
||
80 | } |
||
81 | }); |
||
82 | |||
83 | $this->httpRequest->on('end', function() use (&$dataResult) { |
||
84 | $this->onEnd($dataResult); |
||
85 | }); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * On request end |
||
90 | * @param string $dataResult |
||
91 | * @return void |
||
92 | */ |
||
93 | private function onEnd($dataResult) |
||
94 | { |
||
95 | if ($dataResult === null) return $this->emit('end'); |
||
96 | |||
97 | if ($this->isJson()) $this->parseJson($dataResult); |
||
98 | else $this->parseStr($dataResult); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Parse querystring |
||
103 | * @param string $dataString |
||
104 | * @return void |
||
105 | */ |
||
106 | private function parseStr($dataString) |
||
107 | { |
||
108 | $data = []; |
||
109 | parse_str($dataString, $data); |
||
110 | |||
111 | $this->setContent($dataString); |
||
112 | if(is_array($data)) $this->setData($data); |
||
113 | |||
114 | $this->emit('end'); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Parse json string |
||
119 | * @param string $jsonString |
||
120 | * @return void |
||
121 | */ |
||
122 | private function parseJson($jsonString) |
||
135 | |||
136 | /** |
||
137 | * Check if current request is a json request |
||
138 | * @return boolean |
||
139 | */ |
||
140 | public function isJson() |
||
141 | { |
||
142 | $headers = $this->getHeaders(); |
||
143 | |||
144 | return isset($headers['content-type']) && $headers['content-type'] == 'application/json'; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Set the data array |
||
149 | * @param array $data array of data |
||
150 | */ |
||
151 | public function setData($data) |
||
155 | |||
156 | /** |
||
157 | * Get the data array |
||
158 | * @return array |
||
159 | */ |
||
160 | public function getData() |
||
164 | |||
165 | public function __get($name) |
||
169 | |||
170 | public function __set($name, $value) |
||
174 | } |
||
175 |