Repository navigation
Expand file tree
/
Copy pathResponse.php
More file actions
351 lines (297 loc) · 7.85 KB
/
Copy pathResponse.php
File metadata and controls
351 lines (297 loc) · 7.85 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
namespace MyQEE\Server\Http;
use MyQEE\Server\Server;
class Response extends \Swoole\Http\Response
{
public $fd = 0;
/**
* @var \Swoole\Http\Request
*/
public $request;
/**
* @var array
*/
public $header = [];
public $cookie = [];
/**
* 默认超时时间
*
* @var int
*/
public $keepAlive = 180;
protected $headerIsSend = false;
protected $status = 200;
protected $gzipLevel = 0;
protected $chunked = false;
protected $isClose = false;
/**
* HTTP status codes and messages
*
* @var array
*/
public static $messages = [
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Moved Temporarily',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
507 => 'Insufficient Storage',
509 => 'Bandwidth Limit Exceeded'
];
public function __destruct()
{
if (!$this->isClose)
{
$this->end();
}
}
public function sendHeader($contentLength = null)
{
if ($this->headerIsSend)return;
# 只执行一次
$this->headerIsSend = true;
# 拼接投信息
$head = "HTTP/1.1 {$this->status} ". self::$messages[$this->status] ."\r\n";
$header = $this->header + [
'Server' => 'aaa',
'Connection' => 'keep-alive',
'Date' => date('D, d M Y H:i:s \G\M\T'),
'Content-Type' => 'text/html',
];
if ($header['Connection'] === 'keep-alive')
{
$header['Keep-Alive'] = $this->keepAlive;
}
if ($this->chunked)
{
$header['Transfer-Encoding'] = 'chunked';
}
elseif ($contentLength)
{
$header['Content-Length'] = $contentLength;
}
foreach ($header as $k => $v)
{
$head .= "{$k}: {$v}\r\n";
}
if ($this->cookie)
{
foreach (array_unique($this->cookie) as $cookie)
{
$head .= "Set-Cookie: {$cookie}\r\n";
}
}
if ($this->gzipLevel)
{
$head .= "Content-Encoding: gzip\r\n";
}
$head .= "\r\n";
# 发送头信息
Server::$instance->server->send($this->fd, $head);
}
/**
* 启用Http-Chunk分段向浏览器发送数据
*
* @param $html
*/
public function write($html)
{
if ($this->isClose)
{
Server::$instance->warn('Http request is end.');
return;
}
if (!$this->headerIsSend)
{
$this->chunked = true;
$this->sendHeader();
}
if ($this->gzipLevel)
{
$html = gzencode($html, $this->gzipLevel);
}
$html = dechex(strlen($html)) ."\r\n". $html ."\r\n";
Server::$instance->server->send($this->fd, $html);
}
/**
* 结束Http响应,发送HTML内容
*
* @param string $html
*/
public function end($html = '')
{
if ($this->isClose)
{
Server::$instance->warn('Http request is end.');
return;
}
$this->isClose = true;
if (!$this->chunked)
{
if ($this->gzipLevel)
{
$html = gzencode($html, $this->gzipLevel);
}
if (!$this->headerIsSend)
{
$this->sendHeader(strlen($html));
}
if ($html)
{
Server::$instance->server->send($this->fd, $html);
}
}
else
{
if (!$this->headerIsSend)
{
$this->sendHeader();
}
Server::$instance->server->send($this->fd, "0\r\n\r\n");
}
//Server::$instance->server->close($this->fd);
}
/**
* 发送文件到浏览器
*
* * $filename 要发送的文件名称,文件不存在或没有访问权限sendfile会失败
* * 底层无法推断要发送文件的MIME格式因此需要应用代码指定Content-Type
* * 调用sendfile后会自定执行end,中途不得使用Http-Chunk
* * sendfile不支持gzip压缩
*
* @param $filename
* @return bool
*/
public function sendfile($filename, $offset = null)
{
if (!is_file($filename))return false;
if (substr($filename, -4) === '.php')return false;
if (!$this->headerIsSend)
{
$this->sendHeader(filesize($filename));
}
return Server::$instance->server->sendfile($this->fd, $filename, $offset);
}
/**
* 设置Http头信息
*
* @param $key
* @param $value
*/
public function header($key, $value, $ucwords = null)
{
$k = explode('-', strtolower($key));
$h = function(& $v, $k)
{
$v = ucfirst($v);
};
array_walk($k, $h);
$key = implode('-', $k);
if ($key === 'Set-Cookie')
{
$this->cookie[] = $value;
return;
}
$this->header[$key] = $value;
}
/**
* 设置Cookie
*
* @param string $key
* @param string $value
* @param int $expire
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httponly
*/
public function cookie($key, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = null)
{
$cookie = "{$key}={$value}";
if ($expire)
{
$cookie .= '; expires='. date('D, d M Y H:i:s \G\M\T', $expire);
}
if ($path)
{
$cookie .= "; path={$path}";
}
if ($domain)
{
$cookie .= "; domain={$domain}";
}
if ($secure)
{
$cookie .= "; secure";
}
if ($httponly)
{
$cookie .= "; httponly";
}
$this->cookie[] = $cookie;
}
/**
* 设置HttpCode,如404, 501, 200
*
* @param $code
*/
public function status($code)
{
$this->status = $code;
}
/**
* 设置Http压缩格式
*
* @param int $level
*/
function gzip($level = 1)
{
if ($this->headerIsSend)return;
$this->gzipLevel = $level;
}
}