/** * Store the cookies from a response. * * Store the cookies that haven't expired. If a cookie has been expired * and is currently stored, it will be removed. * * @param \Cake\Network\Http\Response $response The response to read cookies from * @param string $url The request URL used for default host/path values. * @return void */ public function store(Response $response, $url) { $host = parse_url($url, PHP_URL_HOST); $path = parse_url($url, PHP_URL_PATH); $path = $path ?: '/'; $cookies = $response->cookies(); foreach ($cookies as $name => $cookie) { if (empty($cookie['domain'])) { $cookie['domain'] = $host; } if (empty($cookie['path'])) { $cookie['path'] = $path; } $key = implode(';', [$cookie['name'], $cookie['domain'], $cookie['path']]); $expires = isset($cookie['expires']) ? $cookie['expires'] : false; $expiresTime = false; if ($expires) { $expiresTime = strtotime($expires); } if ($expiresTime && $expiresTime <= time()) { unset($this->_cookies[$key]); continue; } $this->_cookies[$key] = $cookie; } }
/** * Internal logged wrapper for HTTP GET request * * @param string $url URL for GET request * * @return type */ protected function _get($url) { $start = microtime(true); try { $result = $this->client()->get($url); } catch (Exception $e) { $result = new Response(['HTTP/1.1 ' . $e->getCode()], $e->getMessage()); } $timeTook = microtime(true) - $start; if (!$result->isOk()) { $this->_httpErrors[] = $result->statusCode() . ' - ' . $result->body(); } if ($this->_connection->logQueries() && $this->_connection->logger()) { $log = new LoggedQuery(); $log->query = $url; $log->took = round($timeTook * 1000); if ($result->isOk()) { $log->numRows = strlen($result->body()); } else { $log->error = new Exception('HTTP Error ' . $result->statusCode() . "\n" . $result->body()); } $this->_connection->logger()->log($log); } return $result; }
/** * Test reading the encoding out. * * @return void */ public function testEncoding() { $headers = ['HTTP/1.0 200 Ok']; $response = new Response($headers, ''); $this->assertNull($response->encoding()); $headers = ['HTTP/1.0 200 Ok', 'Content-Type: text/html']; $response = new Response($headers, ''); $this->assertNull($response->encoding()); $headers = ['HTTP/1.0 200 Ok', 'Content-Type: text/html; charset="UTF-8"']; $response = new Response($headers, ''); $this->assertEquals('UTF-8', $response->encoding()); $headers = ['HTTP/1.0 200 Ok', "Content-Type: text/html; charset='ISO-8859-1'"]; $response = new Response($headers, ''); $this->assertEquals('ISO-8859-1', $response->encoding()); }
/** * Verifica si la respuesta del servidor tenía errores * @param \Cake\Network\Http\Response $response * @return bool */ public function responseHasError($response) { return $response->statusCode() != 200 || $response->body() == null || strpos(strtolower($response->body()), 'error') !== false; }