|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Psr7 |
|
4
|
|
|
* @category modules |
|
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2016, Nazar Mokrynskyi |
|
7
|
|
|
* @license MIT License, see license.txt |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace cs\modules\Psr7; |
|
10
|
|
|
use |
|
11
|
|
|
cs\Request as System_request; |
|
12
|
|
|
|
|
13
|
|
|
class Request { |
|
14
|
|
|
/** |
|
15
|
|
|
* Initialize request from PSR-7 request object |
|
16
|
|
|
* |
|
17
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $Psr7_request |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \cs\ExitException |
|
20
|
|
|
*/ |
|
21
|
|
|
static function init_from_psr7 ($Psr7_request) { |
|
22
|
|
|
++System_request::$id; |
|
23
|
|
|
$System_request = System_request::instance(); |
|
24
|
|
|
self::from_psr7_server($System_request, $Psr7_request); |
|
25
|
|
|
self::from_psr7_query($System_request, $Psr7_request); |
|
26
|
|
|
self::from_psr7_data_and_files($System_request, $Psr7_request); |
|
27
|
|
|
$System_request->init_route(); |
|
28
|
|
|
} |
|
29
|
|
|
/** |
|
30
|
|
|
* @param System_request $System_request |
|
31
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $Psr7_request |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static function from_psr7_server ($System_request, $Psr7_request) { |
|
34
|
|
|
$uri = $Psr7_request->getUri(); |
|
35
|
|
|
$System_request->method = $Psr7_request->getMethod(); |
|
36
|
|
|
$System_request->host = $uri->getHost(); |
|
37
|
|
|
$System_request->scheme = $uri->getScheme(); |
|
38
|
|
|
$System_request->secure = $System_request->scheme == 'https'; |
|
39
|
|
|
if ( |
|
40
|
|
|
(!$System_request->secure && $uri->getPort() != 80) || |
|
41
|
|
|
($System_request->secure && $uri->getPort() != 443) |
|
42
|
|
|
) { |
|
43
|
|
|
$System_request->host .= ':'.$uri->getPort(); |
|
44
|
|
|
} |
|
45
|
|
|
$System_request->protocol = 'HTTP/'.$Psr7_request->getProtocolVersion(); |
|
46
|
|
|
$System_request->path = $uri->getPath(); |
|
47
|
|
|
$System_request->query_string = $uri->getQuery(); |
|
48
|
|
|
/** @noinspection NestedTernaryOperatorInspection */ |
|
49
|
|
|
$System_request->uri = $System_request->path.($System_request->query_string ? "?$System_request->query_string" : '') ?: '/'; |
|
50
|
|
|
$System_request->remote_addr = @$Psr7_request->getServerParams()['REMOTE_ADDR'] ?: '127.0.0.1'; |
|
51
|
|
|
$System_request->ip = $System_request->ip( |
|
|
|
|
|
|
52
|
|
|
[ |
|
53
|
|
|
'HTTP_X_FORWARDED_FOR' => $Psr7_request->getHeaderLine('x-forwarded-for'), |
|
54
|
|
|
'HTTP_CLIENT_IP' => $Psr7_request->getHeaderLine('client-ip'), |
|
55
|
|
|
'HTTP_X_FORWARDED' => $Psr7_request->getHeaderLine('x-forwarded'), |
|
56
|
|
|
'HTTP_X_CLUSTER_CLIENT_IP' => $Psr7_request->getHeaderLine('x-cluster-client-ip'), |
|
57
|
|
|
'HTTP_FORWARDED_FOR' => $Psr7_request->getHeaderLine('forwarded-for'), |
|
58
|
|
|
'HTTP_FORWARDED' => $Psr7_request->getHeaderLine('forwarded') |
|
59
|
|
|
] |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
/** |
|
63
|
|
|
* @param System_request $System_request |
|
64
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $Psr7_request |
|
65
|
|
|
*/ |
|
66
|
|
|
protected static function from_psr7_query ($System_request, $Psr7_request) { |
|
67
|
|
|
$System_request->query = $Psr7_request->getQueryParams(); |
|
68
|
|
|
} |
|
69
|
|
|
/** |
|
70
|
|
|
* @param System_request $System_request |
|
71
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $Psr7_request |
|
72
|
|
|
* |
|
73
|
|
|
* @throws \cs\ExitException |
|
74
|
|
|
*/ |
|
75
|
|
|
protected static function from_psr7_data_and_files ($System_request, $Psr7_request) { |
|
76
|
|
|
Psr7_data_stream::$stream = $Psr7_request->getBody(); |
|
77
|
|
|
$System_request->init_data_and_files([], [], fopen('request-psr7-data://', 'r')); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.