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 ($headers, &$dataResult) { |
84
|
|
|
$data = []; |
85
|
|
|
|
86
|
|
|
if ($dataResult === null) return $this->emit('end'); |
87
|
|
|
|
88
|
|
|
if (isset($headers['content-type']) && $headers['content-type'] == 'application/json') { |
89
|
|
|
$jsonData = json_decode($dataResult, true); |
90
|
|
|
|
91
|
|
|
if ($jsonData === null) { |
92
|
|
|
$this->emit('error', [json_last_error_msg()]); |
93
|
|
|
return; |
94
|
|
|
} else { |
95
|
|
|
$data = $jsonData; |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
parse_str($dataResult, $data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->setContent($dataResult); |
102
|
|
|
$this->setData($data); |
103
|
|
|
$this->emit('end'); |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the data array |
109
|
|
|
* @param array $data array of data |
110
|
|
|
*/ |
111
|
|
|
public function setData($data) |
112
|
|
|
{ |
113
|
|
|
$this->data = array_merge($data, $this->data); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the data array |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getData() |
121
|
|
|
{ |
122
|
|
|
return $this->data; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function __get($name) |
126
|
|
|
{ |
127
|
|
|
return isset($this->data[$name]) ? $this->data[$name] : false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function __set($name, $value) |
131
|
|
|
{ |
132
|
|
|
$this->data[$name] = $value; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|