Esempio n. 1
0
 /**
  * @param MultiInfoEvent $e
  */
 public function handleMultiInfoEvent(MultiInfoEvent $e)
 {
     $r = $e->getRequest();
     foreach ($this->services as $key => $value) {
         if ($value[1][0] === $r) {
             break;
         }
     }
     $requestType = HttpKernelInterface::SUB_REQUEST;
     $stmt = $value[0];
     $request = $stmt->getRequest();
     list($curlRequest, $contentCollector, $headersCollector) = $value[1];
     $this->updateRequestHeadersFromCurlInfos($request, $e->getRequest()->getInfo());
     if (!$headersCollector->getCode()) {
         /*
          * Here we need to use return code from multi event because curl_errno return invalid results
          */
         $error = new CurlTransportException(curl_error($curlRequest->getHandle()), $e->getInfo()->getResult());
         $error = $error->transformToGenericTransportException();
         $stmt->setError($error);
         $event = new Event\GetResponseForExceptionEvent($this, $request, $requestType, $error);
         $this->getEventDispatcher()->dispatch(KernelEvents::EXCEPTION, $event);
     } else {
         $response = new Response($contentCollector->retrieve(), $headersCollector->getCode(), $headersCollector->retrieve());
         foreach ($headersCollector->getCookies() as $cookie) {
             $response->headers->setCookie($cookie);
         }
         $response->setProtocolVersion($headersCollector->getVersion());
         $response->setStatusCode($headersCollector->getCode(), $headersCollector->getMessage());
         $response->setTransferInfos($e->getRequest()->getInfo());
         $event = new Event\FilterResponseEvent($this, $request, $requestType, $response);
         $this->getEventDispatcher()->dispatch(KernelEvents::RESPONSE, $event);
         /*
            populate response for service
         */
         $stmt->setResponse($response);
         $event = new Event\PostResponseEvent($this, $request, $response);
         $this->getEventDispatcher()->dispatch(KernelEvents::TERMINATE);
     }
 }
 /**
  * @param Response $response a response to build the foundation exception
  *
  * @return \Symfony\Component\HttpKernel\Exception\HttpException exception for given http error
  */
 public static function createHttpException(Response $response)
 {
     $message = \Symfony\Component\HttpFoundation\Response::$statusTexts[$response->getStatusCode()];
     /*
      * We create a standard Response exception to forward exception response content if necessary
      */
     $previous = new ResponseException($response, "", 0);
     switch ($response->getStatusCode()) {
         case 400:
             $e = new BadRequestHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 401:
             $e = new UnauthorizedHttpException($response->headers->get('WWW-Authenticate'), $response->getContent(), $previous, $response->getStatusCode());
             break;
         case 403:
             $e = new ForbiddenHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 404:
             $e = new NotFoundHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 405:
             $allowMethods = explode(',', $response->headers->get('allow'));
             foreach ($allowMethods as $k => $m) {
                 $allowMethods[$k] = trim(strtoupper($m));
             }
             $e = new MethodNotAllowedHttpException($allowMethods, $response->getContent(), $previous, $response->getStatusCode());
             break;
         case 406:
             $e = new NotAcceptableHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 407:
             $e = new ProxyAuthenticationRequiredHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 408:
             $e = new RequestTimeoutHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 409:
             $e = new ConflictHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 410:
             $e = new GoneHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 411:
             $e = new LengthRequiredHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 412:
             $e = new PreconditionFailedHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 413:
             $e = new RequestEntityTooLargeHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 414:
             $e = new RequestUriTooLongHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 415:
             $e = new UnsupportedMediaTypeHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 416:
             $e = new RequestedRangeNotSatisfiableHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 417:
             $e = new ExpectationFailedHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 500:
             $e = new InternalServerErrorHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 501:
             $e = new NotImplementedHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 502:
             $e = new BadGatewayHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 503:
             $e = new ServiceUnavailableHttpException($response->headers->get('Retry-After'), $response->getContent(), $previous, $response->getStatusCode());
             break;
         case 504:
             $e = new GatewayTimeoutHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         case 505:
             $e = new HttpVersionNotSupportedHttpException($response->getContent(), $previous, $response->getStatusCode());
             break;
         default:
             $e = $response->getStatusCode() >= 500 ? new ServerErrorHttpException($response->getStatusCode(), "", $previous) : new ClientErrorHttpException("", $previous);
             break;
     }
     return $e;
 }
 /**
  * @dataProvider provideInvalidContentTypes
  */
 public function testInvalidJsonData($contentType, $body)
 {
     list($helper, $httpKernel, $container) = $this->createContext();
     $response = new Response($body, 200, ['Content-Type' => $contentType]);
     $this->assertInstanceOf('\\evaisse\\SimpleHttpBundle\\Http\\Exception\\InvalidResponseBodyException', $response->getError());
 }
Esempio n. 4
0
 /**
  * Set value for $response
  *
  * @param  Response $response value to set to response
  * @return Object          instance for method chaining
  */
 public function setResponse(Response $response)
 {
     $this->response = $response;
     if ($this->response->hasError()) {
         $this->setError($this->response->getError());
     } else {
         $this->deferred->resolve($response->getResult());
     }
     return $this;
 }