xref: /PHP-8.0/ext/curl/tests/bug77535.phpt (revision 50054459)
1--TEST--
2Bug #77535 (Invalid callback, h2 server push)
3--XFAIL--
4http2.golang.org/serverpush is gone
5--SKIPIF--
6<?php
7include 'skipif.inc';
8if (getenv("SKIP_ONLINE_TESTS")) {
9    die("skip online test");
10}
11$curl_version = curl_version();
12if ($curl_version['version_number'] < 0x073d00) {
13    exit("skip: test may crash with curl < 7.61.0");
14}
15die("skip test is slow due to timeout, and XFAILs anyway");
16?>
17--FILE--
18<?php
19class MyHttpClient
20{
21    private $mh;
22    private $curl;
23
24    public function sendRequest()
25    {
26        if (false === $this->mh = curl_multi_init()) {
27            throw new \RuntimeException('Unable to create a new cURL multi handle');
28        }
29
30        $this->addServerPushCallback();
31
32        $this->curl = curl_init();
33        curl_setopt($this->curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
34        curl_setopt($this->curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
35        curl_setopt($this->curl, CURLOPT_HEADER, false);
36        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, false);
37        curl_setopt($this->curl, CURLOPT_FAILONERROR, false);
38        curl_setopt($this->curl, CURLOPT_URL, 'https://http2.golang.org/serverpush');
39        curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
40        curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, function ($ch, $data) {
41            return \strlen($data);
42        });
43        curl_setopt($this->curl, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
44            return \strlen($data);
45        });
46        curl_multi_add_handle($this->mh, $this->curl);
47
48        $stillRunning = null;
49        while (true) {
50            do {
51                $mrc = curl_multi_exec($this->mh, $stillRunning);
52            } while (CURLM_CALL_MULTI_PERFORM === $mrc);
53
54            $info = curl_multi_info_read($this->mh);
55            while (false !== $info && $info['msg'] == CURLMSG_DONE) {
56                if (CURLMSG_DONE !== $info['msg']) {
57                    continue;
58                }
59                echo "Start handle request.\n";
60                return;
61            }
62        }
63    }
64
65    private function addServerPushCallback(): void
66    {
67
68        $callback = static function () {
69            return CURL_PUSH_OK;
70        };
71
72        curl_multi_setopt($this->mh, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
73        curl_multi_setopt($this->mh, CURLMOPT_PUSHFUNCTION, $callback);
74    }
75}
76
77$buzz = new MyHttpClient();
78$buzz->sendRequest();
79?>
80--EXPECT--
81Start handle request.
82
83