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
|
<?php
namespace OAuth\Common\Http\Uri;
use RuntimeException;
/**
* Factory class for uniform resource indicators
*/
class UriFactory implements UriFactoryInterface
{
/**
* Factory method to build a URI from a super-global $_SERVER array.
*
* @param array $_server
*
* @return UriInterface
*/
public function createFromSuperGlobalArray(array $_server)
{
if ($uri = $this->attemptProxyStyleParse($_server)) {
return $uri;
}
$scheme = $this->detectScheme($_server);
$host = $this->detectHost($_server);
$port = $this->detectPort($_server);
$path = $this->detectPath($_server);
$query = $this->detectQuery($_server);
return $this->createFromParts($scheme, '', $host, $port, $path, $query);
}
/**
* @param string $absoluteUri
*
* @return UriInterface
*/
public function createFromAbsolute($absoluteUri)
{
return new Uri($absoluteUri);
}
/**
* Factory method to build a URI from parts
*
* @param string $scheme
* @param string $userInfo
* @param string $host
* @param string $port
* @param string $path
* @param string $query
* @param string $fragment
*
* @return UriInterface
*/
public function createFromParts($scheme, $userInfo, $host, $port, $path = '', $query = '', $fragment = '')
{
$uri = new Uri();
$uri->setScheme($scheme);
$uri->setUserInfo($userInfo);
$uri->setHost($host);
$uri->setPort($port);
$uri->setPath($path);
$uri->setQuery($query);
$uri->setFragment($fragment);
return $uri;
}
/**
* @param array $_server
*
* @return UriInterface|null
*/
private function attemptProxyStyleParse($_server)
{
// If the raw HTTP request message arrives with a proxy-style absolute URI in the
// initial request line, the absolute URI is stored in $_SERVER['REQUEST_URI'] and
// we only need to parse that.
if (isset($_server['REQUEST_URI']) && parse_url($_server['REQUEST_URI'], PHP_URL_SCHEME)) {
return new Uri($_server['REQUEST_URI']);
}
return null;
}
/**
* @param array $_server
*
* @return string
*
* @throws RuntimeException
*/
private function detectPath($_server)
{
if (isset($_server['REQUEST_URI'])) {
$uri = $_server['REQUEST_URI'];
} elseif (isset($_server['REDIRECT_URL'])) {
$uri = $_server['REDIRECT_URL'];
} else {
throw new RuntimeException('Could not detect URI path from superglobal');
}
$queryStr = strpos($uri, '?');
if ($queryStr !== false) {
$uri = substr($uri, 0, $queryStr);
}
return $uri;
}
/**
* @param array $_server
*
* @return string
*/
private function detectHost(array $_server)
{
$host = isset($_server['HTTP_HOST']) ? $_server['HTTP_HOST'] : '';
if (strstr($host, ':')) {
$host = parse_url($host, PHP_URL_HOST);
}
return $host;
}
/**
* @param array $_server
*
* @return string
*/
private function detectPort(array $_server)
{
return isset($_server['SERVER_PORT']) ? $_server['SERVER_PORT'] : 80;
}
/**
* @param array $_server
*
* @return string
*/
private function detectQuery(array $_server)
{
return isset($_server['QUERY_STRING']) ? $_server['QUERY_STRING'] : '';
}
/**
* Determine URI scheme component from superglobal array
*
* When using ISAPI with IIS, the value will be "off" if the request was
* not made through the HTTPS protocol. As a result, we filter the
* value to a bool.
*
* @param array $_server A super-global $_SERVER array
*
* @return string Returns http or https depending on the URI scheme
*/
private function detectScheme(array $_server)
{
if (isset($_server['HTTPS']) && filter_var($_server['HTTPS'], FILTER_VALIDATE_BOOLEAN)) {
return 'https';
} else {
return 'http';
}
}
}
|