-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathHttp.php
484 lines (426 loc) · 15.1 KB
/
Http.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
/**
* This file is part of Adapterman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author Joan Miquel<https://github.com/joanhey>
* @copyright Joan Miquel<https://github.com/joanhey>
* @link https://github.com/joanhey/AdapterMan
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Adapterman;
use Workerman\Connection\TcpConnection;
//use Workerman\Timer;
/**
* http protocol
*/
class Http
{
use ParseMultipart, Session;
/**
* Http status.
*/
public static string $status = '';
/**
* Headers.
*/
public static array $headers = [];
/**
* Cookies.
*/
public static array $cookies = [];
/**
* Cache.
*/
protected static array $cache = [];
/**
* Phrases.
*
* @var array<int,string>
*
* @link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
*/
const CODES = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing', // WebDAV; RFC 2518
103 => 'Early Hints', // RFC 8297
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information', // since HTTP/1.1
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content', // RFC 7233
207 => 'Multi-Status', // WebDAV; RFC 4918
208 => 'Already Reported', // WebDAV; RFC 5842
226 => 'IM Used', // RFC 3229
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // Previously "Moved temporarily"
303 => 'See Other', // since HTTP/1.1
304 => 'Not Modified', // RFC 7232
305 => 'Use Proxy', // since HTTP/1.1
306 => 'Switch Proxy',
307 => 'Temporary Redirect', // since HTTP/1.1
308 => 'Permanent Redirect', // RFC 7538
400 => 'Bad Request',
401 => 'Unauthorized', // RFC 7235
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required', // RFC 7235
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed', // RFC 7232
413 => 'Payload Too Large', // RFC 7231
414 => 'URI Too Long', // RFC 7231
415 => 'Unsupported Media Type', // RFC 7231
416 => 'Range Not Satisfiable', // RFC 7233
417 => 'Expectation Failed',
418 => 'I\'m a teapot', // RFC 2324, RFC 7168
421 => 'Misdirected Request', // RFC 7540
422 => 'Unprocessable Entity', // WebDAV; RFC 4918
423 => 'Locked', // WebDAV; RFC 4918
424 => 'Failed Dependency', // WebDAV; RFC 4918
425 => 'Too Early', // RFC 8470
426 => 'Upgrade Required',
428 => 'Precondition Required', // RFC 6585
429 => 'Too Many Requests', // RFC 6585
431 => 'Request Header Fields Too Large', // RFC 6585
451 => 'Unavailable For Legal Reasons', // RFC 7725
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates', // RFC 2295
507 => 'Insufficient Storage', // WebDAV; RFC 4918
508 => 'Loop Detected', // WebDAV; RFC 5842
510 => 'Not Extended', // RFC 2774
511 => 'Network Authentication Required', // RFC 6585
];
public static function init(): void
{
static::sessionInit();
//static::uploadInit();
}
/**
* Reset.
*
*/
public static function reset(): void
{
static::$status = 'HTTP/1.1 200 OK';
static::$headers = [
'Content-Type' => 'Content-Type: text/html;charset=utf-8',
'Server' => 'Server: workerman',
];
static::$cookies = [];
static::$sessionFile = '';
static::$sessionStarted = false;
}
/**
* The supported HTTP methods
*
* @var string[]
*/
const AVAILABLE_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
/**
* Send a raw HTTP header.
*/
public static function header(string $content, bool $replace = true, int $http_response_code = 0): void
{
if (\str_starts_with($content, 'HTTP')) {
static::$status = $content;
return;
}
$key = \strstr($content, ':', true);
if (empty($key)) {
return;
}
if ('location' === \strtolower($key)) {
if ($http_response_code === 0) {
$http_response_code = 302;
}
static::responseCode($http_response_code);
}
if ($key === 'Set-Cookie') {
static::$cookies[] = $content;
} else {
static::$headers[$key] = $content;
}
}
/**
* Remove previously set headers.
*
*/
public static function headerRemove(?string $name = null): void
{
if ($name === null) {
static::$headers = [];
static::$cookies = [];
return;
}
unset(static::$headers[$name]);
}
/**
* Sets the HTTP response status code.
*
* @param int $code The response code
* @return bool|int The valid status code or FALSE if code is not provided and it is not invoked in a web server environment
*/
public static function responseCode(int $code): bool|int
{
if (isset(static::CODES[$code])) {
static::$status = "HTTP/1.1 $code " . static::CODES[$code];
return $code;
}
return false;
}
/**
* Set cookie.
*
* @see https://www.php.net/manual/en/function.setcookie
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
* @see https://github.com/GoogleChromeLabs/samesite-examples/blob/master/php.md
*/
public static function setCookie(
string $name,
string $value = '',
int $expires = 0,
string $path = '',
string $domain = '',
bool $secure = false,
bool $httponly = false,
string $samesite = '',
): bool
{
if (! static::checkCookieSamesite($samesite)) {
$samesite = '';
}
static::$cookies[] = 'Set-Cookie: ' . $name . '=' . \rawurlencode($value)
. ($domain ? '; Domain=' . $domain : '')
. (($expires === 0) ? '' : '; Max-Age=' . $expires)
. ($path ? '; Path=' . $path : '')
. ($secure ? '; Secure' : '')
. ($httponly ? '; HttpOnly' : '')
. ($samesite ? "; SameSite=$samesite" : '');
return true;
}
// TODO: add setrawcookie
protected static function checkCookieSamesite(string $samesite): bool
{
return \in_array($samesite, ['None', 'Lax', 'Strict']);
}
/**
* Returns a list of response headers sent (or ready to send)
*
* @return array<string>
*/
public static function headers_list(): array
{
return [...static::$cookies, ...static::$headers];
}
/**
* Check the integrity of the package.
*/
public static function input(string $recv_buffer, TcpConnection $connection): int
{
if (isset(static::$cache[$recv_buffer]['input'])) {
return static::$cache[$recv_buffer]['input'];
}
$recv_len = \strlen($recv_buffer);
$crlf_post = \strpos($recv_buffer, "\r\n\r\n");
if (!$crlf_post) {
// Judge whether the package length exceeds the limit.
if ($recv_len >= $connection->maxPackageSize) {
$connection->close();
}
return 0;
}
$head_len = $crlf_post + 4;
$method = \substr($recv_buffer, 0, \strpos($recv_buffer, ' '));
if (!\in_array($method, static::AVAILABLE_METHODS)) {
$connection->send("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", true);
$connection->consumeRecvBuffer($recv_len);
return 0;
}
if ($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD') {
static::$cache[$recv_buffer]['input'] = $head_len;
return $head_len;
}
$match = [];
if (\preg_match("/\r\nContent-Length: ?(\d+)/i", $recv_buffer, $match)) {
$content_length = $match[1] ?? 0;
$total_length = (int) $content_length + $head_len;
if (!isset($recv_buffer[1024])) {
static::$cache[$recv_buffer]['input'] = $total_length;
}
return $total_length;
}
return ($method === 'DELETE' || $method === 'PATCH') ? $head_len : 0;
}
/**
* Parse $_POST、$_GET、$_COOKIE.
*/
public static function decode(string $recv_buffer, TcpConnection $connection): void
{
static::reset();
if (isset(static::$cache[$recv_buffer]['decode'])) {
$cache = static::$cache[$recv_buffer]['decode'];
$_SERVER = $cache['server'];
$_POST = $cache['post'];
$_GET = $cache['get'];
$_COOKIE = $cache['cookie'];
$_REQUEST = $cache['request'];
$GLOBALS['HTTP_RAW_POST_DATA'] = $GLOBALS['HTTP_RAW_REQUEST_DATA'] = '';
return;
}
// Init.
$_POST = $_GET = $_COOKIE = $_REQUEST = $_SESSION = $_FILES = [];
// $_SERVER
$_SERVER = [
'REQUEST_METHOD' => '',
'REQUEST_URI' => '',
'SERVER_PROTOCOL' => '',
'SERVER_ADDR' => $connection->getLocalIp(),
'SERVER_PORT' => $connection->getLocalPort(),
'REMOTE_ADDR' => $connection->getRemoteIp(),
'REMOTE_PORT' => $connection->getRemotePort(),
'SERVER_SOFTWARE' => Adapterman::NAME,
'SERVER_NAME' => '',
'HTTP_HOST' => '',
'HTTP_USER_AGENT' => '',
'HTTP_ACCEPT' => '',
'HTTP_ACCEPT_LANGUAGE' => '',
'HTTP_ACCEPT_ENCODING' => '',
'HTTP_COOKIE' => '',
'HTTP_CONNECTION' => '',
'CONTENT_TYPE' => '',
];
// Parse headers.
[$http_header, $http_body] = \explode("\r\n\r\n", $recv_buffer, 2);
$header_data = \explode("\r\n", $http_header);
[$_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']] = \explode(' ', $header_data[0]);
$http_post_boundary = '';
unset($header_data[0]);
foreach ($header_data as $content) {
// \r\n\r\n
if (empty($content)) {
continue;
}
[$key, $value] = \explode(':', $content, 2);
$key = \str_replace('-', '_', \strtoupper($key));
$value = \trim($value);
$_SERVER['HTTP_' . $key] = $value;
switch ($key) {
// HTTP_HOST
case 'HOST':
$tmp = \explode(':', $value);
$_SERVER['SERVER_NAME'] = $tmp[0];
if (isset($tmp[1])) {
$_SERVER['SERVER_PORT'] = $tmp[1];
}
break;
// cookie
case 'COOKIE':
\parse_str(\str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
break;
// content-type
case 'CONTENT_TYPE':
if (!\preg_match('/boundary="?(\S+)"?/', $value, $match)) {
if ($pos = \strpos($value, ';')) {
$_SERVER['CONTENT_TYPE'] = \substr($value, 0, $pos);
} else {
$_SERVER['CONTENT_TYPE'] = $value;
}
} else {
$_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
$http_post_boundary = '--' . $match[1];
}
break;
case 'CONTENT_LENGTH':
$_SERVER['CONTENT_LENGTH'] = $value;
break;
}
}
// Parse $_POST.
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE']) {
match ($_SERVER['CONTENT_TYPE']) {
'multipart/form-data' => static::parseMultipart($http_body, $http_post_boundary),
'application/json' => $_POST = \json_decode($http_body, true, flags: \JSON_THROW_ON_ERROR) ?? [],
'application/x-www-form-urlencoded' => \parse_str($http_body, $_POST),
default => ''
};
}
// Parse other HTTP action parameters
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'POST') {
$data = [];
match ($_SERVER['CONTENT_TYPE']) {
'application/x-www-form-urlencoded' => \parse_str($http_body, $data),
'application/json' => $data = \json_decode($http_body, true, flags: \JSON_THROW_ON_ERROR) ?? [],
default => ''
};
$_REQUEST = $data;
}
// HTTP_RAW_REQUEST_DATA HTTP_RAW_POST_DATA
$GLOBALS['HTTP_RAW_REQUEST_DATA'] = $GLOBALS['HTTP_RAW_POST_DATA'] = $http_body;
// QUERY_STRING
$_SERVER['QUERY_STRING'] = \parse_url($_SERVER['REQUEST_URI'], \PHP_URL_QUERY);
if ($_SERVER['QUERY_STRING']) {
// $GET
\parse_str($_SERVER['QUERY_STRING'], $_GET);
} else {
$_SERVER['QUERY_STRING'] = '';
}
// REQUEST
$_REQUEST = [...$_GET, ...$_POST, ...$_REQUEST];
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
static::$cache[$recv_buffer]['decode'] = [
'get' => $_GET,
'post' => $_POST,
'cookie' => $_COOKIE,
'server' => $_SERVER,
'files' => $_FILES,
'request'=> $_REQUEST,
];
if (\count(static::$cache) > 256) {
unset(static::$cache[\key(static::$cache)]);
}
}
}
/**
* Http encode.
*
* @param string $content
*/
public static function encode(string $content, TcpConnection $connection): string
{
//$content = (string) $content;
// http-code status line.
$header = static::$status . "\r\n";
// create headers
if ($headers = self::headers_list()) {
$header .= \implode("\r\n", $headers) . "\r\n";
}
if (!empty($connection->gzip)) {
$header .= "Content-Encoding: gzip\r\n";
$content = \gzencode($content, $connection->gzip);
}
// header
$header .= 'Content-Length: ' . \strlen($content) . "\r\n\r\n";
// save session
static::sessionWriteClose();
// the whole http package
return $header . $content;
}
}