@param ResponseInterface $response
@param SessionInterface $session

@return ResponseInterface
167993
private function serializeSession(ResponseInterface $response, SessionInterface $session)
    {
        if ($session->hasChanged()) {
            $serialized = call_user_func([$this->options['session_class'], 'toSerialized'], $session);

            $response = $this->handler->withCookie(
                $response,
                $this->options['cookie_name'],
                $serialized,
                $this->options['lifetime']
            );
        }

        return $response;
    }
927784
protected function finalizeResponse(ResponseInterface $response) : ResponseInterface
    {
        // stop PHP sending a Content-Type automatically
        ini_set('default_mimetype', '');
        if ($this->isEmptyResponseCode($response->getStatusCode())) {
            return $response->withoutHeader('Content-Type')->withoutHeader('Content-Length');
        }

        return $response;
    }
717661
private function processResponse(ResponseInterface $response)
    {
        if (200 !== $response->getStatusCode()) {
            throw new \RuntimeException('Query to the API server did not result in an appropriate response code');
        }

        $body = guzzle_json_decode((string) $response->getBody(), true);

        if ('OK' !== $body['error']) {
            throw new \RuntimeException('Query to the API server did not result in an appropriate response code');
        }

        return $body;
    }
170255
public function execute(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface {
		$httpRequestResponse = new HTTPRequestResponseContainer($request, $response);

		$this->dispatchRequestFilters($httpRequestResponse);

		$this->dispatchResponseFilters($httpRequestResponse);

		return $httpRequestResponse->getResponse();
	}
1280616
private function addCookieToResponse(Request $request, Response $response, SessionInterface $session): Response
    {
        $uri = $request->getUri();
        $path = '/';
        $domain = $uri->getHost();
        $secure = strtolower($uri->getScheme()) === 'https';
        $httpOnly = true;
        return $response->withCookie(new Cookie($session->getName(), $session->getId(), $this->getCookieExpirationDate(), $path, $domain, $secure, $httpOnly));
    }
97584
private function _getSubjectFromSession(Session $session)
    {
        $subject = Subject::createBuilder()
            ->principals($session->getAttribute(DefaultSubjectContext::PRINCIPALS_SESSION_KEY))
            ->authenticated($session->getAttribute(DefaultSubjectContext::AUTHENTICATED_SESSION_KEY))
            ->authorization($session->getAttribute(DefaultSubjectContext::AUTHORIZATION_SESSION_KEY))
            ->session($session)
            ->build();
        return $subject;
    }
739253
public function modifyResponse(Request $request, Response $response)
    {
        if ($this->isCollect() === false) {
            return;
        }

        // if ($this -> created) {
        //     return;
        // }

        $app = $this->app;


        if ($app->runningInConsole() || !$this->isEnabled() || $this->isMyselfRequest()) {
            return $response;
        }

        // Show the Http Response Exception, when available
        if (isset($response->exception)) {
            $this->addThrowable($response->exception);
        }

        /**---------------------------------------------------------
         *   Display config settings
         *---------------------------------------------------------*/

        if ($this->shouldCollect('config', false)) {
            try {
                $configCollector = new ConfigCollector();
                $configCollector->setData($app['config']->all());
                $this->addCollector($configCollector);
            } catch (\Exception $e) {
                $this->addThrowable(
                    new Exception(
                        'Cannot add ConfigCollector to SongshenzongLog: ' . $e->getMessage(),
                        $e->getCode(),
                        $e
                    )
                );
            }
        }


        if ($this->app->bound(SessionManager::class)) {

            /** @var \Illuminate\Session\SessionManager $sessionManager */
            $sessionManager = $app->make(SessionManager::class);
            $httpDriver     = new SymfonyHttpDriver($sessionManager, $response);
            $this->setHttpDriver($httpDriver);

            if ($this->shouldCollect('session') && !$this->hasCollector('session')) {
                try {
                    $this->addCollector(new SessionCollector($sessionManager));
                } catch (\Exception $e) {
                    $this->addThrowable(
                        new Exception(
                            'Cannot add SessionCollector to SongshenzongLog: ' . $e->getMessage(),
                            $e->getCode(),
                            $e
                        )
                    );
                }
            }
        } else {
            $sessionManager = null;
        }


        /**---------------------------------------------------------
         *   Only one can be enabled.
         *---------------------------------------------------------*/
        if ($this->shouldCollect('request', true) && !$this->hasCollector('request')) {
            try {
                $this->addCollector(new RequestCollector($request, $response, $sessionManager));
            } catch (\Exception $e) {
                $this->addThrowable(
                    new Exception(
                        'Cannot add RequestCollector to SongshenzongLog: ' . $e->getMessage(),
                        $e->getCode(),
                        $e
                    )
                );
            }
        }


        /**---------------------------------------------------------
         *   Just collect + store data
         *---------------------------------------------------------*/
        try {
            $this->collect();
        } catch (\Exception $e) {
            $app['log']->error('Songshenzong exception: ' . $e->getMessage());
        }


        return $response;
    }
137337
private function decodeResponse(ResponseInterface $response): ResponseInterface
    {
        $response = $this->decodeOnEncodingHeader('Transfer-Encoding', $response);

        if ($this->useContentEncoding) {
            $response = $this->decodeOnEncodingHeader('Content-Encoding', $response);
        }

        return $response;
    }
685682
public function send(DispatcherResponseInterface $response)
    {
        if($response->isRedirect())
        {
            $session  = $response->getUser()->getSession();

            //Set the messages into the session
            $messages = $response->getMessages();
            if(count($messages))
            {
                //Auto start the session if it's not active.
                if(!$session->isActive()) {
                    $session->start();
                }

                $session->getContainer('message')->add($messages);
            }

            //Set the redirect into the response
            $format = $response->getRequest()->getFormat();
            if($format == 'json')
            {
                array_unshift($messages, sprintf('Redirecting to %1$s', $response->getHeaders()->get('Location')));

                $response->setContent(json_encode(array(
                    'messages' => $messages
                )), 'application/json');
            }

            if($format == 'html')
            {
                $response->setContent(sprintf(
                    '<!DOCTYPE html>
                        <html>
                            <head>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                                <noscript>
                                    <meta http-equiv="refresh" content="1;url=%1$s" />
                                </noscript>
                                <title>Redirecting to %1$s</title>
                            </head>
                            <body onload="window.location = \'%1$s\'">
                                Redirecting to <a href="%1$s">%1$s</a>.
                            </body>
                        </html>'
                    , htmlspecialchars($response->getHeaders()->get('Location'), ENT_QUOTES, 'UTF-8')
                ), 'text/html');
            }

            return parent::send($response);
        }
    }
975776
public function build(ResponseInterface $response)
    {
        $rawData = json_decode((string)$response->getBody(), true) ?: [];
        $data = ModelHelper::camelize($rawData);
        $data['data'] = ArrayHelper::getValue($rawData, 'data', []);

        if ($auth = ArrayHelper::getValue($data, 'auth')) {
            $data['auth'] = new Auth($auth);
        }

        return new Response($data);
    }
881059
protected function getBody( ResponseInterface $response ) {
		$body = $response->getBody();
		if ( $body->isSeekable() ) {
			$body->rewind();
		}
		return $body;
	}
403847
public function process(Request $request, RequestHandlerInterface $delegate) : Response
    {
        $token            = $this->parseToken($request);
        $sessionContainer = LazySession::fromContainerBuildingCallback(function () use ($token) : SessionInterface {
            return $this->extractSessionContainer($token);
        });

        return $this->appendToken(
            $sessionContainer,
            $delegate->handle($request->withAttribute(self::SESSION_ATTRIBUTE, $sessionContainer)),
            $token
        );
    }
1247273
public function handleResponse(ServerRequestInterface $request, ResponseInterface $response)
    {
        $applicationEvent = $this->getApplicationEvent();
        $applicationEvent->setName(self::EVENT_RESPONSE_CREATED);
        $applicationEvent->setResponse($this->getRouter()->dispatch(
            $request,
            $response
        ));

        $this->emit($applicationEvent, $request, $response);

        return $applicationEvent->getResponse();
    }
147883
private function abortSessionUsingResponse(Session $session, $response)
    {
        // $response needs to be a failure
        if (!isset($response[0]) || $response[0] !== 'FAILURE') {
            return false;
        }

        if (!isset($response[1]) || !is_object($response[1])) {
            // there are no other details to send - just fail it
            $session->abort(new \stdClass(), 'thruway.error.authentication_failure');
            return true;
        }

        $details = new \stdClass();
        if (isset($response[1]->details) && is_object($response[1]->details)) {
            $details = $response[1]->details;
        }

        $abortUri = 'thruway.error.authentication_failure';
        if (isset($response[1]->abort_uri) && is_scalar($response[1]->abort_uri)) {
            $abortUri = $response[1]->abort_uri;
        }

        $session->abort($details, $abortUri);
        return true;
    }
491322
public function renderIntoSetCookieHeader(ResponseInterface $response) : ResponseInterface
    {
        $response = $response->withoutHeader(static::SET_COOKIE_HEADER);
        foreach ($this->setCookies as $setCookie) {
            $response = $response->withAddedHeader(static::SET_COOKIE_HEADER, (string) $setCookie);
        }

        return $response;
    }
885054
public static function fromResponse(ResponseInterface $response, $message = null)
    {
        throw new static($message, $response->getStatusCode(), (string) $response->getBody());
    }
1012111
private function processHttpOkResponse(ResponseInterface $response)
    {
        $body = $this->getBodyAsArray($response);

        if (isset($body['error'])) {
            $response = $this->processHttpOkResponseWithError($body);
        } else {
            $response = $this->processHttpOkResponseWithoutError($body);
        }

        return $response;
    }