A call to function fsockopen to read the binary output of a CGI program yields packets duplication

Hello,

I'm trying to download the binary output of a CGI program from a PHP script using 'fsockopen'. My problem is that rather than receiving the proper sequence of binary data, I get the sequence by chunk, and I get some chunks several times. And this duplication happens in a random fashion, i.e. I never get the same output. (but the CGI always sends the same binary data (for the sake of testing)). Also, each chunk seems to be preceded by its size (written in HEX format using ASCII) and a CRLF.

e.g. If the CGI outputs a binary sequence of 256 bytes containing the sequence (0,1,2,3,4,5,...., 255), then my output could be like (61,13,10,0,1,2,3,4,5,6,7,8,9,13,10,...., 255,13,10,13,10) where 61 is ASCII A and A is HEXA for 10 and 10 is the size of the following packet. 13,10 are CRLFs.

There does not seem to be any byte remapping. So this is not about the encoding. It's really a matter of big chucks of data being sent several times.

See the PHP code I have below.

What I tried so far:
- Downloading the binary just by executing my CGI from a browser: it works like a charm.
- Downloading a binary file using the PHP script below: works like a charm.
- Downloading from the CGI output using the stream_get_contents() function: yields the same problem as described above (duplication + packet size)

Thanks for your help.
$ServerURL = 'http://127.0.0.1/testing/win2vo.cgi';
$url = parse_url($ServerURL);
 
// Eventually, this will be a more complex POST request
$request = "GET " . $url['path'] . " HTTP/1.1\n";
$request .= "Connection: Close\n";
$request .= 'Host: ' . $url['host'] . "\n\n";
 
// Eventually, "127.0.0.1" will be different so cannot assume it's local
if ($socket = fsockopen("127.0.0.1", 80, $err_num, $err_msg, 5))
{
	fwrite($socket, $request);
	
	$buffer = '';
	while (!feof($socket))
	{
		// print for testing purposes only
		print $buffer . "\n";
		$buffer .= fread($socket, 128);
	}
	
	fclose($socket);
 
	if ($houtput = fopen($BufferFileName, "wb"))
	{
		fwrite($houtput, $buffer);
		fclose($houtput);
	}
}

Open in new window

MerlinMM3Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

caterham_wwwCommented:
And you'd like to avoid chunked output? In that case, you may change the protocol from HTTP/1.1 into HTTP/1.0
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MerlinMM3Author Commented:
I changed line 5 to:

$request = "GET " . $url['path'] . " HTTP/1.0\n";

but I still have the duplication issue. However, something to point out is that the response I get from the server hosting the CGI program still has HTTP/1.1 in its header. Maybe if I could find a way to enforce the response to be HTTP 1.0. How do you do that?
0
caterham_wwwCommented:
If you've access to the server which responds (win2vo.cgi) you may try the code below in httpd.conf or in a .htaccess file in order to force and/or downgrade to http/1.0:
SetEnvIf Request_URI "/testing/win2vo.cgi" downgrade-1.0 force-response-1.0

Open in new window

0
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

MerlinMM3Author Commented:
This is getting more and more interesting. I tried adding this to the httpd.conf file. Now I get an HTTP 1.0 response but I still get the duplication problem.
0
MerlinMM3Author Commented:
The problem of duplication was solved with your first post. I just assumed I still got the duplication problem because I mistakenly put the print statement inside the fread loop instead of outside. Thanks a lot for your help caterham_www.
0
MerlinMM3Author Commented:
If some readers still have the problem, they might be interested in reading this article:

http://www.makina-corpus.org/2008/08/29/forcing-http10-apache-response-when-php-is-there/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.