Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark cors preflight requests #245

Open
olegbaturin opened this issue May 25, 2024 · 3 comments
Open

Mark cors preflight requests #245

olegbaturin opened this issue May 25, 2024 · 3 comments

Comments

@olegbaturin
Copy link

Mark request as cors preflight here

static fn (ResponseFactoryInterface $responseFactory) => $responseFactory->createResponse(204)

Maybe
$request->withAttribute('preflight', true);

@samdark
Copy link
Member

samdark commented May 25, 2024

Would you please elaborate?

@olegbaturin
Copy link
Author

olegbaturin commented May 25, 2024

I want to make a CorsMiddleware with logic:

public function process() {
   $response = $handler->handle($request);

   if (cors preflight) {
       do cors things with $response
   }
}

I see that $request is immutable, so maybe it's possible to mark the Response?

@olegbaturin
Copy link
Author

Another solution is to pass the CorsState object to $resuest and then modify it in the cors action.

final class CorsState {
    private bool $preflight = false;

    public function getPreflight()
    {
        return $this->preflight;
    }

    public function setPreflight()
    {
        $this->preflight = true;
    }
}

In the CorsMiddleware

public function process() {
   $corsState = new CorsState();
   $request = $request->withAttribute('preflight', $corsState);
   $response = $handler->handle($request);

   if ($corsState->getPreflight()) {
       do cors things with $response
   }
}

In the cors action:

if ($request->hasAttribute('preflight')) {
    $request->getAttribute('preflight')->setPreflight();
}
return $responseFactory->createResponse(204);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants