Example #1
0
 /**
  * Receive a response header from curl
  *
  * @param resource $curl   Curl handle
  * @param string   $header Received header
  *
  * @return int
  */
 public function receiveResponseHeader($curl, $header)
 {
     static $normalize = array("\r", "\n");
     $length = strlen($header);
     $header = str_replace($normalize, '', $header);
     if (strpos($header, 'HTTP/') === 0) {
         $startLine = explode(' ', $header, 3);
         $code = $startLine[1];
         $status = isset($startLine[2]) ? $startLine[2] : '';
         // Only download the body of the response to the specified response
         // body when a successful response is received.
         if ($code >= 200 && $code < 300) {
             $body = $this->request->getResponseBody();
         } else {
             $body = EntityBody::factory();
         }
         $response = new Response($code, null, $body);
         $response->setStatus($code, $status);
         $this->request->startResponse($response);
         $this->request->dispatch('request.receive.status_line', array('request' => $this, 'line' => $header, 'status_code' => $code, 'reason_phrase' => $status));
     } elseif ($pos = strpos($header, ':')) {
         $this->request->getResponse()->addHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1)));
     }
     return $length;
 }
 /**
  * {@inheritdoc}
  */
 protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
 {
     if ($response && $response->isClientError()) {
         $parts = $this->exceptionParser->parse($request, $response);
         return isset(self::$throttlingExceptions[$parts['code']]) ? true : null;
     }
 }
Example #3
0
 /**
  * Parses additional exception information from the response headers
  *
  * @param RequestInterface $request  Request that was issued
  * @param Response         $response The response from the request
  * @param array            $data     The current set of exception data
  */
 protected function parseHeaders(RequestInterface $request, Response $response, array &$data)
 {
     $data['message'] = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
     if ($requestId = $response->getHeader('x-amz-request-id')) {
         $data['request_id'] = $requestId;
         $data['message'] .= " (Request-ID: {$requestId})";
     }
 }
Example #4
0
 protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
 {
     if ($response) {
         //Short circuit the rest of the checks if it was successful
         if ($response->isSuccessful()) {
             return false;
         } else {
             return isset($this->errorCodes[$response->getStatusCode()]) ? true : null;
         }
     }
 }
