1
|
|
|
<?php |
2
|
|
|
namespace Laravoole\Wrapper; |
3
|
|
|
|
4
|
|
|
use swoole_http_server; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
7
|
|
|
|
8
|
|
|
class SwooleHttpWrapper extends Swoole implements ServerInterface |
9
|
|
|
{ |
10
|
|
|
use HttpTrait; |
11
|
|
|
|
12
|
4 |
|
public function __construct($host, $port) |
13
|
|
|
{ |
14
|
4 |
|
$this->server = new swoole_http_server($host, $port); |
15
|
4 |
|
} |
16
|
|
|
|
17
|
8 |
|
public function start() |
18
|
|
|
{ |
19
|
8 |
|
if (!empty($this->handler_config)) { |
20
|
8 |
|
$this->server->set($this->handler_config); |
|
|
|
|
21
|
4 |
|
} |
22
|
8 |
|
$this->callbacks = array_merge([ |
23
|
8 |
|
'Request' => [$this, 'onRequest'], |
24
|
8 |
|
], $this->callbacks); |
25
|
8 |
|
parent::start(); |
26
|
|
|
} |
27
|
|
|
|
28
|
8 |
|
public function onWorkerStart($serv, $worker_id) |
29
|
|
|
{ |
30
|
8 |
|
parent::onWorkerStart($serv, $worker_id); |
31
|
8 |
|
$this->accept_gzip = config('laravoole.base_config.gzip'); |
32
|
8 |
|
} |
33
|
|
|
|
34
|
8 |
|
public function onRequest($request, $response) |
35
|
|
|
{ |
36
|
|
|
// convert request |
37
|
8 |
|
$this->ucHeaders($request); |
38
|
8 |
|
if (config('laravoole.base_config.deal_with_public')) { |
39
|
8 |
|
if ($status = $this->handleStaticFile($request, $response)) { |
40
|
4 |
|
return $status; |
41
|
|
|
} |
42
|
4 |
|
} |
43
|
|
|
// provide response callback |
44
|
8 |
|
$illuminateResponse = parent::handleRequest($request); |
|
|
|
|
45
|
8 |
|
return $this->handleResponse($response, $illuminateResponse, isset($request->header['Accept-Encoding']) ? $request->header['Accept-Encoding'] : ''); |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
protected function ucHeaders($request) |
49
|
|
|
{ |
50
|
|
|
// merge headers into server which ar filted by swoole |
51
|
|
|
// make a new array when php 7 has different behavior on foreach |
52
|
8 |
|
$new_header = []; |
53
|
8 |
|
$uc_header = []; |
54
|
8 |
|
foreach ($request->header as $key => $value) { |
55
|
8 |
|
$new_header['http_' . $key] = $value; |
56
|
8 |
|
$uc_header[ucwords($key, '-')] = $value; |
57
|
4 |
|
} |
58
|
8 |
|
$server = array_merge($request->server, $new_header); |
59
|
|
|
|
60
|
|
|
// swoole has changed all keys to lower case |
61
|
8 |
|
$server = array_change_key_case($server, CASE_UPPER); |
62
|
8 |
|
$request->server = $server; |
63
|
8 |
|
$request->header = $uc_header; |
64
|
8 |
|
return $request; |
65
|
|
|
} |
66
|
|
|
|
67
|
8 |
|
protected function handleStaticFile($request, $response) |
68
|
|
|
{ |
69
|
8 |
|
static $public_path; |
70
|
8 |
|
if (!$public_path) { |
71
|
8 |
|
$app = $this->app; |
72
|
8 |
|
$public_path = $app->make('path.public'); |
73
|
|
|
|
74
|
4 |
|
} |
75
|
|
|
|
76
|
8 |
|
$uri = $request->server['REQUEST_URI']; |
77
|
8 |
|
$file = realpath($public_path . $uri); |
78
|
8 |
|
if (is_file($file)) { |
79
|
4 |
|
if (!strncasecmp($file, $uri, strlen($public_path))) { |
80
|
|
|
$response->status(403); |
81
|
|
|
$response->end(); |
82
|
|
|
} else { |
83
|
4 |
|
$response->header('Content-Type', get_mime_type($file)); |
84
|
4 |
|
if (!filesize($file)) { |
85
|
|
|
$response->end(); |
86
|
|
|
} else { |
87
|
4 |
|
$response->sendfile($file); |
88
|
|
|
} |
89
|
|
|
} |
90
|
4 |
|
return true; |
91
|
|
|
} |
92
|
8 |
|
return false; |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
public function endResponse($response, $content) |
97
|
|
|
{ |
98
|
8 |
|
if (!is_string($content)) { |
99
|
8 |
|
$response->sendfile(realpath($content())); |
100
|
4 |
|
} else { |
101
|
|
|
// send content & close |
102
|
8 |
|
$response->end($content); |
103
|
|
|
} |
104
|
8 |
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.