summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2014-11-28 10:16:22 +0100
committerMorris Jobke <hey@morrisjobke.de>2014-11-28 10:16:22 +0100
commitb188710af33fde461a57e81dfc4aaf1cf7c6fda5 (patch)
tree3d66021b2507fc28e887b5c4b647a65ae11ef654
parent7a9af8c40c4ff2c2005a304a6474e9f328a3be5c (diff)
parente35feadac2ed68f0aad911713cb3d5f8725707e6 (diff)
downloadnextcloud-server-b188710af33fde461a57e81dfc4aaf1cf7c6fda5.tar.gz
nextcloud-server-b188710af33fde461a57e81dfc4aaf1cf7c6fda5.zip
Merge pull request #12472 from owncloud/modifyCookies
Add functions to modify cookies to response class
-rw-r--r--lib/base.php2
-rw-r--r--lib/private/appframework/app.php10
-rw-r--r--lib/private/appframework/http/dispatcher.php8
-rw-r--r--lib/private/server.php28
-rw-r--r--lib/public/appframework/http/response.php73
-rw-r--r--lib/public/iservercontainer.php7
-rw-r--r--tests/lib/appframework/AppTest.php2
-rw-r--r--tests/lib/appframework/http/DispatcherTest.php16
-rw-r--r--tests/lib/appframework/http/ResponseTest.php86
9 files changed, 207 insertions, 25 deletions
diff --git a/lib/base.php b/lib/base.php
index 5c33be351a4..cd5d8feb1f6 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -466,7 +466,7 @@ class OC {
}
// setup the basic server
- self::$server = new \OC\Server();
+ self::$server = new \OC\Server(\OC::$WEBROOT);
\OC::$server->getEventLogger()->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd);
\OC::$server->getEventLogger()->start('boot', 'Initialize');
diff --git a/lib/private/appframework/app.php b/lib/private/appframework/app.php
index baf52d02054..f56ba4af870 100644
--- a/lib/private/appframework/app.php
+++ b/lib/private/appframework/app.php
@@ -53,7 +53,7 @@ class App {
// initialize the dispatcher and run all the middleware before the controller
$dispatcher = $container['Dispatcher'];
- list($httpHeaders, $responseHeaders, $output) =
+ list($httpHeaders, $responseHeaders, $responseCookies, $output) =
$dispatcher->dispatch($controller, $methodName);
if(!is_null($httpHeaders)) {
@@ -64,6 +64,14 @@ class App {
header($name . ': ' . $value);
}
+ foreach($responseCookies as $name => $value) {
+ $expireDate = null;
+ if($value['expireDate'] instanceof \DateTime) {
+ $expireDate = $value['expireDate']->getTimestamp();
+ }
+ setcookie($name, $value['value'], $expireDate, $container->getServer()->getWebRoot(), null, $container->getServer()->getConfig()->getSystemValue('forcessl', false), true);
+ }
+
if(!is_null($output)) {
header('Content-Length: ' . strlen($output));
print($output);
diff --git a/lib/private/appframework/http/dispatcher.php b/lib/private/appframework/http/dispatcher.php
index 29a661d5743..24540ef3c94 100644
--- a/lib/private/appframework/http/dispatcher.php
+++ b/lib/private/appframework/http/dispatcher.php
@@ -48,7 +48,7 @@ class Dispatcher {
* @param Http $protocol the http protocol with contains all status headers
* @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which
* runs the middleware
- * @param ControllerMethodReflector the reflector that is used to inject
+ * @param ControllerMethodReflector $reflector the reflector that is used to inject
* the arguments for the controller
* @param IRequest $request the incoming request
*/
@@ -71,6 +71,7 @@ class Dispatcher {
* @return array $array[0] contains a string with the http main header,
* $array[1] contains headers in the form: $key => value, $array[2] contains
* the response output
+ * @throws \Exception
*/
public function dispatch(Controller $controller, $methodName) {
$out = array(null, array(), null);
@@ -102,13 +103,14 @@ class Dispatcher {
// get the output which should be printed and run the after output
// middleware to modify the response
$output = $response->render();
- $out[2] = $this->middlewareDispatcher->beforeOutput(
+ $out[3] = $this->middlewareDispatcher->beforeOutput(
$controller, $methodName, $output);
// depending on the cache object the headers need to be changed
$out[0] = $this->protocol->getStatusHeader($response->getStatus(),
$response->getLastModified(), $response->getETag());
- $out[1] = $response->getHeaders();
+ $out[1] = array_merge($response->getHeaders());
+ $out[2] = $response->getCookies();
return $out;
}
diff --git a/lib/private/server.php b/lib/private/server.php
index cd57d41ce58..7bd7f8ca45d 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -29,19 +29,27 @@ use OC\Tagging\TagMapper;
* TODO: hookup all manager classes
*/
class Server extends SimpleContainer implements IServerContainer {
- function __construct() {
+ /** @var string */
+ private $webRoot;
+
+ /**
+ * @param string $webRoot
+ */
+ function __construct($webRoot) {
+ $this->webRoot = $webRoot;
+
$this->registerService('ContactsManager', function ($c) {
return new ContactsManager();
});
- $this->registerService('Request', function ($c) {
+ $this->registerService('Request', function (Server $c) {
if (isset($c['urlParams'])) {
$urlParams = $c['urlParams'];
} else {
$urlParams = array();
}
- if (\OC::$server->getSession()->exists('requesttoken')) {
- $requestToken = \OC::$server->getSession()->get('requesttoken');
+ if ($c->getSession()->exists('requesttoken')) {
+ $requestToken = $c->getSession()->get('requesttoken');
} else {
$requestToken = false;
}
@@ -233,8 +241,7 @@ class Server extends SimpleContainer implements IServerContainer {
return new NullQueryLogger();
}
});
- $this->registerService('TempManager', function ($c) {
- /** @var Server $c */
+ $this->registerService('TempManager', function (Server $c) {
return new TempManager(get_temp_dir(), $c->getLogger());
});
$this->registerService('AppManager', function(Server $c) {
@@ -631,4 +638,13 @@ class Server extends SimpleContainer implements IServerContainer {
function getAppManager() {
return $this->query('AppManager');
}
+
+ /**
+ * Get the webroot
+ *
+ * @return string
+ */
+ function getWebRoot() {
+ return $this->webRoot;
+ }
}
diff --git a/lib/public/appframework/http/response.php b/lib/public/appframework/http/response.php
index 354911fee21..67e72cff6d9 100644
--- a/lib/public/appframework/http/response.php
+++ b/lib/public/appframework/http/response.php
@@ -46,8 +46,15 @@ class Response {
/**
+ * Cookies that will be need to be constructed as header
+ * @var array
+ */
+ private $cookies = array();
+
+
+ /**
* HTTP status code - defaults to STATUS OK
- * @var string
+ * @var int
*/
private $status = Http::STATUS_OK;
@@ -70,6 +77,7 @@ class Response {
* Caches the response
* @param int $cacheSeconds the amount of seconds that should be cached
* if 0 then caching will be disabled
+ * @return $this
*/
public function cacheFor($cacheSeconds) {
@@ -83,13 +91,68 @@ class Response {
return $this;
}
+ /**
+ * Adds a new cookie to the response
+ * @param string $name The name of the cookie
+ * @param string $value The value of the cookie
+ * @param \DateTime|null $expireDate Date on that the cookie should expire, if set
+ * to null cookie will be considered as session
+ * cookie.
+ * @return $this
+ */
+ public function addCookie($name, $value, \DateTime $expireDate = null) {
+ $this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate);
+ return $this;
+ }
+
+
+ /**
+ * Set the specified cookies
+ * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null))
+ * @return $this
+ */
+ public function setCookies(array $cookies) {
+ $this->cookies = $cookies;
+ return $this;
+ }
+
+
+ /**
+ * Invalidates the specified cookie
+ * @param string $name
+ * @return $this
+ */
+ public function invalidateCookie($name) {
+ $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00'));
+ return $this;
+ }
+
+ /**
+ * Invalidates the specified cookies
+ * @param array $cookieNames array('foo', 'bar')
+ * @return $this
+ */
+ public function invalidateCookies(array $cookieNames) {
+ foreach($cookieNames as $cookieName) {
+ $this->invalidateCookie($cookieName);
+ }
+ return $this;
+ }
+
+ /**
+ * Returns the cookies
+ * @return array
+ */
+ public function getCookies() {
+ return $this->cookies;
+ }
/**
* Adds a new header to the response that will be called before the render
* function
* @param string $name The name of the HTTP header
* @param string $value The value, null will delete it
- * @return Response Reference to this object
+ * @return $this
*/
public function addHeader($name, $value) {
$name = trim($name); // always remove leading and trailing whitespace
@@ -108,10 +171,10 @@ class Response {
/**
* Set the headers
- * @param array key value header pairs
- * @return Response Reference to this object
+ * @param array $headers value header pairs
+ * @return $this
*/
- public function setHeaders($headers) {
+ public function setHeaders(array $headers) {
$this->headers = $headers;
return $this;
diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php
index b734d1b4161..301f47c68fa 100644
--- a/lib/public/iservercontainer.php
+++ b/lib/public/iservercontainer.php
@@ -298,4 +298,11 @@ interface IServerContainer {
* @return \OCP\App\IAppManager
*/
function getAppManager();
+
+ /**
+ * Get the webroot
+ *
+ * @return string
+ */
+ function getWebRoot();
}
diff --git a/tests/lib/appframework/AppTest.php b/tests/lib/appframework/AppTest.php
index bd565e9765e..86128db118f 100644
--- a/tests/lib/appframework/AppTest.php
+++ b/tests/lib/appframework/AppTest.php
@@ -63,7 +63,7 @@ class AppTest extends \Test\TestCase {
public function testControllerNameAndMethodAreBeingPassed(){
- $return = array(null, array(), null);
+ $return = array(null, array(), array(), null);
$this->dispatcher->expects($this->once())
->method('dispatch')
->with($this->equalTo($this->controller),
diff --git a/tests/lib/appframework/http/DispatcherTest.php b/tests/lib/appframework/http/DispatcherTest.php
index f92e7161e6b..45ebd6fce96 100644
--- a/tests/lib/appframework/http/DispatcherTest.php
+++ b/tests/lib/appframework/http/DispatcherTest.php
@@ -227,7 +227,7 @@ class DispatcherTest extends \Test\TestCase {
$this->assertEquals($httpHeaders, $response[0]);
$this->assertEquals($responseHeaders, $response[1]);
- $this->assertEquals($out, $response[2]);
+ $this->assertEquals($out, $response[3]);
}
@@ -246,7 +246,7 @@ class DispatcherTest extends \Test\TestCase {
$this->assertEquals($httpHeaders, $response[0]);
$this->assertEquals($responseHeaders, $response[1]);
- $this->assertEquals($out, $response[2]);
+ $this->assertEquals($out, $response[3]);
}
@@ -301,7 +301,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
- $this->assertEquals('[3,true,4,1]', $response[2]);
+ $this->assertEquals('[3,true,4,1]', $response[3]);
}
@@ -324,7 +324,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
- $this->assertEquals('[3,true,4,7]', $response[2]);
+ $this->assertEquals('[3,true,4,7]', $response[3]);
}
@@ -350,7 +350,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
- $this->assertEquals('{"text":[3,false,4,1]}', $response[2]);
+ $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
}
@@ -375,7 +375,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'execDataResponse');
- $this->assertEquals('{"text":[3,false,4,1]}', $response[2]);
+ $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
}
@@ -401,7 +401,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
- $this->assertEquals('{"text":[3,false,4,1]}', $response[2]);
+ $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
}
@@ -429,7 +429,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
- $this->assertEquals('{"text":[3,true,4,1]}', $response[2]);
+ $this->assertEquals('{"text":[3,true,4,1]}', $response[3]);
}
diff --git a/tests/lib/appframework/http/ResponseTest.php b/tests/lib/appframework/http/ResponseTest.php
index 04e19fdaf71..b4352348bae 100644
--- a/tests/lib/appframework/http/ResponseTest.php
+++ b/tests/lib/appframework/http/ResponseTest.php
@@ -76,6 +76,92 @@ class ResponseTest extends \Test\TestCase {
}
+ public function testAddCookie() {
+ $this->childResponse->addCookie('foo', 'bar');
+ $this->childResponse->addCookie('bar', 'foo', new \DateTime('1970-01-01'));
+
+ $expectedResponse = array(
+ 'foo' => array(
+ 'value' => 'bar',
+ 'expireDate' => null,
+ ),
+ 'bar' => array(
+ 'value' => 'foo',
+ 'expireDate' => new \DateTime('1970-01-01')
+ )
+ );
+ $this->assertEquals($expectedResponse, $this->childResponse->getCookies());
+ }
+
+
+ function testSetCookies() {
+ $expected = array(
+ 'foo' => array(
+ 'value' => 'bar',
+ 'expireDate' => null,
+ ),
+ 'bar' => array(
+ 'value' => 'foo',
+ 'expireDate' => new \DateTime('1970-01-01')
+ )
+ );
+
+ $this->childResponse->setCookies($expected);
+ $cookies = $this->childResponse->getCookies();
+
+ $this->assertEquals($expected, $cookies);
+ }
+
+
+ function testInvalidateCookie() {
+ $this->childResponse->addCookie('foo', 'bar');
+ $this->childResponse->invalidateCookie('foo');
+ $expected = array(
+ 'foo' => array(
+ 'value' => 'expired',
+ 'expireDate' => new \DateTime('1971-01-01')
+ )
+ );
+
+ $cookies = $this->childResponse->getCookies();
+
+ $this->assertEquals($expected, $cookies);
+ }
+
+
+ function testInvalidateCookies() {
+ $this->childResponse->addCookie('foo', 'bar');
+ $this->childResponse->addCookie('bar', 'foo');
+ $expected = array(
+ 'foo' => array(
+ 'value' => 'bar',
+ 'expireDate' => null
+ ),
+ 'bar' => array(
+ 'value' => 'foo',
+ 'expireDate' => null
+ )
+ );
+ $cookies = $this->childResponse->getCookies();
+ $this->assertEquals($expected, $cookies);
+
+ $this->childResponse->invalidateCookies(array('foo', 'bar'));
+ $expected = array(
+ 'foo' => array(
+ 'value' => 'expired',
+ 'expireDate' => new \DateTime('1971-01-01')
+ ),
+ 'bar' => array(
+ 'value' => 'expired',
+ 'expireDate' => new \DateTime('1971-01-01')
+ )
+ );
+
+ $cookies = $this->childResponse->getCookies();
+ $this->assertEquals($expected, $cookies);
+ }
+
+
public function testRenderReturnNullByDefault(){
$this->assertEquals(null, $this->childResponse->render());
}