-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add strict option for http url in asset #22510
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0ab660
Add strict option for http url in asset
8e4d12a
Move the isSecure check in getBaseUrl
36a8779
Add some tests. Add line to UPGRADE.md.
e1ed357
Handle asset update in Symfony + test
58764b6
Revert change for UPGRADE.md
177ccce
Fix parameter position for is_strict_protocol
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,23 +27,39 @@ | |
* When the request context is available, this package can choose the | ||
* best base URL to use based on the current request scheme: | ||
* | ||
* * For HTTP request, it chooses between all base URLs; | ||
* * For HTTP request: | ||
* * if $isStrictProtocol is set to false, it chooses between all base URLs | ||
* * if $isStrictProtocol is set to true, it will only use HTTP base URLs and relative protocol URLs | ||
* or falls back to any base URL if no secure ones are available; | ||
* * For HTTPs requests, it chooses between HTTPs base URLs and relative protocol URLs | ||
* or falls back to any base URL if no secure ones are available. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class UrlPackage extends Package | ||
{ | ||
/** @var array $baseUrls */ | ||
private $baseUrls = array(); | ||
private $sslPackage; | ||
|
||
/** @var string[] $baseSecureUrls */ | ||
private $baseSecureUrls = array(); | ||
|
||
/** @var string[] $baseUnsecureUrls */ | ||
private $baseInsecureUrls = array(); | ||
|
||
/** @var string[] $baseFullUrls */ | ||
private $baseFullUrls = array(); | ||
|
||
/** @var bool $isStrictProtocol */ | ||
private $isStrictProtocol; | ||
|
||
/** | ||
* @param string|string[] $baseUrls Base asset URLs | ||
* @param VersionStrategyInterface $versionStrategy The version strategy | ||
* @param ContextInterface|null $context Context | ||
* @param string|string[] $baseUrls Base asset URLs | ||
* @param VersionStrategyInterface $versionStrategy The version strategy | ||
* @param ContextInterface|null $context Context | ||
* @param bool $isStrictProtocol Is http strict, or does it allow https on http pages | ||
*/ | ||
public function __construct($baseUrls, VersionStrategyInterface $versionStrategy, ContextInterface $context = null) | ||
public function __construct($baseUrls, VersionStrategyInterface $versionStrategy, ContextInterface $context = null, $isStrictProtocol = false) | ||
{ | ||
parent::__construct($versionStrategy, $context); | ||
|
||
|
@@ -59,11 +75,9 @@ public function __construct($baseUrls, VersionStrategyInterface $versionStrategy | |
$this->baseUrls[] = rtrim($baseUrl, '/'); | ||
} | ||
|
||
$sslUrls = $this->getSslUrls($baseUrls); | ||
$this->isStrictProtocol = $isStrictProtocol; | ||
|
||
if ($sslUrls && $baseUrls !== $sslUrls) { | ||
$this->sslPackage = new self($sslUrls, $versionStrategy); | ||
} | ||
$this->prepareBaseUrl($this->baseUrls); | ||
} | ||
|
||
/** | ||
|
@@ -75,10 +89,6 @@ public function getUrl($path) | |
return $path; | ||
} | ||
|
||
if (null !== $this->sslPackage && $this->getContext()->isSecure()) { | ||
return $this->sslPackage->getUrl($path); | ||
} | ||
|
||
$url = $this->getVersionStrategy()->applyVersion($path); | ||
|
||
if ($this->isAbsoluteUrl($url)) { | ||
|
@@ -105,6 +115,8 @@ public function getBaseUrl($path) | |
return $this->baseUrls[0]; | ||
} | ||
|
||
$this->setCurrentBaseUrls(); | ||
|
||
return $this->baseUrls[$this->chooseBaseUrl($path)]; | ||
} | ||
|
||
|
@@ -123,17 +135,44 @@ protected function chooseBaseUrl($path) | |
return (int) fmod(hexdec(substr(hash('sha256', $path), 0, 10)), count($this->baseUrls)); | ||
} | ||
|
||
private function getSslUrls($urls) | ||
/** | ||
* Set the baseUrls var depending on the context. | ||
*/ | ||
private function setCurrentBaseUrls() | ||
{ | ||
$sslUrls = array(); | ||
if (!empty($this->baseSecureUrls) && $this->getContext()->isSecure()) { | ||
$this->baseUrls = $this->baseSecureUrls; | ||
} elseif (!empty($this->baseInsecureUrls) && $this->isStrictProtocol) { | ||
$this->baseUrls = $this->baseInsecureUrls; | ||
} else { | ||
$this->baseUrls = $this->baseFullUrls; | ||
} | ||
} | ||
|
||
/** | ||
* Split urls in three categories: secure urls, unsecure urls and all urls | ||
* Some url can be found in both secure & insecure categories (// & https depending on $isStrictProtocol option). | ||
* | ||
* @param array $urls | ||
*/ | ||
private function prepareBaseUrl(array $urls) | ||
{ | ||
$this->baseFullUrls = $urls; | ||
|
||
foreach ($urls as $url) { | ||
if ('https://' === substr($url, 0, 8) || '//' === substr($url, 0, 2)) { | ||
$sslUrls[] = $url; | ||
} elseif ('http://' !== substr($url, 0, 7)) { | ||
if ('https://' === substr($url, 0, 8)) { | ||
$this->baseSecureUrls[] = $url; | ||
if ($this->isStrictProtocol === false) { | ||
$this->baseInsecureUrls[] = $url; | ||
} | ||
} elseif ('http://' === substr($url, 0, 7)) { | ||
$this->baseInsecureUrls[] = $url; | ||
} elseif ('//' === substr($url, 0, 2)) { | ||
$this->baseInsecureUrls[] = $url; | ||
$this->baseSecureUrls[] = $url; | ||
} else { | ||
throw new InvalidArgumentException(sprintf('"%s" is not a valid URL', $url)); | ||
} | ||
} | ||
|
||
return $sslUrls; | ||
} | ||
} |
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.
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.
You should add a test case with a protocol-relative URL.
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.
Done