1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Appserver\ServletEngine\Http\Request |
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 Tim Wagner <[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/appserver |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Appserver\ServletEngine\Http; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Lang\String; |
24
|
|
|
use AppserverIo\Http\HttpProtocol; |
25
|
|
|
use AppserverIo\Server\Dictionaries\ServerVars; |
26
|
|
|
use AppserverIo\Psr\Context\ContextInterface; |
27
|
|
|
use AppserverIo\Psr\HttpMessage\PartInterface; |
28
|
|
|
use AppserverIo\Psr\HttpMessage\CookieInterface; |
29
|
|
|
use AppserverIo\Psr\HttpMessage\RequestInterface; |
30
|
|
|
use AppserverIo\Psr\Security\PrincipalInterface; |
31
|
|
|
use AppserverIo\Psr\Servlet\SessionUtils; |
32
|
|
|
use AppserverIo\Psr\Servlet\ServletException; |
33
|
|
|
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface; |
34
|
|
|
use AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface; |
35
|
|
|
use AppserverIo\Psr\Auth\AuthenticationManagerInterface; |
36
|
|
|
use AppserverIo\Appserver\Core\Environment; |
37
|
|
|
use AppserverIo\Appserver\Core\Utilities\EnvironmentKeys; |
38
|
|
|
use AppserverIo\Appserver\ServletEngine\SessionManagerInterface; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* A Http servlet request implementation. |
42
|
|
|
* |
43
|
|
|
* @author Tim Wagner <[email protected]> |
44
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
45
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
46
|
|
|
* @link https://github.com/appserver-io/appserver |
47
|
|
|
* @link http://www.appserver.io |
48
|
|
|
*/ |
49
|
|
|
class Request implements HttpServletRequestInterface, ContextInterface |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The path (URI) to the servlet. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $servletPath; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The request context. |
61
|
|
|
* |
62
|
|
|
* @var \AppserverIo\Appserver\ServletEngine\Http\RequestContextInterface |
63
|
|
|
*/ |
64
|
|
|
protected $requestHandler; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The request URI. |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $requestUri; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The request URL. |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
protected $requestUrl; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The new session name. |
82
|
|
|
* |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
protected $requestedSessionName; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The new session-ID. |
89
|
|
|
* |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
protected $requestedSessionId; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The servlet response instance. |
96
|
|
|
* |
97
|
|
|
* @var \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface |
98
|
|
|
*/ |
99
|
|
|
protected $response; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The server name. |
103
|
|
|
* |
104
|
|
|
* @var string |
105
|
|
|
*/ |
106
|
|
|
protected $serverName; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The query string. |
110
|
|
|
* |
111
|
|
|
* @var string |
112
|
|
|
*/ |
113
|
|
|
protected $queryString; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* The absolute path info. |
117
|
|
|
* |
118
|
|
|
* @var string |
119
|
|
|
*/ |
120
|
|
|
protected $pathInfo; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* The document root. |
124
|
|
|
* |
125
|
|
|
* @var string |
126
|
|
|
*/ |
127
|
|
|
protected $documentRoot; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Base modifier which allows for base path generation within rewritten URL environments. |
131
|
|
|
* |
132
|
|
|
* @var string |
133
|
|
|
*/ |
134
|
|
|
protected $baseModifier; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* The body content stream resource. |
138
|
|
|
* |
139
|
|
|
* @var resource |
140
|
|
|
*/ |
141
|
|
|
protected $bodyStream; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* The request context instance. |
145
|
|
|
* |
146
|
|
|
* @var \AppserverIo\Psr\Context\ContextInterface |
147
|
|
|
*/ |
148
|
|
|
protected $context; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* The application context name. |
152
|
|
|
* |
153
|
|
|
* @var string |
154
|
|
|
*/ |
155
|
|
|
protected $contextPath; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Whether or not the request has been dispatched. |
159
|
|
|
* |
160
|
|
|
* @var boolean |
161
|
|
|
*/ |
162
|
|
|
protected $dispatched = false; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* The HTTP request instance. |
166
|
|
|
* |
167
|
|
|
* @var \AppserverIo\Psr\HttpMessage\RequestInterface |
168
|
|
|
*/ |
169
|
|
|
protected $httpRequest; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* The server variables. |
173
|
|
|
* |
174
|
|
|
* @var array |
175
|
|
|
*/ |
176
|
|
|
protected $serverVars = array(); |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* The uploaded part instances. |
180
|
|
|
* |
181
|
|
|
* @var array |
182
|
|
|
*/ |
183
|
|
|
protected $parts = array(); |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* The available request handlers. |
187
|
|
|
* |
188
|
|
|
* @var array |
189
|
|
|
*/ |
190
|
|
|
protected $handlers = array(); |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Array that contains the attributes of this context. |
194
|
|
|
* |
195
|
|
|
* @var array |
196
|
|
|
*/ |
197
|
|
|
protected $attributes = array(); |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* The user principal of the authenticated user or NULL if the user has not been authenticated. |
201
|
|
|
* |
202
|
|
|
* @var \AppserverIo\Psr\Security\PrincipalInterface |
203
|
|
|
*/ |
204
|
|
|
protected $userPrincipal; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* The request's authentication type. |
208
|
|
|
* |
209
|
|
|
* @var string |
210
|
|
|
*/ |
211
|
|
|
protected $authType; |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* The session manager instance. |
215
|
|
|
* |
216
|
|
|
* @var \AppserverIo\Appserver\ServletEngine\SessionManagerInterface |
217
|
|
|
*/ |
218
|
|
|
protected $sessionManager; |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* The session manager instance. |
222
|
|
|
* |
223
|
|
|
* @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface |
224
|
|
|
*/ |
225
|
|
|
protected $authenticationManager; |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Initializes the request object with the default properties. |
229
|
|
|
*/ |
230
|
3 |
|
public function __construct() |
231
|
|
|
{ |
232
|
|
|
|
233
|
|
|
// init body stream |
234
|
3 |
|
$this->bodyStream = ''; |
|
|
|
|
235
|
|
|
|
236
|
|
|
// the request has not been dispatched initially |
237
|
3 |
|
$this->dispatched = false; |
238
|
|
|
|
239
|
|
|
// initialize the instances |
240
|
3 |
|
$this->context = null; |
241
|
3 |
|
$this->response = null; |
242
|
3 |
|
$this->httpRequest = null; |
243
|
3 |
|
$this->userPrincipal = null; |
244
|
3 |
|
$this->requestHandler = null; |
245
|
3 |
|
$this->sessionManager = null; |
246
|
3 |
|
$this->authenticationManager = null; |
247
|
|
|
|
248
|
|
|
// initialize the strings |
249
|
3 |
|
$this->pathInfo = ''; |
|
|
|
|
250
|
3 |
|
$this->serverName = ''; |
|
|
|
|
251
|
3 |
|
$this->requestUri = ''; |
|
|
|
|
252
|
3 |
|
$this->requestUrl = ''; |
|
|
|
|
253
|
3 |
|
$this->queryString = ''; |
|
|
|
|
254
|
3 |
|
$this->servletPath = ''; |
|
|
|
|
255
|
3 |
|
$this->baseModifier = ''; |
|
|
|
|
256
|
3 |
|
$this->documentRoot = ''; |
|
|
|
|
257
|
3 |
|
$this->requestedSessionId = ''; |
|
|
|
|
258
|
3 |
|
$this->requestedSessionName = ''; |
|
|
|
|
259
|
|
|
|
260
|
|
|
// reset the server variables and the parts |
261
|
3 |
|
$this->parts = array(); |
262
|
3 |
|
$this->handlers = array(); |
263
|
3 |
|
$this->serverVars = array(); |
264
|
3 |
|
$this->attributes = array(); |
265
|
3 |
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Adds the attribute with the passed name to this context. |
269
|
|
|
* |
270
|
|
|
* @param string $key The key to add the value with |
271
|
|
|
* @param mixed $value The value to add to the context |
272
|
|
|
* |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
|
|
public function setAttribute($key, $value) |
276
|
|
|
{ |
277
|
|
|
$this->attributes[$key] = $value; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Returns the value with the passed name from the context. |
282
|
|
|
* |
283
|
|
|
* @param string $key The key of the value to return from the context. |
284
|
|
|
* |
285
|
|
|
* @return mixed The requested attribute |
286
|
|
|
* @see \AppserverIo\Psr\Context\ContextInterface::getAttribute($key) |
287
|
|
|
*/ |
288
|
|
|
public function getAttribute($key) |
289
|
|
|
{ |
290
|
|
|
if (isset($this->attributes[$key])) { |
291
|
|
|
return $this->attributes[$key]; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Returns the attributes from the context. |
297
|
|
|
* |
298
|
|
|
* @return array The array with the attributes |
299
|
|
|
*/ |
300
|
|
|
public function getAttributes() |
301
|
|
|
{ |
302
|
|
|
return $this->attributes; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Initializes the servlet request with the data from the injected HTTP request instance. |
307
|
|
|
* |
308
|
|
|
* @return void |
309
|
|
|
*/ |
310
|
|
|
public function init() |
311
|
|
|
{ |
312
|
|
|
|
313
|
|
|
// reset the servlet request |
314
|
|
|
$httpRequest = $this->getHttpRequest(); |
315
|
|
|
|
316
|
|
|
// initialize the parts |
317
|
|
|
foreach ($httpRequest->getParts() as $part) { |
318
|
|
|
$this->addPart(Part::fromHttpRequest($part)); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// set the body content if we can find one |
322
|
|
|
if ($httpRequest->getHeader(HttpProtocol::HEADER_CONTENT_LENGTH) > 0) { |
323
|
|
|
$this->setBodyStream($httpRequest->getBodyContent()); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
// copy server variables to members |
327
|
|
|
$this->setServerName($this->getServerVar(ServerVars::SERVER_NAME)); |
|
|
|
|
328
|
|
|
$this->setQueryString($this->getServerVar(ServerVars::QUERY_STRING)); |
329
|
|
|
$this->setRequestUri($this->getServerVar(ServerVars::X_REQUEST_URI)); |
330
|
|
|
$this->setDocumentRoot($this->getServerVar(ServerVars::DOCUMENT_ROOT)); |
331
|
|
|
$this->setRequestUrl($this->getServerVar(ServerVars::HTTP_HOST) . $this->getServerVar(ServerVars::X_REQUEST_URI)); |
|
|
|
|
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Prepares the request instance. |
336
|
|
|
* |
337
|
|
|
* @return void |
338
|
|
|
* @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request can't be prepared, because no file handle exists |
339
|
|
|
*/ |
340
|
|
|
public function prepare() |
341
|
|
|
{ |
342
|
|
|
|
343
|
|
|
// prepare the context path |
344
|
|
|
$contextPath = str_replace($this->getContext()->getAppBase(), '', $this->getContext()->getWebappPath()); |
|
|
|
|
345
|
|
|
|
346
|
|
|
// set the context path |
347
|
|
|
$this->setContextPath($contextPath); |
348
|
|
|
|
349
|
|
|
// Fixed #735 - Endless Loop for URLs without servlet name |
350
|
|
|
// Load the request URI and query string from the server vars, because we have to |
351
|
|
|
// take care about changes from other modules like directory or rewrite module! |
352
|
|
|
$uri = $this->getRequestUri(); |
353
|
|
|
$queryString = $this->getQueryString(); |
354
|
|
|
|
355
|
|
|
// get uri without querystring |
356
|
|
|
$uriWithoutQueryString = str_replace('?' . $queryString, '', $uri); |
357
|
|
|
|
358
|
|
|
// initialize the path information and the directory to start with |
359
|
|
|
list ($dirname, $basename, $extension) = array_values(pathinfo($uriWithoutQueryString)); |
360
|
|
|
|
361
|
|
|
// make the registered handlers local |
362
|
|
|
$handlers = $this->getHandlers(); |
363
|
|
|
|
364
|
|
|
// descent the directory structure down to find the (almost virtual) servlet file |
365
|
|
|
do { |
366
|
|
|
// bingo we found a (again: almost virtual) servlet file |
367
|
|
|
if (isset($handlers[".$extension"])) { |
368
|
|
|
// prepare the servlet path (we've to take care, because the |
369
|
|
|
// pathinfo() function converts / to \ on Windows OS |
370
|
|
|
if ($dirname === DIRECTORY_SEPARATOR) { |
371
|
|
|
$servletPath = '/' . $basename; |
372
|
|
|
} else { |
373
|
|
|
$servletPath = $dirname . '/' . $basename; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
// we set the basename, because this is the servlet path |
377
|
|
|
$this->setServletPath($servletPath); |
|
|
|
|
378
|
|
|
|
379
|
|
|
// we set the path info, what is the request URI with stripped dir- and basename |
380
|
|
|
$this->setPathInfo(str_replace($servletPath, '', $uriWithoutQueryString)); |
381
|
|
|
|
382
|
|
|
// we've found what we were looking for, so break here |
383
|
|
|
break; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
// break if we finally can't find a servlet to handle the request |
387
|
|
|
if ($dirname === '/') { |
388
|
|
|
throw new ServletException( |
389
|
|
|
sprintf('Can\'t find a handler for URI %s, either ', $uri) |
390
|
|
|
); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
// descend down the directory tree |
394
|
|
|
list ($dirname, $basename, $extension) = array_values(pathinfo($dirname)); |
395
|
|
|
|
396
|
|
|
} while ($dirname !== false); // stop until we reached the root of the URI |
397
|
|
|
|
398
|
|
|
// prepare and set the servlet path |
399
|
|
|
$this->setServletPath(str_replace($contextPath, '', $this->getServletPath())); |
400
|
|
|
|
401
|
|
|
// prepare the base modifier which allows our apps to provide a base URL |
402
|
|
|
$webappsDir = str_replace($this->getContext()->getBaseDirectory(), '', $this->getContext()->getAppBase()); |
|
|
|
|
403
|
|
|
$relativeRequestPath = strstr($this->getDocumentRoot(), $webappsDir); |
404
|
|
|
$proposedBaseModifier = str_replace(DIRECTORY_SEPARATOR, '/', str_replace($webappsDir, '', $relativeRequestPath)); |
405
|
|
|
|
406
|
|
|
// prepare the base modifier |
407
|
|
|
if (strpos($proposedBaseModifier, $contextPath) === 0) { |
408
|
|
|
$this->setBaseModifier(''); |
|
|
|
|
409
|
|
|
} else { |
410
|
|
|
$this->setBaseModifier($contextPath); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Injects the context that allows access to session and |
416
|
|
|
* server information. |
417
|
|
|
* |
418
|
|
|
* @param \AppserverIo\Psr\Context\ContextInterface $context The request context instance |
419
|
|
|
* |
420
|
|
|
* @return void |
421
|
|
|
*/ |
422
|
|
|
public function injectContext(ContextInterface $context) |
423
|
|
|
{ |
424
|
|
|
$this->context = $context; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Injects the server variables. |
429
|
|
|
* |
430
|
|
|
* @param array $serverVars The server variables |
431
|
|
|
* |
432
|
|
|
* @return void |
433
|
|
|
*/ |
434
|
|
|
public function injectServerVars(array $serverVars) |
435
|
|
|
{ |
436
|
|
|
$this->serverVars = $serverVars; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Injects the Http request instance. |
441
|
|
|
* |
442
|
|
|
* @param \AppserverIo\Psr\HttpMessage\RequestInterface $httpRequest The Http request instance |
443
|
|
|
* |
444
|
|
|
* @return void |
445
|
|
|
*/ |
446
|
1 |
|
public function injectHttpRequest(RequestInterface $httpRequest) |
447
|
|
|
{ |
448
|
1 |
|
$this->httpRequest = $httpRequest; |
449
|
1 |
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Injects the session manager instance. |
453
|
|
|
* |
454
|
|
|
* @param \AppserverIo\Appserver\ServletEngine\SessionManagerInterface $sessionManager The session manager instance |
455
|
|
|
* |
456
|
|
|
* @return void |
457
|
|
|
*/ |
458
|
|
|
public function injectSessionManager(SessionManagerInterface $sessionManager) |
459
|
|
|
{ |
460
|
|
|
$this->sessionManager = $sessionManager; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Return's the session manager instance. |
465
|
|
|
* |
466
|
|
|
* @return \AppserverIo\Appserver\ServletEngine\SessionManagerInterface The session manager instance |
467
|
|
|
*/ |
468
|
|
|
public function getSessionManager() |
469
|
|
|
{ |
470
|
|
|
return $this->sessionManager; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Injects the authentication manager instance. |
475
|
|
|
* |
476
|
|
|
* @param \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager The authentication manager instance |
477
|
|
|
* |
478
|
|
|
* @return void |
479
|
|
|
*/ |
480
|
|
|
public function injectAuthenticationManager(AuthenticationManagerInterface $authenticationManager) |
481
|
|
|
{ |
482
|
|
|
$this->authenticationManager = $authenticationManager; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Return's the authentication manager instance. |
487
|
|
|
* |
488
|
|
|
* @return \AppserverIo\Psr\Auth\AuthenticationManagerInterface The authentication manager instance |
489
|
|
|
*/ |
490
|
|
|
public function getAuthenticationManager() |
491
|
|
|
{ |
492
|
|
|
return $this->authenticationManager; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Returns the Http request instance. |
497
|
|
|
* |
498
|
|
|
* @return \AppserverIo\Psr\HttpMessage\RequestInterface The Http request instance |
499
|
|
|
*/ |
500
|
1 |
|
public function getHttpRequest() |
501
|
|
|
{ |
502
|
1 |
|
return $this->httpRequest; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Injects the available file handlers registered by the webserver configuration. |
507
|
|
|
* |
508
|
|
|
* @param array $handlers The available file handlers |
509
|
|
|
* |
510
|
|
|
* @return void |
511
|
|
|
*/ |
512
|
|
|
public function injectHandlers(array $handlers) |
513
|
|
|
{ |
514
|
|
|
$this->handlers = $handlers; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Returns the available file handlers registered by the webserver configuration. |
519
|
|
|
* |
520
|
|
|
* @return array The file handlers |
521
|
|
|
*/ |
522
|
|
|
public function getHandlers() |
523
|
|
|
{ |
524
|
|
|
return $this->handlers; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* Returns the base modifier which allows for base path generation within rewritten URL environments |
529
|
|
|
* |
530
|
|
|
* @return string |
531
|
|
|
*/ |
532
|
|
|
public function getBaseModifier() |
533
|
|
|
{ |
534
|
|
|
return $this->baseModifier; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Returns the context that allows access to session and |
539
|
|
|
* server information. |
540
|
|
|
* |
541
|
|
|
* @return \AppserverIo\Psr\Context\ContextInterface The request context |
542
|
|
|
*/ |
543
|
|
|
public function getContext() |
544
|
|
|
{ |
545
|
|
|
return $this->context; |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* Returns a part instance |
550
|
|
|
* |
551
|
|
|
* @return \AppserverIo\Psr\HttpMessage\PartInterface |
552
|
|
|
*/ |
553
|
|
|
public function getHttpPartInstance() |
554
|
|
|
{ |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Returns the context that allows access to session and |
559
|
|
|
* server information. |
560
|
|
|
* |
561
|
|
|
* @return \AppserverIo\Appserver\ServletEngine\Http\RequestContextInterface The request context |
562
|
|
|
*/ |
563
|
|
|
public function getRequestHandler() |
564
|
|
|
{ |
565
|
|
|
return $this->requestHandler; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Sets the absolute path to the document root. |
570
|
|
|
* |
571
|
|
|
* @param string $documentRoot The document root |
572
|
|
|
* |
573
|
|
|
* @return void |
574
|
|
|
*/ |
575
|
|
|
public function setDocumentRoot($documentRoot) |
576
|
|
|
{ |
577
|
|
|
return $this->documentRoot = $documentRoot; |
|
|
|
|
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* Returns the absolut path to the document root. |
582
|
|
|
* |
583
|
|
|
* @return string The document root |
584
|
|
|
*/ |
585
|
|
|
public function getDocumentRoot() |
586
|
|
|
{ |
587
|
|
|
return $this->documentRoot; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Sets the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. |
592
|
|
|
* |
593
|
|
|
* @param string $requestUri The request URI |
594
|
|
|
* |
595
|
|
|
* @return void |
596
|
|
|
*/ |
597
|
|
|
public function setRequestUri($requestUri) |
598
|
|
|
{ |
599
|
|
|
$this->requestUri = $requestUri; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. |
604
|
|
|
* |
605
|
|
|
* @return string The request URI |
606
|
|
|
*/ |
607
|
|
|
public function getRequestUri() |
608
|
|
|
{ |
609
|
|
|
return $this->requestUri; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Sets the URL the client used to make the request. |
614
|
|
|
* |
615
|
|
|
* @param string $requestUrl The request URL |
616
|
|
|
* |
617
|
|
|
* @return void |
618
|
|
|
*/ |
619
|
|
|
public function setRequestUrl($requestUrl) |
620
|
|
|
{ |
621
|
|
|
$this->requestUrl = $requestUrl; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* Returns the URL the client used to make the request. |
626
|
|
|
* |
627
|
|
|
* @return string The request URL |
628
|
|
|
*/ |
629
|
|
|
public function getRequestUrl() |
630
|
|
|
{ |
631
|
|
|
return $this->requestUrl; |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Injects the servlet response bound to this request. |
636
|
|
|
* |
637
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $response The servlet response instance |
638
|
|
|
* |
639
|
|
|
* @return void |
640
|
|
|
*/ |
641
|
|
|
public function injectResponse(HttpServletResponseInterface $response) |
642
|
|
|
{ |
643
|
|
|
$this->response = $response; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Returns the servlet response bound to this request. |
648
|
|
|
* |
649
|
|
|
* @return \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface The response instance |
650
|
|
|
*/ |
651
|
|
|
public function getResponse() |
652
|
|
|
{ |
653
|
|
|
return $this->response; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* Returns an array with all request parameters. |
658
|
|
|
* |
659
|
|
|
* @param array $parameterMap The array with the request parameters |
660
|
|
|
* |
661
|
|
|
* @return void |
662
|
|
|
*/ |
663
|
|
|
public function setParameterMap(array $parameterMap) |
664
|
|
|
{ |
665
|
|
|
$this->getHttpRequest()->setParams($parameterMap); |
|
|
|
|
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* Returns an array with all request parameters. |
670
|
|
|
* |
671
|
|
|
* @return array The array with the request parameters |
672
|
|
|
*/ |
673
|
|
|
public function getParameterMap() |
674
|
|
|
{ |
675
|
|
|
return $this->getHttpRequest()->getParams(); |
|
|
|
|
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Return content |
680
|
|
|
* |
681
|
|
|
* @return string $content |
682
|
|
|
*/ |
683
|
|
|
public function getBodyContent() |
684
|
|
|
{ |
685
|
|
|
return $this->getBodyStream(); |
|
|
|
|
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* Returns the body stream as a resource. |
690
|
|
|
* |
691
|
|
|
* @return resource The body stream |
692
|
|
|
*/ |
693
|
|
|
public function getBodyStream() |
694
|
|
|
{ |
695
|
|
|
return $this->bodyStream; |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* Set the base modifier |
700
|
|
|
* |
701
|
|
|
* @param string $baseModifier Base modifier which allows for base path generation within rewritten URL environments |
702
|
|
|
* |
703
|
|
|
* @return null |
704
|
|
|
*/ |
705
|
|
|
public function setBaseModifier($baseModifier) |
706
|
|
|
{ |
707
|
|
|
$this->baseModifier = $baseModifier; |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* Resets the stream resource pointing to body content. |
712
|
|
|
* |
713
|
|
|
* @param resource $bodyStream The body content stream resource |
714
|
|
|
* |
715
|
|
|
* @return void |
716
|
|
|
*/ |
717
|
|
|
public function setBodyStream($bodyStream) |
718
|
|
|
{ |
719
|
|
|
$this->bodyStream = $bodyStream; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Set protocol version |
724
|
|
|
* |
725
|
|
|
* @param string $version The http protocol version |
726
|
|
|
* |
727
|
|
|
* @return void |
728
|
|
|
*/ |
729
|
|
|
public function setVersion($version) |
730
|
|
|
{ |
731
|
|
|
$this->getHttpRequest()->setVersion($version); |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
/** |
735
|
|
|
* Returns protocol version |
736
|
|
|
* |
737
|
|
|
* @return string |
738
|
|
|
*/ |
739
|
|
|
public function getVersion() |
740
|
|
|
{ |
741
|
|
|
return $this->getHttpRequest()->getVersion(); |
|
|
|
|
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* Queries whether the request contains a parameter or not. |
746
|
|
|
* |
747
|
|
|
* @param string $name The name of the param we're query for |
748
|
|
|
* |
749
|
|
|
* @return boolean TRUE if the parameter is available, else FALSE |
750
|
|
|
*/ |
751
|
1 |
|
public function hasParameter($name) |
752
|
|
|
{ |
753
|
1 |
|
return $this->getHttpRequest()->hasParam($name); |
|
|
|
|
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
/** |
757
|
|
|
* Returns the parameter with the passed name if available or null |
758
|
|
|
* if the parameter not exists. |
759
|
|
|
* |
760
|
|
|
* @param string $name The name of the parameter to return |
761
|
|
|
* |
762
|
|
|
* @return string|null The requested value |
763
|
|
|
*/ |
764
|
|
|
public function getParam($name) |
765
|
|
|
{ |
766
|
|
|
return $this->getHttpRequest()->getParam($name); |
|
|
|
|
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
/** |
770
|
|
|
* Returns the parameter with the passed name if available or null |
771
|
|
|
* if the parameter not exists. |
772
|
|
|
* |
773
|
|
|
* @param string $name The name of the parameter to return |
774
|
|
|
* @param integer $filter The filter to use |
775
|
|
|
* |
776
|
|
|
* @return string|null |
777
|
|
|
*/ |
778
|
|
|
public function getParameter($name, $filter = FILTER_SANITIZE_STRING) |
779
|
|
|
{ |
780
|
|
|
$parameterMap = $this->getParameterMap(); |
781
|
|
|
if (isset($parameterMap[$name])) { |
782
|
|
|
return filter_var($parameterMap[$name], $filter); |
783
|
|
|
} |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
/** |
787
|
|
|
* Returns a part object by given name |
788
|
|
|
* |
789
|
|
|
* @param string $name The name of the form part |
790
|
|
|
* |
791
|
|
|
* @return \AppserverIo\Http\HttpPart |
792
|
|
|
*/ |
793
|
|
|
public function getPart($name) |
794
|
|
|
{ |
795
|
|
|
if (isset($this->parts[$name])) { |
796
|
|
|
return $this->parts[$name]; |
797
|
|
|
} |
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* Returns the parts collection as array |
802
|
|
|
* |
803
|
|
|
* @return array A collection of HttpPart objects |
804
|
|
|
*/ |
805
|
|
|
public function getParts() |
806
|
|
|
{ |
807
|
|
|
return $this->parts; |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
/** |
811
|
|
|
* Adds a part to the parts collection. |
812
|
|
|
* |
813
|
|
|
* @param \AppserverIo\Appserver\ServletEngine\Http\Part $part A form part object |
814
|
|
|
* @param string $name A manually defined name |
815
|
|
|
* |
816
|
|
|
* @return void |
817
|
|
|
*/ |
818
|
|
|
public function addPart(PartInterface $part, $name = null) |
819
|
|
|
{ |
820
|
|
|
if ($name == null) { |
821
|
|
|
$name = $part->getName(); |
822
|
|
|
} |
823
|
|
|
$this->parts[$name] = $part; |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
/** |
827
|
|
|
* Sets the application context name (application name prefixed with a slash) for the actual request. |
828
|
|
|
* |
829
|
|
|
* @param string $contextPath The application context name |
830
|
|
|
* |
831
|
|
|
* @return void |
832
|
|
|
*/ |
833
|
|
|
public function setContextPath($contextPath) |
834
|
|
|
{ |
835
|
|
|
$this->contextPath = $contextPath; |
836
|
|
|
} |
837
|
|
|
|
838
|
|
|
/** |
839
|
|
|
* Returns the application context name (application name prefixed with a slash) for the actual request. |
840
|
|
|
* |
841
|
|
|
* @return string The application context name |
842
|
|
|
*/ |
843
|
|
|
public function getContextPath() |
844
|
|
|
{ |
845
|
|
|
return $this->contextPath; |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* Sets the path to the servlet used to handle this request. |
850
|
|
|
* |
851
|
|
|
* @param string $servletPath The path to the servlet |
852
|
|
|
* |
853
|
|
|
* @return void |
854
|
|
|
*/ |
855
|
|
|
public function setServletPath($servletPath) |
856
|
|
|
{ |
857
|
|
|
$this->servletPath = $servletPath; |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
/** |
861
|
|
|
* Returns the path to the servlet used to handle this request. |
862
|
|
|
* |
863
|
|
|
* @return string The relative path to the servlet |
864
|
|
|
*/ |
865
|
|
|
public function getServletPath() |
866
|
|
|
{ |
867
|
|
|
return $this->servletPath; |
868
|
|
|
} |
869
|
|
|
|
870
|
|
|
/** |
871
|
|
|
* Return the session identifier proposed by the actual configuration and request state. |
872
|
|
|
* |
873
|
|
|
* @return string The session identifier proposed for this request |
874
|
|
|
*/ |
875
|
|
|
public function getProposedSessionId() |
876
|
|
|
{ |
877
|
|
|
|
878
|
|
|
// if no session has already been load, initialize the session manager |
879
|
|
|
/** @var \AppserverIo\Appserver\ServletEngine\SessionManagerInterface $manager */ |
880
|
|
|
$manager = $this->getContext()->search(SessionManagerInterface::IDENTIFIER); |
|
|
|
|
881
|
|
|
|
882
|
|
|
// if no session manager was found, we don't support sessions |
883
|
|
|
if ($manager == null) { |
884
|
|
|
return; |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
// if we can't find a requested session name, we try to load the default session cookie |
888
|
|
|
if ($this->getRequestedSessionName() == null) { |
889
|
|
|
$this->setRequestedSessionName($manager->getSessionSettings()->getSessionName()); |
|
|
|
|
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
// load the requested session ID and name |
893
|
|
|
$sessionName = $this->getRequestedSessionName(); |
894
|
|
|
$id = $this->getRequestedSessionId(); |
895
|
|
|
|
896
|
|
|
// try to load session ID from session cookie of request/response |
897
|
|
|
$cookieFound = null; |
898
|
|
|
if ($id == null && $this->getResponse()->hasCookie($sessionName)) { |
899
|
|
|
$cookieFound = $this->getResponse()->getCookie($sessionName); |
900
|
|
|
} elseif ($id == null && $this->hasCookie($sessionName)) { |
901
|
|
|
$cookieFound = $this->getCookie($sessionName); |
902
|
|
|
} |
903
|
|
|
|
904
|
|
|
// check if we can find a cookie |
905
|
|
|
if (is_array($cookieFound)) { |
|
|
|
|
906
|
|
|
// iterate over the cookies and try to find one that is not expired |
907
|
|
|
foreach ($cookieFound as $cookie) { |
908
|
|
|
if ($cookie instanceof CookieInterface && $cookie->isExpired() === false) { |
909
|
|
|
$this->setRequestedSessionId($id = $cookie->getValue()); |
910
|
|
|
} |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
// if we found a single cookie instance |
914
|
|
|
} elseif ($cookieFound instanceof CookieInterface && $cookieFound->isExpired() === false) { |
915
|
|
|
$this->setRequestedSessionId($id = $cookieFound->getValue()); |
916
|
|
|
} |
917
|
|
|
|
918
|
|
|
// return the requested session |
919
|
|
|
return $id; |
920
|
|
|
} |
921
|
|
|
|
922
|
|
|
/** |
923
|
|
|
* Returns the session for this request. |
924
|
|
|
* |
925
|
|
|
* @param boolean $create TRUE to create a new session, else FALSE |
926
|
|
|
* |
927
|
|
|
* @return null|\AppserverIo\Psr\Servlet\Http\HttpSessionInterface The session instance |
928
|
|
|
* |
929
|
|
|
* @throws \Exception |
930
|
|
|
*/ |
931
|
|
|
public function getSession($create = false) |
932
|
|
|
{ |
933
|
|
|
|
934
|
|
|
// load the proposed session-ID and name |
935
|
|
|
$id = $this->getProposedSessionId(); |
936
|
|
|
$sessionName = $this->getRequestedSessionName(); |
937
|
|
|
|
938
|
|
|
// if no session has already been load, initialize the session manager |
939
|
|
|
/** @var \AppserverIo\Appserver\ServletEngine\SessionManagerInterface $manager */ |
940
|
|
|
$manager = $this->getSessionManager(); |
941
|
|
|
|
942
|
|
|
// if no session manager was found, we don't support sessions |
943
|
|
|
if ($manager == null) { |
944
|
|
|
return; |
945
|
|
|
} |
946
|
|
|
|
947
|
|
|
// initialize empty session variable |
948
|
|
|
$session = null; |
949
|
|
|
|
950
|
|
|
// verify that the session id has a valid format |
951
|
|
|
if (preg_match('/^[A-z0-9]+$/', $id)) { |
952
|
|
|
// attempt to retrieve existing session |
953
|
|
|
$session = $manager->find($id); |
954
|
|
|
} |
955
|
|
|
|
956
|
|
|
// if we can't find a session or session has been expired and we want to create a new one |
957
|
|
|
if ($session == null && $create === true) { |
958
|
|
|
// always generate a unique identifier |
959
|
|
|
$id = SessionUtils::generateRandomString(); |
960
|
|
|
|
961
|
|
|
// create a new session and register ID in request |
962
|
|
|
$session = $manager->create($id, $sessionName); |
963
|
|
|
} |
964
|
|
|
|
965
|
|
|
// if we can't find a session and we should NOT create one, return nothing |
966
|
|
|
if ($create === false && $session == null) { |
967
|
|
|
return; |
968
|
|
|
} |
969
|
|
|
|
970
|
|
|
// if we can't find a session although we SHOULD create one, we throw an exception |
971
|
|
|
if ($create === true && $session == null) { |
972
|
|
|
throw new \Exception('Can\'t create a new session!'); |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
// initialize the session wrapper |
976
|
|
|
$wrapper = new SessionWrapper(); |
977
|
|
|
$wrapper->injectSession($session); |
|
|
|
|
978
|
|
|
$wrapper->injectRequest($this); |
979
|
|
|
|
980
|
|
|
// set the session ID in the execution environment |
981
|
|
|
Environment::singleton()->setAttribute(EnvironmentKeys::SESSION_ID, $id); |
982
|
|
|
|
983
|
|
|
// return the found session |
984
|
|
|
return $wrapper; |
985
|
|
|
} |
986
|
|
|
|
987
|
|
|
/** |
988
|
|
|
* Returns the absolute path info started from the context path. |
989
|
|
|
* |
990
|
|
|
* @return string The absolute path info |
991
|
|
|
*/ |
992
|
|
|
public function getPathInfo() |
993
|
|
|
{ |
994
|
|
|
return $this->pathInfo; |
995
|
|
|
} |
996
|
|
|
|
997
|
|
|
/** |
998
|
|
|
* Sets the absolute path info started from the context path. |
999
|
|
|
* |
1000
|
|
|
* @param string $pathInfo The absolute path info |
1001
|
|
|
* |
1002
|
|
|
* @return void |
1003
|
|
|
*/ |
1004
|
|
|
public function setPathInfo($pathInfo) |
1005
|
|
|
{ |
1006
|
|
|
$this->pathInfo = $pathInfo; |
1007
|
|
|
} |
1008
|
|
|
|
1009
|
|
|
/** |
1010
|
|
|
* Adds the passed cookie to this request. |
1011
|
|
|
* |
1012
|
|
|
* @param \AppserverIo\Psr\HttpMessage\CookieInterface $cookie The cookie to add |
1013
|
|
|
* |
1014
|
|
|
* @return void |
1015
|
|
|
*/ |
1016
|
|
|
public function addCookie(CookieInterface $cookie) |
1017
|
|
|
{ |
1018
|
|
|
$this->getHttpRequest()->addCookie($cookie); |
1019
|
|
|
} |
1020
|
|
|
|
1021
|
|
|
/** |
1022
|
|
|
* Returns true if the request has a cookie header with the passed |
1023
|
|
|
* name, else false. |
1024
|
|
|
* |
1025
|
|
|
* @param string $cookieName Name of the cookie header to be checked |
1026
|
|
|
* |
1027
|
|
|
* @return boolean true if the request has the cookie, else false |
1028
|
|
|
*/ |
1029
|
|
|
public function hasCookie($cookieName) |
1030
|
|
|
{ |
1031
|
|
|
return $this->getHttpRequest()->hasCookie($cookieName); |
1032
|
|
|
} |
1033
|
|
|
|
1034
|
|
|
/** |
1035
|
|
|
* Returns the value of the cookie with the passed name. |
1036
|
|
|
* |
1037
|
|
|
* @param string $cookieName The name of the cookie to return |
1038
|
|
|
* |
1039
|
|
|
* @return \AppserverIo\Psr\HttpMessage\CookieInterface The cookie instance |
1040
|
|
|
*/ |
1041
|
|
|
public function getCookie($cookieName) |
1042
|
|
|
{ |
1043
|
|
|
return $this->getHttpRequest()->getCookie($cookieName); |
1044
|
|
|
} |
1045
|
|
|
|
1046
|
|
|
/** |
1047
|
|
|
* Return's the array with cookies. |
1048
|
|
|
* |
1049
|
|
|
* @return array The array with cookies |
1050
|
|
|
*/ |
1051
|
|
|
public function getCookies() |
1052
|
|
|
{ |
1053
|
|
|
return $this->getHttpRequest()->getCookies(); |
|
|
|
|
1054
|
|
|
} |
1055
|
|
|
|
1056
|
|
|
/** |
1057
|
|
|
* Returns header info by given name |
1058
|
|
|
* |
1059
|
|
|
* @param string $name The header key to name |
1060
|
|
|
* |
1061
|
|
|
* @return string|null |
1062
|
|
|
*/ |
1063
|
|
|
public function getHeader($name) |
1064
|
|
|
{ |
1065
|
|
|
return $this->getHttpRequest()->getHeader($name); |
|
|
|
|
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
/** |
1069
|
|
|
* Set headers data |
1070
|
|
|
* |
1071
|
|
|
* @param array $headers The headers array to set |
1072
|
|
|
* |
1073
|
|
|
* @return void |
1074
|
|
|
*/ |
1075
|
|
|
public function setHeaders(array $headers) |
1076
|
|
|
{ |
1077
|
|
|
$this->getHttpRequest()->setHeaders($headers); |
1078
|
|
|
} |
1079
|
|
|
|
1080
|
|
|
/** |
1081
|
|
|
* Returns headers data |
1082
|
|
|
* |
1083
|
|
|
* @return array |
1084
|
|
|
*/ |
1085
|
|
|
public function getHeaders() |
1086
|
|
|
{ |
1087
|
|
|
return $this->getHttpRequest()->getHeaders(); |
1088
|
|
|
} |
1089
|
|
|
|
1090
|
|
|
/** |
1091
|
|
|
* Adds a header information got from connection. |
1092
|
|
|
* |
1093
|
|
|
* @param string $name The header name |
1094
|
|
|
* @param string $value The headers value |
1095
|
|
|
* |
1096
|
|
|
* @return void |
1097
|
|
|
*/ |
1098
|
|
|
public function addHeader($name, $value) |
1099
|
|
|
{ |
1100
|
|
|
$this->getHttpRequest()->addHeader($name, $value); |
1101
|
|
|
} |
1102
|
|
|
|
1103
|
|
|
/** |
1104
|
|
|
* Checks if header exists by given name. |
1105
|
|
|
* |
1106
|
|
|
* @param string $name The header name to check |
1107
|
|
|
* |
1108
|
|
|
* @return boolean |
1109
|
|
|
*/ |
1110
|
|
|
public function hasHeader($name) |
1111
|
|
|
{ |
1112
|
|
|
return $this->getHttpRequest()->hasHeader($name); |
1113
|
|
|
} |
1114
|
|
|
|
1115
|
|
|
/** |
1116
|
|
|
* Set the requested session ID for this request. This is normally called |
1117
|
|
|
* by the HTTP Connector, when it parses the request headers. |
1118
|
|
|
* |
1119
|
|
|
* @param string $requestedSessionId The new session id |
1120
|
|
|
* |
1121
|
|
|
* @return void |
1122
|
|
|
*/ |
1123
|
|
|
public function setRequestedSessionId($requestedSessionId) |
1124
|
|
|
{ |
1125
|
|
|
$this->requestedSessionId = $requestedSessionId; |
1126
|
|
|
} |
1127
|
|
|
|
1128
|
|
|
/** |
1129
|
|
|
* Return the session identifier included in this request, if any. |
1130
|
|
|
* |
1131
|
|
|
* @return string The session identifier included in this request |
1132
|
|
|
*/ |
1133
|
|
|
public function getRequestedSessionId() |
1134
|
|
|
{ |
1135
|
|
|
return $this->requestedSessionId; |
1136
|
|
|
} |
1137
|
|
|
|
1138
|
|
|
/** |
1139
|
|
|
* Set the requested session name for this request. |
1140
|
|
|
* |
1141
|
|
|
* @param string $requestedSessionName The new session name |
1142
|
|
|
* |
1143
|
|
|
* @return void |
1144
|
|
|
*/ |
1145
|
|
|
public function setRequestedSessionName($requestedSessionName) |
1146
|
|
|
{ |
1147
|
|
|
$this->requestedSessionName = $requestedSessionName; |
1148
|
|
|
} |
1149
|
|
|
|
1150
|
|
|
/** |
1151
|
|
|
* Return the session name included in this request, if any. |
1152
|
|
|
* |
1153
|
|
|
* @return string The session name included in this request |
1154
|
|
|
*/ |
1155
|
|
|
public function getRequestedSessionName() |
1156
|
|
|
{ |
1157
|
|
|
return $this->requestedSessionName; |
1158
|
|
|
} |
1159
|
|
|
|
1160
|
|
|
/** |
1161
|
|
|
* Sets the flag to mark the request dispatched. |
1162
|
|
|
* |
1163
|
|
|
* @param boolean $dispatched TRUE if the request has already been dispatched, else FALSE |
1164
|
|
|
* |
1165
|
|
|
* @return void |
1166
|
|
|
*/ |
1167
|
|
|
public function setDispatched($dispatched = true) |
1168
|
|
|
{ |
1169
|
|
|
$this->dispatched = $dispatched; |
1170
|
|
|
} |
1171
|
|
|
|
1172
|
|
|
/** |
1173
|
|
|
* Sets the flag that shows if the request has already been dispatched. |
1174
|
|
|
* |
1175
|
|
|
* @return boolean TRUE if the request has already been dispatched, else FALSE |
1176
|
|
|
*/ |
1177
|
|
|
public function isDispatched() |
1178
|
|
|
{ |
1179
|
|
|
return $this->dispatched; |
1180
|
|
|
} |
1181
|
|
|
|
1182
|
|
|
/** |
1183
|
|
|
* Sets the server name. |
1184
|
|
|
* |
1185
|
|
|
* @param string $serverName The server name |
1186
|
|
|
* |
1187
|
|
|
* @return void |
1188
|
|
|
*/ |
1189
|
|
|
public function setServerName($serverName) |
1190
|
|
|
{ |
1191
|
|
|
$this->serverName = $serverName; |
1192
|
|
|
} |
1193
|
|
|
|
1194
|
|
|
/** |
1195
|
|
|
* Returns the server name. |
1196
|
|
|
* |
1197
|
|
|
* @return string The server name |
1198
|
|
|
*/ |
1199
|
|
|
public function getServerName() |
1200
|
|
|
{ |
1201
|
|
|
return $this->serverName; |
1202
|
|
|
} |
1203
|
|
|
|
1204
|
|
|
/** |
1205
|
|
|
* Sets the query string of the actual request. |
1206
|
|
|
* |
1207
|
|
|
* @param string $queryString The query string |
1208
|
|
|
* |
1209
|
|
|
* @return void |
1210
|
|
|
*/ |
1211
|
|
|
public function setQueryString($queryString) |
1212
|
|
|
{ |
1213
|
|
|
$this->queryString = $queryString; |
1214
|
|
|
} |
1215
|
|
|
|
1216
|
|
|
/** |
1217
|
|
|
* Returns query string of the actual request. |
1218
|
|
|
* |
1219
|
|
|
* @return string|null The query string of the actual request |
1220
|
|
|
*/ |
1221
|
|
|
public function getQueryString() |
1222
|
|
|
{ |
1223
|
|
|
return $this->queryString; |
1224
|
|
|
} |
1225
|
|
|
|
1226
|
|
|
/** |
1227
|
|
|
* Returns request uri |
1228
|
|
|
* |
1229
|
|
|
* @return string |
1230
|
|
|
*/ |
1231
|
|
|
public function getUri() |
1232
|
|
|
{ |
1233
|
|
|
return $this->getHttpRequest()->getUri(); |
|
|
|
|
1234
|
|
|
} |
1235
|
|
|
|
1236
|
|
|
/** |
1237
|
|
|
* Sets the URI. |
1238
|
|
|
* |
1239
|
|
|
* @param string $uri The uri |
1240
|
|
|
* |
1241
|
|
|
* @return void |
1242
|
|
|
*/ |
1243
|
|
|
public function setUri($uri) |
1244
|
|
|
{ |
1245
|
|
|
$this->getHttpRequest()->setUri($uri); |
1246
|
|
|
} |
1247
|
|
|
|
1248
|
|
|
/** |
1249
|
|
|
* Returns request method |
1250
|
|
|
* |
1251
|
|
|
* @return string |
1252
|
|
|
*/ |
1253
|
|
|
public function getMethod() |
1254
|
|
|
{ |
1255
|
|
|
return $this->getHttpRequest()->getMethod(); |
|
|
|
|
1256
|
|
|
} |
1257
|
|
|
|
1258
|
|
|
/** |
1259
|
|
|
* Sets the method to be performed on the resource identified by the |
1260
|
|
|
* Request-URI. |
1261
|
|
|
* |
1262
|
|
|
* While HTTP method names are typically all uppercase characters, HTTP |
1263
|
|
|
* method names are case-sensitive and thus implementations SHOULD NOT |
1264
|
|
|
* modify the given string. |
1265
|
|
|
* |
1266
|
|
|
* @param string $method Case-insensitive method |
1267
|
|
|
* |
1268
|
|
|
* @return void |
1269
|
|
|
*/ |
1270
|
|
|
public function setMethod($method) |
1271
|
|
|
{ |
1272
|
|
|
$this->getHttpRequest()->setMethod($method); |
1273
|
|
|
} |
1274
|
|
|
|
1275
|
|
|
/** |
1276
|
|
|
* Returns the array with the server variables. |
1277
|
|
|
* |
1278
|
|
|
* @return array The array with the server variables |
1279
|
|
|
*/ |
1280
|
|
|
public function getServerVars() |
1281
|
|
|
{ |
1282
|
|
|
return $this->serverVars; |
1283
|
|
|
} |
1284
|
|
|
|
1285
|
|
|
/** |
1286
|
|
|
* Returns the server variable with the requested name. |
1287
|
|
|
* |
1288
|
|
|
* @param string $name The name of the server variable to be returned |
1289
|
|
|
* |
1290
|
|
|
* @return mixed The requested server variable |
1291
|
|
|
*/ |
1292
|
|
|
public function getServerVar($name) |
1293
|
|
|
{ |
1294
|
|
|
if (isset($this->serverVars[$name])) { |
1295
|
|
|
return $this->serverVars[$name]; |
1296
|
|
|
} |
1297
|
|
|
} |
1298
|
|
|
|
1299
|
|
|
/** |
1300
|
|
|
* Set's the passed authentication type. |
1301
|
|
|
* |
1302
|
|
|
* @param string|null $authType The authentication type |
1303
|
|
|
* |
1304
|
|
|
* @return void |
1305
|
|
|
*/ |
1306
|
|
|
public function setAuthType($authType = null) |
1307
|
|
|
{ |
1308
|
|
|
$this->authType = $authType; |
1309
|
|
|
} |
1310
|
|
|
|
1311
|
|
|
/** |
1312
|
|
|
* Return's the authentication type. |
1313
|
|
|
* |
1314
|
|
|
* @return string|null The authentication type |
1315
|
|
|
*/ |
1316
|
|
|
public function getAuthType() |
1317
|
|
|
{ |
1318
|
|
|
return $this->authType; |
1319
|
|
|
} |
1320
|
|
|
|
1321
|
|
|
/** |
1322
|
|
|
* Set's the user principal for this request. |
1323
|
|
|
* |
1324
|
|
|
* @param \AppserverIo\Psr\Security\PrincipalInterface|null $userPrincipal The user principal |
1325
|
|
|
* |
1326
|
|
|
* @return void |
1327
|
|
|
*/ |
1328
|
|
|
public function setUserPrincipal(PrincipalInterface $userPrincipal = null) |
1329
|
|
|
{ |
1330
|
|
|
$this->userPrincipal = $userPrincipal; |
1331
|
|
|
} |
1332
|
|
|
|
1333
|
|
|
/** |
1334
|
|
|
* Return's a PrincipalInterface object containing the name of the current authenticated user. |
1335
|
|
|
* |
1336
|
|
|
* @return \AppserverIo\Psr\Security\PrincipalInterface|null The user principal |
1337
|
|
|
*/ |
1338
|
|
|
public function getUserPrincipal() |
1339
|
|
|
{ |
1340
|
|
|
return $this->userPrincipal; |
1341
|
|
|
} |
1342
|
|
|
|
1343
|
|
|
/** |
1344
|
|
|
* Return's the login of the user making this request, if the user has been authenticated, or null if the |
1345
|
|
|
* user has not been authenticated. Whether the user name is sent with each subsequent request depends on |
1346
|
|
|
* the browser and type of authentication. Same as the value of the CGI variable REMOTE_USER. |
1347
|
|
|
* |
1348
|
|
|
* @return \AppserverIo\Lang\String|null A string specifying the login of the user making this request, or null if the user login is not known |
1349
|
|
|
*/ |
1350
|
|
|
public function getRemoteUser() |
1351
|
|
|
{ |
1352
|
|
|
if ($userPrincipal = $this->getUserPrincipal()) { |
1353
|
|
|
return $userPrincipal->getName(); |
1354
|
|
|
} |
1355
|
|
|
} |
1356
|
|
|
|
1357
|
|
|
/** |
1358
|
|
|
* Return_s a boolean indicating whether the authenticated user is included in the specified logical "role". |
1359
|
|
|
* |
1360
|
|
|
* @param \AppserverIo\Lang\String $role The role we want to test for |
1361
|
|
|
* |
1362
|
|
|
* @return boolean TRUE if the user has the passed role, else FALSE |
1363
|
|
|
*/ |
1364
|
|
|
public function isUserInRole(String $role) |
1365
|
|
|
{ |
1366
|
|
|
|
1367
|
|
|
// query whether or not we've an user principal |
1368
|
|
|
if ($principal = $this->getUserPrincipal()) { |
1369
|
|
|
return $principal->getRoles()->contains($role); |
|
|
|
|
1370
|
|
|
} |
1371
|
|
|
|
1372
|
|
|
// user is not in passed role |
1373
|
|
|
return false; |
1374
|
|
|
} |
1375
|
|
|
|
1376
|
|
|
/** |
1377
|
|
|
* Use the container login mechanism configured for the servlet context to authenticate the user making this |
1378
|
|
|
* request. This method may modify and commit the passed servlet response. |
1379
|
|
|
* |
1380
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The servlet response |
1381
|
|
|
* |
1382
|
|
|
* @return boolean TRUE when non-null values were or have been established as the values returned by getRemoteUser, else FALSE |
1383
|
|
|
*/ |
1384
|
|
|
public function authenticate(HttpServletResponseInterface $servletResponse) |
1385
|
|
|
{ |
1386
|
|
|
|
1387
|
|
|
// load the authentication manager and try to authenticate this request |
1388
|
|
|
/** @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager */ |
1389
|
|
|
if ($authenticationManager = $this->getAuthenticationManager()) { |
1390
|
|
|
return $authenticationManager->handleRequest($this, $servletResponse); |
1391
|
|
|
} |
1392
|
|
|
|
1393
|
|
|
// also return TRUE if we can't find an authentication manager |
1394
|
|
|
return true; |
1395
|
|
|
} |
1396
|
|
|
|
1397
|
|
|
/** |
1398
|
|
|
* Validate the provided username and password in the password validation realm used by the web |
1399
|
|
|
* container login mechanism configured for the ServletContext. |
1400
|
|
|
* |
1401
|
|
|
* @param \AppserverIo\Lang\String $username The username to login |
1402
|
|
|
* @param \AppserverIo\Lang\String $password The password used to authenticate the user |
1403
|
|
|
* |
1404
|
|
|
* @return void |
1405
|
|
|
* @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if no default authenticator can be found |
1406
|
|
|
*/ |
1407
|
|
|
public function login(String $username, String $password) |
1408
|
|
|
{ |
1409
|
|
|
|
1410
|
|
|
// query whether or not we're already authenticated or not |
1411
|
|
|
if ($this->getAuthType() != null || $this->getRemoteUser() != null || $this->getUserPrincipal() != null) { |
1412
|
|
|
throw new ServletException('Already authenticated'); |
1413
|
|
|
} |
1414
|
|
|
|
1415
|
|
|
// load the authentication manager and try to authenticate this request |
1416
|
|
|
/** @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager */ |
1417
|
|
|
if ($authenticationManager = $this->getAuthenticationManager()) { |
1418
|
|
|
// try to load the authentication managers default authenticator |
1419
|
|
|
if (($authenticator = $authenticationManager->getAuthenticator()) == null) { |
1420
|
|
|
throw new ServletException('Can\'t find default authenticator'); |
1421
|
|
|
} |
1422
|
|
|
|
1423
|
|
|
// authenticate the passed username/password combination |
1424
|
|
|
$authenticator->login($username, $password, $this); |
|
|
|
|
1425
|
|
|
} |
1426
|
|
|
} |
1427
|
|
|
|
1428
|
|
|
/** |
1429
|
|
|
* Establish null as the value returned when getUserPrincipal, getRemoteUser, and getAuthType is |
1430
|
|
|
* called on the request. |
1431
|
|
|
* |
1432
|
|
|
* @return void |
1433
|
|
|
* @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if no default authenticator can be found |
1434
|
|
|
*/ |
1435
|
|
|
public function logout() |
1436
|
|
|
{ |
1437
|
|
|
|
1438
|
|
|
// load the authentication manager and try to authenticate this request |
1439
|
|
|
/** @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager */ |
1440
|
|
|
if ($authenticationManager = $this->getAuthenticationManager()) { |
1441
|
|
|
// try to load the authentication managers default authenticator |
1442
|
|
|
if (($authenticator = $authenticationManager->getAuthenticator()) == null) { |
1443
|
|
|
throw new ServletException('Can\'t find default authenticator'); |
1444
|
|
|
} |
1445
|
|
|
|
1446
|
|
|
// logout the actual user |
1447
|
|
|
$authenticator->logout($this); |
|
|
|
|
1448
|
|
|
} |
1449
|
|
|
} |
1450
|
|
|
} |
1451
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..