1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Phiremock. |
4
|
|
|
* |
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Server\Http\Implementation; |
20
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Common\StringStream; |
22
|
|
|
use Mcustiel\Phiremock\Server\Cli\Options\HostInterface; |
23
|
|
|
use Mcustiel\Phiremock\Server\Cli\Options\Port; |
24
|
|
|
use Mcustiel\Phiremock\Server\Cli\Options\SecureOptions; |
25
|
|
|
use Mcustiel\Phiremock\Server\Http\RequestHandlerInterface; |
26
|
|
|
use Mcustiel\Phiremock\Server\Http\ServerInterface; |
27
|
|
|
use Psr\Http\Message\ResponseInterface; |
28
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
29
|
|
|
use Psr\Log\LoggerInterface; |
30
|
|
|
use React\EventLoop\Factory as EventLoop; |
31
|
|
|
use React\Http\Server; |
32
|
|
|
use React\Socket\Server as ReactSocket; |
33
|
|
|
|
34
|
|
|
class ReactPhpServer implements ServerInterface |
35
|
|
|
{ |
36
|
|
|
/** @var \Mcustiel\Phiremock\Server\Http\RequestHandlerInterface */ |
37
|
|
|
private $requestHandler; |
38
|
|
|
|
39
|
|
|
/** @var \React\EventLoop\LoopInterface */ |
40
|
|
|
private $loop; |
41
|
|
|
|
42
|
|
|
/** @var \React\Socket\Server */ |
43
|
|
|
private $socket; |
44
|
|
|
|
45
|
|
|
/** @var \React\Http\Server */ |
46
|
|
|
private $http; |
47
|
|
|
|
48
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
49
|
|
|
private $logger; |
50
|
|
|
|
51
|
|
|
public function __construct(RequestHandlerInterface $requestHandler, LoggerInterface $logger) |
52
|
|
|
{ |
53
|
|
|
$this->loop = EventLoop::create(); |
54
|
|
|
$this->logger = $logger; |
55
|
|
|
$this->requestHandler = $requestHandler; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function listen(HostInterface $host, Port $port, ?SecureOptions $secureOptions): void |
59
|
|
|
{ |
60
|
|
|
$this->http = new Server( |
61
|
|
|
$this->loop, |
62
|
|
|
function (ServerRequestInterface $request) { |
63
|
|
|
return $this->onRequest($request); |
64
|
|
|
} |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$listenConfig = "{$host->asString()}:{$port->asInt()}"; |
68
|
|
|
$this->initSocket($listenConfig, $secureOptions); |
69
|
|
|
$this->http->listen($this->socket); |
70
|
|
|
|
71
|
|
|
// Dispatch pending signals periodically |
72
|
|
|
if (\function_exists('pcntl_signal_dispatch')) { |
73
|
|
|
$this->loop->addPeriodicTimer(0.5, function () { |
74
|
|
|
pcntl_signal_dispatch(); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
$this->loop->run(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function shutdown(): void |
81
|
|
|
{ |
82
|
|
|
$this->http->removeAllListeners(); |
83
|
|
|
$this->socket->close(); |
84
|
|
|
$this->loop->stop(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function onRequest(ServerRequestInterface $request): ResponseInterface |
88
|
|
|
{ |
89
|
|
|
$start = microtime(true); |
90
|
|
|
|
91
|
|
|
// TODO: Remove this patch if ReactPHP is fixed |
92
|
|
|
if ($request->getParsedBody() !== null) { |
93
|
|
|
$request = $request->withBody(new StringStream(http_build_query($request->getParsedBody()))); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$psrResponse = $this->requestHandler->dispatch(new ServerRequestWithCachedBody($request)); |
97
|
|
|
$this->logger->debug('Processing took ' . number_format((microtime(true) - $start) * 1000, 3) . ' milliseconds'); |
98
|
|
|
|
99
|
|
|
return $psrResponse; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function initSocket(string $listenConfig, ?SecureOptions $secureOptions): void |
103
|
|
|
{ |
104
|
|
|
$this->logger->info( |
105
|
|
|
sprintf( |
106
|
|
|
'Phiremock http server listening on %s over %s', |
107
|
|
|
$listenConfig, |
108
|
|
|
null === $secureOptions ? 'http' : 'https' |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
$context = []; |
112
|
|
|
if ($secureOptions !== null) { |
113
|
|
|
$tlsContext = []; |
114
|
|
|
$listenConfig = sprintf('tls://%s', $listenConfig); |
115
|
|
|
$tlsContext['local_cert'] = $secureOptions->getCertificate()->asString(); |
116
|
|
|
$tlsContext['local_pk'] = $secureOptions->getCertificateKey()->asString(); |
117
|
|
|
if ($secureOptions->hasPassphrase()) { |
118
|
|
|
$tlsContext['passphrase'] = $secureOptions->getPassphrase()->asString(); |
119
|
|
|
} |
120
|
|
|
$context['tls'] = $tlsContext; |
121
|
|
|
} |
122
|
|
|
$this->socket = new ReactSocket($listenConfig, $this->loop, $context); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|