@@ -30,22 +30,25 @@ class HttpUtils
3030 private $ urlGenerator ;
3131 private $ urlMatcher ;
3232 private $ domainRegexp ;
33+ private $ secureDomainRegexp ;
3334
3435 /**
35- * @param UrlGeneratorInterface $urlGenerator A UrlGeneratorInterface instance
36- * @param UrlMatcherInterface|RequestMatcherInterface $urlMatcher The URL or Request matcher
37- * @param string|null $domainRegexp A regexp that the target of HTTP redirections must match, scheme included
36+ * @param UrlGeneratorInterface $urlGenerator A UrlGeneratorInterface instance
37+ * @param UrlMatcherInterface|RequestMatcherInterface $urlMatcher The URL or Request matcher
38+ * @param string|null $domainRegexp A regexp the target of HTTP redirections must match, scheme included
39+ * @param string|null $secureDomainRegexp A regexp the target of HTTP redirections must match when the scheme is "https"
3840 *
3941 * @throws \InvalidArgumentException
4042 */
41- public function __construct (UrlGeneratorInterface $ urlGenerator = null , $ urlMatcher = null , $ domainRegexp = null )
43+ public function __construct (UrlGeneratorInterface $ urlGenerator = null , $ urlMatcher = null , string $ domainRegexp = null , string $ secureDomainRegexp = null )
4244 {
4345 $ this ->urlGenerator = $ urlGenerator ;
4446 if (null !== $ urlMatcher && !$ urlMatcher instanceof UrlMatcherInterface && !$ urlMatcher instanceof RequestMatcherInterface) {
4547 throw new \InvalidArgumentException ('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface. ' );
4648 }
4749 $ this ->urlMatcher = $ urlMatcher ;
4850 $ this ->domainRegexp = $ domainRegexp ;
51+ $ this ->secureDomainRegexp = $ secureDomainRegexp ;
4952 }
5053
5154 /**
@@ -59,6 +62,9 @@ public function __construct(UrlGeneratorInterface $urlGenerator = null, $urlMatc
5962 */
6063 public function createRedirectResponse (Request $ request , $ path , $ status = 302 )
6164 {
65+ if (null !== $ this ->secureDomainRegexp && 'https ' === $ this ->urlMatcher ->getContext ()->getScheme () && preg_match ('#^https?://[^/]++#i ' , $ path , $ host ) && !preg_match (sprintf ($ this ->secureDomainRegexp , preg_quote ($ request ->getHttpHost ())), $ host [0 ])) {
66+ $ path = '/ ' ;
67+ }
6268 if (null !== $ this ->domainRegexp && preg_match ('#^https?://[^/]++#i ' , $ path , $ host ) && !preg_match (sprintf ($ this ->domainRegexp , preg_quote ($ request ->getHttpHost ())), $ host [0 ])) {
6369 $ path = '/ ' ;
6470 }
0 commit comments