Example #5
0
 /**
  * Process a prefixed header array
  *
  * @param Response  $response Response that contains the headers
  * @param Parameter $param    Parameter object
  * @param array     $value    Value response array to modify
  */
 protected function processPrefixedHeaders(Response $response, Parameter $param, &$value)
 {
     // Grab prefixed headers that should be placed into an array with the prefix stripped
     if ($prefix = $param->getSentAs()) {
         $container = $param->getName();
         $len = strlen($prefix);
         // Find all matching headers and place them into the containing element
         foreach ($response->getHeaders()->toArray() as $key => $header) {
             if (stripos($key, $prefix) === 0) {
                 // Account for multi-value headers
                 $value[$container][substr($key, $len)] = count($header) == 1 ? end($header) : $header;
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function doParse(array $data, Response $response)
 {
     // Merge in error data from the JSON body
     if ($json = $data['parsed']) {
         $data = array_replace($data, $json);
     }
     // Correct error type from services like Amazon Glacier
     if (!empty($data['type'])) {
         $data['type'] = strtolower($data['type']);
     }
     // Retrieve the error code from services like Amazon Elastic Transcoder
     if ($code = (string) $response->getHeader('x-amzn-ErrorType')) {
         $data['code'] = substr($code, 0, strpos($code, ':'));
     }
     return $data;
 }
Example #7
0
 /**
  * Get a mock response from a file
  *
  * @param string $path File to retrieve a mock response from
  *
  * @return Response
  * @throws InvalidArgumentException if the file is not found
  */
 public static function getMockFile($path)
 {
     if (!file_exists($path)) {
         throw new InvalidArgumentException('Unable to open mock file: ' . $path);
     }
     return Response::fromMessage(file_get_contents($path));
 }
 /**
  * {@inheritdoc}
  */
 public function parse(RequestInterface $request, Response $response)
 {
     // Build array of default error data
     $data = array('code' => null, 'message' => null, 'type' => $response->isClientError() ? 'client' : 'server', 'request_id' => (string) $response->getHeader('x-amzn-RequestId'), 'parsed' => null);
     // Parse the json and normalize key casings
     if (null !== ($json = json_decode($response->getBody(true), true))) {
         $data['parsed'] = array_change_key_case($json);
     }
     // Do additional, protocol-specific parsing and return the result
     $data = $this->doParse($data, $response);
     // Remove "Fault" suffix from exception names
     if (isset($data['code']) && strpos($data['code'], 'Fault')) {
         $data['code'] = preg_replace('/^([a-zA-Z]+)Fault$/', '$1', $data['code']);
     }
     return $data;
 }
 protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
 {
     if ($response && $response->isClientError()) {
         $parts = $this->exceptionParser->parse($request, $response);
         if (!isset($this->retryable[$parts['code']]) || !$request->getClient()) {
             return null;
         }
         /** @var $client AwsClientInterface */
         $client = $request->getClient();
         // Only retry if the credentials can be refreshed
         if (!$client->getCredentials() instanceof AbstractRefreshableCredentials) {
             return null;
         }
         // Resign the request using new credentials
         $client->getSignature()->signRequest($request, $client->getCredentials()->setExpiration(-1));
         // Retry immediately with no delay
         return 0;
     }
 }
Example #10
0
 /**
  * Factory method to create a new response exception based on the response code.
  *
  * @param RequestInterface $request  Request
  * @param Response         $response Response received
  *
  * @return BadResponseException
  */
 public static function factory(RequestInterface $request, Response $response)
 {
     if ($response->isClientError()) {
         $label = 'Client error response';
         $class = __NAMESPACE__ . '\\ClientErrorResponseException';
     } elseif ($response->isServerError()) {
         $label = 'Server error response';
         $class = __NAMESPACE__ . '\\ServerErrorResponseException';
     } else {
         $label = 'Unsuccessful response';
         $class = __CLASS__;
     }
     $message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl()));
     $e = new $class($message);
     $e->setResponse($response);
     $e->setRequest($request);
     return $e;
 }
Example #11
0
 /**
  * Prepare the request for redirection and enforce the maximum number of allowed redirects per client
  *
  * @param RequestInterface $original  Original request
  * @param RequestInterface $request   Request to prepare and validate
  * @param Response         $response  The current response
  *
  * @return RequestInterface
  */
 protected function prepareRedirection(RequestInterface $original, RequestInterface $request, Response $response)
 {
     $params = $original->getParams();
     // This is a new redirect, so increment the redirect counter
     $current = $params[self::REDIRECT_COUNT] + 1;
     $params[self::REDIRECT_COUNT] = $current;
     // Use a provided maximum value or default to a max redirect count of 5
     $max = isset($params[self::MAX_REDIRECTS]) ? $params[self::MAX_REDIRECTS] : $this->defaultMaxRedirects;
     // Throw an exception if the redirect count is exceeded
     if ($current > $max) {
         $this->throwTooManyRedirectsException($original, $max);
         return false;
     } else {
         // Create a redirect request based on the redirect rules set on the request
         return $this->createRedirectRequest($request, $response->getStatusCode(), trim($response->getLocation()), $original);
     }
 }
Example #12
0
 /**
  * Handle a 304 response and ensure that it is still valid
  *
  * @param RequestInterface $request          Request that was sent
  * @param Response         $validateResponse Response received
  * @param Response         $response         Original cached response
  *
  * @return bool Returns true if valid, false if invalid
  */
 protected function handle304Response(RequestInterface $request, Response $validateResponse, Response $response)
 {
     static $replaceHeaders = array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified');
     // Make sure that this response has the same ETag
     if ($validateResponse->getEtag() != $response->getEtag()) {
         return false;
     }
     // Replace cached headers with any of these headers from the
     // origin server that might be more up to date
     $modified = false;
     foreach ($replaceHeaders as $name) {
         if ($validateResponse->hasHeader($name)) {
             $modified = true;
             $response->setHeader($name, $validateResponse->getHeader($name));
         }
     }
     // Store the updated response in cache
     if ($modified && $this->canCache->canCacheResponse($response)) {
         $this->storage->cache($request, $response);
     }
     return true;
 }
Example #13
0
 /**
  * Add the plugin's headers to a response
  *
  * @param RequestInterface $request  Request
  * @param Response         $response Response to add headers to
  */
 protected function addResponseHeaders(RequestInterface $request, Response $response)
 {
     $params = $request->getParams();
     $response->setHeader('Via', sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION));
     $lookup = ($params['cache.lookup'] === true ? 'HIT' : 'MISS') . ' from GuzzleCache';
     if ($header = $response->getHeader('X-Cache-Lookup')) {
         // Don't add duplicates
         $values = $header->toArray();
         $values[] = $lookup;
         $response->setHeader('X-Cache-Lookup', array_unique($values));
     } else {
         $response->setHeader('X-Cache-Lookup', $lookup);
     }
     if ($params['cache.hit'] === true) {
         $xcache = 'HIT from GuzzleCache';
     } elseif ($params['cache.hit'] == 'error') {
         $xcache = 'HIT_ERROR from GuzzleCache';
     } else {
         $xcache = 'MISS from GuzzleCache';
     }
     if ($header = $response->getHeader('X-Cache')) {
         // Don't add duplicates
         $values = $header->toArray();
         $values[] = $xcache;
         $response->setHeader('X-Cache', array_unique($values));
     } else {
         $response->setHeader('X-Cache', $xcache);
     }
     if ($response->isFresh() === false) {
         $response->addHeader('Warning', sprintf('110 GuzzleCache/%s "Response is stale"', Version::VERSION));
         if ($params['cache.hit'] === 'error') {
             $response->addHeader('Warning', sprintf('111 GuzzleCache/%s "Revalidation failed"', Version::VERSION));
         }
     }
 }
 protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
 {
     if ($response) {
         return isset($this->errorCodes[$response->getReasonPhrase()]) ? true : null;
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
 {
     if ($response && $response->getStatusCode() == 400 && strpos($response->getBody(), self::ERR)) {
         return true;
     }
 }
Example #16
0
 public function fetch(RequestInterface $request)
 {
     $key = $this->getCacheKey($request);
     if (!($entries = $this->cache->fetch($key))) {
         return null;
     }
     $match = null;
     $headers = $this->persistHeaders($request);
     $entries = unserialize($entries);
     foreach ($entries as $index => $entry) {
         if ($this->requestsMatch(isset($entry[1]['vary']) ? $entry[1]['vary'] : '', $headers, $entry[0])) {
             $match = $entry;
             break;
         }
     }
     if (!$match) {
         return null;
     }
     // Ensure that the response is not expired
     $response = null;
     if ($match[4] < time()) {
         $response = -1;
     } else {
         $response = new Response($match[2], $match[1]);
         if ($match[3]) {
             if ($body = $this->cache->fetch($match[3])) {
                 $response->setBody($body);
             } else {
                 // The response is not valid because the body was somehow deleted
                 $response = -1;
             }
         }
     }
     if ($response === -1) {
         // Remove the entry from the metadata and update the cache
         unset($entries[$index]);
         if ($entries) {
             $this->cache->save($key, serialize($entries));
         } else {
             $this->cache->delete($key);
         }
         return null;
     }
     return $response;
 }
Example #17
0
 public function setResponse(Response $response, $queued = false)
 {
     $response->setEffectiveUrl((string) $this->url);
     if ($queued) {
         $ed = $this->getEventDispatcher();
         $ed->addListener('request.before_send', $f = function ($e) use($response, &$f, $ed) {
             $e['request']->setResponse($response);
             $ed->removeListener('request.before_send', $f);
         }, -9999);
     } else {
         $this->response = $response;
         // If a specific response body is specified, then use it instead of the response's body
         if ($this->responseBody && !$this->responseBody->getCustomData('default') && !$response->isRedirect()) {
             $this->getResponseBody()->write((string) $this->response->getBody());
         } else {
             $this->responseBody = $this->response->getBody();
         }
         $this->setState(self::STATE_COMPLETE);
     }
     return $this;
 }
Example #18
0
 public function visit(CommandInterface $command, Response $response, Parameter $param, &$value, $context = null)
 {
     $value[$param->getName()] = $response->getReasonPhrase();
 }
Example #19
0
 /**
  * Returns a formatted message
  *
  * @param RequestInterface $request    Request that was sent
  * @param Response         $response   Response that was received
  * @param CurlHandle       $handle     Curl handle associated with the message
  * @param array            $customData Associative array of custom template data
  *
  * @return string
  */
 public function format(RequestInterface $request, Response $response = null, CurlHandle $handle = null, array $customData = array())
 {
     $cache = $customData;
     return preg_replace_callback('/{\\s*([A-Za-z_\\-\\.0-9]+)\\s*}/', function (array $matches) use($request, $response, $handle, &$cache) {
         if (array_key_exists($matches[1], $cache)) {
             return $cache[$matches[1]];
         }
         $result = '';
         switch ($matches[1]) {
             case 'request':
                 $result = (string) $request;
                 break;
             case 'response':
                 $result = (string) $response;
                 break;
             case 'req_body':
                 $result = $request instanceof EntityEnclosingRequestInterface ? (string) $request->getBody() : '';
                 break;
             case 'res_body':
                 $result = $response ? $response->getBody(true) : '';
                 break;
             case 'ts':
                 $result = gmdate('c');
                 break;
             case 'method':
                 $result = $request->getMethod();
                 break;
             case 'url':
                 $result = (string) $request->getUrl();
                 break;
             case 'resource':
                 $result = $request->getResource();
                 break;
             case 'protocol':
                 $result = 'HTTP';
                 break;
             case 'version':
                 $result = $request->getProtocolVersion();
                 break;
             case 'host':
                 $result = $request->getHost();
                 break;
             case 'hostname':
                 $result = gethostname();
                 break;
             case 'port':
                 $result = $request->getPort();
                 break;
             case 'code':
                 $result = $response ? $response->getStatusCode() : '';
                 break;
             case 'phrase':
                 $result = $response ? $response->getReasonPhrase() : '';
                 break;
             case 'connect_time':
                 $result = $handle && $handle->getInfo(CURLINFO_CONNECT_TIME) ? $handle->getInfo(CURLINFO_CONNECT_TIME) : ($response ? $response->getInfo('connect_time') : '');
                 break;
             case 'total_time':
                 $result = $handle && $handle->getInfo(CURLINFO_TOTAL_TIME) ? $handle->getInfo(CURLINFO_TOTAL_TIME) : ($response ? $response->getInfo('total_time') : '');
                 break;
             case 'curl_error':
                 $result = $handle ? $handle->getError() : '';
                 break;
             case 'curl_code':
                 $result = $handle ? $handle->getErrorNo() : '';
                 break;
             case 'curl_stderr':
                 $result = $handle ? $handle->getStderr() : '';
                 break;
             default:
                 if (strpos($matches[1], 'req_header_') === 0) {
                     $result = $request->getHeader(substr($matches[1], 11));
                 } elseif ($response && strpos($matches[1], 'res_header_') === 0) {
                     $result = $response->getHeader(substr($matches[1], 11));
                 }
         }
         $cache[$matches[1]] = $result;
         return $result;
     }, $this->template);
 }
Example #20
0
 public function canCacheResponse(Response $response)
 {
     return $response->isSuccessful() && $response->canCache();
 }
Example #21
0
 public function visit(CommandInterface $command, Response $response, Parameter $param, &$value, $context = null)
 {
     $value[$param->getName()] = $param->filter($response->getBody());
 }
Example #22
0
 public function addCookiesFromResponse(Response $response, RequestInterface $request = null)
 {
     if ($cookieHeader = $response->getHeader('Set-Cookie')) {
         $parser = ParserRegistry::getInstance()->getParser('cookie');
         foreach ($cookieHeader as $cookie) {
             if ($parsed = $request ? $parser->parseCookie($cookie, $request->getHost(), $request->getPath()) : $parser->parseCookie($cookie)) {
                 // Break up cookie v2 into multiple cookies
                 foreach ($parsed['cookies'] as $key => $value) {
                     $row = $parsed;
                     $row['name'] = $key;
                     $row['value'] = $value;
                     unset($row['cookies']);
                     $this->add(new Cookie($row));
                 }
             }
         }
     }
 }