Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File: Fix permanent_url with multiple permanent redirects #812

Merged
merged 1 commit into from
Mar 19, 2023

Conversation

jtojnar
Copy link
Contributor

@jtojnar jtojnar commented Mar 18, 2023

The File class handles redirects by calling the constructor recursively. To avoid setting the permanent_url incorrectly at the top of the constructor, the value of the property was reset after each constructor call. And since local variables were used, only the reset following the first internal constructor call would actually do anything. This was very confusing and would stop considering permanent redirects after the first one.
Let’s avoid doing stuff after popping the stack, tail recursion FTW.

Additionally, the permanent_url would only be updated when HTTP 301 Moved Permanently status code was encountered.
Let’s also support HTTP 308 Permanent Redirect.

Tracking the mutability in a property is not the cleanest but a bigger refactoring is probably not worth it with the upcoming deprecation of File.

Unfortunately, we cannot easily test this since we cannot mock curl.

@jtojnar
Copy link
Contributor Author

jtojnar commented Mar 18, 2023

I managed to test this with

--- a/tests/Unit/FileTest.php
+++ b/tests/Unit/FileTest.php
@@ -45,6 +45,7 @@ declare(strict_types=1);
 namespace SimplePie\Tests\Unit;
 
 use PHPUnit\Framework\TestCase;
+use SimplePie\File;
 
 class FileTest extends TestCase
 {
@@ -57,4 +58,13 @@ class FileTest extends TestCase
     {
         $this->assertTrue(class_exists('SimplePie_File'));
     }
+
+    public function testFoo() {
+        $file = new File(
+            url: 'http://localhost:8000/perm2',
+            redirects: 10,
+        );
+        $this->assertSame('http://localhost:8000/temp2', $file->permanent_url);
+        $this->assertSame('http://localhost:8000/final', $file->url);
+    }
 }

while running the following script with php -S localhost:8000 srv.php:

<?php

function redirect(int $status, string $location): void {
    http_response_code($status);
    header("Location: $location");
}

function output(int $status, string $text): void {
    http_response_code($status);
    echo $text;
}

match ($_SERVER['REQUEST_URI']) {
    '/perm2' => redirect(308, '/perm1'),
    '/perm1' => redirect(301, '/perm0'),
    '/perm0' => redirect(308, '/temp2'),
    '/temp2' => redirect(307, '/temp1'),
    '/temp1' => redirect(302, '/temp0'),
    '/temp0' => redirect(307, '/permA'),
    '/permA' => redirect(301, '/permB'),
    '/permB' => redirect(308, '/permC'),
    '/permC' => redirect(308, '/final'),
    '/final' => output(200, '/ok'),
    default => output(404, 'Not found'),
};

both with curl and fsockopen.

Copy link
Contributor Author

@jtojnar jtojnar Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primary motivation is to make Response::get_permanent_uri() from #774 meaningful.

The `File` class handles redirects by calling the constructor recursively.
To avoid setting the `permanent_url` incorrectly at the top of the constructor,
the value of the property was reset after each constructor call.
And since local variables were used, only the reset following the first internal
constructor call would actually do anything.
This was very confusing and would stop considering permanent redirects
after the first one.
Let’s avoid doing stuff after popping the stack, tail recursion FTW.

Additionally, the `permanent_url` would only be updated when `HTTP 301 Moved Permanently`
status code was encountered.
Let’s also support `HTTP 308 Permanent Redirect`.

Tracking the mutability in a property is not the cleanest but a bigger refactoring
is probably not worth it with the upcoming deprecation of File.

Unfortunately, we cannot easily test this since we cannot mock curl.
@mblaney mblaney merged commit 3c28077 into simplepie:master Mar 19, 2023
@jtojnar jtojnar deleted the fix-permanent-redirect branch March 19, 2023 12:36
jtojnar added a commit to jtojnar/simplepie that referenced this pull request Mar 19, 2023
This is a regression test for simplepie#812.
jtojnar added a commit to jtojnar/simplepie that referenced this pull request May 23, 2023
This is a regression test for simplepie#812.
jtojnar added a commit to jtojnar/simplepie that referenced this pull request May 23, 2023
This is a regression test for simplepie#812.
jtojnar added a commit to jtojnar/simplepie that referenced this pull request May 23, 2023
This is a regression test for simplepie#812.
jtojnar added a commit to jtojnar/simplepie that referenced this pull request Jun 12, 2023
This is a regression test for simplepie#812.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants