-
Notifications
You must be signed in to change notification settings - Fork 386
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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 |
jtojnar
commented
Mar 18, 2023
There was a problem hiding this comment.
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.
60777aa
to
787d2fe
Compare
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.
787d2fe
to
47f2caf
Compare
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
File
class handles redirects by calling the constructor recursively. To avoid setting thepermanent_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 whenHTTP 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.