You are here

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

Same name and namespace in other branches
  1. 8 src/Controller/HttpController.php \Drupal\jsonrpc\Controller\HttpController::resolve()

Resolves an RPC request over HTTP.

Parameters

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

Return value

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

1 string reference to 'HttpController::resolve'
jsonrpc.routing.yml in ./jsonrpc.routing.yml
jsonrpc.routing.yml

File

src/Controller/HttpController.php, line 115

Class

HttpController
The main front controller.

Namespace

Drupal\jsonrpc\Controller

Code

public function resolve(Request $http_request) {

  // Handle preflight requests.
  if ($http_request
    ->getMethod() === Request::METHOD_OPTIONS) {
    return $this
      ->preflight($http_request);
  }

  // Map the HTTP request to an RPC request.
  try {
    $rpc_requests = $this
      ->getRpcRequests($http_request);
  } catch (JsonRpcException $e) {
    return $this
      ->exceptionResponse($e, Response::HTTP_BAD_REQUEST);
  }

  // Execute the RPC request and get the RPC response.
  try {
    $rpc_responses = $this
      ->getRpcResponses($rpc_requests);

    // Aggregate the response headers so we can add them to the HTTP response.
    $header_bag = $this
      ->aggregateResponseHeaders($rpc_responses);

    // If no RPC response(s) were generated (happens if all of the request(s)
    // were notifications), then return a 204 HTTP response.
    if (empty($rpc_responses)) {
      $response = CacheableJsonResponse::create(NULL, Response::HTTP_NO_CONTENT);
      $response->headers
        ->add($header_bag
        ->all());
      return $response;
    }

    // Map the RPC response(s) to an HTTP response.
    $is_batched_response = count($rpc_requests) !== 1 || $rpc_requests[0]
      ->isInBatch();
    $response = $this
      ->getHttpResponse($rpc_responses, $is_batched_response);
    assert($response instanceof Response);
    $response->headers
      ->add($header_bag
      ->all());
    return $response;
  } catch (JsonRpcException $e) {
    return $this
      ->exceptionResponse($e, Response::HTTP_INTERNAL_SERVER_ERROR);
  }
}