negotiates proper mime type for given request

@param   \stubbles\webapp\Request   $request
@param   \stubbles\webapp\Response  $response  response to send
@return  bool
@since   6.0.0
778146
public function negotiateMimeType(Request $request, Response $response): bool
    {
        if ($this->supportedMimeTypes->isContentNegotationDisabled()) {
            return true;
        }

        $mimeType = $this->supportedMimeTypes->findMatch(
                $request->readHeader('HTTP_ACCEPT')
                        ->defaultingTo(emptyAcceptHeader())
                        ->withFilter(new AcceptFilter())
        );
        if (null === $mimeType) {
            $response->notAcceptable($this->supportedMimeTypes->asArray());
            return false;
        }

        if (!$this->supportedMimeTypes->provideClass($mimeType)) {
            $response->write(
                    $response->internalServerError(
                            'No mime type class defined for negotiated content type ' . $mimeType
                    )
            );
            return false;
        }

        $response->adjustMimeType(
                $this->injector->getInstance(
                    $this->supportedMimeTypes->classFor($mimeType)
                )->specialise($mimeType)
        );
        return true;
    }
847398
protected function sendHeaders(Response $response)
    {
        // Send HTTP status & reason-phrase
        header(
            sprintf(
                '%s %s %s',
                $this->getProtocolLine(),
                $response->getStatusCode(),
                $response->getReasonPhrase()
            ),
            true,
            $response->getStatusCode()
        );

        // Send headers
        $headers = $response->getHeaders();
        foreach ($headers as $name => $valueCollection) {
            foreach ($valueCollection as $value) {
                header(sprintf('%s: %s', $name, $value), false);
            }
        }
    }
878321
public function handleResponse(Request $request, Response $response)
    {
        if ($response->getStatusCode() == 422) {
            $errors = $response->getOriginalContent()['validation_errors'];

            throw new HttpResponseException(
                redirect()->back()->withInput($request->input())->withErrors($errors)
            );
        }

        if ($response->getStatusCode() == 403) {
            abort(403);
        }

        return $response->isNotFound() ? abort(404) : $response->getOriginalContent();
    }
1032561
protected function addJquery(Request $request, Response $response)
    {
        if (!$this->config->get('general/add_jquery', false) &&
            !$this->config->get('theme/add_jquery', false)) {
            return;
        }

        // Check zone to skip expensive regex
        if (Zone::isFrontend($request) === false) {
            return;
        }

        $html = $response->getContent();
        $regex = '/<script(.*)jquery(-latest|-[0-9\.]*)?(\.min)?\.js/';
        if (preg_match($regex, $html)) {
            return;
        }

        $this->add(
            (new JavaScript('js/jquery-2.2.4.min.js', 'bolt'))
                ->setLocation(Target::BEFORE_JS)
        );
    }
375228
private function getMimeType(Psr7\Response $response)
    {
        $header = $response->getHeader('Content-Type');
        if ($header !== []) {
            return reset($header);
        }

        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime = finfo_buffer($finfo, $response->getBody()->getContents());
        finfo_close($finfo);

        return $mime;
    }
1212914
protected function corsHandle($request, $response)
    {
        if (!$this->cors->hasAdded()) {
            $this->cors->setRequest('laravel', $request);
            $this->cors->setResponse('laravel', $response);
            $this->cors->handle();
        }

        return $this->cors->getResponse();
    }
1121563
protected function configure(Response $response, $contentType=null)
    {
        if (!$contentType) {
            return $response;
        }

        if (strtolower($contentType) == strtolower($this->serializer->mimeType())) {
            $response->setSerializer($this->serializer);
        }

        return $response;
    }
521791
public function postProcess(Request $request, Response $response): bool
    {
        foreach ($this->postInterceptors as $postInterceptor) {
            if (false === $this->executePostInterceptor($postInterceptor, $request, $response)) {
                return false;
            }
        }

        return true;
    }
