Permalink
Browse files

Refix shutdown scheduling

  • Loading branch information...
1 parent 963de1c commit a81139874dc60478af54c8095acd409eeeecae88 @cdujeu cdujeu committed Oct 8, 2016
@@ -20,8 +20,9 @@
*/
namespace Pydio\Core\Controller;
+use Psr\Http\Message\ResponseInterface;
use Pydio\Core\Exception\PydioException;
-use Pydio\Core\Services\ApplicationState;
+use Pydio\Core\Http\Dav\DAVResponse;
use Pydio\Log\Core\Logger;
use Symfony\Component\Console\Output\OutputInterface;
@@ -51,13 +52,25 @@ public static function getInstance()
}
/**
+ * @param null|DAVResponse|ResponseInterface $responseObject
+ */
+ public static function setCloseHeaders(&$responseObject = null){
+ if($responseObject instanceof DAVResponse){
+ $responseObject->setHeader("Connection", "close");
+ }else if($responseObject instanceof ResponseInterface){
+ $responseObject = $responseObject->withHeader("Connection", "close");
+ }else if(!headers_sent()){
+ header("Connection: close\r\n");
+ }
+ }
+
+ /**
* ShutdownScheduler constructor.
*/
public function __construct()
{
$this->callbacks = array();
register_shutdown_function(array($this, 'callRegisteredShutdown'));
-// ob_start();
}
/**
@@ -101,12 +114,6 @@ public function registerShutdownEvent()
return true;
}
- public function closeAndCallRegisteredShutdown(){
- if(!headers_sent()){
- header("Connection: close\r\n");
- }
- $this->callRegisteredShutdown();
- }
/**
* Trigger the schedulers
@@ -65,9 +65,6 @@ public function handleRequest(ServerRequestInterface $requestInterface, Response
$this->emitResponse($requestInterface, $responseInterface);
- $logHooks = isSet($requestInterface->getParsedBody()["cli-show-hooks"]) ? $output: null;
- ShutdownScheduler::getInstance()->callRegisteredShutdown($logHooks);
-
} catch (AuthRequiredException $e){
$output->writeln("<error>Authentication Failed</error>");
@@ -122,6 +119,8 @@ public function emitResponse(ServerRequestInterface $requestInterface, ResponseI
}else{
echo "".$responseInterface->getBody();
}
+ $logHooks = isSet($requestInterface->getParsedBody()["cli-show-hooks"]) ? $output: null;
+ ShutdownScheduler::getInstance()->callRegisteredShutdown($logHooks);
}
}
@@ -0,0 +1,45 @@
+<?php
+/*
+ * Copyright 2007-2016 Abstrium <contact (at) pydio.com>
+ * This file is part of Pydio.
+ *
+ * Pydio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Pydio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with Pydio. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The latest code can be found at <https://pydio.com/>.
+ */
+namespace Pydio\Core\Http\Dav;
+
+use Pydio\Core\Controller\ShutdownScheduler;
+use Sabre\HTTP\Response;
+
+defined('AJXP_EXEC') or die('Access not allowed');
+
+/**
+ * Class DAVResponse
+ * Make sure to close connection and apply ShutdownScheduler afterward
+ * @package Pydio\Core\Http\Dav
+ */
+class DAVResponse extends Response
+{
+ /**
+ * Override parent function
+ * @param mixed $body
+ */
+ public function sendBody($body)
+ {
+ ShutdownScheduler::setCloseHeaders($this);
+ parent::sendBody($body);
+ ShutdownScheduler::getInstance()->callRegisteredShutdown();
+ }
+}
@@ -99,6 +99,7 @@ public static function handleRoute($baseURI, $davRoute){
$server->setBaseUri($baseURI);
}
+ $server->httpResponse = new DAVResponse();
if((AuthBackendBasic::detectBasicHeader() || ConfService::getGlobalConf("WEBDAV_FORCE_BASIC"))){
$authBackend = new AuthBackendBasic(self::$context);
@@ -28,6 +28,7 @@
use Pydio\Core\Http\Response\SerializableResponseStream;
use Pydio\Core\Http\Server;
use Pydio\Core\Utils\Vars\InputFilter;
+use Zend\Diactoros\Response\SapiEmitter;
defined('AJXP_EXEC') or die('Access not allowed');
@@ -125,8 +126,8 @@ public function emitResponse(ServerRequestInterface $request, ResponseInterface
}
if($response !== false && ($response->getBody()->getSize() || $response instanceof \Zend\Diactoros\Response\EmptyResponse) || $response->getStatusCode() != 200) {
- $emitter = new \Zend\Diactoros\Response\SapiEmitter();
- $response = $response->withHeader("Connection", "close");
+ $emitter = new SapiEmitter();
+ ShutdownScheduler::setCloseHeaders($response);
$emitter->emit($response);
ShutdownScheduler::getInstance()->callRegisteredShutdown();
}
@@ -24,11 +24,13 @@
use \Psr\Http\Message\ResponseInterface;
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Access\Core\Model\NodesList;
+use Pydio\Core\Controller\ShutdownScheduler;
use Pydio\Core\Exception\PydioException;
use Pydio\Core\Exception\RouteNotFoundException;
use Pydio\Core\Http\Message\Message;
use Pydio\Core\Http\Middleware\SapiMiddleware;
use Pydio\Core\Http\Response\SerializableResponseStream;
+use Zend\Diactoros\Response\EmptyResponse;
use Zend\Diactoros\Response\SapiEmitter;
defined('AJXP_EXEC') or die('Access not allowed');
@@ -126,7 +128,7 @@ public function emitResponse(ServerRequestInterface $request, ResponseInterface
if($response !== false && ($response->getBody()->getSize() || $response instanceof EmptyResponse) || $response->getStatusCode() != 200) {
$emitter = new SapiEmitter();
- $response = $response->withHeader("Connection", "close");
+ ShutdownScheduler::setCloseHeaders($response);
$emitter->emit($response);
ShutdownScheduler::getInstance()->callRegisteredShutdown();
}
@@ -26,6 +26,7 @@
use Pydio\Core\Http\Dav\BrowserPlugin;
use Pydio\Core\Http\Dav\Collection;
+use Pydio\Core\Http\Dav\DAVResponse;
use Pydio\Core\Model\Context;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Services\ConfService;
@@ -51,6 +52,8 @@ public function __construct()
$rootCollection = new Collection("/", $this->context);
parent::__construct($rootCollection);
+
+ $this->httpResponse = new DAVResponse();
}
/**

0 comments on commit a811398

Please sign in to comment.