xref: /PHP-8.1/ext/soap/tests/bug73037.phpt (revision 6ba0b068)
1--TEST--
2Bug #73037 SoapServer reports Bad Request when gzipped, var 0
3--EXTENSIONS--
4soap
5zlib
6--SKIPIF--
7<?php
8    if (!file_exists(__DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc")) {
9        echo "skip sapi/cli/tests/php_cli_server.inc required but not found";
10    }
11?>
12--FILE--
13<?php
14
15include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";
16
17function get_data($max)
18{
19    $piece = "<CD>
20        <TITLE>Empire Burlesque</TITLE>
21        <ARTIST>Bob Dylan</ARTIST>
22        <COUNTRY>USA</COUNTRY>
23        <COMPANY>Columbia</COMPANY>
24        <PRICE>10.90</PRICE>
25        <YEAR>1985</YEAR>
26    </CD>";
27
28    $begin = '<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><CATALOG>';
29    $end = '</CATALOG></soapenv:Body></soapenv:Envelope>';
30
31    $min = strlen($begin) + strlen($piece) + strlen($end);
32    $max = $max < $min ? $min : $max;
33
34    $data = $begin;
35    $data .= $piece;
36    while (strlen($data) + strlen($end) < $max) {
37        /* Randomize a bit, taking gzip in account. */
38        $tmp = str_replace(
39            array(
40                "Empire Burlesque",
41                "Bob Dylan",
42            ),
43            array(
44                md5(uniqid()),
45                sha1(uniqid()),
46            ),
47            $piece
48        );
49
50        if (strlen($begin) + strlen($tmp) + strlen($end) > $max) {
51            break;
52        }
53
54        $data .= $tmp;
55    }
56    $data .= $end;
57
58    return $data;
59}
60
61$router = "bug73037_server.php";
62$args = ["-d", "extension_dir=" . ini_get("extension_dir"), "-d", "extension=" . (substr(PHP_OS, 0, 3) == "WIN" ? "php_" : "") . "soap." . PHP_SHLIB_SUFFIX];
63if (php_ini_loaded_file()) {
64    // Necessary such that it works from a development directory in which case extension_dir might not be the real extension dir
65    $args[] = "-c";
66    $args[] = php_ini_loaded_file();
67}
68$code = <<<'PHP'
69$s = new SoapServer(NULL, array('uri' => 'http://here'));
70$s->setObject(new stdclass());
71$s->handle();
72PHP;
73
74php_cli_server_start($code, $router, $args);
75
76foreach (array(1024-1, 1024*8-3, 1024*9+1, 1024*16-1, 1024*32-5, 1024*64+3, 1024*128-7) as $k => $i) {
77    echo "Iteration $k\n\n";
78
79    /* with and without compression */
80    foreach (array(false, true) as $b) {
81        $data = get_data($i);
82        if ($b) {
83            $data = gzencode($data);
84        }
85        $len = strlen($data);
86
87        //echo "len=$len\n";
88
89        $hdrs = <<<HDRS
90POST /bug73037_server.php HTTP/1.1
91Content-Type: application/soap+xml; charset=UTF-8
92Accept: application/soap+xml, application/dime, multipart/related, text/*
93SOAPAction: "urn:adressen#adressen#SetAda"
94Expect: 100-continue
95Content-Length: ${len}
96HDRS;
97        if ($b) {
98            $hdrs .="\nContent-Encoding: gzip";
99        }
100        //echo "Headers sent:\n$hdrs\n\n";
101        $fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errstr, 5);
102        if (!$fp) {
103          die("connect failed");
104        }
105
106        if(fwrite($fp, "$hdrs\n\n$data")) {
107            $out = "";
108            while (!feof($fp)) {
109                $out .= fread($fp, 1024);
110            }
111
112            $pos = strpos($out, "<env:Text>");
113            if (false === $pos) {
114                echo $out;
115                goto cleanup;
116            }
117            $pos0 = $pos + strlen("<env:Text>");
118            $pos = strpos($out, "</env:Text>");
119            if (false === $pos) {
120                echo $out;
121                goto cleanup;
122            }
123            $len = $pos - $pos0;
124            echo substr($out, $pos0, $len);
125        }
126
127cleanup:
128        fclose($fp);
129
130        echo "\n\n";
131        }
132}
133
134?>
135--CLEAN--
136<?php
137unlink(__DIR__ . DIRECTORY_SEPARATOR . "bug73037_server.php");
138?>
139--EXPECT--
140Iteration 0
141
142Function 'CATALOG' doesn't exist
143
144Function 'CATALOG' doesn't exist
145
146Iteration 1
147
148Function 'CATALOG' doesn't exist
149
150Function 'CATALOG' doesn't exist
151
152Iteration 2
153
154Function 'CATALOG' doesn't exist
155
156Function 'CATALOG' doesn't exist
157
158Iteration 3
159
160Function 'CATALOG' doesn't exist
161
162Function 'CATALOG' doesn't exist
163
164Iteration 4
165
166Function 'CATALOG' doesn't exist
167
168Function 'CATALOG' doesn't exist
169
170Iteration 5
171
172Function 'CATALOG' doesn't exist
173
174Function 'CATALOG' doesn't exist
175
176Iteration 6
177
178Function 'CATALOG' doesn't exist
179
180Function 'CATALOG' doesn't exist
181
182