public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = (new ResponseFactory)->createResponse();

        $analyzer = CorsAnalyzer::instance($this->buildSettings($request, $response));
        if ($this->logger) {
            $analyzer->setLogger($this->logger);
        }
        $cors = $analyzer->analyze($request);

        switch ($cors->getRequestType()) {
            case CorsAnalysisResultInterface::ERR_ORIGIN_NOT_ALLOWED:
                $response = $response->withStatus(401);
                return $this->processError($request, $response, [
                    "message" => "CORS request origin is not allowed.",
                ]);
            case CorsAnalysisResultInterface::ERR_METHOD_NOT_SUPPORTED:
                $response = $response->withStatus(401);
                return $this->processError($request, $response, [
                    "message" => "CORS requested method is not supported.",
                ]);
            case CorsAnalysisResultInterface::ERR_HEADERS_NOT_SUPPORTED:
                $response = $response->withStatus(401);
                return $this->processError($request, $response, [
                    "message" => "CORS requested header is not allowed.",
                ]);
            case CorsAnalysisResultInterface::TYPE_PRE_FLIGHT_REQUEST:
                $cors_headers = $cors->getResponseHeaders();
                foreach ($cors_headers as $header => $value) {
                    /* Diactoros errors on integer values. */
                    if (false === is_array($value)) {
                        $value = (string)$value;
                    }
                    $response = $response->withHeader($header, $value);
                }
                return $response->withStatus(200);
            case CorsAnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE:
                return $handler->handle($request);
            default:
                /* Actual CORS request. */
                $response = $handler->handle($request);
                $cors_headers = $cors->getResponseHeaders();
                foreach ($cors_headers as $header => $value) {
                    /* Diactoros errors on integer values. */
                    if (false === is_array($value)) {
                        $value = (string)$value;
                    }
                    $response = $response->withHeader($header, $value);
                }
                return $response;
        }
    }
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = (new ResponseFactory)->createResponse();

        $analyzer = CorsAnalyzer::instance($this->buildSettings($request, $response));
        if ($this->logger) {
            $analyzer->setLogger($this->logger);
        }
        $cors = $analyzer->analyze($request);

        switch ($cors->getRequestType()) {
            case CorsAnalysisResultInterface::ERR_ORIGIN_NOT_ALLOWED:
                $response = $response->withStatus(401);
                return $this->processError($request, $response, [
                    "message" => "CORS request origin is not allowed.",
                ]);
            case CorsAnalysisResultInterface::ERR_METHOD_NOT_SUPPORTED:
                $response = $response->withStatus(401);
                return $this->processError($request, $response, [
                    "message" => "CORS requested method is not supported.",
                ]);
            case CorsAnalysisResultInterface::ERR_HEADERS_NOT_SUPPORTED:
                $response = $response->withStatus(401);
                return $this->processError($request, $response, [
                    "message" => "CORS requested header is not allowed.",
                ]);
            case CorsAnalysisResultInterface::TYPE_PRE_FLIGHT_REQUEST:
                $cors_headers = $cors->getResponseHeaders();
                foreach ($cors_headers as $header => $value) {
                    /* Diactoros errors on integer values. */
                    if (false === is_array($value)) {
                        $value = (string)$value;
                    }
                    $response = $response->withHeader($header, $value);
                }
                return $response->withStatus(200);
            case CorsAnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE:
                return $handler->handle($request);
            default:
                /* Actual CORS request. */
                $response = $handler->handle($request);
                $cors_headers = $cors->getResponseHeaders();
                foreach ($cors_headers as $header => $value) {
                    /* Diactoros errors on integer values. */
                    if (false === is_array($value)) {
                        $value = (string)$value;
                    }
                    $response = $response->withHeader($header, $value);
                }
                return $response;
        }
    }
public function process ( ServerRequestInterface $ request , RequestHandlerInterface $ handler ) : ResponseInterface { $ response = ( new ResponseFactory ) -> createResponse ( ) ; $ analyzer = CorsAnalyzer :: instance ( $ this -> buildSettings ( $ request , $ response ) ) ; if ( $ this -> logger ) { $ analyzer -> setLogger ( $ this -> logger ) ; } $ cors = $ analyzer -> analyze ( $ request ) ; switch ( $ cors -> getRequestType ( ) ) { case CorsAnalysisResultInterface :: ERR_ORIGIN_NOT_ALLOWED : $ response = $ response -> withStatus ( 401 ) ; return $ this -> processError ( $ request , $ response , [ "message" => "CORS request origin is not allowed." , ] ) ; case CorsAnalysisResultInterface :: ERR_METHOD_NOT_SUPPORTED : $ response = $ response -> withStatus ( 401 ) ; return $ this -> processError ( $ request , $ response , [ "message" => "CORS requested method is not supported." , ] ) ; case CorsAnalysisResultInterface :: ERR_HEADERS_NOT_SUPPORTED : $ response = $ response -> withStatus ( 401 ) ; return $ this -> processError ( $ request , $ response , [ "message" => "CORS requested header is not allowed." , ] ) ; case CorsAnalysisResultInterface :: TYPE_PRE_FLIGHT_REQUEST : $ cors_headers = $ cors -> getResponseHeaders ( ) ; foreach ( $ cors_headers as $ header => $ value ) { /* Diactoros errors on integer values. */ if ( false === is_array ( $ value ) ) { $ value = ( string ) $ value ; } $ response = $ response -> withHeader ( $ header , $ value ) ; } return $ response -> withStatus ( 200 ) ; case CorsAnalysisResultInterface :: TYPE_REQUEST_OUT_OF_CORS_SCOPE : return $ handler -> handle ( $ request ) ; default : /* Actual CORS request. */ $ response = $ handler -> handle ( $ request ) ; $ cors_headers = $ cors -> getResponseHeaders ( ) ; foreach ( $ cors_headers as $ header => $ value ) { /* Diactoros errors on integer values. */ if ( false === is_array ( $ value ) ) { $ value = ( string ) $ value ; } $ response = $ response -> withHeader ( $ header , $ value ) ; } return $ response ; } }
Execute as PSR-15 middleware.
Execute as PSR - 15 middleware .