示例#1
0
 public function testConvertsARequestToAContextArray()
 {
     $request = new Message\Request('POST', '/resource/123', 'http://example.com');
     $request->addHeader('Content-Type: application/x-www-form-urlencoded');
     $request->addHeader('Content-Length: 15');
     $request->setContent('foo=bar&bar=baz');
     $client = new StreamClient();
     $client->setMaxRedirects(5);
     $client->setIgnoreErrors(false);
     $client->setTimeout(10);
     $expected = array('http' => array('method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\nContent-Length: 15", 'content' => 'foo=bar&bar=baz', 'protocol_version' => 1.0, 'ignore_errors' => false, 'max_redirects' => 5, 'timeout' => 10));
     $this->assertEquals($client->getStreamContextArray($request), $expected);
 }
 /**
  * Perform request to the source, parse the multipart response,
  * stream the documents with attachments to the target and return
  * the responses along with docs that did not have any attachments.
  *
  * @param string $sourceMethod
  * @param string $sourcePath
  * @param string $targetPath
  * @param string $sourceData
  * @param array $sourceHeaders
  * @return array|ErrorResponse|string
  * @throws HTTPException
  * @throws \Exception
  */
 public function request($sourceMethod, $sourcePath, $targetPath, $sourceData = null, array $sourceHeaders = array())
 {
     $this->sourceConnection = $this->sourceClient->getConnection($sourceMethod, $sourcePath, $sourceData, $sourceHeaders);
     $sourceResponseHeaders = $this->sourceClient->getStreamHeaders();
     $body = '';
     if (empty($sourceResponseHeaders['status'])) {
         try {
             // Close the connection resource.
             fclose($this->sourceConnection);
         } catch (\Exception $e) {
         }
         throw HTTPException::readFailure($this->sourceClient->getOptions()['ip'], $this->sourceClient->getOptions()['port'], 'Received an empty response or not status code', 0);
     } elseif ($sourceResponseHeaders['status'] != 200) {
         while (!feof($this->sourceConnection)) {
             $body .= fgets($this->sourceConnection);
         }
         try {
             fclose($this->sourceConnection);
         } catch (\Exception $e) {
         }
         return new ErrorResponse($sourceResponseHeaders['status'], $sourceResponseHeaders, $body);
     } else {
         try {
             // Body is an array containing:
             // 1) Array of json string documents that don't have
             //  attachments. These should be posted using the Bulk API.
             // 2) Responses of posting docs with attachments.
             $body = $this->parseAndSend($targetPath);
             try {
                 fclose($this->sourceConnection);
             } catch (\Exception $e) {
             }
             return $body;
         } catch (\Exception $e) {
             throw $e;
         }
     }
 }
 public function registerClient(StreamClient $client)
 {
     if (!$this->isLive) {
         // предыдущих клиентов надо скинуть, иначе новый диапазон байт будет при
         // прочтении записан на них тоже. надо только на последнего подключившегося
         // может это логичнее при openStream делать?
         foreach ($this->getClients() as $one) {
             $one->close();
         }
         $this->unfinish();
     }
     $peer = $client->getName();
     $this->clients[$peer] = $client;
     $client->associateStream($this);
     if ($this->isLive) {
         return;
     }
     $client->setEcoMode(false);
     // итак, если у нас не поток (кино), то нужно закрыть источник
     // и открыть его с новыми клиентскими заголовками
     // НО только если поток уже запущен и воспроизводится
     if ($this->state != self::STATE_HDRSENT) {
         return;
     }
     $this->restartStream();
 }