Ejemplo n.º 1
0
 /**
  * @param HttpSocketTransporter $transporter
  * @param HttpResponse          $response
  * @param null                  $continue
  *                              From Events To Tell Just Continue With Body
  *
  * @return mixed
  */
 function __invoke($transporter = null, $response = null, $continue = null)
 {
     ## Close the connection if requested to do so by the server
     $headers = $response->getHeaders();
     if ($headers->has('connection') && strstr($headers->get('connection')->renderValueLine(), 'close') !== false && $transporter->isConnected() && $continue === false) {
         $transporter->close();
     }
 }
Ejemplo n.º 2
0
 /**
  * Construct
  *
  * @param HttpResponse $response
  */
 function __construct(HttpResponse $response)
 {
     $this->rawbody = $response;
     $this->setRawBody($response->getBody());
     /** @var iHeader $h */
     foreach ($response->getHeaders() as $h) {
         $this->meta()->set($h->getLabel(), $h);
     }
     $statusPlugin = new ResposeStatusPlugin(['message_object' => $response]);
     if (!$statusPlugin->isSuccess()) {
         $this->setException(new \RuntimeException($response->getStatReason(), $response->getStatCode()));
     }
 }
 /**
  * @param HttpSocketTransporter $transporter
  * @param HttpResponse          $response
  * @param Streamable            $stream
  * @param iHttpRequest          $request
  *
  * @return mixed
  */
 function __invoke($transporter = null, $response = null, $stream = null, $request = null)
 {
     $statusCode = $response->getStatCode();
     # Handle 100 and 101 responses
     if ($statusCode == 100 || $statusCode == 101) {
         ## receive data will continue after events
         $transporter->reset();
     }
     # HEAD requests and 204 or 304 stat codes are not expected to have a body
     if ($statusCode == 304 || $statusCode == 204 || $request->getMethod() == HttpRequest::METHOD_HEAD) {
         ## do not continue with body
         return ['continue' => false];
     }
     /*$statusPlugin = new Status(['message_object' => $response]);
       if (!$statusPlugin->isSuccess())
           ## always connection will closed, no need to continue
           return ['continue' => false];*/
 }
Ejemplo n.º 4
0
 /**
  * @param HttpSocketTransporter $transporter
  * @param iStreamable           $body
  * @param HttpResponse          $response
  * @param Streamable            $stream
  * @param iHttpRequest          $request
  *
  * @return mixed
  */
 function __invoke($transporter = null, $body = null, $response = null, $stream = null, $request = null)
 {
     $headers = $response->getHeaders();
     // Decoding Data:
     if (!$body || !$transporter->optsData()->isAllowDecoding()) {
         ## do not decode body using raw data
         return ['body' => $body];
     }
     if ($headers->has('Content-Encoding') && strstr(strtolower($headers->get('Content-Encoding')->renderValueLine()), 'gzip') !== false) {
         ## Uses PHP's zlib.inflate filter to inflate deflate or gzipped content
         $body->getResource()->appendFilter(new PhpRegisteredFilter('zlib.inflate'), STREAM_FILTER_READ);
         ### skip the first 10 bytes for zlib
         $body = new Streamable\SegmentWrapStream($body, -1, 10);
     }
     if ($headers->has('transfer-encoding') && strstr(strtolower($headers->get('Transfer-Encoding')->renderValueLine()), 'chunked') !== false) {
         $body->getResource()->appendFilter(new DechunkFilter(), STREAM_FILTER_READ);
     }
     return ['body' => $body];
 }
 /**
  * Fire up action when event listener triggered
  *
  * - if we need back result into Events, must implement-
  *   iEventAware to inject event target into listener,
  *   then you can use setter/getter methods from target-
  *   event.
  *
  * ! stop propagation:
  *   $targetEvent->stopPropagation()
  *
  * @param \Exception $exception
  *
  * @return mixed
  */
 function __invoke($exception = null, $e = null)
 {
     if (!($exception instanceof RouteNotMatchException || $this->response->getStatCode() === 404)) {
         ## unknown error
         return;
     }
     $request = $this->request;
     $uri = $request->getUri();
     $path = $uri->getPath();
     if ($path->isAbsolute()) {
         $path = $path->split(1);
     }
     $mime = null;
     $resolved = $this->assetManager->resolve($path->toString());
     if (!$resolved) {
         ## asset file not resolved!
         return;
     }
     if (pathinfo($resolved, PATHINFO_EXTENSION) == 'css') {
         $mime = 'text/css';
     }
     if (pathinfo($resolved, PATHINFO_EXTENSION) == 'js') {
         $mime = 'application/javascript';
     }
     $size = filesize($resolved);
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $mime = $mime ? $mime : finfo_file($finfo, $resolved);
     finfo_close($finfo);
     $content = file_get_contents($resolved);
     /** @var \Poirot\Http\Headers $headers */
     $headers = $this->response->getHeaders();
     $headers->attach(new HeaderLine('Content-Transfer-Encoding: binary'));
     $headers->attach(new HeaderLine('Content-Type: ' . $mime));
     $headers->attach(new HeaderLine('Content-Length: ' . $size));
     $this->response->setStatCode(200);
     $this->response->setBody($content);
     $this->response->plg()->phpServer()->send();
     die;
 }