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

WIP: Fix session handling #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alexander-schranz
Copy link

@alexander-schranz alexander-schranz commented May 11, 2021

  1. clone branch
  2. install composer dependency
  3. run redis-server
  4. adopt database in .env file (.env.local does not work)
  5. install fixtures bin/console sulu:build dev --destroy
  6. adopt following vendor class:

/vendor/runtime/roadrunner-symfony-nyholm:

diff --git a/src/Runner.php b/src/Runner.php
index 3ae36ca..821ed47 100644
--- a/src/Runner.php
+++ b/src/Runner.php
@@ -8,7 +8,9 @@ use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
 use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
 use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
 use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
-use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Component\HttpFoundation\Cookie;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\Kernel;
 use Symfony\Component\HttpKernel\TerminableInterface;
 use Symfony\Component\Runtime\RunnerInterface;
 
@@ -22,12 +24,23 @@ class Runner implements RunnerInterface
     private $httpMessageFactory;
     private $psrFactory;
 
-    public function __construct(HttpKernelInterface $kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null)
+    /**
+     * @var array<string, mixed>
+     */
+    private $sessionOptions;
+
+    public function __construct(Kernel $kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null)
     {
         $this->kernel = $kernel;
         $this->psrFactory = new Psr7\Factory\Psr17Factory();
         $this->httpFoundationFactory = $httpFoundationFactory ?? new HttpFoundationFactory();
         $this->httpMessageFactory = $httpMessageFactory ?? new PsrHttpFactory($this->psrFactory, $this->psrFactory, $this->psrFactory, $this->psrFactory);
+
+        $kernel->boot();
+        $container = $kernel->getContainer();
+        /** @var array<string, mixed> $sessionOptions */
+        $this->sessionOptions = $container->getParameter('session.storage.options');
+        $kernel->shutdown();
     }
 
     public function run(): int
@@ -38,7 +51,76 @@ class Runner implements RunnerInterface
         while ($request = $worker->waitRequest()) {
             try {
                 $sfRequest = $this->httpFoundationFactory->createRequest($request);
+                $sfRequest->overrideGlobals(); // override $_COOKIE, $_SERVER and other variables
+
+                $sessionOptions = $this->sessionOptions;
+
+                // read session name from php or container config
+                $sessionName = $sessionOptions['name'] ?? session_name();
+                $cookieSessionId = $sfRequest->cookies->get($sessionName, '');
+
+                if ($cookieSessionId) {
+                    $_COOKIE[$sessionName] = $cookieSessionId;
+                }
+
+                // \session_id($cookieSessionId);
+                // \session_abort();
+                \session_name($sessionName);
+                \session_id($cookieSessionId);
+                $currentSessionId = \session_id();
+                $sessionStatus = \session_status();
+
+                $hasRequestSession = $sfRequest->hasSession();
+
+                /** @var Response $sfResponse */
                 $sfResponse = $this->kernel->handle($sfRequest);
+
+                if ($sfRequest->hasSession()) {
+                    $sessionId = \session_id();
+
+                    if ($sessionId && $sessionId !== $cookieSessionId) {
+                        $expires = 0;
+                        $lifetime = $sessionOptions['cookie_lifetime'] ?? null;
+                        if ($lifetime) {
+                            $expires = time() + $lifetime;
+                        }
+
+                        /*$sfResponse->setContent(
+                            '<pre>' .
+                            'Curren Session ID:  ' . $currentSessionId  . '<br/>' .
+                            'Cookie Session ID:  ' . $cookieSessionId  . '<br/>' .
+                            'Session State 0:    ' . (\json_encode($sfRequest->getSession()->getBag('flashes')->peekAll()) ?: '') . '<br/>' .
+                            'Session State 1:    ' . $sfResponse->headers->get('X-SESSION-STATE', '') . '<br/>' .
+                            'New Session ID:     ' . $sessionId . '<br/>' .
+                            'Session Status:     ' . ($sessionStatus === \PHP_SESSION_ACTIVE ? 'active' : 'none') . '<br/>' .
+                            'Has Session:        ' . ($hasRequestSession ? 'true' : 'false') . '<br/>' .
+                            '</pre>'
+                        );*/
+
+                        
+                        $sfResponse->headers->setCookie(
+                            Cookie::create(
+                                $sessionName,
+                                $sessionId,
+                                $expires,
+                                $sessionOptions['cookie_path'] ?? '/',
+                                $sessionOptions['cookie_domain'] ?? null,
+                                $sessionOptions['cookie_secure'] ?? null,
+                                $sessionOptions['cookie_httponly'] ?? true,
+                                false,
+                                $sessionOptions['cookie_samesite'] ?? Cookie::SAMESITE_LAX,
+                            )
+                        );
+                        
+                    }
+                }
+
+                $sfResponse->headers->set('X-SESSION-STATUS', session_status() === \PHP_SESSION_ACTIVE ? 'active' : 'none');
+                $sfResponse->headers->set('X-SESSION-ID', $sessionId ?? '');
+                $sfResponse->headers->set('X-REQUEST-SESSION-ID', $requestSessionId ?? '');
+                $sfResponse->headers->set('X-COOKIE-SESSION-ID', $cookieSessionId ?? '');
+                $sfResponse->headers->set('X-Has-Session', $sfRequest->hasSession() ? 'true' : 'false');
+
                 $worker->respond($this->httpMessageFactory->createResponse($sfResponse));
 
                 if ($this->kernel instanceof TerminableInterface) {
  1. run roadrunner:
bin/rr serve -c .rr.admin.yaml

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

Successfully merging this pull request may close these issues.

1 participant