Exemplo n.º 1
0
 /**
  * Read a FastCGI Packet
  *
  * @param Integer $timeoutMs
  * @return array|null
  * @throws CommunicationException
  * @throws TimedOutException
  */
 protected function readPacket($timeoutMs)
 {
     $s = [$this->sock];
     $a = [];
     socket_select($s, $a, $a, floor($timeoutMs / 1000), $timeoutMs % 1000 * 1000);
     $packet = socket_read($this->sock, self::HEADER_LEN);
     if ($packet === false) {
         $errNo = socket_last_error($this->sock);
         if ($errNo == 110) {
             // ETIMEDOUT from http://php.net/manual/en/function.socket-last-error.php
             throw new TimedOutException('Failed reading socket');
         }
         //TODO: Determine way to check if FPM is blocking the client
         /* Not relevant for socket_create() but very interesting...
            $info = stream_get_meta_data($this->_sock);
            if ($info['unread_bytes'] == 0 && $info['blocked'] && $info['eof']) {
                throw new CommunicationException('Not in white list. Check listen.allowed_clients.');
            }*/
         throw CommunicationException::socketRead($this->sock);
     }
     if (!$packet) {
         return null;
     }
     $resp = $this->decodePacketHeader($packet);
     $resp['content'] = '';
     if ($resp['contentLength']) {
         $len = $resp['contentLength'];
         while ($len && ($buf = socket_read($this->sock, $len))) {
             $len -= strlen($buf);
             $resp['content'] .= $buf;
         }
     }
     if ($resp['paddingLength']) {
         /*$buf = */
         socket_read($this->sock, $resp['paddingLength']);
         // throw-away padding...
     }
     return $resp;
 }
Exemplo n.º 2
0
 /**
  * Read a FastCGI Packet
  *
  * @throws CommunicationException
  * @return array
  */
 private function readPacket()
 {
     $packet = @socket_read($this->sock, self::HEADER_LEN);
     if ($packet === false) {
         throw CommunicationException::socketRead($this->sock);
     }
     $resp = $this->decodePacketHeader($packet);
     if ($len = $resp['contentLength'] + $resp['paddingLength']) {
         $content = @socket_read($this->sock, $len);
         if ($content === false) {
             throw CommunicationException::socketRead($this->sock);
         }
         $resp['content'] = substr($content, 0, $resp['contentLength']);
     } else {
         $resp['content'] = '';
     }
     return $resp;
 }