1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CapMousse\ReactRestify\Http; |
4
|
|
|
|
5
|
|
|
use React\Http\Request as ReactHttpRequest; |
6
|
|
|
use CapMousse\ReactRestify\Evenement\EventEmitter; |
7
|
|
|
|
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) |
23
|
|
|
{ |
24
|
|
|
$this->httpRequest = $httpRequest; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set the raw data of the request |
29
|
|
|
* @param string $content |
30
|
|
|
*/ |
31
|
|
|
public function setContent($content) |
32
|
|
|
{ |
33
|
|
|
$this->content = $content; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the raw data of the request |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getContent() |
41
|
|
|
{ |
42
|
|
|
return $this->content; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get formated headers |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getHeaders() |
50
|
|
|
{ |
51
|
|
|
$headers = array_change_key_case($this->httpRequest->getHeaders(), CASE_LOWER); |
52
|
|
|
|
53
|
|
|
foreach ($headers as $header) { |
54
|
|
|
$header = array_map(function ($value) { |
55
|
|
|
return strtolower($value); |
56
|
|
|
}, $header); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $headers; |
60
|
|
|
} |
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) |
126
|
|
|
{ |
127
|
|
|
$jsonData = json_decode($jsonString, true); |
128
|
|
|
|
129
|
|
|
if ($jsonData === null) { |
130
|
|
|
$this->emit('error', [json_last_error_msg()]); |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->setContent($jsonString); |
135
|
|
|
$this->setData($jsonData); |
136
|
|
|
$this->emit('end'); |
137
|
|
|
} |
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) |
155
|
|
|
{ |
156
|
|
|
$this->data = array_merge($data, $this->data); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the data array |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function getData() |
164
|
|
|
{ |
165
|
|
|
return $this->data; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function __get($name) |
169
|
|
|
{ |
170
|
|
|
return isset($this->data[$name]) ? $this->data[$name] : false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function __set($name, $value) |
174
|
|
|
{ |
175
|
|
|
$this->data[$name] = $value; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|