Bug #80931 [Com]: file_get_contents() hangs with HTTP/1.1 if server doesn't close connection

From: Date: Thu, 12 Jan 2023 18:06:59 +0000
Subject: Bug #80931 [Com]: file_get_contents() hangs with HTTP/1.1 if server doesn't close connection
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-243426@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80931&edit=1

 ID:                 80931
 Comment by:         tgross at m-s dot de
 Reported by:        gilperon at gmail dot com
 Summary:            file_get_contents() hangs with HTTP/1.1 if server
                     doesn't close connection
 Status:             Verified
 Type:               Bug
 Package:            Streams related
 Operating System:   any
 PHP Version:        7.4
 Block user comment: N
 Private report:     N

 New Comment:

I can confirm this problem when using php inside a docker 4.15.0 container.

try these docker commands (In my case, I call them inside WSL2 using Docker Desktop):
This one works well (it returns fast):

> docker run --rm php:7.3 php -d default_socket_timeout=10 -r "file_get_contents('https://github.com/');"

while this one hits the timeout (in this case, 10 seconds) (remove spaces from url!):

> docker run --rm php:7.3 php -d default_socket_timeout=10 -r "file_get_contents('https
> : // www . google . com /');"

It's basically the same with PHP 7.4, 8.1, 8.2. However, with PHP 8.1 and 8.2, the command
reaches twice the timeout - so it takes 20 seconds instead of 10!

Not only file_get_contents() is affected by this problem, but also SoapClient::__construct(), for
example.


Previous Comments:
------------------------------------------------------------------------
[2023-01-09 15:14:46] dominik+phpnet at komail dot ch

I can confirm the issue of Jamie, that file_get_contents() hangs indefinitely, if it's calling
an URL in the web from a docker container. This started to occur when we updated to the latest
docker version. I'm not sure, in which version exactly, though.

Currently using php8.1 and docker4.15

------------------------------------------------------------------------
[2022-12-13 12:58:07] jamie at hazaar dot io

I have noticed this also ocurring in versions of PHP including 7.1.x and 8.1.x when used from inside
a docker container.  It appears that regardless of the protocol used, Docker will alter the request
headers to use HTTP/1.1 which is particularly problematic when the client has requested HTTP/1.0
with a "Connection: close" header as the server will not see this and as such not close
the connection as requested. 

I now have a bunch of projects that I'm trying to port to Docker containers for development and
deployment that simply do not work because of this bug thanks to both PHP and Docker not playing
nice together.

If anyone reads this and is able to confirm that PHP from inside Docker can not use
file_get_contents($url), please respond here.

------------------------------------------------------------------------
[2022-12-02 08:51:06] arjan at avoid dot org

Seems this (or a very similar) issue is back in PHP8.1.12 and 8.1.13.
Super simple test case:

<?php
print
file_get_contents('https'.'://'.'www'.'nrc'.'nl');

?>

It seems impossible to access the Dutch NRC website (national newspaper).
This takes a very long time to run. It does eventually return.
Accessing the same website from the cmdline via wget or curl is super fast.
I've tested this on multiple PHP instances around the world, they all hang when running on
PHP8.1.12 or 8.1.13. 
This same script ran fine (fast) on PHP 7.4.

------------------------------------------------------------------------
[2022-11-23 05:20:34] samira dot akhlaqi314 at gmail dot com

That's great. I was impressed by your writing. I am happy to see such a topic. Please come to
my blog and read it.

<https://www.subwaylistens.me/>/.php.net

------------------------------------------------------------------------
[2021-09-29 10:26:48] cmb@php.net

Related To: Bug #60465

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=80931


--
Edit this bug report at https://bugs.php.net/bug.php?id=80931&edit=1


Thread (35 messages)

« previous php.bugs (#243426) next »