forked from reactphp/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
96 lines (84 loc) · 3.11 KB
/
Server.php
File metadata and controls
96 lines (84 loc) · 3.11 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
<?php
namespace React\Http;
use React\Http\Io\IniUtil;
use React\Http\Io\MiddlewareRunner;
use React\Http\Middleware\LimitConcurrentRequestsMiddleware;
use React\Http\Middleware\RequestBodyBufferMiddleware;
use React\Http\Middleware\RequestBodyParserMiddleware;
use React\Socket\ServerInterface;
/**
* Facade around StreamingServer with sane defaults for 80% of the use cases.
* The requests passed to your callable are fully compatible with PSR-7 because
* the body of the requests are fully buffered and parsed, unlike StreamingServer
* where the body is a raw ReactPHP stream.
*
* Wraps StreamingServer with the following middleware:
* - LimitConcurrentRequestsMiddleware
* - RequestBodyBufferMiddleware
* - RequestBodyParserMiddleware (only when enable_post_data_reading is true (default))
* - The callable you in passed as first constructor argument
*
* All middleware use their default configuration, which can be controlled with
* the the following configuration directives from php.ini:
* - upload_max_filesize
* - post_max_size
* - max_input_nesting_level
* - max_input_vars
* - file_uploads
* - max_file_uploads
* - enable_post_data_reading
*/
final class Server
{
/**
* @internal
*/
const MAXIMUM_CONCURRENT_REQUESTS = 100;
/**
* @var StreamingServer
*/
private $streamingServer;
/**
* @see StreamingServer::__construct()
*/
public function __construct($callback)
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException();
}
$middleware = array();
$middleware[] = new LimitConcurrentRequestsMiddleware($this->getConcurrentRequestsLimit());
$middleware[] = new RequestBodyBufferMiddleware();
// Checking for an empty string because that is what a boolean
// false is returned as by ini_get depending on the PHP version.
// @link http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading
// @link http://php.net/manual/en/function.ini-get.php#refsect1-function.ini-get-notes
// @link https://3v4l.org/qJtsa
$enablePostDataReading = ini_get('enable_post_data_reading');
if ($enablePostDataReading !== '') {
$middleware[] = new RequestBodyParserMiddleware();
}
$middleware[] = $callback;
$this->streamingServer = new StreamingServer(new MiddlewareRunner($middleware));
}
/**
* @see StreamingServer::listen()
*/
public function listen(ServerInterface $server)
{
$this->streamingServer->listen($server);
}
private function getConcurrentRequestsLimit()
{
if (ini_get('memory_limit') == -1) {
return self::MAXIMUM_CONCURRENT_REQUESTS;
}
$availableMemory = IniUtil::iniSizeToBytes(ini_get('memory_limit')) / 4;
$concurrentRequests = $availableMemory / IniUtil::iniSizeToBytes(ini_get('post_max_size'));
$concurrentRequests = ceil($concurrentRequests);
if ($concurrentRequests >= self::MAXIMUM_CONCURRENT_REQUESTS) {
return self::MAXIMUM_CONCURRENT_REQUESTS;
}
return $concurrentRequests;
}
}