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