Skip to content

Commit 797004c

Browse files
author
Dave MacFarlane
committed
[Core/Refactor] Do not mangle request URI
The BaseRouter/Prefix router stripped off the handled part of the URI before passing it to other handlers. The reasoning was that then the modules could treat the request without regard to the location that LORIS is being served for and act as if it's always the root. However, in practice this causes more problems than it solves. The original URI is lost completely, so middleware, error pages, and http headers that may need it do not have access to it without reconstructing it. This updates the handled part of the URI to go into an "unhandledURI" attribute while leaving the request->getURI untouched. Doing so allows us to access the original URI when needed and if ie. you print $request->getURI() in an error message you will get the correct value.
1 parent 00193bb commit 797004c

File tree

11 files changed

+30
-19
lines changed

11 files changed

+30
-19
lines changed

modules/api/php/module.class.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Module extends \Module
6868
}
6969

7070
// Requests sent to this module must start with /api/$version
71-
$url = $request->getURI()->getPath();
71+
$url = $request->getAttribute("unhandledURI")->getPath();
7272
$pieces = [];
7373
if (preg_match(
7474
"/^\/?(v[0-9]+\.[0-9]+\.[0-9]+[^\/]*)\/(.*)/",

modules/biobank/php/endpoint.class.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ abstract class Endpoint
9595
ServerRequestInterface $request,
9696
RequestHandlerInterface $handler
9797
): ResponseInterface {
98-
$path = trim($request->getURI()->getPath(), "/");
98+
$path = trim($request->getAttribute("unhandledURI")->getPath(), "/");
9999
$pathparts = explode('/', $path);
100100

101101
// Check if sub-handlers are supported

modules/biobank/php/module.class.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class Module extends \Module
6565
{
6666
$this->logger->debug("Module handle function called");
6767
$resp = parent::handle($request);
68+
$path = $request->getAttribute("unhandledURI")->getPath();
6869
if ($resp->getStatusCode() != 404) {
69-
$path = $request->getURI()->getPath();
7070
if (preg_match('/(\.css)$/', $path) == 1) {
7171
$resp = $resp->withHeader(
7272
"Content-Type",
@@ -84,7 +84,7 @@ class Module extends \Module
8484

8585
$this->loris = $request->getAttribute("loris");
8686
$pagename = $this->getName();
87-
$path = trim($request->getURI()->getPath(), "/");
87+
$path = trim($path, "/");
8888
if ($path == 'optionsendpoint'
8989
|| $path == 'poolendpoint'
9090
|| $path == 'containerendpoint'

modules/biobank/php/preparations.class.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Preparations implements RequestHandlerInterface
132132
$this->loris = $request->getAttribute("loris");
133133

134134
$db = $this->loris->getDatabaseConnection();
135-
$endpoint = $request->getURI()->getPath();
135+
$endpoint = $request->getAttribute("unhandledURI")->getPath();
136136
$pathparts = explode('/', $endpoint);
137137
$db->delete(
138138
'biobank_specimen_preparation',

modules/candidate_profile/php/module.class.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Module extends \Module
2323
*/
2424
public function handle(ServerRequestInterface $request) : ResponseInterface
2525
{
26-
$candIDStr = ltrim($request->getURI()->getPath(), '/');
26+
$candIDStr = ltrim($request->getAttribute("unhandledURI")->getPath(), '/');
2727
try {
2828
$candID = new CandID($candIDStr);
2929
$candidate = \Candidate::singleton($candID);

modules/conflict_resolver/php/module.class.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Module extends \Module
5858
*/
5959
public function handle(ServerRequestInterface $request) : ResponseInterface
6060
{
61-
$path = trim($request->getURI()->getPath(), "/");
61+
$path = trim($request->getAttribute("unhandledURI")->getPath(), "/");
6262
switch ($path) {
6363
case 'unresolved':
6464
$handler = new Endpoints\Unresolved($this->loris);

modules/statistics/php/charts.class.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ class Charts extends \NDB_Page
8181
// Strip any prefix of '/' to ensure that we don't have an empty string
8282
// when splitting the path, then there should be exactly 2 parts left,
8383
// "charts", and the endpoint requested.
84-
$url = ltrim($request->getURI()->getPath(), '/');
84+
$url = ltrim(
85+
$request->getAttribute("unhandledURI")->getURI()->getPath(),
86+
'/'
87+
);
88+
8589
$pathparts = explode('/', $url);
8690
if (count($pathparts) != 2) {
8791
return new \LORIS\Http\Response\JSON\NotFound();

php/libraries/Module.class.inc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,11 @@ abstract class Module extends \LORIS\Router\PrefixRouter
271271
public function handle(ServerRequestInterface $request) : ResponseInterface
272272
{
273273
$this->logger->debug("Module handle function called");
274-
$resp = parent::handle($request);
274+
$moduleURI = $request->getAttribute("unhandledURI");
275+
$resp = parent::handle($request);
276+
275277
if ($resp->getStatusCode() != 404) {
276-
$path = $request->getURI()->getPath();
278+
$path = $moduleURI->getPath();
277279
if (preg_match('/(\.css)$/', $path) == 1) {
278280
$resp = $resp->withHeader(
279281
"Content-Type",
@@ -290,7 +292,7 @@ abstract class Module extends \LORIS\Router\PrefixRouter
290292
}
291293

292294
$pagename = $this->getName();
293-
$path = trim($request->getURI()->getPath(), "/");
295+
$path = trim($moduleURI->getPath(), "/");
294296
if (!empty($path)) {
295297
// There is a subpage
296298
$pagename = explode("/", $path)[0];

src/Router/BaseRouter.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ public function __construct(
5454
*/
5555
public function handle(ServerRequestInterface $request) : ResponseInterface
5656
{
57-
$uri = $request->getUri();
58-
$path = $uri->getPath();
57+
$request = $request->withAttribute("unhandledURI", $request->getURI());
58+
$uri = $request->getUri();
59+
$path = $uri->getPath();
5960

6061
// Replace multiple slashes in the URL with a single slash
6162
$path = preg_replace("/\/+/", "/", $path);
@@ -157,7 +158,8 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
157158
$module->setLogger(new \PSR\Log\NullLogger);
158159
}
159160
$mr = new ModuleRouter($module);
160-
$request = $request->withURI($suburi);
161+
$request = $request->withAttribute("unhandledURI", $suburi);
162+
161163
return $ehandler->process($request, $mr);
162164
}
163165
// Legacy from .htaccess. A CandID goes to the timepoint_list

src/Router/ModuleFileRouter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,15 @@ public function __construct(\Module $module, string $moduledir, string $subdir,
7676
*/
7777
public function handle(ServerRequestInterface $request) : ResponseInterface
7878
{
79+
$uri = $request->getAttribute("unhandledURI");
80+
$path = !empty($uri) ? $uri->getPath() : $request->getURI()->getPath();
81+
7982
$fullpath = (
8083
$this->moduledir .
8184
"/" .
8285
$this->subdir .
8386
"/"
84-
. $request->getURI()->getPath()
87+
. $path
8588
);
8689

8790
if (is_file($fullpath)) {

0 commit comments

Comments
 (0)