ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
RequestFactoryImpl.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\HTTP\Request;
20 
23 
39 {
40  private const DEFAULT_FORWARDED_HEADER = 'X-Forwarded-Proto';
41  private const DEFAULT_FORWARDED_PROTO = 'https';
42 
43  public function __construct(
44  private ?string $forwarded_header = null,
45  private ?string $forwarded_proto = null
46  ) {
47  }
48 
49  public function create(): ServerRequestInterface
50  {
51  $server_request = ServerRequest::fromGlobals();
52 
53  if ($this->forwarded_header !== null && $this->forwarded_proto !== null) {
54  if (in_array(
55  $this->forwarded_proto,
56  $server_request->getHeader($this->forwarded_header),
57  true
58  )) {
59  return $server_request->withUri($server_request->getUri()->withScheme($this->forwarded_proto));
60  }
61 
62  // alternative if ini settings are used which look like X_FORWARDED_PROTO
63  $header_names = array_keys($server_request->getHeaders());
64  foreach ($header_names as $header_name) {
65  if (str_replace("-", "_", strtoupper($header_name)) !== $this->forwarded_header) {
66  continue;
67  }
68  if (!in_array($this->forwarded_proto, $server_request->getHeader($header_name), true)) {
69  continue;
70  }
71  return $server_request->withUri($server_request->getUri()->withScheme($this->forwarded_proto));
72  }
73  }
74 
75  return $server_request;
76  }
77 }
__construct(private ?string $forwarded_header=null, private ?string $forwarded_proto=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
create()
Creates a new ServerRequest object with the help of the underlying library.