1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Server\Contexts\RequestContext |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Johann Zelger <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/server |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Server\Contexts; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Server\Dictionaries\EnvVars; |
24
|
|
|
use AppserverIo\Server\Dictionaries\ServerVars; |
25
|
|
|
use AppserverIo\Server\Interfaces\RequestContextInterface; |
26
|
|
|
use AppserverIo\Server\Interfaces\ServerConfigurationInterface; |
27
|
|
|
use AppserverIo\Server\Traits\EnvVarsArrayTrait; |
28
|
|
|
use AppserverIo\Server\Traits\ModuleVarsArrayTrait; |
29
|
|
|
use AppserverIo\Server\Traits\ServerVarsArrayTrait; |
30
|
|
|
use AppserverIo\Server\Exceptions\ServerException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class ServerContext |
34
|
|
|
* |
35
|
|
|
* @author Johann Zelger <[email protected]> |
36
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
37
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
38
|
|
|
* @link https://github.com/appserver-io/server |
39
|
|
|
* @link http://www.appserver.io |
40
|
|
|
*/ |
41
|
|
|
class RequestContext implements RequestContextInterface |
42
|
|
|
{ |
43
|
|
|
// use traits for server-, env- and module var functionality |
44
|
|
|
use ServerVarsArrayTrait, ModuleVarsArrayTrait, EnvVarsArrayTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Defines the handler to use as default |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
const REQUEST_HANDLER_DEFAULT = 'core'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This member will hold the server variables which different modules can set/get in order to change the processing |
55
|
|
|
* of the incoming request. |
56
|
|
|
* This will also contain server variables as one might suspect in $_SERVER |
57
|
|
|
* |
58
|
|
|
* @var array $serverVars |
59
|
|
|
*/ |
60
|
|
|
protected $serverVars; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* This member will hold the module variables which different modules can set/get to communicate with each |
64
|
|
|
* other without knowing each other. |
65
|
|
|
* |
66
|
|
|
* @var array $moduleVars |
67
|
|
|
*/ |
68
|
|
|
protected $moduleVars; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* This member will hold the environment (env) variables which different modules can set/get to provide |
72
|
|
|
* the context similar to $_ENV in the plain php world |
73
|
|
|
* |
74
|
|
|
* @var array $envVars |
75
|
|
|
*/ |
76
|
|
|
protected $envVars; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Constructs the request context |
80
|
|
|
*/ |
81
|
1 |
|
public function __construct() |
82
|
|
|
{ |
83
|
|
|
// init data holders as hash map objects ... user objects traits for that |
84
|
|
|
// $this->serverVars = new HashMap(); |
85
|
|
|
// $this->envVars = new HashMap(); |
86
|
|
|
// $this->moduleVars = new HashMap(); |
87
|
|
|
|
88
|
|
|
// you can use stackable trait when doing this to be synchronised with these hashtables |
89
|
|
|
// $this->serverVars = new \Stackable(); |
90
|
|
|
// $this->envVars = new \Stackable(); |
91
|
|
|
// $this->moduleVars = new \Stackable(); |
92
|
|
|
|
93
|
|
|
// or you just use normal internal arrays |
94
|
1 |
|
$this->serverVars = array(); |
95
|
1 |
|
$this->envVars = array(); |
96
|
1 |
|
$this->moduleVars = array(); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Initialises the request context by given server config |
101
|
|
|
* |
102
|
|
|
* @param \AppserverIo\Server\Interfaces\ServerConfigurationInterface $serverConfig The servers config |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function init(ServerConfigurationInterface $serverConfig) |
107
|
|
|
{ |
108
|
|
|
// set server context ref |
109
|
|
|
$this->serverConfig = $serverConfig; |
|
|
|
|
110
|
|
|
// init all vars |
111
|
|
|
$this->initVars(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return's the server config instance |
116
|
|
|
* |
117
|
|
|
* @return \AppserverIo\Server\Interfaces\ServerConfigurationInterface |
118
|
|
|
*/ |
119
|
|
|
public function getServerConfig() |
120
|
|
|
{ |
121
|
|
|
return $this->serverConfig; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Resets all var used in server context |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function initVars() |
130
|
|
|
{ |
131
|
|
|
$this->initServerVars(); |
132
|
|
|
$this->initModuleVars(); |
133
|
|
|
$this->initEnvVars(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* init's module vars |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function initModuleVars() |
142
|
|
|
{ |
143
|
|
|
// init module vars |
144
|
|
|
$this->clearModuleVars(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* init's env vars |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
public function initEnvVars() |
153
|
|
|
{ |
154
|
|
|
// init env vars array |
155
|
|
|
$this->clearEnvVars(); |
156
|
|
|
$this->setEnvVar(EnvVars::LOGGER_SYSTEM, $this->getServerConfig()->getLoggerName()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* init's server vars |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function initServerVars() |
165
|
|
|
{ |
166
|
|
|
// get local refs |
167
|
|
|
$serverConfig = $this->getServerConfig(); |
168
|
|
|
|
169
|
|
|
// clear server var storage |
170
|
|
|
$this->clearServerVars(); |
171
|
|
|
|
172
|
|
|
// set server vars to local var to shorter usage |
173
|
|
|
$serverSoftware = $serverConfig->getSoftware() . ' (PHP ' . PHP_VERSION . ')'; |
174
|
|
|
$serverAddress = $serverConfig->getAddress(); |
175
|
|
|
$serverPort = $serverConfig->getPort(); |
176
|
|
|
|
177
|
|
|
// set document root |
178
|
|
|
$documentRoot = $serverConfig->getDocumentRoot(); |
179
|
|
|
|
180
|
|
|
// check if relative path is given and make is absolute by using getcwd() as prefix |
181
|
|
|
if (!preg_match("/^([a-zA-Z]:|\/)/", $documentRoot)) { |
182
|
|
|
$documentRoot = getcwd() . DIRECTORY_SEPARATOR . $documentRoot; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// check if document root exists on file system |
186
|
|
|
if (!is_dir($documentRoot)) { |
187
|
|
|
throw new ServerException( |
188
|
|
|
sprintf( |
189
|
|
|
"Document root '%s' does not exist for '%s' listing on '%s:%s'", |
190
|
|
|
$documentRoot, |
191
|
|
|
$serverConfig->getName(), |
192
|
|
|
$serverAddress, |
193
|
|
|
$serverPort |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// build initial server vars |
199
|
|
|
$this->setServerVar(ServerVars::DOCUMENT_ROOT, $documentRoot); |
200
|
|
|
$this->setServerVar(ServerVars::SERVER_ADMIN, $serverConfig->getAdmin()); |
201
|
|
|
$this->setServerVar(ServerVars::SERVER_NAME, $serverAddress); |
202
|
|
|
$this->setServerVar(ServerVars::SERVER_ADDR, $serverAddress); |
203
|
|
|
$this->setServerVar(ServerVars::SERVER_PORT, $serverPort); |
204
|
|
|
$this->setServerVar(ServerVars::GATEWAY_INTERFACE, "PHP/" . PHP_VERSION); |
205
|
|
|
$this->setServerVar(ServerVars::SERVER_SOFTWARE, $serverSoftware); |
206
|
|
|
$this->setServerVar( |
207
|
|
|
ServerVars::SERVER_SIGNATURE, |
208
|
|
|
"<address>$serverSoftware Server at $serverAddress Port $serverPort</address>\r\n" |
209
|
|
|
); |
210
|
|
|
$this->setServerVar(ServerVars::SERVER_HANDLER, RequestContext::REQUEST_HANDLER_DEFAULT); |
211
|
|
|
$this->setServerVar(ServerVars::SERVER_ERRORS_PAGE_TEMPLATE_PATH, $serverConfig->getErrorsPageTemplatePath()); |
212
|
|
|
$this->setServerVar(ServerVars::SERVER_WELCOME_PAGE_TEMPLATE_PATH, $serverConfig->getWelcomePageTemplatePath()); |
213
|
|
|
$this->setServerVar(ServerVars::SERVER_AUTO_INDEX_TEMPLATE_PATH, $serverConfig->getAutoIndexTemplatePath()); |
214
|
|
|
$this->setServerVar(ServerVars::SERVER_AUTO_INDEX, $serverConfig->getAutoIndex()); |
|
|
|
|
215
|
|
|
$this->setServerVar(ServerVars::HTTPS, $serverConfig->getTransport()); |
216
|
|
|
$this->setServerVar(ServerVars::PATH, getenv('PATH')); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: