-
Notifications
You must be signed in to change notification settings - Fork 15
/
Request.php
167 lines (150 loc) · 4.47 KB
/
Request.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/5/24
* Time: 下午3:40
*/
namespace EasySwoole\Http;
use EasySwoole\Http\Message\ServerRequest;
use EasySwoole\Http\Message\Stream;
use EasySwoole\Http\Message\UploadFile;
use EasySwoole\Http\Message\Uri;
class Request extends ServerRequest
{
private $request;
function __construct(\Swoole\Http\Request $request = null)
{
if($request){
$this->request = $request;
$this->initHeaders();
$protocol = str_replace('HTTP/', '', $request->server['server_protocol']) ;
//为单元测试准备
if($request->fd){
$body = new Stream($request->rawContent());
}else{
$body = new Stream('');
}
$uri = $this->initUri();
$files = $this->initFiles();
$method = $request->server['request_method'];
parent::__construct($method, $uri, null, $body, $protocol, $request->server);
$this->withCookieParams($this->initCookie())->withQueryParams($this->initGet())->withParsedBody($this->initPost())->withUploadedFiles($files);
}
}
function getRequestParam(...$key)
{
$data = $this->getParsedBody() + $this->getQueryParams();
if(empty($key)){
return $data;
}else{
$res = [];
foreach ($key as $item){
$res[$item] = $data[$item] ?? null;
}
if(count($key) == 1){
return array_shift($res);
}else{
return $res;
}
}
}
function getSwooleRequest()
{
return $this->request;
}
private function initUri()
{
$uri = new Uri();
$uri->withScheme("http");
$uri->withPath($this->request->server['path_info']);
$query = $this->request->server['query_string'] ?? '';
$uri->withQuery($query);
//host与port以header为准,防止经过proxy
if(isset($this->request->header['host'])){
$host = $this->request->header['host'];
$host = explode(":",$host);
$realHost = $host[0];
$port = $host[1] ?? null;
}else{
$realHost = '127.0.0.1';
$port = $this->request->server['server_port'];
}
$uri->withHost($realHost);
$uri->withPort($port);
return $uri;
}
private function initHeaders()
{
$headers = $this->request->header ?? [];
foreach ($headers as $header => $val){
$this->withAddedHeader($header,$val);
}
}
private function initFiles(): array
{
if(isset($this->request->ignoreFile) && $this->request->ignoreFile){
return [];
}
if(isset($this->request->files)){
$normalized = array();
foreach($this->request->files as $key => $value){
//如果是二维数组文件
if(is_array($value) && empty($value['tmp_name'])){
$normalized[$key] = [];
foreach($value as $file){
if (empty($file['tmp_name'])){
continue;
}
$file = $this->initFile($file);
if($file){
$normalized[$key][] = $file;
}
}
}else{
$file = $this->initFile($value);
if($file){
$normalized[$key] = $file;
}
}
}
return $normalized;
}else{
return [];
}
}
private function initFile(array $file)
{
if(empty($file['tmp_name'])){
return null;
}
return new UploadFile(
$file['tmp_name'],
(int) $file['size'],
(int) $file['error'],
$file['name'],
$file['type']
);
}
private function initCookie()
{
return $this->request->cookie ?? [];
}
private function initPost()
{
return $this->request->post ?? [];
}
private function initGet()
{
return $this->request->get ?? [];
}
final public function __toString():string
{
return Utility::toString($this);
}
public function __destruct()
{
$this->getBody()->close();
$this->request = null;
}
}