This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Weew\HttpServer; |
||
4 | |||
5 | use Exception; |
||
6 | use Weew\Timer\ITimer; |
||
7 | use Weew\Timer\Timer; |
||
8 | |||
9 | class HttpServer implements IHttpServer { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $host; |
||
14 | |||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $port; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $root; |
||
24 | |||
25 | /** |
||
26 | * @var float |
||
27 | */ |
||
28 | protected $waitForServer; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $enableOutput; |
||
34 | |||
35 | /** |
||
36 | * @var Commander |
||
37 | */ |
||
38 | protected $commander; |
||
39 | |||
40 | /** |
||
41 | * @var Messenger |
||
42 | */ |
||
43 | protected $messenger; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $logFile = '/dev/null'; |
||
49 | |||
50 | /** |
||
51 | * @param $host |
||
52 | * @param $port |
||
53 | * @param $root |
||
54 | * @param float $waitForServer |
||
55 | * @param bool $enableOutput |
||
56 | */ |
||
57 | public function __construct( |
||
58 | $host, $port, $root, |
||
59 | $waitForServer = 5.0, |
||
60 | $enableOutput = false |
||
61 | ) { |
||
62 | $this->host = $host; |
||
63 | $this->port = $port; |
||
64 | $this->root = $root; |
||
65 | $this->enableOutput = $enableOutput; |
||
66 | $this->waitForServer = $waitForServer; |
||
67 | |||
68 | $this->commander = $this->createCommander(); |
||
69 | $this->messenger = $this->createMessenger(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Disable server output. |
||
74 | */ |
||
75 | public function disableOutput() { |
||
76 | $this->enableOutput = false; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Enable server output. |
||
81 | */ |
||
82 | public function enableOutput() { |
||
83 | $this->enableOutput = true; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * File the server will be logging into. |
||
88 | * Use /dev/null to disable logging |
||
89 | * |
||
90 | * @param string $logFile |
||
91 | */ |
||
92 | public function setLogFile($logFile) { |
||
93 | $this->logFile = $logFile; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Start server. |
||
98 | */ |
||
99 | public function start() { |
||
100 | if ($this->isRunning()) { |
||
101 | $this->echoMessage( |
||
102 | $this->messenger->getServerIsAlreadyRunningMessage( |
||
103 | date('r'), $this->host, $this->port, $this->getPid() |
||
104 | ) |
||
105 | ); |
||
106 | } else { |
||
107 | $this->startServer(); |
||
108 | $this->registerShutdownHandler(); |
||
109 | $this->waitForServerToStart(); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Stop server. |
||
115 | * |
||
116 | * @throws Exception |
||
117 | */ |
||
118 | public function stop() { |
||
119 | if ($this->isRunning()) { |
||
120 | $this->stopServer(); |
||
121 | $this->waitForServerToStop(); |
||
122 | } else { |
||
123 | $this->echoMessage( |
||
124 | $this->messenger->getServerIsNotRunningMessage( |
||
125 | date('r'), $this->host, $this->port |
||
126 | ) |
||
127 | ); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return bool |
||
133 | */ |
||
134 | View Code Duplication | public function isRunning() { |
|
0 ignored issues
–
show
|
|||
135 | $command = $this->commander |
||
136 | ->getPidCommand($this->host, $this->port); |
||
137 | exec($command, $output); |
||
138 | |||
139 | return count($output) > 0; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param string $message |
||
144 | */ |
||
145 | public function echoMessage($message = '') { |
||
146 | if ( ! $this->enableOutput) { |
||
147 | return; |
||
148 | } |
||
149 | |||
150 | if ($message) { |
||
151 | $message = s('[HTTP SERVER] %s', $message); |
||
152 | } |
||
153 | |||
154 | echo PHP_EOL . $message . PHP_EOL; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return mixed |
||
159 | */ |
||
160 | View Code Duplication | public function getPid() { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
161 | $command = $this->commander |
||
162 | ->getPidCommand($this->host, $this->port); |
||
163 | exec($command, $output); |
||
164 | |||
165 | return array_get($output, 0); |
||
0 ignored issues
–
show
It seems like
$output can also be of type null ; however, array_get() does only seem to accept array , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Start server. |
||
170 | */ |
||
171 | protected function startServer() { |
||
172 | $command = $this->commander |
||
173 | ->getStartCommand($this->host, $this->port, $this->root, $this->logFile); |
||
174 | exec($command, $output); |
||
175 | |||
176 | $this->echoMessage($this->messenger->getStartMessage( |
||
177 | date('r'), $this->host, $this->port, $this->getPid() |
||
178 | )); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Stop server. |
||
183 | */ |
||
184 | protected function stopServer() { |
||
185 | $pid = $this->getPid(); |
||
186 | $command = $this->commander->getStopCommand($pid); |
||
187 | exec($command); |
||
188 | |||
189 | $this->echoMessage( |
||
190 | $this->messenger->getStopMessage(date('r'), $pid) |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Fail-safe server shutdown. |
||
196 | */ |
||
197 | protected function registerShutdownHandler() { |
||
198 | register_shutdown_function([$this, 'stop']); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @throws Exception |
||
203 | */ |
||
204 | View Code Duplication | protected function waitForServerToStart() { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
205 | if ($this->waitForServer > 0) { |
||
206 | $timer = $this->createTimer(); |
||
207 | $timer->start(); |
||
208 | |||
209 | while ( ! $this->isRunning()) { |
||
210 | if ($timer->getDuration() < $this->waitForServer) { |
||
211 | usleep(100000); // 0.1 second |
||
212 | } else { |
||
213 | throw new Exception( |
||
214 | s('Could not start server after %d seconds.', $this->waitForServer) |
||
215 | ); |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @throws Exception |
||
223 | */ |
||
224 | View Code Duplication | protected function waitForServerToStop() { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
225 | if ($this->waitForServer > 0) { |
||
226 | $timer = $this->createTimer(); |
||
227 | $timer->start(); |
||
228 | |||
229 | while ($this->isRunning()) { |
||
230 | if ($timer->getDuration() < $this->waitForServer) { |
||
231 | usleep(100000); // 0.1 second |
||
232 | } else { |
||
233 | throw new Exception( |
||
234 | s('Could not stop server after %d seconds.', $this->waitForServer) |
||
235 | ); |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return ITimer |
||
243 | */ |
||
244 | protected function createTimer() { |
||
245 | return new Timer(); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @return Commander |
||
250 | */ |
||
251 | protected function createCommander() { |
||
252 | return new Commander(); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @return Messenger |
||
257 | */ |
||
258 | protected function createMessenger() { |
||
259 | return new Messenger(); |
||
260 | } |
||
261 | } |
||
262 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.