1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
<?php
/**
* High-performance PHP process supervisor and load balancer written in Go
*
* @author Wolfy-J
*/
declare(strict_types=1);
namespace Spiral\RoadRunner;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
/**
* Manages PSR-7 request and response.
*/
class PSR7Client
{
/** @var HttpClient */
private $httpClient;
/** @var ServerRequestFactoryInterface */
private $requestFactory;
/** @var StreamFactoryInterface */
private $streamFactory;
/*** @var UploadedFileFactoryInterface */
private $uploadsFactory;
private $originalServer = [];
/** @var array Valid values for HTTP protocol version */
private static $allowedVersions = ['1.0', '1.1', '2',];
/**
* @param Worker $worker
* @param ServerRequestFactoryInterface|null $requestFactory
* @param StreamFactoryInterface|null $streamFactory
* @param UploadedFileFactoryInterface|null $uploadsFactory
*/
public function __construct(
Worker $worker,
ServerRequestFactoryInterface $requestFactory = null,
StreamFactoryInterface $streamFactory = null,
UploadedFileFactoryInterface $uploadsFactory = null
) {
$this->httpClient = new HttpClient($worker);
$this->requestFactory = $requestFactory ?? new Diactoros\ServerRequestFactory();
$this->streamFactory = $streamFactory ?? new Diactoros\StreamFactory();
$this->uploadsFactory = $uploadsFactory ?? new Diactoros\UploadedFileFactory();
$this->originalServer = $_SERVER;
}
/**
* @return Worker
*/
public function getWorker(): Worker
{
return $this->httpClient->getWorker();
}
/**
* @return ServerRequestInterface|null
*/
public function acceptRequest()
{
$rawRequest = $this->httpClient->acceptRequest();
if ($rawRequest === null) {
return null;
}
$_SERVER = $this->configureServer($rawRequest['ctx']);
$request = $this->requestFactory->createServerRequest(
$rawRequest['ctx']['method'],
$rawRequest['ctx']['uri'],
$_SERVER
);
parse_str($rawRequest['ctx']['rawQuery'], $query);
$request = $request
->withProtocolVersion(static::fetchProtocolVersion($rawRequest['ctx']['protocol']))
->withCookieParams($rawRequest['ctx']['cookies'])
->withQueryParams($query)
->withUploadedFiles($this->wrapUploads($rawRequest['ctx']['uploads']));
foreach ($rawRequest['ctx']['attributes'] as $name => $value) {
$request = $request->withAttribute($name, $value);
}
foreach ($rawRequest['ctx']['headers'] as $name => $value) {
$request = $request->withHeader($name, $value);
}
if ($rawRequest['ctx']['parsed']) {
$request = $request->withParsedBody(json_decode($rawRequest['body'], true));
} else {
if ($rawRequest['body'] !== null) {
$request = $request->withBody($this->streamFactory->createStream($rawRequest['body']));
}
}
return $request;
}
/**
* Send response to the application server.
*
* @param ResponseInterface $response
*/
public function respond(ResponseInterface $response): void
{
$this->httpClient->respond(
$response->getStatusCode(),
$response->getBody()->__toString(),
$response->getHeaders()
);
}
/**
* Returns altered copy of _SERVER variable. Sets ip-address,
* request-time and other values.
*
* @param array $ctx
* @return array
*/
protected function configureServer(array $ctx): array
{
$server = $this->originalServer;
$server['REQUEST_URI'] = $ctx['uri'];
$server['REQUEST_TIME'] = time();
$server['REQUEST_TIME_FLOAT'] = microtime(true);
$server['REMOTE_ADDR'] = $ctx['attributes']['ipAddress'] ?? $ctx['remoteAddr'] ?? '127.0.0.1';
$server['HTTP_USER_AGENT'] = '';
foreach ($ctx['headers'] as $key => $value) {
$key = strtoupper(str_replace('-', '_', $key));
if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
$server[$key] = implode(', ', $value);
} else {
$server['HTTP_' . $key] = implode(', ', $value);
}
}
return $server;
}
/**
* Wraps all uploaded files with UploadedFile.
*
* @param array $files
*
* @return array
*/
private function wrapUploads($files): array
{
if (empty($files)) {
return [];
}
$result = [];
foreach ($files as $index => $f) {
if (!isset($f['name'])) {
$result[$index] = $this->wrapUploads($f);
continue;
}
if (UPLOAD_ERR_OK === $f['error']) {
$stream = $this->streamFactory->createStreamFromFile($f['tmpName']);
} else {
$stream = $this->streamFactory->createStream();
}
$result[$index] = $this->uploadsFactory->createUploadedFile(
$stream,
$f['size'],
$f['error'],
$f['name'],
$f['mime']
);
}
return $result;
}
/**
* Normalize HTTP protocol version to valid values
*
* @param string $version
* @return string
*/
private static function fetchProtocolVersion(string $version): string
{
$v = substr($version, 5);
if ($v === '2.0') {
return '2';
}
// Fallback for values outside of valid protocol versions
if (!in_array($v, static::$allowedVersions, true)) {
return '1.1';
}
return $v;
}
}
|