forked from windwalker-io/http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebHttpServer.php
470 lines (400 loc) · 10.2 KB
/
WebHttpServer.php
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
/**
* Part of Windwalker project.
*
* @copyright Copyright (C) 2016 LYRASOFT. All rights reserved.
* @license GNU General Public License version 2 or later.
*/
namespace Windwalker\Http;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Windwalker\Http\Helper\ServerHelper;
use Windwalker\Http\Output\HttpCompressor;
use Windwalker\Http\Output\OutputInterface;
use Windwalker\Uri\PsrUri;
use Windwalker\Uri\UriData;
/**
* The WebServer class.
*
* @property-read UriData uri
* @property-read HttpCompressor compressor
*
* @since 3.0
*/
class WebHttpServer extends HttpServer
{
const CACHE_ENABLE = true;
const CACHE_DISABLE = false;
const CACHE_CUSTOM_HEADER = null;
/**
* Property uri.
*
* @var PsrUri
*/
protected $psrUri;
/**
* Property uriData.
*
* @var UriData
*/
protected $uriData;
/**
* Property cachable.
*
* @var boolean
*/
protected $cachable;
/**
* Property mimeType.
*
* @var string
*/
protected $contentType = 'text/html';
/**
* Property charSet.
*
* @var string
*/
protected $charSet = 'utf-8';
/**
* Property modifiedDate.
*
* @var \DateTime
*/
protected $modifiedDate;
/**
* Property compressor.
*
* @var HttpCompressor
*/
protected $compressor;
/**
* Server constructor.
*
* @param callable $handler
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param OutputInterface $output
*/
public function __construct($handler = null, ServerRequestInterface $request, ResponseInterface $response = null, OutputInterface $output = null)
{
parent::__construct($handler, $request, $response, $output);
$this->uriData = new UriData;
$this->loadSystemUris();
$this->compressor = $this->createHttpCompressor();
}
/**
* listen
*
* @param callable $errorHandler
*/
public function listen($errorHandler = null)
{
$response = $this->execute($errorHandler);
$this->output->respond($response);
}
/**
* execute
*
* @param callable $nextHandler
*
* @return ResponseInterface
*/
public function execute($nextHandler = null)
{
if (version_compare(PHP_VERSION, '5.4', '>=') && $this->handler instanceof \Closure)
{
$this->handler = $this->handler->bindTo($this);
}
$response = call_user_func($this->handler, $this->request, $this->response, $nextHandler);
if (!$response instanceof ResponseInterface)
{
$response = $this->response;
}
if (!$response->hasHeader('content-type'))
{
$response = $response->withHeader('content-type', $this->getContentType() . '; charset=' . $this->getCharSet());
}
return $this->prepareCache($response);
}
/**
* prepareCache
*
* @param ResponseInterface $response
*
* @return ResponseInterface
*/
public function prepareCache(ResponseInterface $response)
{
/** @var MessageInterface|ResponseInterface $response */
// Force cachable
if ($this->getCachable() === static::CACHE_ENABLE)
{
// Expires.
$response = $response->withoutHeader('Expires')->withHeader('Expires', gmdate('D, d M Y H:i:s', time() + 900) . ' GMT');
// Last modified.
if ($this->modifiedDate instanceof \DateTime)
{
$this->modifiedDate->setTimezone(new \DateTimeZone('UTC'));
$response = $response->withoutHeader('Last-Modified')->withHeader('Last-Modified', $this->modifiedDate->format('D, d M Y H:i:s') . ' GMT');
}
}
// Force uncachable
elseif ($this->getCachable() === static::CACHE_DISABLE)
{
// Expires in the past.
$response = $response->withoutHeader('Expires')->withHeader('Expires', 'Mon, 1 Jan 2001 00:00:00 GMT');
// Always modified.
$response = $response->withoutHeader('Last-Modified')->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
$response = $response->withoutHeader('Cache-Control')->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
// HTTP 1.0
$response = $response->withHeader('pragma', 'no-cache');
}
return $response;
}
/**
* Method to load the system URI strings for the application.
*
* @param string $requestUri An optional request URI to use instead of detecting one from the
* server environment variables.
*
* @return void
*
* @since 2.0
*/
protected function loadSystemUris($requestUri = null)
{
$server = $this->getRequest()->getServerParams();
$uri = $this->getSystemUri($requestUri);
$original = $requestUri ? new PsrUri($requestUri) : $this->getRequest()->getUri();
// Get the host and path from the URI.
$host = $uri->withQuery('')->withFragment('')->withPath('')->__toString();
$path = rtrim($uri->getPath(), '/\\');
$script = trim(ServerHelper::getValue($server, 'SCRIPT_NAME', ''), '/');
// Check if the path includes "index.php".
if ($script && strpos($path, $script) === 0)
{
// Remove the index.php portion of the path.
$path = substr_replace($path, '', strpos($path, $script), strlen($script));
$path = rtrim($path, '/\\');
}
$scriptName = pathinfo($script, PATHINFO_BASENAME);
// Set the base URI both as just a path and as the full URI.
$this->uriData->full = rtrim($original->__toString(), '/');
$this->uriData->current = rtrim($original->withQuery('')->withFragment('')->__toString(), '/');
$this->uriData->script = $scriptName;
$this->uriData->root = $host . $path;
$this->uriData->host = $host;
$this->uriData->path = $path;
// Set the extended (non-base) part of the request URI as the route.
$route = substr_replace($this->uriData->current, '', 0, strlen($this->uriData->root));
$route = ltrim($route, '/');
// Only variables should be passed by reference so we use two lines.
$file = explode('/', $script);
$file = array_pop($file);
if (substr($route, 0, strlen($file)) == $file)
{
$route = trim(substr($route, strlen($file)), '/');
}
$this->uriData->route = $route;
}
/**
* Get system Uri object.
*
* @param string $requestUri The request uri string.
* @param bool $refresh Refresh the uri.
*
* @return PsrUri The system Uri object.
*
* @since 2.0
*/
protected function getSystemUri($requestUri = null, $refresh = false)
{
if ($this->psrUri && !$refresh)
{
return $this->psrUri;
}
$uri = $requestUri ? new PsrUri($requestUri) : $this->getRequest()->getUri();
$server = $this->getRequest()->getServerParams();
// If we are working from a CGI SAPI with the 'cgi.fix_pathinfo' directive disabled we use PHP_SELF.
if (strpos(php_sapi_name(), 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($server['REQUEST_URI']))
{
// We aren't expecting PATH_INFO within PHP_SELF so this should work.
$uri = $uri->withPath(rtrim(dirname(ServerHelper::getValue($server, 'PHP_SELF')), '/\\'));
}
// Pretty much everything else should be handled with SCRIPT_NAME.
else
{
$uri = $uri->withPath(rtrim(dirname(ServerHelper::getValue($server, 'SCRIPT_NAME')), '/\\'));
}
// Clear the unused parts of the requested URI.
$uri = $uri->withFragment('');
return $this->psrUri = $uri;
}
/**
* Method to get property MimeType
*
* @return string
*/
public function getContentType()
{
return $this->contentType;
}
/**
* Method to set property mimeType
*
* @param string $contentType
*
* @return static Return self to support chaining.
*/
public function setContentType($contentType)
{
$this->contentType = $contentType;
return $this;
}
/**
* Method to get property CharSet
*
* @return string
*/
public function getCharSet()
{
return $this->charSet;
}
/**
* Method to set property charSet
*
* @param string $charSet
*
* @return static Return self to support chaining.
*/
public function setCharSet($charSet)
{
$this->charSet = $charSet;
return $this;
}
/**
* Method to get property ModifiedDate
*
* @return \DateTime
*/
public function getModifiedDate()
{
return $this->modifiedDate;
}
/**
* Method to set property modifiedDate
*
* @param \DateTime $modifiedDate
*
* @return static Return self to support chaining.
*/
public function setModifiedDate(\DateTime $modifiedDate)
{
$this->modifiedDate = $modifiedDate;
return $this;
}
/**
* Method to get property UriData
*
* @return UriData
*/
public function getUriData()
{
return $this->uriData;
}
/**
* Method to set property uriData
*
* @param array|UriData $uriData
*
* @return static Return self to support chaining.
*/
public function setUriData($uriData)
{
if (!$uriData instanceof UriData)
{
$uriData = new UriData($uriData);
}
$this->uriData = $uriData;
return $this;
}
/**
* __get
*
* @param string $name
*
* @return mixed
*/
public function __get($name)
{
if ($name == 'uri')
{
return $this->uriData;
}
if ($name == 'compressor')
{
return $this->compressor;
}
throw new \OutOfRangeException('Property: ' . $name . ' not exists.');
}
/**
* Method to get property Compressor
*
* @return HttpCompressor
*/
public function getCompressor()
{
return $this->compressor;
}
/**
* Method to set property compressor
*
* @param HttpCompressor $compressor
*
* @return static Return self to support chaining.
*/
public function setCompressor(HttpCompressor $compressor)
{
$this->compressor = $compressor;
return $this;
}
/**
* Method to get property Cachable
*
* @return boolean
*/
public function getCachable()
{
return $this->cachable;
}
/**
* Method to set property cachable
*
* @param boolean $cachable
*
* @return static Return self to support chaining.
*/
public function cachable($cachable = self::CACHE_CUSTOM_HEADER)
{
$this->cachable = $cachable;
return $this;
}
/**
* Create Compressor object.
*
* @param string $acceptEncoding The HTTP_ACCEPT_ENCODING param, the common is "gzip, deflate".
*
* @return HttpCompressor
*/
public function createHttpCompressor($acceptEncoding = null)
{
if (!$acceptEncoding)
{
$servers = $this->getRequest()->getServerParams();
$acceptEncoding = isset($servers['HTTP_ACCEPT_ENCODING']) ? $servers['HTTP_ACCEPT_ENCODING'] : '';
}
return new HttpCompressor($acceptEncoding);
}
}