Skip to content

Commit c59cab1

Browse files
committed
add a feature that allows you to modify the hostname of the proxy URL at runtime
1 parent a14eef8 commit c59cab1

File tree

8 files changed

+197
-5
lines changed

8 files changed

+197
-5
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public function getConfigTreeBuilder()
5050
})
5151
->end()
5252
->end()
53+
->scalarNode('proxy_host_filter_service')
54+
->defaultNull()
55+
->end()
5356
->end()
5457
->end()
5558
->end()

src/DependencyInjection/PHPMentorsProxyURLRewriteExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private function transformConfigToContainer(array $config, ContainerBuilder $con
5151
if ($config['enabled']) {
5252
foreach ($config['proxy_urls'] as $id => $proxyUrl) {
5353
$definition = new DefinitionDecorator('phpmentors_proxy_url_rewrite.proxy_url');
54-
$definition->setArguments(array($id, $proxyUrl['path'], $proxyUrl['proxy_url']));
54+
$definition->setArguments(array($id, $proxyUrl['path'], $proxyUrl['proxy_url'], $proxyUrl['proxy_host_filter_service'] === null ? null : new Reference($proxyUrl['proxy_host_filter_service'])));
5555

5656
$serviceId = 'phpmentors_proxy_url_rewrite.proxy_url.'.sha1($id);
5757
$container->setDefinition($serviceId, $definition);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <[email protected]>,
4+
* All rights reserved.
5+
*
6+
* This file is part of PHPMentorsProxyURLRewriteBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\ProxyURLRewriteBundle\ProxyUrl;
14+
15+
use PHPMentors\DomainKata\Service\ServiceInterface;
16+
17+
/**
18+
* @since Interface available since Release 1.2.0
19+
*/
20+
interface ProxyHostFilterInterface extends ServiceInterface
21+
{
22+
/**
23+
* @param string
24+
*
25+
* @return string
26+
*/
27+
public function filter($host);
28+
}

src/ProxyUrl/ProxyUrlFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@
1717
class ProxyUrlFactory implements ServiceInterface
1818
{
1919
/**
20-
* @param int|string $proxyUrlId
21-
* @param string $path
22-
* @param string $proxyUrl
20+
* @param int|string $proxyUrlId
21+
* @param string $path
22+
* @param string $proxyUrl
23+
* @param ProxyHostFilterInterface $hostFilterService
2324
*
2425
* @return ProxyUrl
2526
*/
26-
public function create($proxyUrlId, $path, $proxyUrl)
27+
public function create($proxyUrlId, $path, $proxyUrl, ProxyHostFilterInterface $proxyHostFilter = null)
2728
{
2829
list($proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort) = static::parseUrl($proxyUrl);
2930

31+
if ($proxyHostFilter !== null) {
32+
$proxyUrlHost = $proxyHostFilter->filter($proxyUrlHost);
33+
}
34+
3035
return new ProxyUrl($proxyUrlId, $path, $proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort);
3136
}
3237

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <[email protected]>,
4+
* All rights reserved.
5+
*
6+
* This file is part of PHPMentorsProxyURLRewriteBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\ProxyURLRewriteBundle\Functional\Bundle\TestBundle\ProxyUrl;
14+
15+
use PHPMentors\ProxyURLRewriteBundle\ProxyUrl\ProxyHostFilterInterface;
16+
17+
/**
18+
* @since Class available since Release 1.2.0
19+
*/
20+
class ProxyHostFilter implements ProxyHostFilterInterface
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function filter($host)
26+
{
27+
if ($host === null) {
28+
return null;
29+
}
30+
31+
return 'baz.'.$host;
32+
}
33+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <[email protected]>,
4+
* All rights reserved.
5+
*
6+
* This file is part of PHPMentorsProxyURLRewriteBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\ProxyURLRewriteBundle\Functional;
14+
15+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
16+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\Filesystem\Filesystem;
19+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20+
21+
/**
22+
* @since Class available since Release 1.2.0
23+
*/
24+
class HostFilterTest extends WebTestCase
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function setUp()
30+
{
31+
parent::setUp();
32+
33+
$_SERVER['KERNEL_DIR'] = __DIR__.'/app';
34+
$_SERVER['SYMFONY__SECRET'] = hash('sha1', uniqid(mt_rand()));
35+
36+
$this->removeCacheDir();
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function tearDown()
43+
{
44+
parent::tearDown();
45+
46+
$this->removeCacheDir();
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected static function createKernel(array $options = array())
53+
{
54+
$kernel = KernelTestCase::createKernel($options);
55+
if (array_key_exists('config', $options)) {
56+
$kernel->setConfig($options['config']);
57+
}
58+
59+
return $kernel;
60+
}
61+
62+
protected function removeCacheDir()
63+
{
64+
$fileSystem = new Filesystem();
65+
$fileSystem->remove($_SERVER['KERNEL_DIR'].'/cache/test');
66+
}
67+
68+
public function filterData()
69+
{
70+
return array(
71+
array('/foo/bar/', true, 'http://backend1.example.com/foo/bar/url-rewriting-in-controllers/'),
72+
array('/foo/bar/', false, 'http://backend1.example.com/foo/bar/url-rewriting-in-controllers/'),
73+
array('//example.com/foo/bar/', true, 'http://baz.example.com/foo/bar/url-rewriting-in-controllers/'),
74+
array('//example.com/foo/bar/', false, 'http://example.com/foo/bar/url-rewriting-in-controllers/'),
75+
array('http://example.com/foo/bar/', true, 'http://baz.example.com/foo/bar/url-rewriting-in-controllers/'),
76+
array('http://example.com/foo/bar/', false, 'http://example.com/foo/bar/url-rewriting-in-controllers/'),
77+
array('https://example.com/foo/bar/', true, 'https://baz.example.com/foo/bar/url-rewriting-in-controllers/'),
78+
array('https://example.com/foo/bar/', false, 'https://example.com/foo/bar/url-rewriting-in-controllers/'),
79+
array('http://example.com:8180/foo/bar/', true, 'http://baz.example.com:8180/foo/bar/url-rewriting-in-controllers/'),
80+
array('http://example.com:8180/foo/bar/', false, 'http://example.com:8180/foo/bar/url-rewriting-in-controllers/'),
81+
);
82+
}
83+
84+
/**
85+
* @test
86+
* @dataProvider filterData
87+
*
88+
* @param string $proxyUrl
89+
* @param bool $proxyHostFilterService
90+
* @param string $rewroteUrl
91+
*/
92+
public function filter($proxyUrl, $proxyHostFilterService, $rewroteUrl)
93+
{
94+
$client = $this->createClient(array('config' => function (ContainerBuilder $container) use ($proxyUrl, $proxyHostFilterService) {
95+
$config = array(
96+
'path' => '!^.*!',
97+
'proxy_url' => $proxyUrl,
98+
);
99+
if ($proxyHostFilterService) {
100+
$config['proxy_host_filter_service'] = 'phpmentors_proxy_url_rewrite_test.proxy_host_filter';
101+
}
102+
103+
$container->loadFromExtension('phpmentors_proxy_url_rewrite', array(
104+
'proxy_urls' => array(
105+
'foo' => $config,
106+
), ));
107+
}));
108+
109+
$client->request('GET', sprintf('http://backend1.example.com:8080/url-rewriting-in-controllers/?referenceType=%s', UrlGeneratorInterface::ABSOLUTE_URL));
110+
111+
$this->assertThat($client->getResponse()->getStatusCode(), $this->equalTo(200), $client->getResponse()->getContent());
112+
$this->assertThat($client->getCrawler()->filterXpath("//*[@id='generateUrl']")->text(), $this->equalTo($rewroteUrl));
113+
}
114+
}

tests/Functional/app/config/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
imports:
2+
- { resource: services.yml }
3+
14
framework:
25
secret: "%secret%"
36
router:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
phpmentors_proxy_url_rewrite_test.proxy_host_filter.class: "PHPMentors\\ProxyURLRewriteBundle\\Functional\\Bundle\\TestBundle\\ProxyUrl\\ProxyHostFilter"
3+
4+
services:
5+
phpmentors_proxy_url_rewrite_test.proxy_host_filter:
6+
class: "%phpmentors_proxy_url_rewrite_test.proxy_host_filter.class%"

0 commit comments

Comments
 (0)