Skip to content

Commit bad24d0

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 bad24d0

File tree

8 files changed

+26
-13
lines changed

8 files changed

+26
-13
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/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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function __construct(
5454
*/
5555
public function handle(ServerRequestInterface $request) : ResponseInterface
5656
{
57+
$request = $request->withAttribute("unhandledURI", $request->getURI());
5758
$uri = $request->getUri();
5859
$path = $uri->getPath();
5960

@@ -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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ 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();
7981
$fullpath = (
8082
$this->moduledir .
8183
"/" .
8284
$this->subdir .
8385
"/"
84-
. $request->getURI()->getPath()
86+
. $path
8587
);
8688

8789
if (is_file($fullpath)) {

src/Router/PrefixRouter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ protected function stripPrefix(string $prefix, URIInterface $uri) : URIInterface
9292
*/
9393
public function handle(ServerRequestInterface $request) : ResponseInterface
9494
{
95+
$uri = $request->getAttribute("unhandledURI");
96+
if(empty($uri)) {
97+
$uri = $request->getURI();
98+
}
9599
foreach ($this->paths as $path => $subhandler) {
96-
if ($this->hasPrefix($path, $request->getURI())) {
97-
// Strip the prefix before passing it to the subhandler.
98-
$newURI = $this->stripPrefix($path, $request->getURI());
99-
$request = $request->withURI($newURI);
100+
if ($this->hasPrefix($path, $uri)) {
101+
$newURI = $this->stripPrefix($path, $uri);
102+
$request = $request->withAttribute("unhandledURI", $newURI);
100103
return $subhandler->handle($request);
101104
}
102105
}

0 commit comments

Comments
 (0)