From 3cde840e2babc17b3ddf6d9afe4880465da47873 Mon Sep 17 00:00:00 2001 From: Luc Didry Date: Fri, 12 Jun 2015 23:03:42 +0200 Subject: [PATCH] Canonicalize URLs to be W3C compliant The W3C feed validator (https://validator.w3.org/feed/) says that http://cachet.example.org is not a valid value for a link, but that http://cachet.example.org/ is. The canonicalizeUrl method add a trailing slash if needed. --- app/Http/Controllers/AtomController.php | 14 ++++++++++++-- app/Http/Controllers/RssController.php | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/AtomController.php b/app/Http/Controllers/AtomController.php index e6423cdd..32c03458 100644 --- a/app/Http/Controllers/AtomController.php +++ b/app/Http/Controllers/AtomController.php @@ -30,7 +30,7 @@ class AtomController extends AbstractController $feed = Feed::make(); $feed->title = Setting::get('app_name'); $feed->description = trans('cachet.feed'); - $feed->link = Setting::get('app_domain'); + $feed->link = $this->canonicalizeUrl(Setting::get('app_domain')); $feed->setDateFormat('datetime'); @@ -60,9 +60,19 @@ class AtomController extends AbstractController $feed->add( $incident->name, Setting::get('app_name'), - Setting::get('app_domain'), + $this->canonicalizeUrl(Setting::get('app_domain')), $incident->created_at->toAtomString(), $incident->message ); } + + /** + * Add a / at the end of an URL to in order to be W3C compliant + * + * @param string $url + */ + private function canonicalizeUrl($url) + { + return preg_replace('/([^\/])$/', '$1/', $url); + } } diff --git a/app/Http/Controllers/RssController.php b/app/Http/Controllers/RssController.php index e2a91e1b..f38fecbc 100644 --- a/app/Http/Controllers/RssController.php +++ b/app/Http/Controllers/RssController.php @@ -30,7 +30,7 @@ class RssController extends AbstractController $feed = Feed::make(); $feed->title = Setting::get('app_name'); $feed->description = trans('cachet.feed'); - $feed->link = Setting::get('app_domain'); + $feed->link = $this->canonicalizeUrl(Setting::get('app_domain')); $feed->setDateFormat('datetime'); @@ -60,9 +60,19 @@ class RssController extends AbstractController $feed->add( $incident->name, Setting::get('app_name'), - Setting::get('app_domain'), + $this->canonicalizeUrl(Setting::get('app_domain')), $incident->created_at->toRssString(), $incident->message ); } + + /** + * Add a / at the end of an URL to in order to be W3C compliant + * + * @param string $url + */ + private function canonicalizeUrl($url) + { + return preg_replace('/([^\/])$/', '$1/', $url); + } }