Skip to content

Commit 54dd0b4

Browse files
committed
add a feature that allows you to specify the proxy URL with the port number as the following:
phpmentors_proxy_url_rewrite: proxy_urls: foo_bar: path: "!^.*!" proxy_url: "http://www.example.com:8080/foo/bar"
1 parent a657d0a commit 54dd0b4

File tree

6 files changed

+59
-19
lines changed

6 files changed

+59
-19
lines changed

src/Asset/ProxyPackages.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ public function getUrl($path, $packageName = null)
7171
if ($matchedProxyUrl->getHost() !== null) {
7272
$requestContext->setHost($matchedProxyUrl->getHost());
7373
}
74-
if ($matchedProxyUrl->getScheme() !== null) {
75-
$requestContext->setScheme($matchedProxyUrl->getScheme());
74+
$requestContext->setScheme($matchedProxyUrl->getScheme());
75+
76+
if ($matchedProxyUrl->getPort() !== null) {
77+
if ($matchedProxyUrl->getScheme() == 'http') {
78+
$requestContext->setHttpPort($matchedProxyUrl->getPort());
79+
} elseif ($matchedProxyUrl->getScheme() == 'https') {
80+
$requestContext->setHttpsPort($matchedProxyUrl->getPort());
81+
}
7682
}
7783

7884
$urlGenerator = new UrlGenerator($routeCollection, $requestContext);

src/EventListener/ProxyUrlRewriteListener.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,18 @@ public function onKernelRequest(GetResponseEvent $event)
5858
$matchedProxyUrl = $this->proxyUrlMatcher->match($this->router->getContext()->getPathInfo());
5959
if ($matchedProxyUrl !== null) {
6060
$this->router->getContext()->setBaseUrl($matchedProxyUrl->getPath().$this->router->getContext()->getBaseUrl());
61-
62-
if ($matchedProxyUrl->getScheme() !== null) {
63-
$this->router->getContext()->setScheme($matchedProxyUrl->getScheme());
64-
}
61+
$this->router->getContext()->setScheme($matchedProxyUrl->getScheme());
6562

6663
if ($matchedProxyUrl->getHost() !== null) {
6764
$this->router->getContext()->setHost($matchedProxyUrl->getHost());
6865
}
6966

70-
if ($this->router->getContext()->getScheme() == 'http') {
71-
$this->router->getContext()->setHttpPort('80');
72-
} elseif ($this->router->getContext()->getScheme() == 'https') {
73-
$this->router->getContext()->setHttpsPort('443');
67+
if ($matchedProxyUrl->getPort() !== null) {
68+
if ($this->router->getContext()->getScheme() == 'http') {
69+
$this->router->getContext()->setHttpPort($matchedProxyUrl->getPort());
70+
} elseif ($this->router->getContext()->getScheme() == 'https') {
71+
$this->router->getContext()->setHttpsPort($matchedProxyUrl->getPort());
72+
}
7473
}
7574
}
7675
}

src/ProxyUrl/ProxyUrl.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,43 @@ class ProxyUrl implements EntityInterface, IdentifiableInterface
4444
*/
4545
private $target;
4646

47+
/**
48+
* @var int
49+
*
50+
* @since Property available since Release 1.2.0
51+
*/
52+
private $port;
53+
4754
/**
4855
* @param int|string $id
4956
* @param string $target
5057
* @param string $path
5158
* @param string $host
5259
* @param string $scheme
60+
* @param int $port
5361
*/
54-
public function __construct($id, $target, $path, $host, $scheme)
62+
public function __construct($id, $target, $path, $host, $scheme, $port)
5563
{
5664
$this->id = $id;
5765
$this->target = $target;
5866
$this->path = rtrim($path, '/');
5967
$this->host = $host;
60-
$this->scheme = $scheme;
68+
69+
if ($scheme === null) {
70+
$this->scheme = 'http';
71+
} else {
72+
$this->scheme = $scheme;
73+
}
74+
75+
if ($port === null) {
76+
if ($this->scheme == 'http') {
77+
$this->port = 80;
78+
} elseif ($this->scheme == 'https') {
79+
$this->port = 443;
80+
}
81+
} else {
82+
$this->port = $port;
83+
}
6184
}
6285

6386
/**
@@ -103,4 +126,12 @@ public function getTarget()
103126
{
104127
return $this->target;
105128
}
129+
130+
/**
131+
* @return int
132+
*/
133+
public function getPort()
134+
{
135+
return $this->port;
136+
}
106137
}

