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 mixed */ |
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 mixed $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 mixed |
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
|
|
|
$headers = array_map(function ($value) { |
53
|
|
|
return strtolower($value); |
54
|
|
|
}, $headers); |
55
|
|
|
|
56
|
|
|
return $headers; |
57
|
|
|
} |
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
|
|
|
private function onEnd($dataResult) |
89
|
|
|
{ |
90
|
|
|
if ($dataResult === null) return $this->emit('end'); |
91
|
|
|
|
92
|
|
|
if ($this->isJson()) $this->paseJson($dataResult); |
|
|
|
|
93
|
|
|
else $this->parseStr($dataResult); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Parse querystring |
98
|
|
|
* @param string $dataString |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
private function parseStr($dataString) |
102
|
|
|
{ |
103
|
|
|
$data = []; |
104
|
|
|
parse_str($dataString, $data); |
105
|
|
|
|
106
|
|
|
$this->setContent($dataString); |
107
|
|
|
$this->setData($data); |
|
|
|
|
108
|
|
|
$this->emit('end'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Parse json string |
113
|
|
|
* @param string $jsonString |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
private function parseJson($jsonString) |
117
|
|
|
{ |
118
|
|
|
$jsonData = json_decode($jsonString, true); |
119
|
|
|
|
120
|
|
|
if ($jsonData === null) { |
121
|
|
|
$this->emit('error', [json_last_error_msg()]); |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->setContent($jsonString); |
126
|
|
|
$this->setData($jsonData); |
127
|
|
|
$this->emit('end'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if current request is a json request |
132
|
|
|
* @return boolean |
133
|
|
|
*/ |
134
|
|
|
public function isJson() |
135
|
|
|
{ |
136
|
|
|
$header = $this->getHeaders(); |
|
|
|
|
137
|
|
|
|
138
|
|
|
return isset($headers['content-type']) && $headers['content-type'] == 'application/json'; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the data array |
143
|
|
|
* @param array $data array of data |
144
|
|
|
*/ |
145
|
|
|
public function setData($data) |
146
|
|
|
{ |
147
|
|
|
$this->data = array_merge($data, $this->data); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the data array |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function getData() |
155
|
|
|
{ |
156
|
|
|
return $this->data; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function __get($name) |
160
|
|
|
{ |
161
|
|
|
return isset($this->data[$name]) ? $this->data[$name] : false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function __set($name, $value) |
165
|
|
|
{ |
166
|
|
|
$this->data[$name] = $value; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.