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