Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'prolic/dos2unix' into prolic-zen-52
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 96 deletions.
166 changes: 83 additions & 83 deletions src/Response/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@
class Stream extends Response
{

/**
* The Content-Length value, if set
*
* @var int
*/
protected $contentLength = null;

/**
* The portion of the body that has alredy been streamed
*
* @var int
*/
protected $contentStreamed = 0;
/**
* The Content-Length value, if set
*
* @var int
*/
protected $contentLength = null;

/**
* The portion of the body that has alredy been streamed
*
* @var int
*/
protected $contentStreamed = 0;

/**
* Response as stream
Expand Down Expand Up @@ -140,21 +140,21 @@ public function setStreamName($streamName)
}


/**
* Create a new Zend\Http\Response\Stream object from a stream
/**
* Create a new Zend\Http\Response\Stream object from a stream
*
* @param string $responseString
* @param resource $stream
* @return Stream
*/
public static function fromStream($responseString, $stream)
{

if (!is_resource($stream)) {
throw new Exception\InvalidArgumentException('A valid stream is required');
}
* @param string $responseString
* @param resource $stream
* @return Stream
*/
public static function fromStream($responseString, $stream)
{

if (!is_resource($stream)) {
throw new Exception\InvalidArgumentException('A valid stream is required');
}

$headerComplete = false;
$headerComplete = false;
$headersString = '';

$responseArray = explode("\n",$responseString);
Expand All @@ -164,51 +164,51 @@ public static function fromStream($responseString, $stream)
$headersString .= $nextLine."\n";
$nextLineTrimmed = trim($nextLine);
if ($nextLineTrimmed == "") {
$headerComplete = true;
break;
}
$headerComplete = true;
break;
}

}

if (!$headerComplete) {
while (false !== ($nextLine = fgets($stream))) {

$headersString .= trim($nextLine)."\r\n";

$headersString .= trim($nextLine)."\r\n";
if ($nextLine == "\r\n" || $nextLine == "\n") {
$headerComplete = true;
break;
}
$headerComplete = true;
break;
}
}
}

if (!$headerComplete) {
throw new Exception\OutOfRangeException('End of header not found');
}

if (!$headerComplete) {
throw new Exception\OutOfRangeException('End of header not found');
}

$response = static::fromString($headersString);

if (is_resource($stream)) {
$response->setStream($stream);

$response = static::fromString($headersString);

if (is_resource($stream)) {
$response->setStream($stream);
}

if (count($responseArray)) {
$response->content = implode("\n", $responseArray);
}

$headers = $response->headers();
foreach($headers as $header) {
if ($header instanceof \Zend\Http\Header\ContentLength) {
if (count($responseArray)) {
$response->content = implode("\n", $responseArray);
}

$headers = $response->headers();
foreach($headers as $header) {
if ($header instanceof \Zend\Http\Header\ContentLength) {
$response->contentLength = (int) $header->getFieldValue();
if (strlen($response->content) > $response->contentLength) {
throw new Exception\OutOfRangeException(
sprintf('Too much content was extracted from the stream (%d instead of %d bytes)',
strlen($this->content), $this->contentLength));
}
break;
}
}

return $response;
}
break;
}
}

return $response;
}


Expand Down Expand Up @@ -248,33 +248,33 @@ public function getRawBody()
return $this->content;
}


/**
* Read stream content and return it as string
*
* Function reads the remainder of the body from the stream and closes the stream.
*
* @return string
*/
protected function readStream()
{
if (!is_null($this->contentLength)) {
$bytes = $this->contentLength - $this->contentStreamed;
} else {
$bytes = -1; //Read the whole buffer
}

if (!is_resource($this->stream) || $bytes == 0) {
return '';
}

$this->content .= stream_get_contents($this->stream, $bytes);
$this->contentStreamed += strlen($this->content);

if ($this->contentLength == $this->contentStreamed) {
$this->stream = null;
}
}

/**
* Read stream content and return it as string
*
* Function reads the remainder of the body from the stream and closes the stream.
*
* @return string
*/
protected function readStream()
{
if (!is_null($this->contentLength)) {
$bytes = $this->contentLength - $this->contentStreamed;
} else {
$bytes = -1; //Read the whole buffer
}

if (!is_resource($this->stream) || $bytes == 0) {
return '';
}

$this->content .= stream_get_contents($this->stream, $bytes);
$this->contentStreamed += strlen($this->content);

if ($this->contentLength == $this->contentStreamed) {
$this->stream = null;
}
}

/**
* Destructor
Expand Down
26 changes: 13 additions & 13 deletions test/Response/ResponseStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function test300isRedirect()

public function testMultilineHeader()
{
$values = $this->readResponse('response_multiline_header');
$response = Stream::fromStream($values['data'],$values['stream']);
$values = $this->readResponse('response_multiline_header');
$response = Stream::fromStream($values['data'],$values['stream']);

// Make sure we got the corrent no. of headers
$this->assertEquals(6, count($response->headers()), 'Header count is expected to be 6');
Expand All @@ -82,17 +82,17 @@ public function testMultilineHeader()
protected function readResponse($response)
{

$stream = fopen(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $response, 'rb');

$data = '';
while(false!== ($newLine = fgets($stream))) {
$data .= $newLine;
if($newLine == "\n" || $newLine == "\r\n") {
break;
}
}


$stream = fopen(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $response, 'rb');

$data = '';
while(false!== ($newLine = fgets($stream))) {
$data .= $newLine;
if($newLine == "\n" || $newLine == "\r\n") {
break;
}
}


$data .= fread($stream, 100); //Should accept also part of body as text

$return = array();
Expand Down

0 comments on commit 1fd7c61

Please sign in to comment.