8000 Fixed absolute url generation for query strings and hash urls by alexander-schranz · Pull Request #23261 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fixed absolute url generation for query strings and hash urls #23261

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fixed absolute url generation for query strings
  • Loading branch information
alexander-schranz committed Jun 22, 2017
commit 508d9a45a315f1c06036463ee7a3bbef9b4bf23f
17 changes: 17 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/HttpFoundationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public function generateAbsoluteUrl($path)
$port = ':'.$this->requestContext->getHttpsPort();
}

if ('#' === $path[0]) {
$queryString = $this->requestContext->getQueryString();
$path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path;
}

if ('?' === $path[0]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use elseif here and also below?

$path = $this->requestContext->getPathInfo().$path;
}

if ('/' !== $path[0]) {
$path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
}
Expand All @@ -80,6 +89,14 @@ public function generateAbsoluteUrl($path)
return $path;
}

if ('#' === $path[0]) {
$path = $request->getRequestUri().$path;
}

if ('?' === $path[0]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

also elseif?

$path = $request->getPathInfo().$path;
}

if (!$path || '/' !== $path[0]) {
$prefix = $request->getPathInfo();
$last = strlen($prefix) - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function getGenerateAbsoluteUrlData()
array('http://example.com/baz', 'http://example.com/baz', '/'),
array('https://example.com/baz', 'https://example.com/baz', '/'),
array('//example.com/baz', '//example.com/baz', '/'),

array('http://localhost/foo/bar?baz', '?baz', '/foo/bar'),
array('http://localhost/foo/bar?baz=1', '?baz=1', '/foo/bar?foo=1'),
array('http://localhost/foo/baz?baz=1', 'baz?baz=1', '/foo/bar?foo=1'),

array('http://localhost/foo/bar#baz', '#baz', '/foo/bar'),
array('http://localhost/foo/bar?baz=1#baz', '?baz=1#baz', '/foo/bar?foo=1'),
array('http://localhost/foo/baz?baz=1#baz', 'baz?baz=1#baz', '/foo/bar?foo=1'),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add this test case?

array('http://localhost/foo/bar?0#foo', '#foo', '/foo/bar?0'),

I think your current solution will strip out the existing ?0 query string. I know its an edge case but still 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test added seems not failing as a 0 as string is true.

Copy link
Contributor

Choose a reason for hiding this comment

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

php -r "var_dump((bool)'0');"

==> bool(false)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dmaicher can you tell me whats wrong with the test as it should fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah this is only executed when no current request is given (CLI) and the request context is used instead 😉

So your changes from L 73-79 are currently not covered by tests. You need to add some more test cases into the getGenerateAbsoluteUrlRequestContextData provider 😉

}

Expand Down
0