/** * Taken from Mink\BrowserKitDriver * * @param Response $response * * @return \Symfony\Component\BrowserKit\Response */ protected function createResponse(Response $response) { $contentType = $response->getHeader('Content-Type'); $matches = null; if (!$contentType or strpos($contentType, 'charset=') === false) { $body = $response->getBody(true); if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) { $contentType .= ';charset=' . $matches[1]; } $response->setHeader('Content-Type', $contentType); } $headers = $response->getHeaders(); $status = $response->getStatusCode(); $matchesMeta = null; $matchesHeader = null; $isMetaMatch = preg_match('/\\<meta[^\\>]+http-equiv="refresh" content="(\\d*)\\s*;?\\s*url=(.*?)"/i', $response->getBody(true), $matchesMeta); $isHeaderMatch = preg_match('~(\\d*);?url=(.*)~', (string) $response->getHeader('Refresh'), $matchesHeader); $matches = $isMetaMatch ? $matchesMeta : $matchesHeader; if (!empty($matches) && (empty($matches[1]) || $matches[1] < $this->refreshMaxInterval)) { $uri = $this->getAbsoluteUri($matches[2]); $partsUri = parse_url($uri); $partsCur = parse_url($this->getHistory()->current()->getUri()); foreach ($partsCur as $key => $part) { if ($key === 'fragment') { continue; } if (!isset($partsUri[$key]) || $partsUri[$key] !== $part) { $status = 302; $headers['Location'] = $uri; break; } } } return new BrowserKitResponse($response->getBody(), $status, $headers); }
/** * @return string */ protected function detectEncoding() { $encoding = ''; $contentType = $this->response->getHeader('Content-Type'); if ($contentType != '' && strpos($contentType, 'charset') !== FALSE) { $encoding = $this->detectEncodingOutOfContentTypeString($contentType); } if ($encoding === '' && $this->detectMimeType() === 'text/html') { $encoding = $this->extractContentTypeStringOutOfMetaTag(); } return $encoding != '' ? strtoupper(trim($encoding)) : $this->defaultEncoding; }
protected function verifyHttpResponseHash(Response $httpResponse) { $hash = $httpResponse->getHeader('hash'); $responseData = $httpResponse->getBody(true); $validHash = $this->getHMACMD5Hash($this->getSecret(), $responseData); $isValid = $validHash == $hash; return $isValid; }
/** * Response constructor * * @param HttpResponse $httpResponse * @return self */ public function __construct(HttpResponse $httpResponse) { if (stripos($httpResponse->getHeader('Content-Type'), 'text/javascript') !== false) { $this->_response = $this->processJson($httpResponse->getBody()); } else { $this->_response = $this->processXml($httpResponse->getBody()); } }
public function parse(Response $response) { $collection = new Collection(); if (!$response->getBody()) { return $collection; } // help bad responses be more multipart compliant $body = "\r\n" . $response->getBody()->__toString() . "\r\n"; // multipart preg_match('/boundary\\=\\"(.*?)\\"/', $response->getHeader('Content-Type'), $matches); if (isset($matches[1])) { $boundary = $matches[1]; } else { preg_match('/boundary\\=(.*?)(\\s|$|\\;)/', $response->getHeader('Content-Type'), $matches); $boundary = $matches[1]; } // strip quotes off of the boundary $boundary = preg_replace('/^\\"(.*?)\\"$/', '\\1', $boundary); // clean up the body to remove a reamble and epilogue $body = preg_replace('/^(.*?)\\r\\n--' . $boundary . '\\r\\n/', "\r\n--{$boundary}\r\n", $body); // make the last one look like the rest for easier parsing $body = preg_replace('/\\r\\n--' . $boundary . '--/', "\r\n--{$boundary}\r\n", $body); // cut up the message $multi_parts = explode("\r\n--{$boundary}\r\n", $body); // take off anything that happens before the first boundary (the preamble) array_shift($multi_parts); // take off anything after the last boundary (the epilogue) array_pop($multi_parts); $message_parser = new MessageParser(); $parser = new Single(); // go through each part of the multipart message foreach ($multi_parts as $part) { // get Guzzle to parse this multipart section as if it's a whole HTTP message $parts = $message_parser->parseResponse("HTTP/1.1 200 OK\r\n" . $part); // now throw this single faked message through the Single GetObject response parser $single = new Response($parts['code'], $parts['headers'], Stream::factory($parts['body'])); $obj = $parser->parse($single); // add information about this multipart to the returned collection $collection->push($obj); } return $collection; }
/** * Parse the returned response. * * @param \GuzzleHttp\Message\Response $response * @return array * * @throws \RuntimeException */ protected function parseResponse(Response $response) { $contentType = explode(';', $response->getHeader('content-type'))[0]; switch ($contentType) { case 'text/javascript': case 'application/json': return $response->json(); case 'application/xml': return $response->xml(); } throw new \RuntimeException("Unsupported returned content-type [{$contentType}]"); }
/** * @param \GuzzleHttp\Message\Response $response * @return Error */ protected function handleResponse($response) { $contentType = $response->getHeader('content-type'); if (stripos($contentType, 'application/json') === false) { return $response; } $json = $response->json(); if ($response->getStatusCode() >= 400) { // oops an error with the response return new Error($response->getStatusCode(), $json['code'], $json['message']); } return $json; }
/** * Taken from Mink\BrowserKitDriver * * @param Response $response * * @return \Symfony\Component\BrowserKit\Response */ protected function createResponse(Response $response) { $contentType = $response->getHeader('Content-Type'); if (!$contentType or strpos($contentType, 'charset=') === false) { $body = $response->getBody(true); if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) { $contentType .= ';charset=' . $matches[1]; } $response->setHeader('Content-Type', $contentType); } $headers = $response->getHeaders(); $status = $response->getStatusCode(); if (preg_match('/\\<meta[^\\>]+http-equiv="refresh" content=".*?url=(.*?)"/i', $response->getBody(true), $matches)) { $status = 302; $headers['Location'] = $matches[1]; } if (preg_match('~url=(.*)~', (string) $response->getHeader('Refresh'), $matches)) { $status = 302; $headers['Location'] = $matches[1]; } return new BrowserKitResponse($response->getBody(), $status, $headers); }
/** * @param Response $response * @return array|mixed * @throws JsonApiResponseException */ public function handleResponse($response) { $contentType = $response->getHeader('content-type'); // if its not json, then just return the response and handle it in your own object. if (stripos($contentType, 'application/json') === false) { return $response; } $json = $response->json(); // adobe says hey this didn't work! if ($response->getStatusCode() >= 400) { // oops an error with the response, from Adobe complaining about something in your code. throw new JsonApiResponseException($response->getStatusCode(), $json['message'], $json['code']); } return $json; }
private function setResponseStream(Response $response, $socket, EmitterInterface $emitter, $useFilter = false) { if ($response->getHeader('Transfer-Encoding') == "chunked") { stream_filter_append($socket, 'dechunk'); } // Attach filter if ($useFilter) { stream_filter_append($socket, 'event', STREAM_FILTER_READ, ['emitter' => $emitter, 'content_type' => $response->getHeader('Content-Type')]); } $stream = new Stream($socket); $response->setBody($stream); }
/** * @param $key * @return string */ public function getHeader($key) { return $this->response->getHeader($key); }
/** * Fetch a named header. * * @param string $name * * @return string|null */ public function getHeader($name) { return $this->response->getHeader($name); }
private function setResponseStream(Response $response, $socket) { if ($response->getHeader('Transfer-Encoding') == "chunked") { $stream = new ChunkedStream($socket); } elseif ($response->getHeader('Content-Type') == "application/vnd.docker.raw-stream") { $stream = new AttachStream($socket); } else { $stream = new Stream($socket); } $response->setBody($stream); }
private function detectType(\GuzzleHttp\Message\Response $response) { switch ($response->getHeader('content-type')) { case 'application/json': return $response->json(); break; case 'application/xml': case 'text/xml': return json_decode(json_encode($response->xml()), true); break; default: return $response->getBody(); } }
public function evaluateResponse(Response $response) { $errors = []; try { // Test response code if (array_key_exists('response.statusCode', $this->expectations)) { $this->expectations['response.statusCode']->evaluate((int) $response->getStatusCode(), 'Status code was expected to be `%s` but was actually `' . $response->getStatusCode() . '`'); } } catch (AssertionFailedException $e) { $errors[] = $e->getMessage(); } // Test headers foreach ($this->expectations as $name => $expectation) { try { if (substr($name, 0, 8) === 'headers.') { $headerName = substr($name, 8); $headerValue = $response->getHeader($headerName); $this->expectations['headers.' . strtolower($headerName)]->evaluate($headerValue, 'Header `' . $headerName . '` expected to be `%s` but was `%s`'); } } catch (AssertionFailedException $e) { $errors[] = $e->getMessage(); } } // Test body $body = json_decode($response->getBody(), true); foreach ($this->expectations as $name => $expectation) { try { if (substr($name, 0, 5) === 'body.') { $keyName = substr($name, 5); $keyValue = array_get($body, $keyName); $this->expectations['body.' . strtolower($keyName)]->evaluate($keyValue, 'Body key `' . $keyName . '` expected to be `%s` but was `%s`'); } } catch (AssertionFailedException $e) { $errors[] = $e->getMessage(); } } return $errors; }