/** * Sends the response to the client * * @param \BearFramework\App\Response $response The response object to be sent * @return void No value is returned */ private function sendResponse($response) { if (!headers_sent()) { $statusCodes = []; $statusCodes[200] = 'OK'; $statusCodes[201] = 'Created'; $statusCodes[202] = 'Accepted'; $statusCodes[203] = 'Non-Authoritative Information'; $statusCodes[204] = 'No Content'; $statusCodes[205] = 'Reset Content'; $statusCodes[206] = 'Partial Content'; $statusCodes[300] = 'Multiple Choices'; $statusCodes[301] = 'Moved Permanently'; $statusCodes[302] = 'Found'; $statusCodes[303] = 'See Other'; $statusCodes[304] = 'Not Modified'; $statusCodes[305] = 'Use Proxy'; $statusCodes[307] = 'Temporary Redirect'; $statusCodes[400] = 'Bad Request'; $statusCodes[401] = 'Unauthorized'; $statusCodes[402] = 'Payment Required'; $statusCodes[403] = 'Forbidden'; $statusCodes[404] = 'Not Found'; $statusCodes[405] = 'Method Not Allowed'; $statusCodes[406] = 'Not Acceptable'; $statusCodes[407] = 'Proxy Authentication Required'; $statusCodes[408] = 'Request Timeout'; $statusCodes[409] = 'Conflict'; $statusCodes[410] = 'Gone'; $statusCodes[411] = 'Length Required'; $statusCodes[412] = 'Precondition Failed'; $statusCodes[413] = 'Request Entity Too Large'; $statusCodes[414] = 'Request-URI Too Long'; $statusCodes[415] = 'Unsupported Media Type'; $statusCodes[416] = 'Requested Range Not Satisfiable'; $statusCodes[417] = 'Expectation Failed'; $statusCodes[500] = 'Internal Server Error'; $statusCodes[501] = 'Not Implemented'; $statusCodes[502] = 'Bad Gateway'; $statusCodes[503] = 'Service Unavailable'; $statusCodes[504] = 'Gateway Timeout'; $statusCodes[505] = 'HTTP Version Not Supported'; if (isset($statusCodes[$response->statusCode])) { header((isset($_SERVER, $_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1') . ' ' . $response->statusCode . ' ' . $statusCodes[$response->statusCode]); } if ($response->isPropertyUsed('headers')) { // performance optimization if (count($response->headers) > 0) { $headers = $response->headers->getList(); foreach ($headers as $header) { if ($header['name'] === 'Content-Type') { $header['value'] .= '; charset=' . $response->charset; } header($header['name'] . ': ' . $header['value']); } } } if ($response->isPropertyUsed('cookies')) { // performance optimization if (count($response->cookies) > 0) { $baseUrlParts = parse_url($this->request->base); $cookies = $response->cookies->getList(); foreach ($cookies as $cookie) { setcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'] === null ? isset($baseUrlParts['path']) ? $baseUrlParts['path'] . '/' : '' : $cookie['path'], $cookie['domain'] === null ? isset($baseUrlParts['host']) ? $baseUrlParts['host'] : '' : $cookie['domain'], $cookie['secure'] === null ? $this->request->scheme === 'https' : $cookie['secure'], $cookie['httpOnly']); } } } } if ($response instanceof App\Response\FileReader) { readfile($response->filename); } else { echo $response->content; } if ($this->isPropertyUsed('hooks')) { // performance optimization $this->hooks->execute('responseSent', $response); } }