|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request as IlluminateRequest; |
|
|
|
|
|
|
6
|
|
|
use Swoole\Http\Request as SwooleRequest; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class Request |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
protected $swooleRequest; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(SwooleRequest $request) |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
$this->swooleRequest = $request; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Convert SwooleRequest to IlluminateRequest |
|
20
|
|
|
* @param array $rawServer |
|
|
|
|
|
|
21
|
|
|
* @param array $rawEnv |
|
|
|
|
|
|
22
|
|
|
* @return IlluminateRequest |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
public function toIlluminateRequest(array $rawServer = [], array $rawEnv = []) |
|
25
|
|
|
{ |
|
26
|
|
|
$__GET = isset($this->swooleRequest->get) ? $this->swooleRequest->get : []; |
|
27
|
|
|
$__POST = isset($this->swooleRequest->post) ? $this->swooleRequest->post : []; |
|
28
|
|
|
$__COOKIE = isset($this->swooleRequest->cookie) ? $this->swooleRequest->cookie : []; |
|
29
|
|
|
$server = isset($this->swooleRequest->server) ? $this->swooleRequest->server : []; |
|
30
|
|
|
$headers = isset($this->swooleRequest->header) ? $this->swooleRequest->header : []; |
|
31
|
|
|
$__FILES = isset($this->swooleRequest->files) ? $this->swooleRequest->files : []; |
|
32
|
|
|
$__CONTENT = empty($__FILES) ? $this->swooleRequest->rawContent() : ''; // Cannot call rawContent() to avoid double the file memory when uploading a file. |
|
33
|
|
|
$_REQUEST = []; |
|
34
|
|
|
$_SESSION = []; |
|
35
|
|
|
|
|
36
|
|
|
static $headerServerMapping = [ |
|
37
|
|
|
'x-real-ip' => 'REMOTE_ADDR', |
|
38
|
|
|
'x-real-port' => 'REMOTE_PORT', |
|
39
|
|
|
'server-protocol' => 'SERVER_PROTOCOL', |
|
40
|
|
|
'server-name' => 'SERVER_NAME', |
|
41
|
|
|
'server-addr' => 'SERVER_ADDR', |
|
42
|
|
|
'server-port' => 'SERVER_PORT', |
|
43
|
|
|
'scheme' => 'REQUEST_SCHEME', |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
$_ENV = $rawEnv; |
|
47
|
|
|
$_SERVER = $rawServer; |
|
48
|
|
|
foreach ($headers as $key => $value) { |
|
49
|
|
|
// Fix client && server's info |
|
50
|
|
|
if (isset($headerServerMapping[$key])) { |
|
51
|
|
|
$server[$headerServerMapping[$key]] = $value; |
|
52
|
|
|
} else { |
|
53
|
|
|
$key = str_replace('-', '_', $key); |
|
54
|
|
|
$server['http_' . $key] = $value; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
$server = array_change_key_case($server, CASE_UPPER); |
|
58
|
|
|
$_SERVER = array_merge($_SERVER, $server); |
|
59
|
|
|
if (isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] === 'https') { |
|
60
|
|
|
$_SERVER['HTTPS'] = 'on'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Fix REQUEST_URI with QUERY_STRING |
|
64
|
|
|
if (strpos($_SERVER['REQUEST_URI'], '?') === false |
|
65
|
|
|
&& isset($_SERVER['QUERY_STRING']) |
|
66
|
|
|
&& $_SERVER['QUERY_STRING'] !== '' |
|
67
|
|
|
) { |
|
68
|
|
|
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Fix argv & argc |
|
72
|
|
|
if (!isset($_SERVER['argv'])) { |
|
73
|
|
|
$_SERVER['argv'] = isset($GLOBALS['argv']) ? $GLOBALS['argv'] : []; |
|
74
|
|
|
$_SERVER['argc'] = isset($GLOBALS['argc']) ? $GLOBALS['argc'] : 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Initialize laravel request |
|
78
|
|
|
IlluminateRequest::enableHttpMethodParameterOverride(); |
|
79
|
|
|
$request = IlluminateRequest::createFromBase(new \Symfony\Component\HttpFoundation\Request($__GET, $__POST, [], $__COOKIE, $__FILES, $_SERVER, $__CONTENT)); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
if (0 === strpos($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded') |
|
82
|
|
|
&& in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH']) |
|
83
|
|
|
) { |
|
84
|
|
|
parse_str($request->getContent(), $data); |
|
85
|
|
|
$request->request = new ParameterBag($data); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $request; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|