Skip to content

Commit

Permalink
Merge pull request #646 from joomlatools/feature/645-canonical
Browse files Browse the repository at this point in the history
Canonical url is not correct
  • Loading branch information
johanjanssens authored Apr 12, 2021
2 parents e6c747b + e5579af commit 4e63c14
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 43 deletions.
24 changes: 0 additions & 24 deletions code/site/components/com_pages/controller/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,6 @@ public function getModel()
return $this->_model;
}

protected function _actionRender(KControllerContextInterface $context)
{
if(!$context->response->isError())
{
if($route = $context->router->generate($this->getPage()))
{
$location = $context->router->qualify($route);

/**
* If Content-Location is included in a 2xx (Successful) response message and its value refers (after
* conversion to absolute form) to a URI that is the same as the effective request URI, then the recipient
* MAY consider the payload to be a current representation of that resource at the time indicated by the
* message origination date
*
* See: https://tools.ietf.org/html/rfc7231#section-3.1.4.2
*/
$context->response->headers->set('Content-Location', $location);
}
}

return parent::_actionRender($context);
}


protected function _actionBrowse(KControllerContextInterface $context)
{
$entity = $this->getModel()->fetch();
Expand Down
23 changes: 6 additions & 17 deletions code/site/components/com_pages/dispatcher/behavior/cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function _actionValidate(KDispatcherContextInterface $context)
$response->setDate(new DateTime('now'));
$response->headers->set('Age', null);
$response->headers->set('Cache-Status', self::CACHE_REVALIDATED, false);
$response->headers->set('Content-Location', (string) $this->getContentLocation());
//$response->headers->set('Content-Location', (string) $this->getContentLocation());
}
else $response->headers->set('Age', max(time() - $response->getDate()->format('U'), 0));

Expand Down Expand Up @@ -190,8 +190,8 @@ protected function _actionCache(KDispatcherContextInterface $context)
}

$data = array(
'id' => $this->getContentLocation()->toString(KHttpUrl::PATH + KHttpUrl::QUERY),
'url' => rtrim((string) $this->getContentLocation(), '/'),
'id' => $this->getCacheUrl()->toString(KHttpUrl::PATH + KHttpUrl::QUERY),
'url' => rtrim((string) $this->getCacheUrl(), '/'),
'validators' => $this->getCacheValidators(),
'status' => !$response->isNotModified() ? $response->getStatusCode() : '200',
'token' => $this->getCacheToken(),
Expand Down Expand Up @@ -341,19 +341,8 @@ public function getCacheToken()
return $token;
}

public function getContentLocation()
public function getCacheUrl()
{
/**
* Get content location
*
* If Content-Location is included in a 2xx (Successful) response message and its value refers (after
* conversion to absolute form) to a URI that is the same as the effective request URI, then the recipient
* MAY consider the payload to be a current representation of that resource at the time indicated by the
* message origination date. For a GET (Section 4.3.1) or HEAD (Section 4.3.2) request, this is the same
* as the default semantics when no Content-Location is provided by the server.
*
* See: https://tools.ietf.org/html/rfc7231#section-3.1.4.2
*/
if(!$location = $this->getResponse()->headers->get('Content-Location'))
{
$location = $this->getRequest()->getUrl()
Expand All @@ -366,7 +355,7 @@ public function getContentLocation()

public function locateCache($url = null)
{
$key = $url ?? $this->getContentLocation()->toString(KHttpUrl::PATH + KHttpUrl::QUERY);
$key = $url ?? $this->getCacheUrl()->toString(KHttpUrl::PATH + KHttpUrl::QUERY);
$file = $this->getConfig()->cache_path . '/response_' . crc32($key) . '.php';

return $file;
Expand Down Expand Up @@ -465,7 +454,7 @@ public function storeCache($data)
{
if($this->getConfig()->cache)
{
$url = (string) $this->getContentLocation();
$url = (string) $this->getCacheUrl();
$path = $this->getConfig()->cache_path;

if(!is_dir($path) && (false === @mkdir($path, 0777, true) && !is_dir($path))) {
Expand Down
16 changes: 16 additions & 0 deletions code/site/components/com_pages/dispatcher/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ protected function _beforeDispatch(KDispatcherContextInterface $context)
throw new KHttpExceptionNotAcceptable('Format not supported');
}
}

/**
* If Content-Location is included in a 2xx (Successful) response message and its value refers (after
* conversion to absolute form) to a URI that is the same as the effective request URI, then the recipient
* MAY consider the payload to be a current representation of that resource at the time indicated by the
* message origination date
*
* See: https://tools.ietf.org/html/rfc7231#section-3.1.4.2
*/
if($context->request->isSafe())
{
$route = $context->router->generate($route);
$location = $context->router->qualify($route);

$context->response->headers->set('Content-Location', (string) $location);
}
}

protected function _actionDispatch(KDispatcherContextInterface $context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function onAfterDispatcherCache(KEventInterface $event)
$response = $dispatcher->getResponse();

//Only cache static pages without a query string
if($event->result && !$dispatcher->getContentLocation()->getQuery() && $this->getConfig()->cache_path)
if($event->result && !$dispatcher->getCacheUrl()->getQuery() && $this->getConfig()->cache_path)
{
$file = $this->getFilePath($dispatcher);
$dir = dirname($file);
Expand Down Expand Up @@ -89,7 +89,7 @@ public function getCachePath()

public function getFilePath(ComPagesDispatcherHttp $dispatcher)
{
$path = $dispatcher->getContentLocation()->getPath(true);
$path = $dispatcher->getCacheUrl()->getPath(true);
$filename = array_pop($path);
$format = pathinfo($filename, PATHINFO_EXTENSION);

Expand Down

0 comments on commit 4e63c14

Please sign in to comment.