protected function debugResponse(ResponseInterface $response) { $this->printDebug(AbstractMessage::getStartLineAndHeaders($response)); $body = (string) $response->getBody(); $contentType = $response->getHeader('Content-Type'); if ($contentType == 'application/json' || strpos($contentType, '+json') !== false) { $data = json_decode($body); if ($data === null) { // invalid JSON! $this->printDebug($body); } else { // valid JSON, print it pretty $this->printDebug(json_encode($data, JSON_PRETTY_PRINT)); } } else { // the response is HTML - see if we should print all of it or some of it $isValidHtml = strpos($body, '</body>') !== false; if ($isValidHtml) { $this->printDebug(''); $crawler = new Crawler($body); // very specific to Symfony's error page $isError = $crawler->filter('#traces-0')->count() > 0 || strpos($body, 'looks like something went wrong') !== false; if ($isError) { $this->printDebug('There was an Error!!!!'); $this->printDebug(''); } else { $this->printDebug('HTML Summary (h1 and h2):'); } // finds the h1 and h2 tags and prints them only foreach ($crawler->filter('h1, h2')->extract(array('_text')) as $header) { // avoid these meaningless headers if (strpos($header, 'Stack Trace') !== false) { continue; } if (strpos($header, 'Logs') !== false) { continue; } // remove line breaks so the message looks nice $header = str_replace("\n", ' ', trim($header)); // trim any excess whitespace "foo bar" => "foo bar" $header = preg_replace('/(\\s)+/', ' ', $header); if ($isError) { $this->printErrorBlock($header); } else { $this->printDebug($header); } } $profilerUrl = $response->getHeader('X-Debug-Token-Link'); if ($profilerUrl) { $fullProfilerUrl = $response->getHeader('Host') . $profilerUrl; $this->printDebug(''); $this->printDebug(sprintf('Profiler URL: <comment>%s</comment>', $fullProfilerUrl)); } // an extra line for spacing $this->printDebug(''); } else { $this->printDebug($body); } } }
/** * Get a cache control directive from a message. * * @param MessageInterface $message Message to retrieve * @param string $part Cache directive to retrieve * * @return mixed|bool|null */ public static function getDirective(MessageInterface $message, $part) { $parts = AbstractMessage::parseHeader($message, 'Cache-Control'); foreach ($parts as $line) { if (isset($line[$part])) { return $line[$part]; } elseif (in_array($part, $line)) { return true; } } return null; }
/** * Gets the number of seconds from the current time in which a response * is still considered fresh. * * @param ResponseInterface $response * * @return int|null Returns the number of seconds */ public static function getMaxAge(ResponseInterface $response) { $parts = AbstractMessage::parseHeader($response, 'Cache-Control'); if (isset($parts['s-maxage'])) { return $parts['s-maxage']; } elseif (isset($parts['max-age'])) { return $parts['max-age']; } elseif ($response->hasHeader('Expires')) { return strtotime($response->getHeader('Expires')) - time(); } return null; }
/** * Gets the state of a message as a hash. * * @param MessageInterface $msg * * @return array|null */ private function messageState(MessageInterface $msg = null) { return !$msg ? null : array('start-line' => AbstractMessage::getStartLine($msg), 'headers' => AbstractMessage::getHeadersAsString($msg), 'body' => $this->streamState($msg->getBody())); }
private function getDefaultOptions(RequestInterface $request) { $headers = AbstractMessage::getHeadersAsString($request); $context = ['http' => ['method' => $request->getMethod(), 'header' => trim($headers), 'protocol_version' => $request->getProtocolVersion(), 'ignore_errors' => true, 'follow_location' => 0]]; if ($body = $request->getBody()) { $context['http']['content'] = (string) $body; // Prevent the HTTP adapter from adding a Content-Type header. if (!$request->hasHeader('Content-Type')) { $context['http']['header'] .= "\r\nContent-Type:"; } } return $context; }
protected function handleOptions(array &$options) { parent::handleOptions($options); // Use a custom emitter if one is specified, and remove it from // options that are exposed through getConfig() if (isset($options['emitter'])) { $this->emitter = $options['emitter']; unset($options['emitter']); } }
/** * Accepts and modifies the options provided to the response in the * constructor. * * @param array $options Options array passed by reference. */ protected function handleOptions(array &$options = []) { parent::handleOptions($options); if (isset($options['reason_phrase'])) { $this->reasonPhrase = $options['reason_phrase']; } }
/** * @expectedException \InvalidArgumentException */ public function testThrowsWhenMessageIsUnknown() { $m = $this->getMockBuilder('GuzzleHttp\\Message\\AbstractMessage')->getMockForAbstractClass(); AbstractMessage::getStartLine($m); }
/** * Return a sorted list of Vary headers. * * While headers are case-insensitive, header values are not. We can only * normalize the order of headers to combine cache entries. * * @param ResponseInterface $response The Response with Vary headers. * * @return array An array of sorted headers. */ private function normalizeVary(ResponseInterface $response) { $parts = AbstractMessage::normalizeHeader($response, 'vary'); sort($parts); return $parts; }