php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66275 HttpResponse::setStream($handle); don't run with a resource from StreamWrapper
Submitted: 2013-12-12 13:26 UTC Modified: 2014-07-16 19:50 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: erwan dot oger35 at gmail dot com Assigned: mike (profile)
Status: Not a bug Package: pecl_http (PECL)
PHP Version: 5.4.22 OS: linux
Private report: No CVE-ID: None
 [2013-12-12 13:26 UTC] erwan dot oger35 at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/httpresponse.setstream
---


When a resource is created by a streamWrapper::stream_open() method, it can't be "read" by the HttpResponse::setStream() method.

However I can read it with fread() function.

Test script:
---------------



Expected result:
----------------
When I want to display a file contents, I expect to see it.

Actual result:
--------------
Actually, I see nothing (a white page on a browser)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-12 13:32 UTC] erwan dot oger35 at gmail dot com
Note that the same problem can be met with the http_send_stream() function.
 [2013-12-12 19:22 UTC] aharvey@php.net
-Package: Streams related +Package: pecl_http
 [2013-12-12 20:58 UTC] mike@php.net
-Assigned To: +Assigned To: mike
 [2014-02-14 08:32 UTC] erwan dot oger35 at gmail dot com
Hello !

Do you have any additional informations about this problem which is blocking for me ?

In fact, I use the MongoDB's filesystem GridFS in my company, and I really need this feature which is safer rather than fixing responses' headers by hand.

Thank you in advance

Erwan
 [2014-03-27 16:28 UTC] mike@php.net
-Status: Assigned +Status: Feedback
 [2014-03-27 16:28 UTC] mike@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

Works here (tm).

Does your wrapper implement seek and does the seek method return true/false?
 [2014-07-10 14:48 UTC] mike@php.net
Ping
 [2014-07-16 09:19 UTC] erwan dot oger35 at gmail dot com
Hello,


Sorry to be late !

After severals tests : IT'S NOT A BUG => the problem was between the chair and the keybord !!!

It was a bad stram_stat function's implementation =S

You can try the following code if you want ! (sorry for the size of this example but it's very difficult to do better)

Bye !


<?php

stream_wrapper_register('foobar', 'MyStreamWrapper');
$handle = fopen('foobar://fguyefhnizenhrczego', 'r');
HttpResponse::setCache(true);
HttpResponse::setCacheControl('public', 3600, false);
HttpResponse::setContentType('text/plain');
HttpResponse::setStream($handle);
HttpResponse::send();
die;


class MyStreamWrapper {

	private $path;
	private $mode;
	private $length;
	private $position;
	
	public function stream_stat () {
		$ino		= mt_rand(0, 9999999999);
		$mode		= 100755;
		$nlink		= 1;
		$rdev		= 0;
		$size		= $this->length;
		$atime		= time();
		$mtime		= time()-(60*60*24);
		$blksize	= -1;
		$blocks		= -1;
		
		$res =  array(	0			=> 0,
						'dev'		=> 0,
						1			=> $ino,
						'ino'		=> $ino,
						2			=> $mode,
						'mode'		=> $mode,
						3			=> $nlink,
						'nlink'		=> $nlink,
						4			=> 0,
						'uid'		=> 0,
						5			=> 0,
						'gid'		=> 0,
						6			=> $rdev,
						'rdev'		=> $rdev,
						7			=> $size,
						'size'		=> $size,
						8			=> $atime,
						'atime'		=> $atime,
						9			=> $mtime,
						'mtime'		=> $mtime,
						10			=> $mtime,
						'ctime'		=> $mtime,
						11			=> $blksize,
						'blksize'	=> $blksize,
						12			=> $blocks,
						'blocks'	=> $blocks
				);
				
		return $res;
	}
			
	public function stream_open ( $path, $mode ) {
		$this->path = $path;
		$this->mode = $mode;
		$this->length = 1024*1024; // 32Mb
		$this->stream_seek(0);
		return true;
	}
	public function stream_seek ( $offset, $whence = SEEK_SET ) {
		switch ( $whence ) {
			case SEEK_SET :
				$this->position = $offset;
				break;

			case SEEK_CUR :
				$this->position += $offset;
				break;

			case SEEK_END :
				$this->position = $this->length + $offset;
				break;
				
			default :
				return false;
				break;
		}
		
		// si le curseur dépasse la taille du fichier, on le remet à la fin du fichier
		if ( $this->stream_eof() ) $this->position =  $this->length;
		
		/* ... */
		
		return true;
	}
	public function stream_read ( $count ) {
		if ( $this->mode != 'r' )  return false;
		
		// initialisation de la variable de retour
		$data = '';
		
		while ( !$this->stream_eof() && ($count > 0) ) {
			$tempData = randString(mt_rand(1, $count));
			$tempData_size = strlen($tempData);
			$data .= $tempData;
			$count -= $tempData_size;
			$this->stream_seek($tempData_size, SEEK_CUR);
		}
		
		return $data;
	}
	public function stream_tell () {
		return $this->position;
	}
	public function stream_eof () {
		return $this->position >= $this->length;
	}
	public function stream_close () {
		return true;
	}
}

function randString ( $length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789' ) {
    $str = '';
    $count = strlen($charset);
    while ($length--) {
        $str .= $charset[mt_rand(0, $count-1)];
    }
    return $str;
}
 [2014-07-16 19:50 UTC] mike@php.net
-Status: Feedback +Status: Not a bug
 [2014-07-16 19:50 UTC] mike@php.net
Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 22:01:28 2024 UTC