Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 59 additions & 35 deletions Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@
#
# THIS IS AN EXPERIMENTAL FEATURE
# DO NOT USE THIS IN PRODUCTION, YOU HAVE BEEN WARNED.
{
metrics
frankenphp {
num_threads 192
max_threads 256
# max_requests 500
}
}

localhost {
php_server {
worker {
worker {
file index.php
num 32
watch
}
worker {
file remote.php
num 32
watch
}
worker {
file ocs/v1.php
num 32
watch
}
worker {
file ocs/v2.php
num 32
watch
}
}
Expand All @@ -17,42 +41,42 @@ localhost {
output stderr
}

encode gzip
encode gzip

redir /.well-known/carddav /remote.php/dav 301
redir /.well-known/caldav /remote.php/dav 301
redir /.well-known/carddav /remote.php/dav 301
redir /.well-known/caldav /remote.php/dav 301

# Rule: Maps most RFC 8615 compliant well-known URIs to our main frontend controller (/index.php) by default
@wellKnown {
path "/.well-known/"
not {
path /.well-known/acme-challenge
path /.well-known/pki-validation
}
}
rewrite @wellKnown /index.php
# Rule: Maps most RFC 8615 compliant well-known URIs to our main frontend controller (/index.php) by default
@wellKnown {
path "/.well-known/"
not {
path /.well-known/acme-challenge
path /.well-known/pki-validation
}
}
rewrite @wellKnown /index.php

rewrite /ocm-provider/ /index.php
rewrite /ocm-provider/ /index.php

@forbidden {
path /.htaccess
path /data/*
path /config/*
path /db_structure
path /.xml
path /README
path /3rdparty/*
path /lib/*
path /templates/*
path /occ
path /build
path /tests
path /console.php
path /autotest
path /issue
path /indi
path /db_
path /console
}
respond @forbidden 404
@forbidden {
path /.htaccess
path /data/*
path /config/*
path /db_structure
path /.xml
path /README
path /3rdparty/*
path /lib/*
path /templates/*
path /occ
path /build
path /tests
path /console.php
path /autotest
path /issue
path /indi
path /db_
path /console
}
respond @forbidden 404
}
38 changes: 4 additions & 34 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

require_once __DIR__ . '/lib/versioncheck.php';

use OC\Files\Filesystem;
use OC\ServiceUnavailableException;
use OC\User\LoginException;
use OCP\HintException;
Expand All @@ -24,23 +23,10 @@

\OC::boot();

function resetStaticProperties(): void {
// FIXME needed because these use a static var
\OC_Hook::clear();
\OC_Util::$styles = [];
\OC_Util::$headers = [];
\OC_User::setIncognitoMode(false);
\OC_User::$_setupedBackends = [];
\OC_App::reset();
\OC_Helper::reset();
Filesystem::reset();
}

$handler = static function () {
\OC::handleRequests(static function () {
try {
resetStaticProperties();
OC::init();
OC::handleRequest();
\OC::initForRequest();
\OC::handleRequest();
} catch (ServiceUnavailableException $ex) {
Server::get(LoggerInterface::class)->error($ex->getMessage(), [
'app' => 'index',
Expand Down Expand Up @@ -124,20 +110,4 @@ function resetStaticProperties(): void {
}
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
}
};

if (function_exists('frankenphp_handle_request') && isset($_SERVER['FRANKENPHP_WORKER']) && $_SERVER['FRANKENPHP_WORKER'] === '1') {
$maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 0);
for ($nbRequests = 0; !$maxRequests || $nbRequests < $maxRequests; ++$nbRequests) {
$keepRunning = \frankenphp_handle_request($handler);

// Call the garbage collector to reduce the chances of it being triggered in the middle of a page generation
gc_collect_cycles();

if (!$keepRunning) {
break;
}
}
} else {
$handler();
}
});
47 changes: 46 additions & 1 deletion lib/OC.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

use OC\Files\Filesystem;
use OC\Profiler\BuiltInProfiler;
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
use OC\Share20\GroupDeletedListener;
Expand Down Expand Up @@ -665,6 +666,9 @@ private static function addSecurityHeaders(): void {
}
}

/*
* Called only once at the beginning to setup things
*/
public static function boot(): void {
// prevent any XML processing from loading external entities
libxml_set_external_entity_loader(static function () {
Expand Down Expand Up @@ -725,7 +729,12 @@ public static function boot(): void {
}
}

public static function init(): void {
/*
* Called before each request served if the same worker serves several request
*/
public static function initForRequest(): void {
self::resetStaticProperties();

// First handle PHP configuration and copy auth headers to the expected
// $_SERVER variable before doing anything Server object related
self::setRequiredIniValues();
Expand Down Expand Up @@ -1324,4 +1333,40 @@ protected static function tryAppAPILogin(OCP\IRequest $request): bool {
return false;
}
}

/**
* @internal
*/
private static function resetStaticProperties(): void {
// FIXME needed because these use a static var
\OC_Hook::clear();
\OC_Util::$styles = [];
\OC_Util::$headers = [];
\OC_User::setIncognitoMode(false);
\OC_User::$_setupedBackends = [];
\OC_App::reset();
\OC_Helper::reset();
Filesystem::reset();
}

/**
* @internal
*/
public static function handleRequests(callable $handler): void {
if (function_exists('frankenphp_handle_request') && isset($_SERVER['FRANKENPHP_WORKER']) && $_SERVER['FRANKENPHP_WORKER'] === '1') {
$maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 0);
for ($nbRequests = 0; !$maxRequests || $nbRequests < $maxRequests; ++$nbRequests) {
$keepRunning = \frankenphp_handle_request($handler);

// Call the garbage collector to reduce the chances of it being triggered in the middle of a page generation
gc_collect_cycles();

if (!$keepRunning) {
break;
}
}
} else {
$handler();
}
}
}
2 changes: 1 addition & 1 deletion lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
require_once __DIR__ . '/OC.php';

\OC::boot();
\OC::init();
\OC::initForRequest();
107 changes: 56 additions & 51 deletions ocs/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

require_once __DIR__ . '/../lib/versioncheck.php';
require_once __DIR__ . '/../lib/base.php';

use OC\OCS\ApiHelper;
use OC\Route\Router;
use OC\SystemConfig;
Expand All @@ -28,60 +25,68 @@
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

$request = Server::get(IRequest::class);
require_once __DIR__ . '/../lib/versioncheck.php';
require_once __DIR__ . '/../lib/OC.php';

if ((Util::needUpgrade() || Server::get(IConfig::class)->getSystemValueBool('maintenance')) && $request->getPathInfo() !== '/core/update') {
// since the behavior of apps or remotes are unpredictable during
// an upgrade, return a 503 directly
ApiHelper::respond(503, 'Service unavailable', ['X-Nextcloud-Maintenance-Mode' => '1'], 503);
exit;
}
\OC::boot();

/*
* Try the appframework routes
*/
try {
$appManager = Server::get(IAppManager::class);
$appManager->loadApps(['session']);
$appManager->loadApps(['authentication']);
$appManager->loadApps(['extended_authentication']);
\OC::handleRequests(static function () {
\OC::initForRequest();
$request = Server::get(IRequest::class);

if ((Util::needUpgrade() || Server::get(IConfig::class)->getSystemValueBool('maintenance')) && $request->getPathInfo() !== '/core/update') {
// since the behavior of apps or remotes are unpredictable during
// an upgrade, return a 503 directly
ApiHelper::respond(503, 'Service unavailable', ['X-Nextcloud-Maintenance-Mode' => '1'], 503);
exit;
}

/*
* Try the appframework routes
*/
try {
$appManager = Server::get(IAppManager::class);
$appManager->loadApps(['session']);
$appManager->loadApps(['authentication']);
$appManager->loadApps(['extended_authentication']);

$request->throwDecodingExceptionIfAny();
$request->throwDecodingExceptionIfAny();

if ($request->getPathInfo() !== '/core/update') {
// load all apps to get all api routes properly setup
// FIXME: this should ideally appear after handleLogin but will cause
// side effects in existing apps
$appManager->loadApps();
if (!Server::get(IUserSession::class)->isLoggedIn()) {
OC::handleLogin($request);
if ($request->getPathInfo() !== '/core/update') {
// load all apps to get all api routes properly setup
// FIXME: this should ideally appear after handleLogin but will cause
// side effects in existing apps
$appManager->loadApps();
if (!Server::get(IUserSession::class)->isLoggedIn()) {
OC::handleLogin($request);
}
} else {
$appManager->loadApps(['core']);
}
} else {
$appManager->loadApps(['core']);
}

Server::get(Router::class)->match('/ocsapp' . $request->getRawPathInfo());
} catch (MaxDelayReached $ex) {
ApiHelper::respond(Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage());
} catch (ResourceNotFoundException $e) {
$txt = 'Invalid query, please check the syntax. API specifications are here:'
. ' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.' . "\n";
ApiHelper::respond(OCSController::RESPOND_NOT_FOUND, $txt);
} catch (MethodNotAllowedException $e) {
ApiHelper::setContentType();
http_response_code(405);
} catch (LoginException $e) {
ApiHelper::respond(OCSController::RESPOND_UNAUTHORISED, 'Unauthorised');
} catch (\Exception $e) {
Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
Server::get(Router::class)->match('/ocsapp' . $request->getRawPathInfo());
} catch (MaxDelayReached $ex) {
ApiHelper::respond(Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage());
} catch (ResourceNotFoundException $e) {
$txt = 'Invalid query, please check the syntax. API specifications are here:'
. ' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.' . "\n";
ApiHelper::respond(OCSController::RESPOND_NOT_FOUND, $txt);
} catch (MethodNotAllowedException $e) {
ApiHelper::setContentType();
http_response_code(405);
} catch (LoginException $e) {
ApiHelper::respond(OCSController::RESPOND_UNAUTHORISED, 'Unauthorised');
} catch (\Exception $e) {
Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);

$txt = 'Internal Server Error' . "\n";
try {
if (Server::get(SystemConfig::class)->getValue('debug', false)) {
$txt .= $e->getMessage();
$txt = 'Internal Server Error' . "\n";
try {
if (Server::get(SystemConfig::class)->getValue('debug', false)) {
$txt .= $e->getMessage();
}
} catch (\Throwable $e) {
// Just to be save
}
} catch (\Throwable $e) {
// Just to be save
ApiHelper::respond(OCSController::RESPOND_SERVER_ERROR, $txt);
}
ApiHelper::respond(OCSController::RESPOND_SERVER_ERROR, $txt);
}
});
Loading
Loading