Handle classic CORS request to avoid duplicate code.

@param string $type            the kind of headers we would handle
@param array  $requestHeaders  CORS headers request by client
@param array  $responseHeaders CORS response headers sent to the client
517546
protected function prepareAllowHeaders($type, $requestHeaders, &$responseHeaders)
    {
        $requestHeaderField = 'Access-Control-Request-'.$type;
        $responseHeaderField = 'Access-Control-Allow-'.$type;
        if (!isset($requestHeaders[$requestHeaderField], $this->_cors[$requestHeaderField])) {
            return;
        }
        if (isset($this->_cors[$requestHeaderField]) && in_array('*', $this->_cors[$requestHeaderField])) {
            $responseHeaders[$responseHeaderField] = $this->headerize($requestHeaders[$requestHeaderField]);
        } else {
            $requestedData = preg_split('/[\\s,]+/', $requestHeaders[$requestHeaderField], -1, PREG_SPLIT_NO_EMPTY);
            $acceptedData = array_uintersect($requestedData, $this->_cors[$requestHeaderField], 'strcasecmp');
            if (!empty($acceptedData)) {
                $responseHeaders[$responseHeaderField] = implode(', ', $acceptedData);
            }
        }
    }
373446
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
    {
        $response = parent::handle($request);
        if ($this->corsEnabled) {
            $response->headers->set('Access-Control-Allow-Origin', '*');
            $response->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
            $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, X-Requested-With');
        }
        return $response;
    }
362513
public static function setResponseHeaders($headers)
    {
        foreach ($headers as $name => $headerValues) {
            $header = $name . ": " . implode(", ", $headerValues);
            header($header);
        }
    }
41395
public function setResponder($type, ResponderInterface $responder)
    {
        if (!is_string($type) || empty($type)) {
            throw new \InvalidArgumentException('The responder type is invalid');
        }

        $this->responders[$type] = $responder;
    }
1081428
private function send(Response $response, $type = self::MASTER_REQUEST)
    {
        if (null !== $this->application && null !== $this->application->getEventDispatcher()) {
            $event = new FilterResponseEvent($this, $this->getRequest(), $type, $response);
            $this->application->getEventDispatcher()->dispatch('frontcontroller.response', $event);
            $this->application->getEventDispatcher()->dispatch(KernelEvents::RESPONSE, $event);
        }

        $response->send();
        $this->response = $response;
        $this->application->stop();
    }
195081
public function setResponse(string $type, $response)
    {
        $this->response = new Response($type, $response);
        $this->initialized['response'] = true;

        return $this;
    }
1371536
protected function _handleRedirect($type, $options) {
		if (isset($options[$type . 'RedirectUrl']) && $options[$type . 'RedirectUrl'] !== false) {
			$controller = $this->getController();
			$result = $controller->redirect($options[$type . 'RedirectUrl']);
			$this->_redirectResponse = $result;

			return $result;
		}

		$this->_redirectResponse = null;

		return false;
	}
1034392
public function respond($type = 'json', array $response = array()) {
        if ($response) {
            $response = $response + array(
                'success' => false,
                'data' => '',
                'code' => null
            );

            $this->response($response['success'], $response['data'], $response['code']);
        }

        if ($type === 'html') {
            $this->RequestHandler->renderAs($this->controller, 'ajax');

        } else {
            $this->RequestHandler->respondAs($type);
            $this->controller->autoLayout = false;
            $this->controller->autoRender = false;

            echo $this->_format($type);
        }
    }
488028
public function handle(Request $request, $requestType = self::REQUEST_TYPE_MASTER)
  {
    // Boot on first request
    if (!$this->booted) $this->boot();
    
    try
    {
      return $this->handleRaw($request,$requestType);
    } 
    catch (\Exception $ex) // TODO: Implement Exception listener
    {
      $class = get_class($ex);
      $message = $ex->getMessage();
      switch($class)
      {
        case 'Symfony\Component\Security\Core\Exception\AccessDeniedException':
          $code = 401;
          break;
        default:
          $code = 401;
      }
      $response = new ResponseJson(['error' => $message],$code);
      
      // Need this so auth headers get set
      $dispatcher = $this->container->get('event_dispatcher');
      return $this->dispatchResponse($dispatcher,$request,$response);
    }
  }
824736
public function data($data, $type = 'auto', $code = 200, $header = [], $options = [])
    {
        $type == 'auto' && $type = is_array($data) ? 'json' : '';
        $response = \think\Response::create($data, $type, $code, $header, $options);
        throw new HttpResponseException($response);
    }
1210336
protected function setRequestType(string $type):void
    {
        if ($type !== Protocol::REQUEST_TYPE_NOTIFY && $type !== Protocol::REQUEST_TYPE_FORWARD && $type !== protocol::REQUEST_TYPE_REGISTER) {
            throw new \TypeError("Unsupported request type: $type!");
        }

        $this->requestType = $type;
        $this->setHeader(Protocol::VERSION." ".$type);

    }
1098718
private function setRequestHeaders($headers = array()) {
        foreach ($this->headers as $key => $value) {
            $headers[] = $key . ': ' . $value;
        }
        curl_setopt($this->request, CURLOPT_HTTPHEADER, $headers);
    }
1120780
private function injectHeaders(ResponseInterface $response, array $headers = []): ResponseInterface
    {
        foreach ($headers as $name => $value) {
            $response = $response->withHeader($name, $value);
        }

        return $response;
    }
290879
private void setCorsResponseHeaders(ServiceRequestContext ctx, HttpRequest req,
                                        HttpHeaders headers) {
        final CorsPolicy policy = setCorsOrigin(ctx, req, headers);
        if (policy != null) {
            policy.setCorsAllowCredentials(headers);
            policy.setCorsAllowHeaders(headers);
            policy.setCorsExposeHeaders(headers);
        }
    }
282966
public function handle(SymfonyRequest $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
    {
        $response = $this->app->handle($request, $type, $catch);

        $response->headers->set('X-Frame-Options', 'SAMEORIGIN', false);

        return $response;
    }
722983
public function headers( array $headers )
      : Request
   {

      $this->_defaultHeaders = array_merge( $this->_defaultHeaders, $headers );

      return $this;

   }
409265
public function setType(string $type): void {
		if ($this->compiled) {
			throw new ResponseException("Attempt to alter response after compilation.", ResponseException::AFTER_COMPILE_ALTERATION);
		}
		
		$type = strtolower($type);
		
		if (!in_array($type, ["html", "json", "text", "redirect"])) {
			throw new ResponseException("Unexpected response type: $type.", ResponseException::UNKNOWN_RESPONSE_TYPE);
		}
		
		$this->type = $type;
	}