forked from clue/framework-x
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSapiHandler.php
More file actions
134 lines (116 loc) · 4.83 KB
/
Copy pathSapiHandler.php
File metadata and controls
134 lines (116 loc) · 4.83 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
<?php
namespace FrameworkX\Io;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;
use React\Http\Message\ServerRequest;
use React\Stream\ReadableStreamInterface;
/**
* @internal
*/
class SapiHandler
{
public function requestFromGlobals(): ServerRequestInterface
{
$host = null;
$headers = array();
// @codeCoverageIgnoreStart
if (\function_exists('getallheaders')) {
$headers = \getallheaders();
$host = \array_change_key_case($headers, \CASE_LOWER)['host'] ?? null;
} else {
foreach ($_SERVER as $key => $value) {
if (\strpos($key, 'HTTP_') === 0) {
$key = str_replace(' ', '-', \ucwords(\strtolower(\str_replace('_', ' ', \substr($key, 5)))));
$headers[$key] = $value;
if ($host === null && $key === 'Host') {
$host = $value;
}
}
}
}
// @codeCoverageIgnoreEnd
$target = ($_SERVER['REQUEST_URI'] ?? '/');
$url = $target;
if (($target[0] ?? '/') === '/' || $target === '*') {
$url = (($_SERVER['HTTPS'] ?? null) === 'on' ? 'https://' : 'http://') . ($host ?? 'localhost') . ($target === '*' ? '' : $target);
}
$body = file_get_contents('php://input');
assert(\is_string($body));
$request = new ServerRequest(
$_SERVER['REQUEST_METHOD'] ?? 'GET',
$url,
$headers,
$body,
substr($_SERVER['SERVER_PROTOCOL'] ?? 'http/1.1', 5),
$_SERVER
);
if ($host === null) {
$request = $request->withoutHeader('Host');
}
if (isset($target[0]) && $target[0] !== '/') {
$request = $request->withRequestTarget($target);
}
$request = $request->withParsedBody($_POST);
// Content-Length / Content-Type are special <3
if ($request->getHeaderLine('Content-Length') === '') {
$request = $request->withoutHeader('Content-Length');
}
if ($request->getHeaderLine('Content-Type') === '' && !isset($_SERVER['HTTP_CONTENT_TYPE'])) {
$request = $request->withoutHeader('Content-Type');
}
return $request;
}
/**
* @param ResponseInterface $response
*/
public function sendResponse(ResponseInterface $response): void
{
$status = $response->getStatusCode();
$body = $response->getBody();
if ($status === Response::STATUS_NO_CONTENT) {
// `204 No Content` MUST NOT include "Content-Length" response header
$response = $response->withoutHeader('Content-Length');
} elseif (!$response->hasHeader('Content-Length') && $body->getSize() !== null && ($status !== Response::STATUS_NOT_MODIFIED || $body->getSize() !== 0)) {
// automatically assign "Content-Length" response header if known and not already present
$response = $response->withHeader('Content-Length', (string) $body->getSize());
}
// remove default "Content-Type" header set by PHP (default_mimetype)
if (!$response->hasHeader('Content-Type')) {
header('Content-Type:');
header_remove('Content-Type');
}
// send all headers without applying default "; charset=utf-8" set by PHP (default_charset)
$old = ini_get('default_charset');
assert(\is_string($old));
ini_set('default_charset', '');
foreach ($response->getHeaders() as $name => $values) {
foreach ($values as $value) {
header($name . ': ' . $value, false);
}
}
ini_set('default_charset', $old);
header($_SERVER['SERVER_PROTOCOL'] . ' ' . $status . ' ' . $response->getReasonPhrase());
if (($_SERVER['REQUEST_METHOD'] ?? '') === 'HEAD' || $status === Response::STATUS_NO_CONTENT || $status === Response::STATUS_NOT_MODIFIED) {
$body->close();
return;
}
if ($body instanceof ReadableStreamInterface) {
// try to disable nginx buffering (http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering)
if (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'nginx') === 0) {
header('X-Accel-Buffering: no');
}
// clear output buffer to show streaming output (default in cli-server)
if (\PHP_SAPI === 'cli-server') {
\ob_end_flush(); // @codeCoverageIgnore
}
// flush data whenever stream reports one data chunk
$body->on('data', function ($chunk) {
echo $chunk;
flush();
});
} else {
echo $body;
}
}
}