Repository navigation
Expand file tree
/
Copy pathServer.php
More file actions
219 lines (183 loc) · 7.5 KB
/
Copy pathServer.php
File metadata and controls
219 lines (183 loc) · 7.5 KB
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
215
216
217
218
219
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\HttpServer;
use FastRoute\Dispatcher;
use Hyperf\Context\RequestContext;
use Hyperf\Context\ResponseContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\MiddlewareInitializerInterface;
use Hyperf\Contract\OnRequestInterface;
use Hyperf\Coordinator\Constants;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Dispatcher\HttpDispatcher;
use Hyperf\Engine\Http\WritableConnection;
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
use Hyperf\HttpMessage\Server\Request as Psr7Request;
use Hyperf\HttpMessage\Server\Response as Psr7Response;
use Hyperf\HttpServer\Contract\CoreMiddlewareInterface;
use Hyperf\HttpServer\Event\RequestHandled;
use Hyperf\HttpServer\Event\RequestReceived;
use Hyperf\HttpServer\Event\RequestTerminated;
use Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Hyperf\Server\Option;
use Hyperf\Server\ServerFactory;
use Hyperf\Support\SafeCaller;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
use function Hyperf\Coroutine\defer;
use function Hyperf\Support\make;
class Server implements OnRequestInterface, MiddlewareInitializerInterface
{
protected array $middlewares = [];
protected ?CoreMiddlewareInterface $coreMiddleware = null;
protected array $exceptionHandlers = [];
protected ?string $serverName = null;
protected ?EventDispatcherInterface $event = null;
protected ?Option $option = null;
public function __construct(
protected ContainerInterface $container,
protected HttpDispatcher $dispatcher,
protected ExceptionHandlerDispatcher $exceptionHandlerDispatcher,
protected ResponseEmitter $responseEmitter
) {
if ($this->container->has(EventDispatcherInterface::class)) {
$this->event = $this->container->get(EventDispatcherInterface::class);
}
}
public function initCoreMiddleware(string $serverName): void
{
$this->serverName = $serverName;
$this->coreMiddleware = $this->createCoreMiddleware();
$config = $this->container->get(ConfigInterface::class);
$this->middlewares = $config->get('middlewares.' . $serverName, []);
$this->exceptionHandlers = $config->get('exceptions.handler.' . $serverName, $this->getDefaultExceptionHandler());
$this->initOption();
}
public function onRequest($request, $response): void
{
try {
CoordinatorManager::until(Constants::WORKER_START)->yield();
[$psr7Request, $psr7Response] = $this->initRequestAndResponse($request, $response);
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
$this->option?->isEnableRequestLifecycle() && $this->event?->dispatch(new RequestReceived(
request: $psr7Request,
response: $psr7Response,
server: $this->serverName
));
/** @var Dispatched $dispatched */
$dispatched = $psr7Request->getAttribute(Dispatched::class);
$middlewares = $this->middlewares;
$registeredMiddlewares = [];
if ($dispatched->isFound()) {
$registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
$middlewares = array_merge($middlewares, $registeredMiddlewares);
}
if ($this->option?->isMustSortMiddlewares() || $registeredMiddlewares) {
$middlewares = MiddlewareManager::sortMiddlewares($middlewares);
}
$psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware);
} catch (Throwable $throwable) {
// Delegate the exception to exception handler.
$psr7Response = $this->container->get(SafeCaller::class)->call(function () use ($throwable) {
return $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
}, static function () {
return (new Psr7Response())->withStatus(400);
});
} finally {
if (isset($psr7Request) && $this->option?->isEnableRequestLifecycle()) {
defer(fn () => $this->event?->dispatch(new RequestTerminated(
request: $psr7Request,
response: $psr7Response ?? null,
exception: $throwable ?? null,
server: $this->serverName
)));
$this->event?->dispatch(new RequestHandled(
request: $psr7Request,
response: $psr7Response ?? null,
exception: $throwable ?? null,
server: $this->serverName
));
}
// Send the Response to client.
if (! isset($psr7Response) || ! $psr7Response instanceof ResponseInterface) {
return;
}
if (isset($psr7Request) && $psr7Request->getMethod() === 'HEAD') {
$this->responseEmitter->emit($psr7Response, $response, false);
} else {
$this->responseEmitter->emit($psr7Response, $response);
}
}
}
public function getServerName(): string
{
return $this->serverName;
}
/**
* @return $this
*/
public function setServerName(string $serverName)
{
$this->serverName = $serverName;
return $this;
}
protected function initOption(): void
{
$ports = $this->container->get(ServerFactory::class)->getConfig()?->getServers();
if (! $ports) {
return;
}
foreach ($ports as $port) {
if ($port->getName() === $this->serverName) {
$this->option = $port->getOptions();
}
}
$this->option ??= Option::make([]);
$this->option->setMustSortMiddlewaresByMiddlewares($this->middlewares);
}
protected function createDispatcher(string $serverName): Dispatcher
{
$factory = $this->container->get(DispatcherFactory::class);
return $factory->getDispatcher($serverName);
}
protected function getDefaultExceptionHandler(): array
{
return [
HttpExceptionHandler::class,
];
}
protected function createCoreMiddleware(): CoreMiddlewareInterface
{
return make(CoreMiddleware::class, [$this->container, $this->serverName]);
}
/**
* Initialize PSR-7 Request and Response objects.
* @param mixed $request swoole request or psr server request
* @param mixed $response swoole response or swow connection
*/
protected function initRequestAndResponse($request, $response): array
{
ResponseContext::set($psr7Response = new Psr7Response());
$psr7Response->setConnection(new WritableConnection($response));
if ($request instanceof ServerRequestInterface) {
$psr7Request = $request;
} else {
$psr7Request = Psr7Request::loadFromSwooleRequest($request);
}
RequestContext::set($psr7Request);
return [$psr7Request, $psr7Response];
}
}