xref: /PHP-8.2/ext/standard/tests/http/bug47021.phpt (revision 95f25837)
1--TEST--
2Bug #47021 (SoapClient stumbles over WSDL delivered with "Transfer-Encoding: chunked")
3--INI--
4allow_url_fopen=1
5--SKIPIF--
6<?php require 'server.inc'; http_server_skipif(); ?>
7--FILE--
8<?php
9require 'server.inc';
10
11function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
12
13    switch($notification_code) {
14        case STREAM_NOTIFY_MIME_TYPE_IS:
15            echo "Type='$message'\n";
16        break;
17        case STREAM_NOTIFY_FILE_SIZE_IS:
18            echo "Size=$bytes_max\n";
19            break;
20    }
21}
22
23function do_test($num_spaces, $leave_trailing_space=false) {
24  // SOAPClient exhibits the bug because it forces HTTP/1.1,
25  // whereas file_get_contents() uses HTTP/1.0 by default.
26  $options = [
27    'http' => [
28      'protocol_version' => '1.1',
29      'header' => 'Connection: Close'
30    ],
31  ];
32
33  $ctx = stream_context_create($options);
34  stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
35
36  $spaces = str_repeat(' ', $num_spaces);
37  $trailing = ($leave_trailing_space ? ' ' : '');
38  $responses = [
39    "data://text/plain,HTTP/1.1 200 OK\r\n"
40      . "Content-Type:{$spaces}text/plain{$trailing}\r\n"
41      . "Transfer-Encoding:{$spaces}Chunked{$trailing}\r\n\r\n"
42      . "5\nHello\n0\n",
43    "data://text/plain,HTTP/1.1 200 OK\r\n"
44      . "Content-Type\r\n" // Deliberately invalid header
45      . "Content-Length:{$spaces}5{$trailing}\r\n\r\n"
46      . "World"
47  ];
48  ['pid' => $pid, 'uri' => $uri] = http_server($responses);
49
50  echo file_get_contents($uri, false, $ctx);
51  echo "\n";
52  echo file_get_contents($uri, false, $ctx);
53  echo "\n";
54
55  http_server_kill($pid);
56}
57
58// Chunked decoding should be recognised by the HTTP stream wrapper regardless of whitespace
59// Transfer-Encoding:Chunked
60do_test(0);
61echo "\n";
62// Transfer-Encoding: Chunked
63do_test(1);
64echo "\n";
65// Transfer-Encoding:  Chunked
66do_test(2);
67echo "\n";
68// Trailing space at end of header
69do_test(1, true);
70echo "\n";
71
72?>
73--EXPECT--
74Type='text/plain'
75Hello
76Size=5
77World
78
79Type='text/plain'
80Hello
81Size=5
82World
83
84Type='text/plain'
85Hello
86Size=5
87World
88
89Type='text/plain'
90Hello
91Size=5
92World
93