1
|
|
|
<?php |
2
|
|
|
namespace nebula\component\request; |
3
|
|
|
|
4
|
|
|
use nebula\component\request\Request; |
5
|
|
|
use nebula\component\request\header\HeaderItem; |
6
|
|
|
use nebula\component\request\attribute\AttributeTrait; |
7
|
|
|
use nebula\component\request\attribute\AttributeAbstract; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* HTTP请求解析器 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class Request implements AttributeAbstract |
14
|
|
|
{ |
15
|
|
|
use AttributeTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* 请求方法 |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $method; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* 请求头 |
26
|
|
|
* |
27
|
|
|
* @var HeaderItem[] |
28
|
|
|
*/ |
29
|
|
|
protected $headers; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 服务器属性 |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $server; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 构建请求 |
40
|
|
|
* |
41
|
|
|
* @param string $method |
42
|
|
|
* @param string $uri |
43
|
|
|
* @param HeaderItem[] $headers |
44
|
|
|
*/ |
45
|
|
|
public function __construct(string $method, string $uri, array $headers = []) |
46
|
|
|
{ |
47
|
|
|
$this->uri = $uri; |
48
|
|
|
$this->method = strtoupper($method); |
49
|
|
|
$this->headers = $headers; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get 请求方法 |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getMethod(): string |
58
|
|
|
{ |
59
|
|
|
return $this->method; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 获取请求头 |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* @param mixed $default |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function getHeader(string $name, $default =null) |
70
|
|
|
{ |
71
|
|
|
if (array_key_exists(strtolower($name), $this->headers)) { |
72
|
|
|
return $this->headers[$name]; |
73
|
|
|
} |
74
|
|
|
return $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 判断请求头 |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
|
|
public function hasHeader(string $name) |
84
|
|
|
{ |
85
|
|
|
return $this->getHeader($name) !== null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* 获取请求IP |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function ip() |
94
|
|
|
{ |
95
|
|
|
static $ipFrom = ['HTTP_CLIENT_IP','HTTP_X_FORWARDED_FOR','HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP','HTTP_FORWARDED_FOR','HTTP_FORWARDED','REMOTE_ADDR']; |
96
|
|
|
foreach ($ipFrom as $key) { |
97
|
|
|
if (array_key_exists($key, $_SERVER)) { |
98
|
|
|
foreach (explode(',', $_SERVER[$key]) as $ip) { |
99
|
|
|
$ip = trim($ip); |
100
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { |
101
|
|
|
return $ip; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
return '127.0.0.1'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* 从环境构建请求 |
111
|
|
|
* |
112
|
|
|
* @param array|null $server |
113
|
|
|
* @return Request |
114
|
|
|
*/ |
115
|
|
|
public static function buildFromServer(?array $server = null): Request |
116
|
|
|
{ |
117
|
|
|
$simulate = false; |
118
|
|
|
if ($server === null) { |
119
|
|
|
$server = $_SERVER; |
120
|
|
|
$simulate = true; |
121
|
|
|
} |
122
|
|
|
$method = strtoupper($server['REQUEST_METHOD'] ?? 'GET'); |
123
|
|
|
$uri = $server['REQUEST_Uri']??'/'; |
124
|
|
|
$headers = HeaderItem::buildFromServer($server ?: $_SERVER); |
125
|
|
|
$request = new Request($method, $uri, $headers); |
126
|
|
|
$request->setDocumentRoot($server['DOCUMENT_ROOT']); |
127
|
|
|
if ($simulate && $server !== null) { |
128
|
|
|
$request->setServer($server); |
129
|
|
|
} |
130
|
|
|
return $request; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get 服务器属性 |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getServer() |
139
|
|
|
{ |
140
|
|
|
return $this->server ?? $_SERVER; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set 服务器属性 |
145
|
|
|
* |
146
|
|
|
* @param array $server 服务器属性 |
147
|
|
|
* |
148
|
|
|
* @return self |
149
|
|
|
*/ |
150
|
|
|
public function setServer(array $server) |
151
|
|
|
{ |
152
|
|
|
$this->server = $server; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|