src/ProxyUrl/ProxyUrlFactory.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class ProxyUrlFactory implements ServiceInterface
2525
*/
2626
public function create($proxyUrlId, $path, $proxyUrl)
2727
{
28-
list($proxyUrlPath, $proxyUrlHost, $proxyUrlScheme) = static::parseUrl($proxyUrl);
28+
list($proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort) = static::parseUrl($proxyUrl);
2929

30-
return new ProxyUrl($proxyUrlId, $path, $proxyUrlPath, $proxyUrlHost, $proxyUrlScheme);
30+
return new ProxyUrl($proxyUrlId, $path, $proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort);
3131
}
3232

3333
/**
@@ -46,13 +46,10 @@ public static function parseUrl($url)
4646
throw new \UnexpectedValueException(sprintf('The proxy URL "%s" is malformed.', $url));
4747
}
4848

49-
if (array_key_exists('port', $components)) {
50-
throw new \UnexpectedValueException(sprintf('The proxy URL "%s" cannot contain port number.', $url));
51-
}
52-
5349
$path = array_key_exists('path', $components) ? $components['path'] : null;
5450
$host = array_key_exists('host', $components) ? $components['host'] : null;
5551
$scheme = array_key_exists('scheme', $components) ? $components['scheme'] : null;
52+
$port = array_key_exists('port', $components) ? $components['port'] : null;
5653

5754
if (strpos($path, '//') === 0) {
5855
$endOfHostPosition = strpos($path, '/', 2);
@@ -67,6 +64,6 @@ public static function parseUrl($url)
6764
}
6865
}
6966

70-
return array($path, $host, $scheme);
67+
return array($path, $host, $scheme, $port);
7168
}
7269
}

tests/Functional/UrlRewritingInControllersTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public function rewriteUrlInGenerateUrlData()
7777
array('https://example.com/foo/bar/', UrlGeneratorInterface::ABSOLUTE_PATH, '/foo/bar/url-rewriting-in-controllers/'),
7878
array('https://example.com/foo/bar/', UrlGeneratorInterface::ABSOLUTE_URL, 'https://example.com/foo/bar/url-rewriting-in-controllers/'),
7979
array('https://example.com/foo/bar/', UrlGeneratorInterface::NETWORK_PATH, '//example.com/foo/bar/url-rewriting-in-controllers/'),
80+
array('http://example.com:8180/foo/bar/', UrlGeneratorInterface::ABSOLUTE_PATH, '/foo/bar/url-rewriting-in-controllers/'),
81+
array('http://example.com:8180/foo/bar/', UrlGeneratorInterface::ABSOLUTE_URL, 'http://example.com:8180/foo/bar/url-rewriting-in-controllers/'),
82+
array('http://example.com:8180/foo/bar/', UrlGeneratorInterface::NETWORK_PATH, '//example.com:8180/foo/bar/url-rewriting-in-controllers/'),
8083
);
8184
}
8285

tests/Functional/UrlRewritingInTemplatesTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function rewriteUrlInAssetData()
7575
array('http://example.com/foo/bar/', true, 'http://example.com/foo/bar/bundles/test/foo.png'),
7676
array('https://example.com/foo/bar/', false, '/foo/bar/bundles/test/foo.png'),
7777
array('https://example.com/foo/bar/', true, 'https://example.com/foo/bar/bundles/test/foo.png'),
78+
array('http://example.com:8180/foo/bar/', false, '/foo/bar/bundles/test/foo.png'),
79+
array('http://example.com:8180/foo/bar/', true, 'http://example.com:8180/foo/bar/bundles/test/foo.png'),
80+
array('https://example.com:8180/foo/bar/', false, '/foo/bar/bundles/test/foo.png'),
81+
array('https://example.com:8180/foo/bar/', true, 'https://example.com:8180/foo/bar/bundles/test/foo.png'),
7882
);
7983
}
8084

0 commit comments

Comments
 (0)