1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Igni\Http\Server; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class HttpConfiguration |
7
|
|
|
* @package Igni\Http\Server |
8
|
|
|
*/ |
9
|
|
|
class HttpConfiguration |
10
|
|
|
{ |
11
|
|
|
public const DISPATCH_ROUND_ROBIN = 1; |
12
|
|
|
public const DISPATCH_MODULO = 2; |
13
|
|
|
public const DISPATCH_PREEMPTIVE_ASSIGNMENT = 3; |
14
|
|
|
public const DEFAULT_ADDRESS = '0.0.0.0'; |
15
|
|
|
public const DEFAULT_PORT = 8080; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $settings = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* HttpConfiguration constructor. |
24
|
|
|
* |
25
|
|
|
* @param string $address |
26
|
|
|
* @param int $port |
27
|
|
|
*/ |
28
|
5 |
|
public function __construct(string $address = self::DEFAULT_ADDRESS, int $port = self::DEFAULT_PORT) |
29
|
|
|
{ |
30
|
5 |
|
$this->settings['address'] = $address; |
31
|
5 |
|
$this->settings['port'] = $port; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Checks if ssl is enabled. |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
2 |
|
public function isSslEnabled(): bool |
40
|
|
|
{ |
41
|
2 |
|
return isset($this->settings['ssl_cert_file']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Enables ssl on the server |
46
|
|
|
* |
47
|
|
|
* @param string $certFile |
48
|
|
|
* @param string $keyFile |
49
|
|
|
*/ |
50
|
1 |
|
public function enableSsl(string $certFile, string $keyFile): void |
51
|
|
|
{ |
52
|
|
|
$this->settings += [ |
53
|
1 |
|
'ssl_cert_file' => $certFile, |
54
|
1 |
|
'ssl_key_file' => $keyFile, |
55
|
|
|
]; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Checks if server is daemonized. |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function isDaemonEnabled(): bool |
64
|
|
|
{ |
65
|
|
|
return isset($this->settings['daemonize']) && $this->settings['daemonize']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sets the max tcp connection number of the server. |
70
|
|
|
* |
71
|
|
|
* @param int $max |
72
|
|
|
*/ |
73
|
|
|
public function setMaxConnections(int $max = 10000): void |
74
|
|
|
{ |
75
|
|
|
$this->settings['max_conn'] = $max; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets the number of worker processes. |
80
|
|
|
* |
81
|
|
|
* @param int $count |
82
|
|
|
*/ |
83
|
|
|
public function setWorkers(int $count = 1): void |
84
|
|
|
{ |
85
|
|
|
$this->settings['worker_num'] = $count; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets the number of requests processed by the worker process before been recycled. |
90
|
|
|
* |
91
|
|
|
* @param int $max |
92
|
|
|
*/ |
93
|
|
|
public function setMaxRequests(int $max = 0): void |
94
|
|
|
{ |
95
|
|
|
$this->settings['max_request'] = $max; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Sets path to the file that will be used to persist server log. |
100
|
|
|
* |
101
|
|
|
* @param string $filename |
102
|
|
|
*/ |
103
|
|
|
public function setLogFile(string $filename): void |
104
|
|
|
{ |
105
|
|
|
$this->settings['log_file'] = $filename; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets the maximum number of pending connections. This refers to the number of clients |
110
|
|
|
* that can be waiting to be served. Exceeding this number results in the client getting |
111
|
|
|
* an error when attempting to connect. |
112
|
|
|
* |
113
|
|
|
* @param int $max |
114
|
|
|
*/ |
115
|
|
|
public function setMaximumBacklog(int $max = 0): void |
116
|
|
|
{ |
117
|
|
|
$this->settings['backlog'] = $max; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets dispatch mode for child processes. |
122
|
|
|
* |
123
|
|
|
* @param int $mode |
124
|
|
|
*/ |
125
|
|
|
public function setDispatchMode(int $mode = self::DISPATCH_ROUND_ROBIN): void |
126
|
|
|
{ |
127
|
|
|
$this->settings['dispatch_mode'] = $mode; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Allows server to be run as a background process. |
132
|
|
|
* |
133
|
|
|
* @param string $pid |
134
|
|
|
*/ |
135
|
|
|
public function enableDaemon(string $pid): void |
136
|
|
|
{ |
137
|
|
|
$this->settings += [ |
138
|
|
|
'daemonize' => true, |
139
|
|
|
'pid_file' => $pid, |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Sets temporary dir for uploaded files |
145
|
|
|
* |
146
|
|
|
* @param string $dir |
147
|
|
|
*/ |
148
|
|
|
public function setUploadDir(string $dir): void |
149
|
|
|
{ |
150
|
|
|
$this->settings['upload_tmp_dir'] = $dir; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns swoole compatible settings array. |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
2 |
|
public function getSettings(): array |
159
|
|
|
{ |
160
|
2 |
|
return $this->settings; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|