1151216
public function applyPostInterceptors(Request $request, Response $response): bool
    {
        return $this->interceptors->postProcess($request, $response);
    }
1046933
public function postProcess(Request $request, Response $response)
    {
        if (empty($this->allowedOriginHosts) || !$request->hasHeader('HTTP_ORIGIN')) {
            return;
        }

        $originHost = $request->readHeader('HTTP_ORIGIN')->unsecure();
        foreach ($this->allowedOriginHosts as $allowedOriginHost) {
            if (preg_match('~' . $allowedOriginHost . '~', $originHost) === 1) {
                $response->addHeader('Access-Control-Allow-Origin', $originHost);
            }
        }
    }
166639
public function info(Request $request, Response $response)
    {
        $url = $request->getQueryParam('url') ?: $request->getQueryParam('v');

        if (isset($url) && !empty($url)) {
            $this->video = new Video($url, $this->getFormat($request), $this->getPassword($request));

            if ($this->config->convert && $request->getQueryParam('audio')) {
                // We skip the info page and get directly to the download.
                return $response->withRedirect(
                    $this->container->get('router')->pathFor('download').
                    '?'.http_build_query($request->getQueryParams())
                );
            } else {
                return $this->getInfoResponse($request, $response);
            }
        } else {
            return $response->withRedirect($this->container->get('router')->pathFor('index'));
        }
    }
1196453
public function record(Request $request, Response $response)
    {
        if ($this->hasResponse($request)) {
            return;
        }

        $recording = array(
            'request'  => $request->toArray(),
            'response' => $response->toArray()
        );

        $this->storage->storeRecording($recording);
    }
42503
private function create404(Http\Request $request, Http\Response $response)
    {
        $response        = $response->withStatus(404);
        $originalRequest = $request->getOriginalRequest();
        $uri             = $originalRequest->getUri();
        $escaper         = new Escaper();
        $message         = sprintf(
            "Cannot %s %s\n",
            $escaper->escapeHtml($request->getMethod()),
            $escaper->escapeHtml((string) $uri)
        );
        return $response->end($message);
    }
1147742
private function getRemuxStream(Request $request, Response $response)
    {
        if (!$this->config->remux) {
            throw new Exception(_('You need to enable remux mode to merge two formats.'));
        }
        $stream = $this->video->getRemuxStream();
        $response = $response->withHeader('Content-Type', 'video/x-matroska');
        if ($request->isGet()) {
            $response = $response->withBody(new Stream($stream));
        }

        return $response->withHeader(
            'Content-Disposition',
            'attachment; filename="'.$this->video->getFileNameWithExtension('mkv')
        );
    }
329567
public function parse( Request $request, Response $response )
    {
        $error = parent::parse( $request, $response );

        if ( $error )
        {
            return $error;
        }

        $method = null;
        $params = xmlrpc_decode_request(
            $request->getContent(),
            $method,
            'utf-8'
        );

        if ( ! $params )
        {
            return self::PARSE;
        }

        if ( empty( $method ) || empty( $params ) )
        {
            return self::INVALID_REQUEST;
        }

        $this->method   = $method;
        $this->params   = $params;

        return null;
    }
648733
protected function wrapResponse(Request $request, JsonResponse $response)
    {
        switch ($request->query->get(self::PARAMETER_WRAPPER)) {
            case self::WRAPPER_POST_MESSAGE:
                $response->setContent(
                    sprintf(
                        $this->getPostMessageTemplate(),
                        $response->getContent(),
                        $this->getCallbackFromRequest($request),
                        $this->getPostMessageOrigin()
                    )
                )->headers->set('Content-Type', 'text/html');
                break;
            default:
                $response->setCallback(
                    $request->query->get(self::PARAMETER_CALLBACK)
                );
                break;
        }
    }
171096
private function responseContentTypeIsJson(ResponseInterface $response): bool
    {
        return $response->hasHeader('Content-Type')
               && \in_array($response->getHeader('Content-Type')[0], $this->jsonContentTypes);
    }