I ran into an issue when setting a Page Redirect in a content type.
My content type, callled Document, has a Media Entity reference field (field_file) to allow user to upload files (e.g. PDF, DOC, DOCX). I set a Page Redirect when accessing the Document content type to redirect to the media file from field_file, using the following token:

[node:field_file:entity:field_media_file]

Redirect works fine, except when the file name has a space, for instance "my document.pdf". In this case, the user is redirect to a nonexisting page. For instance:

Correct (expected) file path:
/sites/default/files/documents/my%20document.pdf

Given file path:
/sites/default/files/documents/my%2520document.pdf

What's happening here is that the received token target is already urlencoded, and as long as the path isn't external, the function toString() on line 173 apparently urlencodes target again, replacing % for %25 (ASCii code for percent symbol).

I created a patch which undoes the token urlencoding (using rawurldecode(), preventing the duplication. Of course, it needs review, especially for more complex cases such as multilingual sites.

Comments

maursilveira created an issue. See original summary.

maursilveira’s picture

StatusFileSize
new546 bytes

This is the patch I wrote to fix it. Of course, it needs review, especially for more complex cases such as multilingual sites.

maursilveira’s picture

I wrote the following patch to solve the duplicated urlencoding. Of course, it needs review, especially for more complex cases such as multilingual sites.

maursilveira’s picture

Status: Active » Needs work

I wrote the following patch to fix the duplicated urlencoding. Of course, it needs review, especially for more complex cases such as multilingual sites.

maursilveira’s picture

Issue summary: View changes
danielveza’s picture

Not working in my case, redirecting to [term:tid]. I'll chuck a patch up if I can spot the cause.

Technorange’s picture

Status: Needs work » Needs review
StatusFileSize
new573 bytes

Needs to decode it using htmlspecialchars_decode() instead of rawurldecode() I have updated the patch.

avpaderno’s picture

Version: 8.x-1.0-beta4 » 8.x-1.x-dev
jimafisk’s picture

Version: 8.x-1.x-dev » 8.x-1.0-beta7
StatusFileSize
new759 bytes

Rerolled patch from #2.

jimafisk’s picture

StatusFileSize
new759 bytes

Actual patch attached.

jimafisk’s picture

StatusFileSize
new759 bytes

Ok I think I got it this time :).

jimafisk’s picture

StatusFileSize
new631 bytes

Maybe this one will actually apply...

matroskeen’s picture

Hey there,
Can anyone confirm that issue still exist in the latest dev version? (8.x-1.x branch)

I was trying to reproduce it recently, but it seems ok. Our tests also don't complain about the file with space in the file name, see: https://git.drupalcode.org/project/rabbit_hole/-/blob/8.x-1.x/tests/src/...

If you're able to reproduce it somehow, please let me know.
Thanks!

matroskeen’s picture

Status: Needs review » Fixed

I believe it was fixed at some point, so closing this one.
Feel free to re-open if the issue still exists in the 8.x-1.x branch.

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

gillesv’s picture

This is still an issue in the latest versions of Rabbit Hole 1.x and 2.x
When there are spaces in the filename, and you try to redirect to it (via [node:field_file] for example), it double encodes the URL resulting in a "Page not found" error.

@jimafisk ' s solutions to replace "PlainTextOutput::renderFromHtml" with "rawurldecode" in PageRedirect.php seems to fix the problem.