public function rev(Response $response) { if ($response->isSuccessful()) { return $this->decode($response->getHeader('Etag')); } throw new RugException('not_found', 'The specified document or revision cannot be found or has been deleted'); }
/** * Processes Set-Cookie headers from a request/response pair. * * @param Message\Request $request A request object * @param Message\Response $response A response object */ public function processSetCookieHeaders(Message\Request $request, Message\Response $response) { foreach ($response->getHeader('Set-Cookie', false) as $header) { $cookie = new Cookie(); $cookie->fromSetCookieHeader($header, parse_url($request->getHost(), PHP_URL_HOST)); $this->addCookie($cookie); } }
protected function assertHttpResponseHasHeader(HttpResponse $response, $header, $expectedValue = null) { $headerValue = $response->getHeader($header); self::assertNotNull($headerValue, "Failed asserting that response has a {$header} header"); if ($expectedValue !== null) { self::assertEquals($expectedValue, $headerValue); } }
/** * @param \Buzz\Message\Response $response */ private function handleResponseAuthHeader(\Buzz\Message\Response $response) { $token = $response->getHeader('Update-Client-Auth'); if ($token) { $this->client->setAuthToken($token); $this->setTokenToCache($token); } }
/** * @param string|null $message */ public function __construct(RequestInterface $request, Response $response, $message = null) { $this->request = $request; $this->response = $response; if ($message === null) { $curlError = $response->getHeader('X-Curl-Error-Result'); $message = sprintf('HTTP %s request to "%s%s" failed: %d - "%s".', $request->getMethod(), $request->getHost(), $request->getResource(), $curlError ?: $response->getStatusCode(), $curlError ? curl_strerror($curlError) : $response->getReasonPhrase()); } parent::__construct($message); }
/** * @param Response $response * @param int $minFresh * @return bool */ public function isExpired(Response $response, $minFresh = 5) { $expires = $response->getHeader('expires'); if ($expires !== null) { $parsedExpires = strtotime($expires); if ($parsedExpires === false || time() + $minFresh > $parsedExpires) { return true; } } return false; }
private function decompressContent(\Buzz\Message\Response $response) { if (!($content_encoding = $response->getHeader('Content-Encoding'))) { return; } $content = $response->getContent(); if (strpos($content_encoding, 'deflate') !== false) { $content = gzuncompress($content); } if (strpos($content_encoding, 'gzip') !== false) { $content = gzinflate(substr($content, 10)); } $response->setContent($content); }
protected static function getPagination(Response $response) { $header = $response->getHeader('Link'); if (empty($header)) { return; } $pagination = []; foreach (explode(',', $header) as $link) { preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match); if (3 === count($match)) { $pagination[$match[2]] = $match[1]; } } return $pagination; }
private static function assertHttpResponseDeletesSessionCookie($session, Response $response) { self::assertStringStartsWith("{$session->name}=deleted;", $response->getHeader('set-cookie')); }
/** * @param Response $response * @param string $method * @param array $arguments * @return \stdClass * @throws \RuntimeException */ protected function validateResponse($response, $method, $arguments) { if (!in_array($response->getStatusCode(), array(200, 401, 409))) { throw new \RuntimeException('Unexpected response received from Transmission'); } if ($response->getStatusCode() == 401) { throw new \RuntimeException('Access to Transmission requires authentication'); } if ($response->getStatusCode() == 409) { $this->setToken($response->getHeader(self::TOKEN_HEADER)); return $this->call($method, $arguments); } return json_decode($response->getContent()); }
/** * @param Response $response * * @return string|null */ protected function mimeTypeByResponse(Response $response) { $mimeType = $response->getHeader('Content-Type', false); if (count($mimeType) > 0) { $mimeType = array_pop($mimeType); $mimeType = explode(';', $mimeType); $mimeType = trim(array_shift($mimeType)); if ('text/plain' !== strtolower($mimeType)) { return $mimeType; } } return null; }
/** * Send cookies to client from a contao response object * * @param Response $response * @return bool */ protected function sendCookiesFromResponse(Response $response) { // Set cookies from the contao response if ($response->getHeader('set-cookie')) { $delimiter = ' || '; $cookies = explode($delimiter, $response->getHeader('set-cookie', $delimiter)); foreach ($cookies as $cookie) { // The second param has to be false otherwise other cookies like from phpbb itself are replaced header('Set-Cookie: ' . $cookie, false); } // The following won't work because the expire value is not an int and conversion something like // 16-Jan-2016 18:07:35 GMT to an int is really unnecessary overhead // although it's looks cleaner at first like above solution // $cookieJar = new CookieJar(); // $cookieJar->processSetCookieHeaders($browser->getLastRequest(), $response); // foreach($cookieJar->getCookies() as $cookie) { // setcookie($cookie->getName(), $cookie->getValue(), $cookie->getAttribute('expires'), $cookie->getAttribute('path'), $cookie->getAttribute('domain'), $cookie->getAttribute('secure'),$cookie->getAttribute('httponly')); // } } } }
/** * Checks if we had a successfull response with Json content in it * * @param Response $response * @return bool */ protected function isJsonResponse(Response $response) { return $response->getStatusCode() == 200 && $response->getHeader('content-type') == 'application/json'; }
/** * @Then /^save new item location as "([^"]*)"$/ * @param string $location * * @return boolean */ public function collectLocations($locationIndex) { $this->response = $this->client->getLastResponse(); $this->locations[$locationIndex] = $this->response->getHeader('X-Location'); }
protected function getSessionIdFromResponse(Response $response) { $statusCode = $response->getStatusCode(); if ($statusCode === 200) { $content = json_decode($response->getContent(), true); if ($content['status'] !== 0) { throw new LibraryException(sprintf('Expected status 0, got "%s".', $content['status'])); } return $content['sessionId']; } if ($statusCode !== 302) { echo $response->getContent(); throw new LibraryException(sprintf('The response should be a redirection, response code from server was "%s"', $statusCode)); } $location = $response->getHeader('Location'); if (!preg_match('#/session/([0-9a-f\\-]+)?#', $location, $vars)) { throw new LibraryException(sprintf('The Location should end with /session/<session-id> (location returned: %s)', $location)); } return $vars[1]; }