This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
HttpKernel.php
164 lines (136 loc) · 5.04 KB
/
HttpKernel.php
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
<?php
namespace M6Web\Bundle\PhpProcessManagerBundle\Bridge;
use Symfony\Component\HttpKernel as SymfonyHttpKernel;
use React\Http\Request as ReactRequest;
use React\Http\Response as ReactResponse;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;
/**
* Http Kernel Bridge
*/
class HttpKernel implements BridgeInterface
{
/**
* An application implementing the HttpKernelInterface
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $application;
/**
* @param SymfonyHttpKernel\HttpKernelInterface $application
*/
public function __construct(SymfonyHttpKernel\HttpKernelInterface $application)
{
$this->application = $application;
}
/**
* Handle a request
*
* @param ReactRequest $request
* @param ReactResponse $response
*/
public function onRequest(ReactRequest $request, ReactResponse $response)
{
$content = '';
$headers = $request->getHeaders();
$contentLength = isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : 0;
$request->on('data', function ($data) use ($request, $response, &$content, $contentLength) {
// Read data (may be empty for GET request)
$content.= $data;
// Handle request after receive
if (strlen($content) >= $contentLength) {
$symfonyRequest = static::mapRequest($request, $content);
try {
// Execute
$symfonyResponse = $this->application->handle($symfonyRequest);
} catch (\Throwable $t) {
// Executed only in PHP 7, will not match in PHP 5.x
$this->fatalError($response, $t);
return;
} catch (\Exception $e) {
// Executed only in PHP 5.x, will not be reached in PHP 7
$this->fatalError($response, $e);
return;
}
static::mapResponse($response, $symfonyResponse);
if ($this->application instanceof SymfonyHttpKernel\TerminableInterface) {
$this->application->terminate($symfonyRequest, $symfonyResponse);
}
}
});
}
/**
* Manager Internal server error
*
* @param ReactResponse $response
* @param Exception|Throwable $error
*/
protected function fatalError(ReactResponse $response, $error)
{
$response->writeHead(500);
$response->write(
sprintf("Internal server error : %s", $error->getMessage())
);
$response->end();
}
/**
* Convert React\Http\Request to Symfony\Component\HttpFoundation\Request
*
* @param ReactRequest $reactRequest
* @param string $content
*
* @return SymfonyRequest $symfonyRequest
*/
protected static function mapRequest(ReactRequest $reactRequest, $content)
{
$method = $reactRequest->getMethod();
$headers = $reactRequest->getHeaders();
$query = $reactRequest->getQuery();
$post = [];
// Parse body?
if (in_array(strtoupper($method), ['POST', 'PUT', 'DELETE', 'PATCH']) &&
isset($headers['Content-Type']) && (0 === strpos($headers['Content-Type'], 'application/x-www-form-urlencoded'))
) {
parse_str($content, $post);
}
// Map to a SymfonyRequest
$symfonyRequest = new SymfonyRequest(
$query, // $query
$post, // $request
array(), // @todo $attributes
array(), // @todo $cookies
array(), // @todo $files
array(), // @todo $server
$content
);
$symfonyRequest->setMethod($method);
$symfonyRequest->headers->replace($headers);
$symfonyRequest->server->set('REQUEST_URI', $reactRequest->getPath());
if (isset($headers['Host'])) {
$symfonyRequest->server->set('SERVER_NAME', explode(':', $headers['Host'])[0]);
}
return $symfonyRequest;
}
/**
* Convert Symfony\Component\HttpFoundation\Response to React\Http\Response
*
* @param ReactResponse $reactResponse
* @param SymfonyResponse $symfonyResponse
*/
protected static function mapResponse(ReactResponse $reactResponse, SymfonyResponse $symfonyResponse)
{
$headers = $symfonyResponse->headers->all();
$reactResponse->writeHead($symfonyResponse->getStatusCode(), $headers);
// @TODO convert StreamedResponse in an async manner
if ($symfonyResponse instanceof SymfonyStreamedResponse) {
ob_start();
$symfonyResponse->sendContent();
$content = ob_get_contents();
ob_end_clean();
} else {
$content = $symfonyResponse->getContent();
}
$reactResponse->end($content);
}
}