1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPFastCGI\FastCGIDaemon\Http; |
4
|
|
|
|
5
|
|
|
use Interop\Http\Factory\ServerRequestFactoryInterface; |
6
|
|
|
use Nyholm\Psr7Server\ServerRequestCreatorInterface; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest; |
8
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The default implementation of the RequestInterface. |
12
|
|
|
*/ |
13
|
|
|
class Request implements RequestInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ServerRequestCreatorInterface|null |
17
|
|
|
*/ |
18
|
|
|
private static $serverRequestCreator = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $params; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var resource |
27
|
|
|
*/ |
28
|
|
|
private $stdin; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param array $params The FastCGI server params as an associative array |
34
|
|
|
* @param resource $stdin The FastCGI stdin data as a stream resource |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $params, $stdin) |
37
|
|
|
{ |
38
|
|
|
$this->params = []; |
39
|
|
|
|
40
|
|
|
foreach ($params as $name => $value) { |
41
|
|
|
$this->params[strtoupper($name)] = $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->stdin = $stdin; |
45
|
|
|
|
46
|
|
|
rewind($this->stdin); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void |
50
|
|
|
{ |
51
|
|
|
self::$serverRequestCreator = $serverRequestCreator; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getParams() |
58
|
|
|
{ |
59
|
|
|
return $this->params; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getQuery() |
66
|
|
|
{ |
67
|
|
|
$query = null; |
68
|
|
|
|
69
|
|
|
if (isset($this->params['QUERY_STRING'])) { |
70
|
|
|
parse_str($this->params['QUERY_STRING'], $query); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $query ?: []; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function getPost() |
80
|
|
|
{ |
81
|
|
|
$post = null; |
82
|
|
|
|
83
|
|
|
if (isset($this->params['REQUEST_METHOD']) && isset($this->params['CONTENT_TYPE'])) { |
84
|
|
|
$requestMethod = $this->params['REQUEST_METHOD']; |
85
|
|
|
$contentType = $this->params['CONTENT_TYPE']; |
86
|
|
|
|
87
|
|
|
if (strcasecmp($requestMethod, 'POST') === 0 && stripos($contentType, 'application/x-www-form-urlencoded') === 0) { |
88
|
|
|
$postData = stream_get_contents($this->stdin); |
89
|
|
|
rewind($this->stdin); |
90
|
|
|
|
91
|
|
|
parse_str($postData, $post); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $post ?: []; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function getCookies() |
102
|
|
|
{ |
103
|
|
|
$cookies = []; |
104
|
|
|
|
105
|
|
|
if (isset($this->params['HTTP_COOKIE'])) { |
106
|
|
|
$cookiePairs = explode(';', $this->params['HTTP_COOKIE']); |
107
|
|
|
|
108
|
|
|
foreach ($cookiePairs as $cookiePair) { |
109
|
|
|
list($name, $value) = explode('=', trim($cookiePair)); |
110
|
|
|
$cookies[$name] = $value; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $cookies; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function getStdin() |
121
|
|
|
{ |
122
|
|
|
return $this->stdin; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function getServerRequest() |
129
|
|
|
{ |
130
|
|
|
if (null === self::$serverRequestCreator) { |
131
|
|
|
throw new \RuntimeException('You need to add an object of \Nyholm\Psr7Server\ServerRequestCreatorInterface to \PHPFastCGI\FastCGIDaemon\Http\Request::setServerRequestCreator to use PSR-7 requests. Please install and read more at https://github.com/nyholm/psr7-server'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$server = $this->params; |
135
|
|
|
$query = $this->getQuery(); |
136
|
|
|
$post = $this->getPost(); |
137
|
|
|
$cookies = $this->getCookies(); |
138
|
|
|
|
139
|
|
|
return self::$serverRequestCreator->fromArrays( |
140
|
|
|
$server, |
141
|
|
|
self::$serverRequestCreator->getHeadersFromServer($server), |
142
|
|
|
$cookies, |
143
|
|
|
$query, |
144
|
|
|
$post, |
145
|
|
|
[] /*$this->uploadedFiles*/ |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function getHttpFoundationRequest() |
153
|
|
|
{ |
154
|
|
|
if (!class_exists(HttpFoundationRequest::class)) { |
155
|
|
|
throw new \RuntimeException('You need to install symfony/http-foundation:^4.0 to use HttpFoundation requests.'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$query = $this->getQuery(); |
159
|
|
|
$post = $this->getPost(); |
160
|
|
|
$cookies = $this->getCookies(); |
161
|
|
|
|
162
|
|
|
return new HttpFoundationRequest($query, $post, [], $cookies, [], $this->params, $this->stdin); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|