Transforms a Symfony request into a PSR-7 request object

@param \Illuminate\Http\Request $nativeRequest Laravel request object
@return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
514321
protected function createRequest( \Illuminate\Http\Request $nativeRequest )
	{
		$files = ServerRequestFactory::normalizeFiles( $this->getFiles( $nativeRequest->files->all() ) );
		$server = ServerRequestFactory::normalizeServer( $nativeRequest->server->all() );
		$headers = $nativeRequest->headers->all();
		$cookies = $nativeRequest->cookies->all();
		$post = $nativeRequest->request->all();
		$query = $nativeRequest->query->all();
		$method = $nativeRequest->getMethod();
		$uri = $nativeRequest->getUri();

		$body = new Stream( 'php://temp', 'wb+' );
		$body->write( $nativeRequest->getContent() );

		$request = new ServerRequest( $server, $files, $uri, $method, $body, $headers, $cookies, $query, $post );

		foreach( $nativeRequest->attributes->all() as $key => $value ) {
			$request = $request->withAttribute( $key, $value );
		}

		return $request;
	}
853992
protected function reviveRequest(ServerRequestInterface $request)
    {
        $isStale = interface_exists('Jasny\HttpMessage\GlobalEnvironmentInterface') &&
            $request instanceof GlobalEnvironmentInterface &&
            $request->isStale();
        
        return $isStale ? $request->revive() : $request;
    }
732102
public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $stack = new Stack(
            $this->handlers,
            $this->handlerResolver
        );
        return $stack->handle($request);
    }
951454
public function callback(ServerRequestInterface $request)
    {
        $response = new Response(200, [
            'Content-Type' => 'text/plain; charset=utf-8',
        ], '');

        return new Promise(function ($resolve, $reject) use (&$request, &$response) {
            try {
                $context = new Context($request, $response);
                $context->setLogger($this->getLogger());

                /**
                 * @var HttpBodyStream $body
                 */
                $body = $request->getBody();
                $body->on('data', function ($data) use (&$request, &$context) {
                    if (!$data) return;
                    $method = $request->getMethod();
                    $contentType = $request->getHeader('Content-Type');
                    if (isset($contentType[0])
                        && (strcasecmp($method, 'POST') === 0 || strcasecmp($method, 'PUT') === 0)
                    ) {
                        if (strpos(strtolower($contentType[0]), 'application/x-www-form-urlencoded') !== false) {
                            parse_str($data, $parsedBody);
                            $request = $request->withParsedBody($parsedBody);
                            $context->setRequest($request);
                        } else if (strpos(strtolower($contentType[0]), 'application/json') !== false) {
                            $parsedBody = json_decode($data, JSON_OBJECT_AS_ARRAY);
                            $request = $request->withParsedBody($parsedBody);
                            $context->setRequest($request);
                        }
                    }
                    $context->setAttribut('httpRawData', $data);
                });
                $body->on('end', function () use (&$context, $resolve) {
                    co(...$this->middlewares)($context);
                    $resolve($context->getReturn());
                });
                $body->on('error', function ($reason) use ($reject) {
                    $reject($reason);
                });
            } catch (\Throwable $exception) {
                // 触发错误异常处理中间件
                $this->onException($exception, $context);
                $rsp = $context->getResponse();
                if ($rsp === $response) {
                    $rsp = $response->withStatus(500);
                    $response->getBody()->write('Internal Server Error');
                }
                $resolve($rsp);
            }
        });
    }
1293086
public function handle(ServerRequestInterface $request): ResponseInterface
    {
        if (!isset($this->middlewaresInPipe[$this->currentMiddlewareInPipeIndex])) {
            return $this->generate404($request);
        }

        $middleware = $this->getMiddlewareInPipe();
        if (\is_callable($middleware)) {
            return \call_user_func_array($middleware, [$request, [$this, 'handle']]);
        } elseif ($middleware instanceof MiddlewareInterface) {
            return $middleware->process($request, $this);
        } elseif (\is_string($middleware)) {
            $middlewareInstance = (new $middleware());
            if (\method_exists($middlewareInstance, 'process')) {
                return $middlewareInstance->process($request, $this);
            }
        } elseif ($middleware instanceof self) {
            if ($middleware->findRouteRequest($request)) {
                return $middleware->dispatch($request);
            }

            return $this->handle($request);
        }
        throw new RouterException(\sprintf('Middleware is invalid: %s', \gettype($middleware)));
    }
