php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54917 Incorrect behavior of the sockets functions when using HTTP-connections
Submitted: 2011-05-24 15:46 UTC Modified: 2011-05-29 17:34 UTC
From: euxomen at mail dot ru Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 5.3SVN-2011-05-24 (snap) OS: Windows 7, Ubuntu 10.10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: euxomen at mail dot ru
New email:
PHP Version: OS:

 

 [2011-05-24 15:46 UTC] euxomen at mail dot ru
Description:
------------
When I try to make HTTP keep-alive connection, script behaves ununderstood.

When I am making two request per one connection, using header "Connection:keep-
alive", script processes only one. Second request, although it was written into 
socket, it returns an empty result, due to the fact that the pointer is EOF.

Test script:
---------------
<?php
/*
file 1.txt on localhost contains text: 1
file 2.txt on localhost contains text: 2
*/
function run($fp, $c, $u) {
	if ($fp) {
		fwrite($fp, "GET {$u} HTTP/1.0\r\nHost: localhost\r\nConnection: {$c}\r\n\r\n");
		$data = '';
		while (!feof($fp)) {
			$data .= fgets($fp);
		}
		return $data;
	}
}

//trying to use keep-alive
$fp = stream_socket_client("tcp://localhost:80", $errno, $errstr, 30);
echo run($fp, "keep-alive", "/1.txt");
echo run($fp, "keep-alive", "/2.txt");
fclose($fp);

//trying to use close
$fp = stream_socket_client("tcp://localhost:80", $errno, $errstr, 30);
echo run($fp, "close", "/1.txt");
fclose($fp);
$fp = stream_socket_client("tcp://localhost:80", $errno, $errstr, 30);
echo run($fp, "close", "/2.txt");
fclose($fp);


Expected result:
----------------
HTTP/1.1 200 OK
Date: Tue, 24 May 2011 13:35:50 GMT
Server: Apache
Last-Modified: Tue, 24 May 2011 13:16:42 GMT
ETag: "3200000001c164-3-4a405662479e4"
Accept-Ranges: bytes
Content-Length: 3
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/plain

1
HTTP/1.1 200 OK
Date: Tue, 24 May 2011 13:35:50 GMT
Server: Apache
Last-Modified: Tue, 24 May 2011 13:16:42 GMT
ETag: "3200000001c164-3-4a405662479e4"
Accept-Ranges: bytes
Content-Length: 3
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/plain

2
HTTP/1.1 200 OK
Date: Tue, 24 May 2011 13:35:57 GMT
Server: Apache
Last-Modified: Tue, 24 May 2011 13:16:42 GMT
ETag: "3200000001c164-3-4a405662479e4"
Accept-Ranges: bytes
Content-Length: 3
Connection: close
Content-Type: text/plain

1
HTTP/1.1 200 OK
Date: Tue, 24 May 2011 13:35:58 GMT
Server: Apache
Last-Modified: Tue, 24 May 2011 13:16:53 GMT
ETag: "1700000001c166-1-4a40566ce2b4c"
Accept-Ranges: bytes
Content-Length: 1
Connection: close
Content-Type: text/plain

2

Actual result:
--------------
HTTP/1.1 200 OK
Date: Tue, 24 May 2011 13:35:50 GMT
Server: Apache
Last-Modified: Tue, 24 May 2011 13:16:42 GMT
ETag: "3200000001c164-3-4a405662479e4"
Accept-Ranges: bytes
Content-Length: 3
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/plain

1
HTTP/1.1 200 OK
Date: Tue, 24 May 2011 13:35:57 GMT
Server: Apache
Last-Modified: Tue, 24 May 2011 13:16:42 GMT
ETag: "3200000001c164-3-4a405662479e4"
Accept-Ranges: bytes
Content-Length: 3
Connection: close
Content-Type: text/plain

1
HTTP/1.1 200 OK
Date: Tue, 24 May 2011 13:35:58 GMT
Server: Apache
Last-Modified: Tue, 24 May 2011 13:16:53 GMT
ETag: "1700000001c166-1-4a40566ce2b4c"
Accept-Ranges: bytes
Content-Length: 1
Connection: close
Content-Type: text/plain

2

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-29 17:34 UTC] bjori@php.net
-Status: Open +Status: Bogus
 [2011-05-29 17:34 UTC] bjori@php.net
Your script is wrong.
feof() will not return true until the connection has timedout, which means your 
second request never gets written.

If you change the while loop to read the content-length data after the headers it 
will work fine.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 17:01:30 2024 UTC