1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Ariadne Component Library. |
4
|
|
|
* |
5
|
|
|
* (c) Muze <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace ac\http; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ServerRequest |
14
|
|
|
* Implements a simple container to retrieve information from the http server request |
15
|
|
|
* |
16
|
|
|
* Usage: |
17
|
|
|
* $request = \arc\http::serverRequest(); |
18
|
|
|
* echo $request->url; |
19
|
|
|
* |
20
|
|
|
* @package arc |
21
|
|
|
* @property string $protocol The HTTP request protocol |
22
|
|
|
* @property string $method The HTTP request method |
23
|
|
|
* @property \arc\url\Url $url The requested URL |
24
|
|
|
* @property array $headers The Headers sent in the request. Note that Apache's REDIRECT_ prefixes aren't parsed, you have to do that yourself. |
25
|
|
|
* @property string $body The HTTP request body |
26
|
|
|
* @property string $user The user name from HTTP Basic authentication, if specified |
27
|
|
|
* @property string $password The user password from HTTP Basic authentication, if specified |
28
|
|
|
*/ |
29
|
|
|
class ServerRequest |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Lazy load one of the Request properties |
34
|
|
|
*/ |
35
|
|
|
public function __get($name) |
36
|
|
|
{ |
37
|
|
|
switch($name) { |
38
|
|
|
case 'protocol': |
39
|
|
|
$this->protocol = $this->getProtocol(); |
40
|
|
|
return $this->protocol; |
41
|
|
|
break; |
42
|
|
|
case 'method': |
43
|
|
|
$this->method = $this->getMethod(); |
44
|
|
|
return $this->method; |
45
|
|
|
break; |
46
|
|
|
case 'url': |
47
|
|
|
$this->url = $this->getURL(); |
48
|
|
|
return $this->url; |
49
|
|
|
break; |
50
|
|
|
case 'headers': |
51
|
|
|
$this->headers = $this->getHeaders(); |
52
|
|
|
return $this->headers; |
53
|
|
|
break; |
54
|
|
|
case 'body': |
55
|
|
|
$this->body = $this->getBody(); |
56
|
|
|
return $this->body; |
57
|
|
|
break; |
58
|
|
|
case 'user': |
59
|
|
|
$this->user = $this->getUser(); |
60
|
|
|
return $this->user; |
61
|
|
|
break; |
62
|
|
|
case 'password': |
63
|
|
|
$this->password = $this->getPassword(); |
64
|
|
|
return $this->password; |
65
|
|
|
break; |
66
|
|
|
default: |
67
|
|
|
throw new \arc\IllegalRequest('Unknown property '.$name, \arc\exceptions::OBJECT_NOT_FOUND); |
68
|
|
|
break; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the first header found from a list of headers. |
74
|
|
|
* Will try REDIRECT_* headers first, if $followRedirects>0. |
75
|
|
|
* ( Apache adds 'REDIRECT_' to some headers when you use mod_rewrite. ) |
76
|
|
|
* @param array $list An array of headers to try, in order |
77
|
|
|
* @param int $followRedirects The maximum number of REDIRECT_ prefixes to try |
78
|
|
|
*/ |
79
|
|
|
public function getHeader($list, $followRedirects=0) { |
80
|
|
|
$redirect = 'REDIRECT_'; |
81
|
|
|
if (!is_array($list)) { |
82
|
|
|
$list = [ $list => false ]; |
83
|
|
|
} |
84
|
|
|
foreach ( $list as $header => $extraInfo ) { |
85
|
|
|
for ($i=$followRedirects; $i>=0; $i--) { |
86
|
|
|
$check = str_repeat($redirect, $i).$header; |
87
|
|
|
if ( isset($_SERVER[$check]) ) { |
88
|
|
|
return [$header, $_SERVER[$check]]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return [false, '']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
private static function getUser() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$checks = [ |
98
|
|
|
'PHP_AUTH_USER' => false, |
99
|
|
|
'REMOTE_USER' => false, |
100
|
|
|
'HTTP_AUTHORIZATION' => function($v) { list($user,$password)=http::parseAuthUser($v); return $user; }, |
|
|
|
|
101
|
|
|
]; |
102
|
|
|
list($header, $headerValue) = $this->getHeader($checks, 3); |
|
|
|
|
103
|
|
|
if (isset($checks[$header]) && is_callable($checks[$header])) { |
104
|
|
|
$headerValue = ($checks[$header])($headerValue); |
105
|
|
|
} |
106
|
|
|
return $headerValue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
private static function getPassword() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$checks = [ |
112
|
|
|
'PHP_AUTH_PW' => false, |
113
|
|
|
'HTTP_AUTHORIZATION' => function($v) { list($user,$password)=http::parseAuthUser($v); return $password; }, |
|
|
|
|
114
|
|
|
]; |
115
|
|
|
list($header, $headerValue) = $this->getHeader($checks, 3); |
|
|
|
|
116
|
|
|
if (isset($checks[$header]) && is_callable($checks[$header])) { |
117
|
|
|
$headerValue = ($checks[$header])($headerValue); |
118
|
|
|
} |
119
|
|
|
return $headerValue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private static function parseAuthUser($auth) { |
|
|
|
|
123
|
|
|
return explode(':',base64_decode(substr($auth, 6))); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function getProtocol() |
127
|
|
|
{ |
128
|
|
|
list($header, $headerValue) = $this->getHeader('SERVER_PROTOCOL',3); |
|
|
|
|
129
|
|
|
return $headerValue ?: 'HTTP/1.1'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function getMethod() |
133
|
|
|
{ |
134
|
|
|
list($header, $headerValue) = $this->getHeader('REQUEST_METHOD',3); |
|
|
|
|
135
|
|
|
return $headerValue; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function getURL() |
139
|
|
|
{ |
140
|
|
|
return \arc\url($_SERVER['REQUEST_URI']); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function getHeaders() |
144
|
|
|
{ |
145
|
|
|
return getallheaders(); //polyfill via composer require ralouphie/getallheaders |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function getBody() |
149
|
|
|
{ |
150
|
|
|
return stream_get_contents(STDIN); |
151
|
|
|
} |
152
|
|
|
} |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.