Exemplo n.º 1
0
 public function testParserWillWaitForFullContentLength()
 {
     // Make our body and split it up.
     $body = "Testbody withNULL octets.";
     $body_parts = str_split($body, 5);
     // Send our header.
     $content_length = (string) strlen($body);
     $this->parser->addData("MESSAGE\ncontent-length:{$content_length}\n\n");
     // This should not parse yet.
     $this->assertFalse($this->parser->parse());
     // Remove our last part so we can loop over the rest.
     $last_body_part = array_pop($body_parts);
     // Send our parts, checking that we don't parse.
     foreach ($body_parts as $part) {
         $this->parser->addData($part);
         $this->assertFalse($this->parser->parse());
     }
     // Adding our last part should allow it to parse.
     $this->parser->addData($last_body_part);
     $this->assertTrue($this->parser->parse());
     // Check our frame matches our expectation.
     $expected = new Frame('MESSAGE', ['content-length' => $content_length], $body);
     $actual = $this->parser->getFrame();
     $this->assertEquals($expected, $actual);
 }
Exemplo n.º 2
0
 /**
  * Read any newline left in the data to read.
  *
  * Newlines will not be added to the parser, if this method encounters a different character or result,
  * it'll add that to the parser's data buffer and abort.
  */
 private function gobbleNewLines()
 {
     // Only test the stream, return immediately if nothing is left
     while ($this->connectionHasDataToRead(0, 0) && ($data = @fread($this->connection, 1)) !== false) {
         // If its not a newline, it indicates a new messages has been added,
         // so add that to the data-buffer of the parser.
         if ($data !== "\n" && $data !== "\r") {
             $this->parser->addData($data);
             break;
         }
     }
 }