summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2017-06-18 22:21:40 -0500
committerGitHub <noreply@github.com>2017-06-18 22:21:40 -0500
commit381850bd07f2378a0be2732ca37136932a5ceb1d (patch)
tree97cbeab5718b757d52552cbf181fc0b46d02f022
parent76d10bc46bc9fce92fb648c2e6f5974e297c21e1 (diff)
parent41621d3b5992c15fe7b131c85df9d5c1bacf5c04 (diff)
downloadnextcloud-server-381850bd07f2378a0be2732ca37136932a5ceb1d.tar.gz
nextcloud-server-381850bd07f2378a0be2732ca37136932a5ceb1d.zip
Merge pull request #5429 from nextcloud/cache-baseurl
Use base url for cache prefix and SCSS caching
-rw-r--r--lib/private/Server.php22
-rw-r--r--lib/private/Template/SCSSCacher.php15
-rw-r--r--lib/private/URLGenerator.php28
-rw-r--r--lib/public/IURLGenerator.php6
-rw-r--r--tests/lib/Template/SCSSCacherTest.php51
-rw-r--r--tests/lib/UrlGeneratorTest.php77
6 files changed, 133 insertions, 66 deletions
diff --git a/lib/private/Server.php b/lib/private/Server.php
index 75e9d911632..489683aa127 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -414,9 +414,11 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerService(\OCP\IURLGenerator::class, function (Server $c) {
$config = $c->getConfig();
$cacheFactory = $c->getMemCacheFactory();
+ $request = $c->getRequest();
return new \OC\URLGenerator(
$config,
- $cacheFactory
+ $cacheFactory,
+ $request
);
});
$this->registerAlias('URLGenerator', \OCP\IURLGenerator::class);
@@ -433,27 +435,31 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias('UserCache', \OCP\ICache::class);
$this->registerService(Factory::class, function (Server $c) {
+
+ $arrayCacheFactory = new \OC\Memcache\Factory('', $c->getLogger(),
+ '\\OC\\Memcache\\ArrayCache',
+ '\\OC\\Memcache\\ArrayCache',
+ '\\OC\\Memcache\\ArrayCache'
+ );
$config = $c->getConfig();
+ $request = $c->getRequest();
+ $urlGenerator = new URLGenerator($config, $arrayCacheFactory, $request);
if ($config->getSystemValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
$v = \OC_App::getAppVersions();
- $v['core'] = md5(file_get_contents(\OC::$SERVERROOT . '/version.php'));
+ $v['core'] = implode(',', \OC_Util::getVersion());
$version = implode(',', $v);
$instanceId = \OC_Util::getInstanceId();
$path = \OC::$SERVERROOT;
- $prefix = md5($instanceId . '-' . $version . '-' . $path . '-' . \OC::$WEBROOT);
+ $prefix = md5($instanceId . '-' . $version . '-' . $path . '-' . $urlGenerator->getBaseUrl());
return new \OC\Memcache\Factory($prefix, $c->getLogger(),
$config->getSystemValue('memcache.local', null),
$config->getSystemValue('memcache.distributed', null),
$config->getSystemValue('memcache.locking', null)
);
}
+ return $arrayCacheFactory;
- return new \OC\Memcache\Factory('', $c->getLogger(),
- '\\OC\\Memcache\\ArrayCache',
- '\\OC\\Memcache\\ArrayCache',
- '\\OC\\Memcache\\ArrayCache'
- );
});
$this->registerAlias('MemCacheFactory', Factory::class);
$this->registerAlias(ICacheFactory::class, Factory::class);
diff --git a/lib/private/Template/SCSSCacher.php b/lib/private/Template/SCSSCacher.php
index 6b1c594bd2e..fe5a095c773 100644
--- a/lib/private/Template/SCSSCacher.php
+++ b/lib/private/Template/SCSSCacher.php
@@ -92,7 +92,7 @@ class SCSSCacher {
$path = explode('/', $root . '/' . $file);
$fileNameSCSS = array_pop($path);
- $fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
+ $fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS));
$path = implode('/', $path);
@@ -119,7 +119,7 @@ class SCSSCacher {
*/
public function getCachedCSS($appName, $fileName) {
$folder = $this->appData->getFolder($appName);
- return $folder->getFile($fileName);
+ return $folder->getFile($this->prependBaseurlPrefix($fileName));
}
/**
@@ -292,8 +292,17 @@ class SCSSCacher {
public function getCachedSCSS($appName, $fileName) {
$tmpfileLoc = explode('/', $fileName);
$fileName = array_pop($tmpfileLoc);
- $fileName = str_replace('.scss', '.css', $fileName);
+ $fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName));
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
}
+
+ /**
+ * Prepend hashed base url to the css file
+ * @param $cssFile
+ * @return string
+ */
+ private function prependBaseurlPrefix($cssFile) {
+ return md5($this->urlGenerator->getBaseUrl()) . '-' . $cssFile;
+ }
}
diff --git a/lib/private/URLGenerator.php b/lib/private/URLGenerator.php
index 2387deb6100..073d40b0de8 100644
--- a/lib/private/URLGenerator.php
+++ b/lib/private/URLGenerator.php
@@ -35,7 +35,9 @@ namespace OC;
use OCP\ICacheFactory;
use OCP\IConfig;
+use OCP\IRequest;
use OCP\IURLGenerator;
+use OCP\Route\IRoute;
use RuntimeException;
/**
@@ -46,15 +48,20 @@ class URLGenerator implements IURLGenerator {
private $config;
/** @var ICacheFactory */
private $cacheFactory;
+ /** @var IRequest */
+ private $request;
/**
* @param IConfig $config
* @param ICacheFactory $cacheFactory
+ * @param IRequest $request
*/
public function __construct(IConfig $config,
- ICacheFactory $cacheFactory) {
+ ICacheFactory $cacheFactory,
+ IRequest $request) {
$this->config = $config;
$this->cacheFactory = $cacheFactory;
+ $this->request = $request;
}
/**
@@ -142,7 +149,7 @@ class URLGenerator implements IURLGenerator {
* Returns the path to the image.
*/
public function imagePath($app, $image) {
- $cache = $this->cacheFactory->create('imagePath');
+ $cache = $this->cacheFactory->create('imagePath-'.md5($this->getBaseUrl()).'-');
$cacheKey = $app.'-'.$image;
if($key = $cache->get($cacheKey)) {
return $key;
@@ -223,14 +230,12 @@ class URLGenerator implements IURLGenerator {
if (\OC::$CLI && !defined('PHPUNIT_RUN')) {
return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
}
-
// The ownCloud web root can already be prepended.
- $webRoot = substr($url, 0, strlen(\OC::$WEBROOT)) === \OC::$WEBROOT
- ? ''
- : \OC::$WEBROOT;
+ if(substr($url, 0, strlen(\OC::$WEBROOT)) === \OC::$WEBROOT) {
+ $url = substr($url, strlen(\OC::$WEBROOT));
+ }
- $request = \OC::$server->getRequest();
- return $request->getServerProtocol() . '://' . $request->getServerHost() . $webRoot . $separator . $url;
+ return $this->getBaseUrl() . $separator . $url;
}
/**
@@ -241,4 +246,11 @@ class URLGenerator implements IURLGenerator {
$theme = \OC::$server->getThemingDefaults();
return $theme->buildDocLinkToKey($key);
}
+
+ /**
+ * @return string base url of the current request
+ */
+ public function getBaseUrl() {
+ return $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
+ }
}
diff --git a/lib/public/IURLGenerator.php b/lib/public/IURLGenerator.php
index e309336c0f0..3869d2f86f8 100644
--- a/lib/public/IURLGenerator.php
+++ b/lib/public/IURLGenerator.php
@@ -91,4 +91,10 @@ interface IURLGenerator {
* @since 8.0.0
*/
public function linkToDocs($key);
+
+ /**
+ * @return string base url of the current request
+ * @since 13.0.0
+ */
+ public function getBaseUrl();
}
diff --git a/tests/lib/Template/SCSSCacherTest.php b/tests/lib/Template/SCSSCacherTest.php
index fb7c6c5e034..345972bb1af 100644
--- a/tests/lib/Template/SCSSCacherTest.php
+++ b/tests/lib/Template/SCSSCacherTest.php
@@ -72,6 +72,10 @@ class SCSSCacherTest extends \Test\TestCase {
$this->depsCache
);
$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
+
+ $this->urlGenerator->expects($this->any())
+ ->method('getBaseUrl')
+ ->willReturn('http://localhost/nextcloud');
}
public function testProcessUncachedFileNoAppDataFolder() {
@@ -84,14 +88,15 @@ class SCSSCacherTest extends \Test\TestCase {
$fileDeps = $this->createMock(ISimpleFile::class);
$gzfile = $this->createMock(ISimpleFile::class);
+ $filePrefix = md5('http://localhost/nextcloud') . '-';
$folder->method('getFile')
- ->will($this->returnCallback(function($path) use ($file, $gzfile) {
- if ($path === 'styles.css') {
+ ->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
+ if ($path === $filePrefix.'styles.css') {
return $file;
- } else if ($path === 'styles.css.deps') {
+ } else if ($path === $filePrefix.'styles.css.deps') {
throw new NotFoundException();
- } else if ($path === 'styles.css.gzip') {
+ } else if ($path === $filePrefix.'styles.css.gzip') {
return $gzfile;
} else {
$this->fail();
@@ -99,9 +104,13 @@ class SCSSCacherTest extends \Test\TestCase {
}));
$folder->expects($this->once())
->method('newFile')
- ->with('styles.css.deps')
+ ->with($filePrefix.'styles.css.deps')
->willReturn($fileDeps);
+ $this->urlGenerator->expects($this->once())
+ ->method('getBaseUrl')
+ ->willReturn('http://localhost/nextcloud');
+
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
$this->assertTrue($actual);
}
@@ -113,14 +122,15 @@ class SCSSCacherTest extends \Test\TestCase {
$file->expects($this->any())->method('getSize')->willReturn(1);
$fileDeps = $this->createMock(ISimpleFile::class);
$gzfile = $this->createMock(ISimpleFile::class);
+ $filePrefix = md5('http://localhost/nextcloud') . '-';
$folder->method('getFile')
- ->will($this->returnCallback(function($path) use ($file, $gzfile) {
- if ($path === 'styles.css') {
+ ->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
+ if ($path === $filePrefix.'styles.css') {
return $file;
- } else if ($path === 'styles.css.deps') {
+ } else if ($path === $filePrefix.'styles.css.deps') {
throw new NotFoundException();
- } else if ($path === 'styles.css.gzip') {
+ } else if ($path === $filePrefix.'styles.css.gzip') {
return $gzfile;
}else {
$this->fail();
@@ -128,7 +138,7 @@ class SCSSCacherTest extends \Test\TestCase {
}));
$folder->expects($this->once())
->method('newFile')
- ->with('styles.css.deps')
+ ->with($filePrefix.'styles.css.deps')
->willReturn($fileDeps);
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
@@ -142,14 +152,15 @@ class SCSSCacherTest extends \Test\TestCase {
$fileDeps = $this->createMock(ISimpleFile::class);
$fileDeps->expects($this->any())->method('getSize')->willReturn(1);
$gzFile = $this->createMock(ISimpleFile::class);
+ $filePrefix = md5('http://localhost/nextcloud') . '-';
$folder->method('getFile')
- ->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile) {
- if ($name === 'styles.css') {
+ ->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
+ if ($name === $filePrefix.'styles.css') {
return $file;
- } else if ($name === 'styles.css.deps') {
+ } else if ($name === $filePrefix.'styles.css.deps') {
return $fileDeps;
- } else if ($name === 'styles.css.gzip') {
+ } else if ($name === $filePrefix.'styles.css.gzip') {
return $gzFile;
}
$this->fail();
@@ -174,14 +185,14 @@ class SCSSCacherTest extends \Test\TestCase {
$fileDeps->expects($this->any())->method('getSize')->willReturn(1);
$gzFile = $this->createMock(ISimpleFile::class);
-
+ $filePrefix = md5('http://localhost/nextcloud') . '-';
$folder->method('getFile')
- ->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile) {
- if ($name === 'styles.css') {
+ ->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
+ if ($name === $filePrefix.'styles.css') {
return $file;
- } else if ($name === 'styles.css.deps') {
+ } else if ($name === $filePrefix.'styles.css.deps') {
return $fileDeps;
- } else if ($name === 'styles.css.gzip') {
+ } else if ($name === $filePrefix.'styles.css.gzip') {
return $gzFile;
}
$this->fail();
@@ -374,7 +385,7 @@ class SCSSCacherTest extends \Test\TestCase {
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
->with('core.Css.getCss', [
- 'fileName' => 'styles.css',
+ 'fileName' => md5('http://localhost/nextcloud') . '-styles.css',
'appName' => $appName
])
->willReturn(\OC::$WEBROOT . $result);
diff --git a/tests/lib/UrlGeneratorTest.php b/tests/lib/UrlGeneratorTest.php
index 28fd2d336d2..69067f51e08 100644
--- a/tests/lib/UrlGeneratorTest.php
+++ b/tests/lib/UrlGeneratorTest.php
@@ -9,6 +9,8 @@
namespace Test;
use OCP\ICacheFactory;
use OCP\IConfig;
+use OCP\IRequest;
+use OCP\IURLGenerator;
/**
* Class UrlGeneratorTest
@@ -17,6 +19,37 @@ use OCP\IConfig;
*/
class UrlGeneratorTest extends \Test\TestCase {
+ /** @var \PHPUnit_Framework_MockObject_MockObject|IConfig */
+ private $config;
+ /** @var \PHPUnit_Framework_MockObject_MockObject|ICacheFactory */
+ private $cacheFactory;
+ /** @var \PHPUnit_Framework_MockObject_MockObject|IRequest */
+ private $request;
+ /** @var IURLGenerator */
+ private $urlGenerator;
+
+ public function setUp() {
+ parent::setUp();
+ $this->config = $this->createMock(IConfig::class);
+ $this->cacheFactory = $this->createMock(ICacheFactory::class);
+ $this->request = $this->createMock(IRequest::class);
+ $this->urlGenerator = new \OC\URLGenerator(
+ $this->config,
+ $this->cacheFactory,
+ $this->request
+ );
+ }
+
+ private function mockBaseUrl() {
+ $this->request->expects($this->once())
+ ->method('getServerProtocol')
+ ->willReturn('http');
+ $this->request->expects($this->once())
+ ->method('getServerHost')
+ ->willReturn('localhost');
+
+ }
+
/**
* @small
* test linkTo URL construction
@@ -24,11 +57,7 @@ class UrlGeneratorTest extends \Test\TestCase {
*/
public function testLinkToDocRoot($app, $file, $args, $expectedResult) {
\OC::$WEBROOT = '';
- $config = $this->createMock(IConfig::class);
- $cacheFactory = $this->createMock(ICacheFactory::class);
- $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
- $result = $urlGenerator->linkTo($app, $file, $args);
-
+ $result = $this->urlGenerator->linkTo($app, $file, $args);
$this->assertEquals($expectedResult, $result);
}
@@ -39,11 +68,7 @@ class UrlGeneratorTest extends \Test\TestCase {
*/
public function testLinkToSubDir($app, $file, $args, $expectedResult) {
\OC::$WEBROOT = '/owncloud';
- $config = $this->createMock(IConfig::class);
- $cacheFactory = $this->createMock(ICacheFactory::class);
- $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
- $result = $urlGenerator->linkTo($app, $file, $args);
-
+ $result = $this->urlGenerator->linkTo($app, $file, $args);
$this->assertEquals($expectedResult, $result);
}
@@ -51,13 +76,10 @@ class UrlGeneratorTest extends \Test\TestCase {
* @dataProvider provideRoutes
*/
public function testLinkToRouteAbsolute($route, $expected) {
+ $this->mockBaseUrl();
\OC::$WEBROOT = '/owncloud';
- $config = $this->createMock(IConfig::class);
- $cacheFactory = $this->createMock(ICacheFactory::class);
- $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
- $result = $urlGenerator->linkToRouteAbsolute($route);
+ $result = $this->urlGenerator->linkToRouteAbsolute($route);
$this->assertEquals($expected, $result);
-
}
public function provideRoutes() {
@@ -89,13 +111,9 @@ class UrlGeneratorTest extends \Test\TestCase {
* @dataProvider provideDocRootURLs
*/
function testGetAbsoluteURLDocRoot($url, $expectedResult) {
-
+ $this->mockBaseUrl();
\OC::$WEBROOT = '';
- $config = $this->createMock(IConfig::class);
- $cacheFactory = $this->createMock(ICacheFactory::class);
- $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
- $result = $urlGenerator->getAbsoluteURL($url);
-
+ $result = $this->urlGenerator->getAbsoluteURL($url);
$this->assertEquals($expectedResult, $result);
}
@@ -105,13 +123,9 @@ class UrlGeneratorTest extends \Test\TestCase {
* @dataProvider provideSubDirURLs
*/
function testGetAbsoluteURLSubDir($url, $expectedResult) {
-
+ $this->mockBaseUrl();
\OC::$WEBROOT = '/owncloud';
- $config = $this->createMock(IConfig::class);
- $cacheFactory = $this->createMock(ICacheFactory::class);
- $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
- $result = $urlGenerator->getAbsoluteURL($url);
-
+ $result = $this->urlGenerator->getAbsoluteURL($url);
$this->assertEquals($expectedResult, $result);
}
@@ -132,5 +146,14 @@ class UrlGeneratorTest extends \Test\TestCase {
array("apps/index.php", "http://localhost/owncloud/apps/index.php"),
);
}
+
+ public function testGetBaseUrl() {
+ $this->mockBaseUrl();
+ \OC::$WEBROOT = '/nextcloud';
+ $actual = $this->urlGenerator->getBaseUrl();
+ $expected = "http://localhost/nextcloud";
+ $this->assertEquals($expected, $actual);
+ }
+
}