Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpSocket does not support connection keep-alive #1961

Closed
ghost opened this issue Oct 11, 2013 · 6 comments
Closed

HttpSocket does not support connection keep-alive #1961

ghost opened this issue Oct 11, 2013 · 6 comments

Comments

@ghost
Copy link

ghost commented Oct 11, 2013

Created by Christophe Vandeplas, 4th May 2012. (originally Lighthouse ticket #2854):


What I did

  1. create an HttpSocket()
  2. set the request parameters with 'Connection' => 'keep-alive'
  3. do more than one request using the socket

What happened

Only one connection was performed.

What I expected to happen

The same socket/connection is reused.

Code example

$HttpSocket = new HttpSocket();
$uri = $this->Server->data['Server']['url'].'/events';
$request = array(
        'header' => array(
                'Accept' => 'application/xml',
                'Content-Type' => 'application/xml',
                'Connection' => 'keep-alive'
        )
);
for ($i=1; $i< 5; $i++) {
    $response = $HttpSocket->get($this->Server->data['Server']['url'].'/events/'.$i, '', $request);
    print_r($response);
}
$HttpSocket->disconnect();

This only prints one $response.
The subsequent $response are empty:

HttpResponse Object
(
    [body] => 
    [headers] => Array
        (
        )

    [cookies] => Array
        (
        )

    [httpVersion] => HTTP/1.1
    [code] => 0
    [reasonPhrase] => 
    [raw] => 
)

It seems that someone else had the same problem/feature-request in 2009: http://www.mail-archive.com/tickets-cakephp`@googlegroups`.com/msg01754.html

Thanks

@ravage84
Copy link
Member

Link above is broken, should be this:
https://www.mail-archive.com/tickets-cakephp@googlegroups.com/msg01754.html

@ravage84
Copy link
Member

After some debugging and research I think this is a limitation of the used function stream_socket_client().

Anyway, I couldn't get it to work, even when I made the connection persistent.

Then i tried it outside of CakePHP with the bare minimum:

<?php
$errno = null;
$errstr = null;
$timeout = 20;

$data = 'GET / HTTP/1.1
Host: www.example.com
Connection: Keep-Alive
User-Agent: CakePHP
Accept: text/html
Content-Type: text/html

';

$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, $timeout);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    for ($i=1; $i< 5; $i++) {
        echo "<h2>try $i</h2>";

        fwrite($fp, $data);
        while (!feof($fp)) {
            $buffer = fgets($fp, 1024);
            echo $buffer;
        }
        echo "<h2>end $i</h2>";
    }
}
fclose($fp);

It fails at feof($fp) after the second write.

Hope this link is still valid to show the result of the script above:
http://69.195.223.90/21037-2/

@markstory I guess this is no issue anymore after the recent HttpSocket rewrite, right?

@markstory
Copy link
Member

It might still be an issue as the new Http\Client still uses PHP streams under the hood. I've not looked at whether or not they support keep alives though.

@tersmitten
Copy link
Contributor

Have a look at Socket.php from Zend Framework (using stream_socket_client). There seems to be support for keep alives.

@ravage84
Copy link
Member

@dereuromark dereuromark modified the milestones: 2.6.0, Future Jul 5, 2014
@markstory markstory modified the milestones: 2.6.0, 2.7.0 Nov 19, 2014
@markstory markstory modified the milestones: 2.7.0, 2.7.1 Jul 12, 2015
@markstory markstory modified the milestones: 2.7.1, 2.7.2, 2.8.0 Jul 24, 2015
@markstory markstory modified the milestones: 2.8.0, 2.9.0 Feb 7, 2016
@lorenzo
Copy link
Member

lorenzo commented May 20, 2016

Closing all enhancement requests that were not actioned in a long time. If you still think this a good idea and would like to help making this happen, please leave a comment.

@lorenzo lorenzo closed this as completed May 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants