Ejemplo n.º 1
0
 /**
  * Factory method to create a new exception with a normalized error message
  *
  * @param puzzle_message_RequestInterface  $request  Request
  * @param puzzle_message_ResponseInterface $response Response received
  * @param Exception        $previous Previous exception
  *
  * @return self
  */
 public static function create(puzzle_message_RequestInterface $request, puzzle_message_ResponseInterface $response = null, Exception $previous = null)
 {
     if (!$response) {
         return new self('Error completing request', $request, null, $previous);
     }
     $statusCode = $response->getStatusCode();
     $level = $statusCode[0];
     if ($level == '4') {
         $label = 'Client error response';
         $className = 'puzzle_exception_ClientException';
     } elseif ($level == '5') {
         $label = 'Server error response';
         $className = 'puzzle_exception_ServerException';
     } else {
         $label = 'Unsuccessful response';
         $className = __CLASS__;
     }
     $message = $label . ' [url] ' . $request->getUrl() . ' [status code] ' . $response->getStatusCode() . ' [reason phrase] ' . $response->getReasonPhrase();
     return new $className($message, $request, $response, $previous);
 }
Ejemplo n.º 2
0
 /**
  * Create a redirect request for a specific request object
  *
  * Takes into account strict RFC compliant redirection (e.g. redirect POST
  * with POST) vs doing what most clients do (e.g. redirect POST with GET).
  *
  * @param puzzle_message_RequestInterface  $request
  * @param puzzle_message_ResponseInterface $response
  *
  * @return puzzle_message_RequestInterface Returns a new redirect request
  * @throws puzzle_exception_CouldNotRewindStreamException If the body cannot be rewound.
  */
 private function createRedirectRequest(puzzle_message_RequestInterface $request, puzzle_message_ResponseInterface $response)
 {
     $config = $request->getConfig();
     // Use a GET request if this is an entity enclosing request and we are
     // not forcing RFC compliance, but rather emulating what all browsers
     // would do. Be sure to disable redirects on the clone.
     $redirectRequest = clone $request;
     $redirectRequest->getEmitter()->detach($this);
     $statusCode = $response->getStatusCode();
     if ($statusCode == 303 || $statusCode <= 302 && $request->getBody() && !$config->getPath('redirect/strict')) {
         $redirectRequest->setMethod('GET');
         $redirectRequest->setBody(null);
     }
     $this->setRedirectUrl($redirectRequest, $response);
     $this->rewindEntityBody($redirectRequest);
     // Add the Referer header if it is told to do so and only
     // add the header if we are not redirecting from https to http.
     if ($config->getPath('redirect/referer') && ($redirectRequest->getScheme() == 'https' || $redirectRequest->getScheme() == $request->getScheme())) {
         $url = puzzle_Url::fromString($request->getUrl());
         $url->setUsername(null)->setPassword(null);
         $redirectRequest->setHeader('Referer', (string) $url);
     }
     return $redirectRequest;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function getStatusCode()
 {
     return $this->_delegate->getStatusCode();
 }