1259445
public static function requestIsNotValid($request)
    {
        return new self(sprintf(
            'The request must be a string, an array or implement "Psr\Http\Message\RequestInterface" ("%s" given).',
            is_object($request) ? get_class($request) : gettype($request)
        ));
    }
820051
protected function mapRequest(ServerRequestInterface $psrRequest)
    {
        $method = $psrRequest->getMethod();
        $query = $psrRequest->getQueryParams();

        // cookies
        $_COOKIE = [];

        foreach ($psrRequest->getHeader('Cookie') as $cookieHeader) {
            $cookies = explode(';', $cookieHeader);

            foreach ($cookies as $cookie) {
                if (strpos($cookie, '=') == false) {
                    continue;
                }
                list($name, $value) = explode('=', trim($cookie));
                $_COOKIE[$name] = $value;

                if ($name === session_name()) {
                    session_id($value);
                }
            }
        }

        /** @var \React\Http\Io\UploadedFile $file */
        $uploadedFiles = $psrRequest->getUploadedFiles();

        $this->mapFiles($uploadedFiles);

        // @todo check howto handle additional headers
        // @todo check howto support other HTTP methods with bodies
        $post = $psrRequest->getParsedBody() ?: [];

        if ($this->bootstrap instanceof RequestClassProviderInterface) {
            $class = $this->bootstrap->requestClass();
        } else {
            $class = SymfonyRequest::class;
        }

        /** @var SymfonyRequest $syRequest */
        $syRequest = new $class($query, $post, $attributes = [], $_COOKIE, $uploadedFiles, $_SERVER, (string)$psrRequest->getBody());

        $syRequest->setMethod($method);

        if ($syRequest instanceof \Illuminate\Http\Request && $syRequest->isJson()) {
            $syRequest->request = $syRequest->json();
        }

        return $syRequest;
    }
40191
public function authenticate(HTTPRequest $request): HTTPRequest
    {
        $requestAdapter = new HttpRequestAdapter();
        $responseAdapter = new HttpResponseAdapter();

        // missing vars (cli)
        $this->addMissingServerVariables($requestAdapter);

        $server = $this->getServer();
        $psrRequest = $requestAdapter->toPsr7($request);
        $psrResponse = new Response();

        try {
            $psrRequest = $server->validateAuthenticatedRequest($psrRequest);
        } catch (OAuthServerException $exception) {
            // convert to authentication exception
            throw new AuthenticationException(
                $exception->getMessage(),
                $exception->getCode(),
                $responseAdapter->fromPsr7($exception->generateHttpResponse($psrResponse))
            );
        } catch (\Exception $exception) {
            // convert to authentication exception
            throw new AuthenticationException(
                $exception->getMessage(),
                $exception->getCode(),
                $responseAdapter->fromPsr7(
                    (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500))
                        ->generateHttpResponse($psrResponse)
                )
            );
        }
        // add the request attributes as custom auth headers
        foreach ($psrRequest->getAttributes() as $attribute => $value) {
            $request->addHeader($attribute, $value);
        }

        return $request;
    }
1103657
public function edit(ServerRequestInterface $request): ResponseInterface
    {
        $user_id = (int) $request->getQueryParams()['user_id'];
        $user    = $this->user_service->find($user_id);

        if ($user === null) {
            throw new NotFoundHttpException(I18N::translate('%1$s does not exist.', 'user_id:' . $user_id));
        }

        return $this->viewResponse('admin/users-edit', [
            'contact_methods' => FunctionsEdit::optionsContactMethods(),
            'default_locale'  => WT_LOCALE,
            'locales'         => I18N::installedLocales(),
            'roles'           => $this->roles(),
            'trees'           => Tree::getAll(),
            'theme_options'   => $this->themeOptions(),
            'title'           => I18N::translate('Edit the user'),
            'user'            => $user,
        ]);
    }
