php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51268 fsockopen+fread behaves differently with port 443 vs. port 80
Submitted: 2010-03-11 01:49 UTC Modified: 2010-05-19 16:46 UTC
From: arlo at arlomedia dot com Assigned:
Status: Not a bug Package: Network related
PHP Version: 5.2.13 OS: Red Hat Linux
Private report: No CVE-ID: None
 [2010-03-11 01:49 UTC] arlo at arlomedia dot com
Description:
------------
If I open a network connection with fsockopen on port 80, then read the first 
chunk of the response with fread, I receive the response headers plus enough of 
the response body to fulfill the read length that I requested.

However, if I do the same thing over port 443, I receive only the response headers 
in my initial fread command, regardless of their length. I don't receive any of 
the response body until I call fread again.

The script below demonstrates this. If you run it as shown, you will see the 
header plus part of the body as response 1, and a continuation of the body as 
response 2. But if you change $connection_port to 443, you will see only the 
header as response 1, with the body starting at response 2.

I repeated this issue on one server running PHP 5.2.13 and another running 5.3.0.

Test script:
---------------
<?php
	$connection_port = 80;

	$source_domain = "www.msgen.com";
	$source_path = "/assembled/home.html";

	$source_host = ($connection_port == 443) ? "ssl://$source_domain" : $source_domain ;
	$request = "GET /$source_path HTTP/1.0\nHost: $source_domain\nConnection: close\n\n";

	$socket = fsockopen($source_host, $connection_port, $error_number, $error_string);
	fputs($socket, $request);

	$response_1 = fread($socket, 4096);
	$response_2 = fread($socket, 4096);
	
	print "response 1:<br />\n".htmlentities($response_1);
	print "<br /><br />\n";
	print "response 2:<br />\n".htmlentities($response_2);
?>

Expected result:
----------------
I would expect fread to return the same results regardless of the port used for 
fsockopen.

Actual result:
--------------
The first instance of fread stops at the end of the headers when using port 443.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-19 15:36 UTC] mike@php.net
-Status: Open +Status: Bogus
 [2010-05-19 15:36 UTC] mike@php.net
Quoting documentation:

Warning

When reading from anything that is not a regular local file, such as streams returned when reading remote files or from popen() and fsockopen(), reading will stop after a packet is available. This means that you should collect the data together in chunks as shown in the examples below.
 [2010-05-19 16:46 UTC] arlo at arlomedia dot com
This comment doesn't address my issue. The issue is that the function behaves 
differently depending on the port used for the remote connection. Over port 80, I 
requested 4096 bytes of data from fread and got 4096 bytes of data, but over port 
443 I only got the headers. Why is this not consistent?

The practical result is that I set up a utility using port 80 on a testing server, 
then when I tried to implement it on port 443 on a development server, I had to 
troubleshoot and then rework the code because of this undocumented difference.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Nov 13 22:01:29 2019 UTC