|
19 | 19 | use OCP\Template\ITemplateManager; |
20 | 20 | use Psr\Log\LoggerInterface; |
21 | 21 |
|
22 | | -try { |
23 | | - require_once __DIR__ . '/lib/base.php'; |
| 22 | +require_once __DIR__ . '/lib/base.php'; |
24 | 23 |
|
25 | | - OC::handleRequest(); |
26 | | -} catch (ServiceUnavailableException $ex) { |
27 | | - Server::get(LoggerInterface::class)->error($ex->getMessage(), [ |
28 | | - 'app' => 'index', |
29 | | - 'exception' => $ex, |
30 | | - ]); |
31 | | - |
32 | | - //show the user a detailed error page |
33 | | - Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 503); |
34 | | -} catch (HintException $ex) { |
| 24 | +$handler = static function () { |
35 | 25 | try { |
36 | | - Server::get(ITemplateManager::class)->printErrorPage($ex->getMessage(), $ex->getHint(), 503); |
37 | | - } catch (Exception $ex2) { |
| 26 | + // In worker mode, script name is empty in FrankenPHP |
| 27 | + if ($_SERVER['SCRIPT_NAME'] === '') { |
| 28 | + $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; |
| 29 | + } |
| 30 | + OC::init(); |
| 31 | + OC::handleRequest(); |
| 32 | + } catch (ServiceUnavailableException $ex) { |
| 33 | + Server::get(LoggerInterface::class)->error($ex->getMessage(), [ |
| 34 | + 'app' => 'index', |
| 35 | + 'exception' => $ex, |
| 36 | + ]); |
| 37 | + |
| 38 | + //show the user a detailed error page |
| 39 | + Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 503); |
| 40 | + } catch (HintException $ex) { |
| 41 | + try { |
| 42 | + Server::get(ITemplateManager::class)->printErrorPage($ex->getMessage(), $ex->getHint(), 503); |
| 43 | + } catch (Exception $ex2) { |
| 44 | + try { |
| 45 | + Server::get(LoggerInterface::class)->error($ex->getMessage(), [ |
| 46 | + 'app' => 'index', |
| 47 | + 'exception' => $ex, |
| 48 | + ]); |
| 49 | + Server::get(LoggerInterface::class)->error($ex2->getMessage(), [ |
| 50 | + 'app' => 'index', |
| 51 | + 'exception' => $ex2, |
| 52 | + ]); |
| 53 | + } catch (Throwable $e) { |
| 54 | + // no way to log it properly - but to avoid a white page of death we try harder and ignore this one here |
| 55 | + } |
| 56 | + |
| 57 | + //show the user a detailed error page |
| 58 | + Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500); |
| 59 | + } |
| 60 | + } catch (LoginException $ex) { |
| 61 | + $request = Server::get(IRequest::class); |
| 62 | + /** |
| 63 | + * Routes with the @CORS annotation and other API endpoints should |
| 64 | + * not return a webpage, so we only print the error page when html is accepted, |
| 65 | + * otherwise we reply with a JSON array like the SecurityMiddleware would do. |
| 66 | + */ |
| 67 | + if (stripos($request->getHeader('Accept'), 'html') === false) { |
| 68 | + http_response_code(401); |
| 69 | + header('Content-Type: application/json; charset=utf-8'); |
| 70 | + echo json_encode(['message' => $ex->getMessage()]); |
| 71 | + exit(); |
| 72 | + } |
| 73 | + Server::get(ITemplateManager::class)->printErrorPage($ex->getMessage(), $ex->getMessage(), 401); |
| 74 | + } catch (MaxDelayReached $ex) { |
| 75 | + $request = Server::get(IRequest::class); |
| 76 | + /** |
| 77 | + * Routes with the @CORS annotation and other API endpoints should |
| 78 | + * not return a webpage, so we only print the error page when html is accepted, |
| 79 | + * otherwise we reply with a JSON array like the BruteForceMiddleware would do. |
| 80 | + */ |
| 81 | + if (stripos($request->getHeader('Accept'), 'html') === false) { |
| 82 | + http_response_code(429); |
| 83 | + header('Content-Type: application/json; charset=utf-8'); |
| 84 | + echo json_encode(['message' => $ex->getMessage()]); |
| 85 | + exit(); |
| 86 | + } |
| 87 | + http_response_code(429); |
| 88 | + Server::get(ITemplateManager::class)->printGuestPage('core', '429'); |
| 89 | + } catch (Exception $ex) { |
| 90 | + Server::get(LoggerInterface::class)->error($ex->getMessage(), [ |
| 91 | + 'app' => 'index', |
| 92 | + 'exception' => $ex, |
| 93 | + ]); |
| 94 | + |
| 95 | + //show the user a detailed error page |
| 96 | + Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500); |
| 97 | + } catch (Error $ex) { |
38 | 98 | try { |
39 | 99 | Server::get(LoggerInterface::class)->error($ex->getMessage(), [ |
40 | 100 | 'app' => 'index', |
41 | 101 | 'exception' => $ex, |
42 | 102 | ]); |
43 | | - Server::get(LoggerInterface::class)->error($ex2->getMessage(), [ |
44 | | - 'app' => 'index', |
45 | | - 'exception' => $ex2, |
46 | | - ]); |
47 | | - } catch (Throwable $e) { |
48 | | - // no way to log it properly - but to avoid a white page of death we try harder and ignore this one here |
49 | | - } |
| 103 | + } catch (Error $e) { |
| 104 | + http_response_code(500); |
| 105 | + header('Content-Type: text/plain; charset=utf-8'); |
| 106 | + print("Internal Server Error\n\n"); |
| 107 | + print("The server encountered an internal error and was unable to complete your request.\n"); |
| 108 | + print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
| 109 | + print("More details can be found in the webserver log.\n"); |
50 | 110 |
|
51 | | - //show the user a detailed error page |
| 111 | + throw $ex; |
| 112 | + } |
52 | 113 | Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500); |
53 | 114 | } |
54 | | -} catch (LoginException $ex) { |
55 | | - $request = Server::get(IRequest::class); |
56 | | - /** |
57 | | - * Routes with the @CORS annotation and other API endpoints should |
58 | | - * not return a webpage, so we only print the error page when html is accepted, |
59 | | - * otherwise we reply with a JSON array like the SecurityMiddleware would do. |
60 | | - */ |
61 | | - if (stripos($request->getHeader('Accept'), 'html') === false) { |
62 | | - http_response_code(401); |
63 | | - header('Content-Type: application/json; charset=utf-8'); |
64 | | - echo json_encode(['message' => $ex->getMessage()]); |
65 | | - exit(); |
66 | | - } |
67 | | - Server::get(ITemplateManager::class)->printErrorPage($ex->getMessage(), $ex->getMessage(), 401); |
68 | | -} catch (MaxDelayReached $ex) { |
69 | | - $request = Server::get(IRequest::class); |
70 | | - /** |
71 | | - * Routes with the @CORS annotation and other API endpoints should |
72 | | - * not return a webpage, so we only print the error page when html is accepted, |
73 | | - * otherwise we reply with a JSON array like the BruteForceMiddleware would do. |
74 | | - */ |
75 | | - if (stripos($request->getHeader('Accept'), 'html') === false) { |
76 | | - http_response_code(429); |
77 | | - header('Content-Type: application/json; charset=utf-8'); |
78 | | - echo json_encode(['message' => $ex->getMessage()]); |
79 | | - exit(); |
80 | | - } |
81 | | - http_response_code(429); |
82 | | - Server::get(ITemplateManager::class)->printGuestPage('core', '429'); |
83 | | -} catch (Exception $ex) { |
84 | | - Server::get(LoggerInterface::class)->error($ex->getMessage(), [ |
85 | | - 'app' => 'index', |
86 | | - 'exception' => $ex, |
87 | | - ]); |
| 115 | +}; |
88 | 116 |
|
89 | | - //show the user a detailed error page |
90 | | - Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500); |
91 | | -} catch (Error $ex) { |
92 | | - try { |
93 | | - Server::get(LoggerInterface::class)->error($ex->getMessage(), [ |
94 | | - 'app' => 'index', |
95 | | - 'exception' => $ex, |
96 | | - ]); |
97 | | - } catch (Error $e) { |
98 | | - http_response_code(500); |
99 | | - header('Content-Type: text/plain; charset=utf-8'); |
100 | | - print("Internal Server Error\n\n"); |
101 | | - print("The server encountered an internal error and was unable to complete your request.\n"); |
102 | | - print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
103 | | - print("More details can be found in the webserver log.\n"); |
| 117 | +if (function_exists('frankenphp_handle_request')) { |
| 118 | + $maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 0); |
| 119 | + for ($nbRequests = 0; !$maxRequests || $nbRequests < $maxRequests; ++$nbRequests) { |
| 120 | + $keepRunning = \frankenphp_handle_request($handler); |
104 | 121 |
|
105 | | - throw $ex; |
| 122 | + // Call the garbage collector to reduce the chances of it being triggered in the middle of a page generation |
| 123 | + gc_collect_cycles(); |
| 124 | + |
| 125 | + if (!$keepRunning) { |
| 126 | + break; |
| 127 | + } |
106 | 128 | } |
107 | | - Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500); |
| 129 | +} else { |
| 130 | + $handler(); |
108 | 131 | } |
0 commit comments