From b59f4e28056ee7ca97282ab1e0ae42bbcc5fbf4d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 2 Nov 2019 11:30:09 +0100 Subject: [PATCH] Defined new configs for not found redirects --- CHANGELOG.md | 2 ++ config/autoload/redirects.global.php | 13 ++++++++ config/autoload/url-shortener.global.php | 4 --- config/config.php | 1 + docker/README.md | 13 ++++++-- docker/config/shlink_in_docker.local.php | 14 ++++---- .../src/Config/DeprecatedConfigParser.php | 29 +++++++++++++++++ .../src/Config/SimplifiedConfigParser.php | 32 +++++++++++++++---- .../Config/SimplifiedConfigParserTest.php | 32 ++++++++++++++++--- 9 files changed, 115 insertions(+), 25 deletions(-) create mode 100644 config/autoload/redirects.global.php create mode 100644 module/Core/src/Config/DeprecatedConfigParser.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9800cfb0..b0695906 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this Generated short codes have 5 characters, and shlink makes sure they keep unique, while making it backwards-compatible. +* [#497](https://github.com/shlinkio/shlink/issues/497) Officially added support for MariaDB. + #### Changed * [#458](https://github.com/shlinkio/shlink/issues/458) Updated coding styles to use [shlinkio/php-coding-standard](https://github.com/shlinkio/php-coding-standard) v2.0.0. diff --git a/config/autoload/redirects.global.php b/config/autoload/redirects.global.php new file mode 100644 index 00000000..14aa684a --- /dev/null +++ b/config/autoload/redirects.global.php @@ -0,0 +1,13 @@ + [ + 'invalid_short_url' => null, // Formerly url_shortener.not_found_short_url.redirect_to + '404' => null, + 'base_url' => null, + ], + +]; diff --git a/config/autoload/url-shortener.global.php b/config/autoload/url-shortener.global.php index 3d3689ea..7c7e19de 100644 --- a/config/autoload/url-shortener.global.php +++ b/config/autoload/url-shortener.global.php @@ -12,10 +12,6 @@ return [ 'hostname' => env('SHORTENED_URL_HOSTNAME'), ], 'validate_url' => true, - 'not_found_short_url' => [ - 'enable_redirection' => false, - 'redirect_to' => null, - ], ], ]; diff --git a/config/config.php b/config/config.php index ed04d037..352a27ca 100644 --- a/config/config.php +++ b/config/config.php @@ -31,4 +31,5 @@ return (new ConfigAggregator\ConfigAggregator([ ], 'data/cache/app_config.php', [ Core\Config\SimplifiedConfigParser::class, Core\Config\BasePathPrefixer::class, + Core\Config\DeprecatedConfigParser::class, ]))->getMergedConfig(); diff --git a/docker/README.md b/docker/README.md index eca69d36..3855a524 100644 --- a/docker/README.md +++ b/docker/README.md @@ -101,7 +101,9 @@ This is the complete list of supported env vars: * `DISABLE_TRACK_PARAM`: The name of a query param that can be used to visit short URLs avoiding the visit to be tracked. This feature won't be available if not value is provided. * `DELETE_SHORT_URL_THRESHOLD`: The amount of visits on short URLs which will not allow them to be deleted. Defaults to `15`. * `VALIDATE_URLS`: Boolean which tells if shlink should validate a status 20x (after following redirects) is returned when trying to shorten a URL. Defaults to `true`. -* `NOT_FOUND_REDIRECT_TO`: If a URL is provided here, when a user tries to access an invalid short URL, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page. +* `INVALID_SHORT_URL_REDIRECT_TO`: If a URL is provided here, when a user tries to access an invalid short URL, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page. +* `404_REDIRECT_TO`: If a URL is provided here, when a user tries to access a URL not matching any one supported by the router, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page. +* `BASE_URL_REDIRECT_TO`: If a URL is provided here, when a user tries to access Shlink's base URL, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page. * `BASE_PATH`: The base path from which you plan to serve shlink, in case you don't want to serve it from the root of the domain. Defaults to `''`. * `REDIS_SERVERS`: A comma-separated list of redis servers where Shlink locks are stored (locks are used to prevent some operations to be run more than once in parallel). @@ -111,6 +113,7 @@ This is the complete list of supported env vars: In the future, these redis servers could be used for other caching operations performed by shlink. +* `NOT_FOUND_REDIRECT_TO`: **Deprecated since v1.20 in favor of `INVALID_SHORT_URL_REDIRECT_TO`** If a URL is provided here, when a user tries to access an invalid short URL, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page. * `SHORTCODE_CHARS`: **Ignored when using Shlink 1.20 or newer**. A charset to use when building short codes. Only needed when using more than one shlink instance ([Multi instance considerations](#multi-instance-considerations)). An example using all env vars could look like this: @@ -151,7 +154,9 @@ The whole configuration should have this format, but it can be split into multip "short_domain_schema": "https", "short_domain_host": "doma.in", "validate_url": false, - "not_found_redirect_to": "https://my-landing-page.com", + "invalid_short_url_redirect_to": "https://my-landing-page.com", + "404_redirect_to": "https://my-landing-page.com", + "base_url_redirect_to": "https://my-landing-page.com", "redis_servers": [ "tcp://172.20.0.1:6379", "tcp://172.20.0.2:6379" @@ -163,11 +168,13 @@ The whole configuration should have this format, but it can be split into multip "password": "123abc", "host": "something.rds.amazonaws.com", "port": "3306" - } + }, + "not_found_redirect_to": "https://my-landing-page.com" } ``` > This is internally parsed to how shlink expects the config. If you are using a version previous to 1.17.0, this parser is not present and you need to provide a config structure like the one [documented previously](https://github.com/shlinkio/shlink-docker-image/tree/v1.16.3#provide-config-via-volumes). +> The `not_found_redirect_to` option has been deprecated when `404_redirect_to` and `base_url_redirect_to` have been introduced. Use `invalid_short_url_redirect_to` instead (however, it will still work for backwards compatibility). Once created just run shlink with the volume: diff --git a/docker/config/shlink_in_docker.local.php b/docker/config/shlink_in_docker.local.php index 5477e892..a90d775b 100644 --- a/docker/config/shlink_in_docker.local.php +++ b/docker/config/shlink_in_docker.local.php @@ -77,7 +77,7 @@ $helper = new class { } $driverOptions = ! contains(['maria', 'mysql'], $driver) ? [] : [ - // PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', + // 1002 -> PDO::MYSQL_ATTR_INIT_COMMAND 1002 => 'SET NAMES utf8', ]; return [ @@ -91,13 +91,12 @@ $helper = new class { ]; } - public function getNotFoundConfig(): array + public function getNotFoundRedirectsConfig(): array { - $notFoundRedirectTo = env('NOT_FOUND_REDIRECT_TO'); - return [ - 'enable_redirection' => $notFoundRedirectTo !== null, - 'redirect_to' => $notFoundRedirectTo, + 'invalid_short_url' => env('INVALID_SHORT_URL_REDIRECT_TO', env('NOT_FOUND_REDIRECT_TO')), + '404' => env('404_REDIRECT_TO'), + 'base_url' => env('BASE_URL_REDIRECT_TO'), ]; } }; @@ -126,9 +125,10 @@ return [ 'hostname' => env('SHORT_DOMAIN_HOST', ''), ], 'validate_url' => (bool) env('VALIDATE_URLS', true), - 'not_found_short_url' => $helper->getNotFoundConfig(), ], + 'not_found_redirects' => $helper->getNotFoundRedirectsConfig(), + 'logger' => [ 'handlers' => [ 'shlink_rotating_handler' => [ diff --git a/module/Core/src/Config/DeprecatedConfigParser.php b/module/Core/src/Config/DeprecatedConfigParser.php new file mode 100644 index 00000000..faa56c38 --- /dev/null +++ b/module/Core/src/Config/DeprecatedConfigParser.php @@ -0,0 +1,29 @@ + ['url_shortener', 'domain', 'schema'], 'short_domain_host' => ['url_shortener', 'domain', 'hostname'], 'validate_url' => ['url_shortener', 'validate_url'], - 'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'redirect_to'], + 'not_found_redirect_to' => ['not_found_redirects', 'invalid_short_url'], // Deprecated + 'invalid_short_url_redirect_to' => ['not_found_redirects', 'invalid_short_url'], + '404_redirect_to' => ['not_found_redirects', '404'], + 'base_url_redirect_to' => ['not_found_redirects', 'base_path'], 'db_config' => ['entity_manager', 'connection'], 'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'], 'redis_servers' => ['redis', 'servers'], 'base_path' => ['router', 'base_path'], ]; private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [ - 'not_found_redirect_to' => [ - 'path' => ['url_shortener', 'not_found_short_url', 'enable_redirection'], - 'value' => true, - ], 'delete_short_url_threshold' => [ 'path' => ['delete_short_urls', 'check_visits_threshold'], 'value' => true, @@ -43,9 +45,9 @@ class SimplifiedConfigParser public function __invoke(array $config): array { - $existingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING); + $configForExistingKeys = $this->getConfigForKeysInMappingOrderedByMapping($config); - return reduce_left($existingKeys, function ($value, string $key, $c, PathCollection $collection) { + return reduce_left($configForExistingKeys, function ($value, string $key, $c, PathCollection $collection) { $path = self::SIMPLIFIED_CONFIG_MAPPING[$key]; if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) { $value = ArrayUtils::merge($collection->getValueInPath($path), $value); @@ -60,4 +62,20 @@ class SimplifiedConfigParser return $collection; }, new PathCollection($config))->toArray(); } + + private function getConfigForKeysInMappingOrderedByMapping(array $config): array + { + // Ignore any config which is not defined in the mapping + $configForExistingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING); + + // Order the config by their key, based on the order it was defined in the mapping. + // This mainly allows deprecating keys and defining new ones that will replace the older and always take + // preference, while the old one keeps working for backwards compatibility if the new one is not provided. + $simplifiedConfigOrder = array_flip(array_keys(self::SIMPLIFIED_CONFIG_MAPPING)); + uksort($configForExistingKeys, function (string $a, string $b) use ($simplifiedConfigOrder): int { + return $simplifiedConfigOrder[$a] - $simplifiedConfigOrder[$b]; + }); + + return $configForExistingKeys; + } } diff --git a/module/Core/test/Config/SimplifiedConfigParserTest.php b/module/Core/test/Config/SimplifiedConfigParserTest.php index c2b95fd5..f58cb5f5 100644 --- a/module/Core/test/Config/SimplifiedConfigParserTest.php +++ b/module/Core/test/Config/SimplifiedConfigParserTest.php @@ -75,10 +75,6 @@ class SimplifiedConfigParserTest extends TestCase 'hostname' => 'doma.in', ], 'validate_url' => false, - 'not_found_short_url' => [ - 'redirect_to' => 'foobar.com', - 'enable_redirection' => true, - ], ], 'delete_short_urls' => [ @@ -102,10 +98,38 @@ class SimplifiedConfigParserTest extends TestCase 'router' => [ 'base_path' => '/foo/bar', ], + + 'not_found_redirects' => [ + 'invalid_short_url' => 'foobar.com', + ], ]; $result = ($this->postProcessor)(array_merge($config, $simplified)); $this->assertEquals(array_merge($expected, $simplified), $result); } + + /** + * @test + * @dataProvider provideConfigWithDeprecates + */ + public function properlyMapsDeprecatedConfigs(array $config, string $expected): void + { + $result = ($this->postProcessor)($config); + $this->assertEquals($expected, $result['not_found_redirects']['invalid_short_url']); + } + + public function provideConfigWithDeprecates(): iterable + { + yield 'only deprecated config' => [['not_found_redirect_to' => 'old_value'], 'old_value']; + yield 'only new config' => [['invalid_short_url_redirect_to' => 'new_value'], 'new_value']; + yield 'both configs, new first' => [ + ['invalid_short_url_redirect_to' => 'new_value', 'not_found_redirect_to' => 'old_value'], + 'new_value', + ]; + yield 'both configs, deprecated first' => [ + ['not_found_redirect_to' => 'old_value', 'invalid_short_url_redirect_to' => 'new_value'], + 'new_value', + ]; + } }