You are here

public function HttpController::preflight in JSON-RPC 2.x

Resolves a preflight request for an RPC request.

Parameters

\Symfony\Component\HttpFoundation\Request $http_request: The HTTP request.

Return value

\Drupal\Core\Cache\CacheableResponseInterface The HTTP response.

1 call to HttpController::preflight()
HttpController::resolve in src/Controller/HttpController.php
Resolves an RPC request over HTTP.

File

src/Controller/HttpController.php, line 77

Class

HttpController
The main front controller.

Namespace

Drupal\jsonrpc\Controller

Code

public function preflight(Request $http_request) {
  try {
    $rpc_requests = $this
      ->getRpcRequests($http_request);
  } catch (JsonRpcException $e) {
    return $this
      ->exceptionResponse($e, Response::HTTP_BAD_REQUEST);
  }
  $response_headers = array_reduce($rpc_requests, function (?HeaderBag $carry, RpcRequest $request) {
    $headers = $this->handler
      ->getMethod($request
      ->getMethod())->responseHeaders;
    $intersected_headers = $carry ? array_intersect_key($carry
      ->all(), $headers) : $headers;
    return new HeaderBag($intersected_headers);
  });
  $http_response = CacheableJsonResponse::create(NULL, Response::HTTP_NO_CONTENT, $response_headers
    ->all());

  // Make sure to provide allowed methods.
  $http_response->headers
    ->add([
    'allow' => [
      'OPTIONS, GET, HEAD, POST',
    ],
  ]);

  // Varies the response based on the 'query' parameter.
  $cache_context = (new CacheableMetadata())
    ->setCacheContexts([
    'url.query_args:query',
    'headers:origin',
  ]);
  $http_response
    ->addCacheableDependency($cache_context);
  return $http_response;
}