ID: 25939 User updated by: mlemos at acm dot org Reported By: mlemos at acm dot org Status: Verified Bug Type: Sockets related Operating System: * PHP Version: 4.3.4 New Comment:
Actually there is a small bug in the patch. Here is the correct version: *** network.c 2003-11-11 03:06:07.000000000 -0200 --- network.c.fixed 2003-11-10 18:41:16.000000000 -0200 *************** *** 1033,1038 **** --- 1033,1049 ---- nr_bytes = recv(sock->socket, buf, count, 0); stream->eof = (nr_bytes == 0 || (nr_bytes == -1 && php_socket_errno() != EWOULDBLOCK)); + + /* We did not reach the end of data but it did not fill the buffer? + * Maybe the server closed the connection. Lets peek ahead. + */ + if (!stream->eof && (count - nr_bytes > 0)) { + int more_bytes; + + more_bytes = recv(sock->socket, buf + nr_bytes, 1, MSG_PEEK); + + stream->eof = (more_bytes == 0 || (more_bytes == -1 && php_socket_errno() != EWOULDBLOCK)); + } } if (nr_bytes > 0) { Previous Comments: ------------------------------------------------------------------------ [2003-11-04 01:05:18] mlemos at acm dot org This patch fixes the problem. What it does is to peek ahead to see if there are would be any more bytes to read or not eventually due to the closure of the connection by the server. *** php-4.3.4/main/network.c 2003-10-17 09:09:49.000000000 -0200 --- php-4.3.4/main/network.c.fixed 2003-11-04 03:57:06.000000000 -0200 *************** *** 1033,1038 **** --- 1033,1049 ---- nr_bytes = recv(sock->socket, buf, count, 0); stream->eof = (nr_bytes == 0 || (nr_bytes == -1 && php_socket_errno() != EWOULDBLOCK)); + + /* We did not reach the end of data but it did not fill the buffer? + * Maybe the server closed the connection. Lets peek ahead. + */ + if (!stream->eof && (count - nr_bytes > 0)) { + int more_bytes; + + more_bytes = recv(sock->socket, buf, 1, MSG_PEEK); + + stream->eof = (more_bytes == 0 || (more_bytes == -1 && php_socket_errno() != EWOULDBLOCK)); + } } if (nr_bytes > 0) { ------------------------------------------------------------------------ [2003-10-21 15:45:32] mlemos at acm dot org Description: ------------ It seems that since the changes introduced after PHP 4.3.1, the feof function is no longer returning true when the server closes the socket connection after transmiting all the contents. This seems to make it impossible to determine if there was a real network error or the server closed the connection normally breaking the compatibility of scripts that rely on feof to determine the end of connection condition. Reproduce code: --------------- The following script demonstrates the change of behaviour. You should try it either PHP versions before and after PHP 4.3.2 to see the difference. <?php $socket=fsockopen("www.php.net",80,$error); if(!$socket) { echo "socket opening error\n"; exit; } echo "connection opened\n"; if(!fputs($socket,"GET / HTTP/1.1\r\nHost: www.php.net\r\n\r\n")) { echo "socket writing error\n"; exit; } while(!feof($socket)) { $data=fread($socket,1000); if(!$data) { echo "socket reading error\n"; exit; } echo "read ",strlen($data)," bytes\n"; } echo "reached the end of data\n"; fclose($socket); ?> Expected result: ---------------- connection opened read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 278 bytes reached the end of data Actual result: -------------- connection opened read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 1000 bytes read 278 bytes socket reading error ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=25939&edit=1