Skip to content

Commit

Permalink
Remove some usages of Glpi\Http\Response::send()
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne authored and trasher committed Nov 6, 2024
1 parent 36f8cee commit 72d4893
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 37 deletions.
11 changes: 4 additions & 7 deletions ajax/criteria_filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use Glpi\Exception\Http\BadRequestHttpException;
use Glpi\Exception\Http\NotFoundHttpException;
use Glpi\Exception\Http\UnprocessableEntityHttpException;
use Glpi\Http\Response;
use Glpi\Search\FilterableInterface;

// Read endpoint
Expand Down Expand Up @@ -83,9 +82,8 @@
throw new UnprocessableEntityHttpException('Unable to process data');
}

// OK
(new Response(200))->send();
break;
// Send empty response when OK
return;

case "delete_filter":
// Default values for this endpoint
Expand Down Expand Up @@ -117,7 +115,6 @@
throw new UnprocessableEntityHttpException('Unable to process data');
}

// OK
(new Response(200))->send();
break;
// Send empty response when OK
return;
}
1 change: 0 additions & 1 deletion ajax/stencil.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
*/

use Glpi\Exception\Http\NotFoundHttpException;
use Glpi\Http\Response;

if (isset($_POST['id'])) {
$stencil = Stencil::getStencilFromID($_POST['id']);
Expand Down
1 change: 0 additions & 1 deletion ajax/task.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
*/

use Glpi\Exception\Http\BadRequestHttpException;
use Glpi\Http\Response;

/** @var \Glpi\Controller\LegacyFileLoadController $this */
$this->setAjax();
Expand Down
28 changes: 11 additions & 17 deletions src/Glpi/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use Glpi\Http\HeaderlessStreamedResponse;
use Glpi\Http\JSONResponse;
use Glpi\Http\Request;
use Glpi\Http\Response;
use Glpi\Security\Attribute\SecurityStrategy;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
Expand All @@ -60,15 +59,6 @@ public function __invoke(SymfonyRequest $request): SymfonyResponse
{
$_SERVER['PATH_INFO'] = $request->get('request_parameters');

return new HeaderlessStreamedResponse($this->call(...));
}

private function call(): void
{
/**
* High-level API entrypoint
*/

// Ensure errors will not break API output.
ErrorHandler::getInstance()->disableOutput();

Expand All @@ -79,10 +69,10 @@ private function call(): void

// If the relative URI starts with /v1/ or is /v1 then we are dealing with a legacy API request
if (preg_match('/^\/v1(\/|$)/', $relative_uri)) {
// Include the legacy API entrypoint and then die
$api = new \Glpi\Api\APIRest();
$api->call();
return;
return new HeaderlessStreamedResponse(function () {
$api = new \Glpi\Api\APIRest();
$api->call();
});
}

$supported_versions = Router::getAPIVersions();
Expand All @@ -104,7 +94,6 @@ private function call(): void

try {
$response = $router->handleRequest($request);
$response->send();
} catch (\InvalidArgumentException $e) {
$response = new JSONResponse(
ApiAbstractController::getErrorResponseBody(
Expand All @@ -115,8 +104,13 @@ private function call(): void
);
} catch (\Throwable $e) {
ErrorHandler::getInstance()->handleException($e, true);
$response = new Response(500);
$response->send();
$response = new JSONResponse(null, 500);
}

return new SymfonyResponse(
$response->getBody()->getContents(),
$response->getStatusCode(),
$response->getHeaders()
);
}
}
19 changes: 9 additions & 10 deletions src/Glpi/Controller/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
use Session;
use Glpi\Api\HL\Router;
use Glpi\Application\ErrorHandler;
use Glpi\Http\HeaderlessStreamedResponse;
use Glpi\Http\JSONResponse;
use Glpi\Http\Request;
use Glpi\Http\Response;
use Glpi\Security\Attribute\SecurityStrategy;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
Expand All @@ -53,24 +52,24 @@ final class StatusController extends AbstractController
)]
#[SecurityStrategy('no_check')]
public function __invoke(SymfonyRequest $request): SymfonyResponse
{
return new HeaderlessStreamedResponse($this->call(...));
}

private function call(): void
{
// Force in normal mode
$_SESSION['glpi_use_mode'] = Session::NORMAL_MODE;

// Redirect handling to the High-Level API (we may eventually remove this script)
$request = new Request('GET', '/Status/All', getallheaders() ?? []);

try {
$response = Router::getInstance()->handleRequest($request);
$response->send();
} catch (\Throwable $e) {
ErrorHandler::getInstance()->handleException($e, true);
$response = new Response(500);
$response->send();
$response = new JSONResponse(null, 500);
}

return new SymfonyResponse(
$response->getBody()->getContents(),
$response->getStatusCode(),
$response->getHeaders()
);
}
}
1 change: 0 additions & 1 deletion src/QueuedWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use Glpi\Application\View\TemplateRenderer;
use Glpi\DBAL\QueryExpression;
use Glpi\DBAL\QueryFunction;
use Glpi\Http\Response;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
Expand Down

0 comments on commit 72d4893

Please sign in to comment.