Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Refix shutdown scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Oct 8, 2016
1 parent 963de1c commit a811398
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
23 changes: 15 additions & 8 deletions core/src/core/src/pydio/Core/Controller/ShutdownScheduler.php
Expand Up @@ -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;

Expand Down Expand Up @@ -50,14 +51,26 @@ public static function getInstance()
return self::$instance;
}

/**
* @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();
}

/**
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions core/src/core/src/pydio/Core/Http/Cli/CliMiddleware.php
Expand Up @@ -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>");
Expand Down Expand Up @@ -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);

}
}
45 changes: 45 additions & 0 deletions core/src/core/src/pydio/Core/Http/Dav/DAVResponse.php
@@ -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();
}
}
1 change: 1 addition & 0 deletions core/src/core/src/pydio/Core/Http/Dav/DAVServer.php
Expand Up @@ -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);
Expand Down
Expand Up @@ -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');

Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/core/src/pydio/Core/Http/Wopi/Middleware.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/plugins/core.ocs/src/Server/Dav/Server.php
Expand Up @@ -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;
Expand All @@ -51,6 +52,8 @@ public function __construct()
$rootCollection = new Collection("/", $this->context);

parent::__construct($rootCollection);

$this->httpResponse = new DAVResponse();
}

/**
Expand Down

0 comments on commit a811398

Please sign in to comment.