711637
public function legacy(common_http_Request $pRequest)
    {
        $request = ServerRequest::fromGlobals();
        $response = new Response();
        $this($request, $response);
    }
963927
public static function convert(ServerRequestInterface $serverRequest)
    {
        $stream = fopen('php://temp', 'rw');

        foreach ($serverRequest->getHeaders() as $key => $values) {
            foreach ($values as $value) {
                fwrite($stream, "$key: $value\r\n");
            }
        }
        fwrite($stream, "\r\n");

        $body = $serverRequest->getBody();
        $body->rewind();

        while (!$body->eof()) {
            fwrite($stream, $body->read(1024));
        }

        rewind($stream);

        return new StreamedPart($stream);
    }
959265
public function replace(ServerRequestInterface $request): ResponseInterface
    {
        $search  = $request->get('search', '');
        $replace = $request->get('replace', '');
        $context = $request->get('context', '');

        if ($context !== 'name' && $context !== 'place') {
            $context = 'all';
        }

        $title = I18N::translate('Search and replace');

        return $this->viewResponse('search-replace-page', [
            'context' => $context,
            'replace' => $replace,
            'search'  => $search,
            'title'   => $title,
        ]);
    }
1086915
public function create(ServerRequestInterface $request): ResponseInterface
    {
        $tree_name  = $request->get('tree_name', '');
        $tree_title = $request->get('tree_title', '');

        // We use the tree name as a file name, so no directory separators allowed.
        $tree_name = basename($tree_name);

        if (Tree::findByName($tree_name)) {
            FlashMessages::addMessage(I18N::translate('The family tree “%s” already exists.', e($tree_name)), 'danger');

            $url = route('admin-trees');
        } else {
            $tree = Tree::create($tree_name, $tree_title);
            FlashMessages::addMessage(I18N::translate('The family tree “%s” has been created.', e($tree->name())), 'success');

            $url = route('admin-trees', ['ged' => $tree->name()]);
        }

        return redirect($url);
    }
1015579
private function transformRequest(\swoole_http_request $swooleRequest)
    {
        return $this->psr7Factory->createRequest(
            $this->transformUri($swooleRequest),
            $this->transformRequestMethod($swooleRequest),
            $this->transformBody($swooleRequest),
            $this->transformHeader($swooleRequest)
        )
            ->withProtocolVersion($this->transformProtocolVersion($swooleRequest))
        ;
    }
725965
private function createRequest(InternalRequestInterface $internalRequest)
    {
        return new Request(
            $internalRequest->getMethod(),
            $internalRequest->getUri(),
            $this->prepareHeaders($internalRequest),
            $this->prepareBody($internalRequest),
            $internalRequest->getProtocolVersion()
        );
    }
722838
public function setRequest(\Psr\Http\Message\ServerRequestInterface $p_request)
    {
        $this->request = $p_request;
        return $this;
    }
371365
private function transformUri(ReactRequest $reactRequest)
    {
        $headers = $reactRequest->getHeaders();

        //Scheme
        if (isset($headers['X-Forwarded-Proto'])) {
            $scheme = $headers['X-Forwarded-Proto'];
        } elseif (isset($headers['Https']) && 'off' !== $headers['Https']) {
            $scheme = 'https';
        } else {
            $scheme = 'http';
        }

        //Host
        if (isset($headers['X-Forwarded-Host'])) {
            $hostAndPort = $headers['X-Forwarded-Host'];
        } elseif (isset($headers['Host'])) {
            $hostAndPort = $headers['Host'];
        } else {
            $hostAndPort = 'localhost';
        }

        return $scheme.'://'.$hostAndPort.$this->transformRequestUri($reactRequest);
    }