xref: /PHP-8.3/ext/standard/tests/http/bug38802.phpt (revision 95f25837)
1--TEST--
2Bug #38802 (ignore_errors and max_redirects)
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 genResponses($server) {
12    $uri = 'http://' . stream_socket_get_name($server, false);
13    yield "data://text/plain,HTTP/1.1 302 Moved Temporarily\r\nLocation: $uri/foo/bar2\r\n\r\n1";
14    yield "data://text/plain,HTTP/1.1 301 Moved Permanently\r\nLocation: $uri/foo/bar3\r\n\r\n";
15    yield "data://text/plain,HTTP/1.1 302 Moved Temporarily\r\nLocation: $uri/foo/bar4\r\n\r\n3";
16    yield "data://text/plain,HTTP/1.1 200 OK\r\n\r\ndone.";
17}
18
19function do_test($context_options) {
20
21    $context = stream_context_create(array('http' => $context_options));
22
23	$uri = null;
24    ['pid' => $pid, 'uri' => $uri ] = http_server('genResponses', $output);
25
26    $fd = fopen("$uri/foo/bar", 'rb', false, $context);
27    var_dump($fd);
28
29    if ($fd) {
30        $meta_data = stream_get_meta_data($fd);
31        var_dump($meta_data['wrapper_data']);
32
33        var_dump(stream_get_contents($fd));
34    }
35
36    fseek($output, 0, SEEK_SET);
37    var_dump(stream_get_contents($output));
38
39    http_server_kill($pid);
40}
41
42echo "-- Test: follow all redirections --\n";
43
44do_test(array(), 4);
45
46echo "-- Test: fail after 2 redirections --\n";
47
48do_test(array('max_redirects' => 2), 2);
49
50echo "-- Test: fail at first redirection --\n";
51
52do_test(array('max_redirects' => 0), 1);
53
54echo "-- Test: fail at first redirection (2) --\n";
55
56do_test(array('max_redirects' => 1), 1);
57
58echo "-- Test: return at first redirection --\n";
59
60do_test(array('max_redirects' => 0, 'ignore_errors' => 1), 1);
61
62echo "-- Test: return at first redirection (2) --\n";
63
64do_test(array('max_redirects' => 1, 'ignore_errors' => 1), 1);
65
66echo "-- Test: return at second redirection --\n";
67
68do_test(array('max_redirects' => 2, 'ignore_errors' => 1), 2);
69
70?>
71--EXPECTF--
72-- Test: follow all redirections --
73resource(%d) of type (stream)
74array(7) {
75  [0]=>
76  string(30) "HTTP/1.1 302 Moved Temporarily"
77  [1]=>
78  string(%d) "Location: http://%s:%d/foo/bar2"
79  [2]=>
80  string(30) "HTTP/1.1 301 Moved Permanently"
81  [3]=>
82  string(%d) "Location: http://%s:%d/foo/bar3"
83  [4]=>
84  string(30) "HTTP/1.1 302 Moved Temporarily"
85  [5]=>
86  string(%d) "Location: http://%s:%d/foo/bar4"
87  [6]=>
88  string(15) "HTTP/1.1 200 OK"
89}
90string(5) "done."
91string(%d) "GET /foo/bar HTTP/1.1
92Host: %s:%d
93Connection: close
94
95GET /foo/bar2 HTTP/1.1
96Host: %s:%d
97Connection: close
98
99GET /foo/bar3 HTTP/1.1
100Host: %s:%d
101Connection: close
102
103GET /foo/bar4 HTTP/1.1
104Host: %s:%d
105Connection: close
106
107"
108-- Test: fail after 2 redirections --
109
110Warning: fopen(http://%s:%d/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
111bool(false)
112string(%d) "GET /foo/bar HTTP/1.1
113Host: %s:%d
114Connection: close
115
116GET /foo/bar2 HTTP/1.1
117Host: %s:%d
118Connection: close
119
120"
121-- Test: fail at first redirection --
122
123Warning: fopen(http://%s:%d/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
124bool(false)
125string(%d) "GET /foo/bar HTTP/1.1
126Host: %s:%d
127Connection: close
128
129"
130-- Test: fail at first redirection (2) --
131
132Warning: fopen(http://%s:%d/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
133bool(false)
134string(%d) "GET /foo/bar HTTP/1.1
135Host: %s:%d
136Connection: close
137
138"
139-- Test: return at first redirection --
140resource(%d) of type (stream)
141array(2) {
142  [0]=>
143  string(30) "HTTP/1.1 302 Moved Temporarily"
144  [1]=>
145  string(%d) "Location: http://%s:%d/foo/bar2"
146}
147string(1) "1"
148string(%d) "GET /foo/bar HTTP/1.1
149Host: %s:%d
150Connection: close
151
152"
153-- Test: return at first redirection (2) --
154resource(%d) of type (stream)
155array(2) {
156  [0]=>
157  string(30) "HTTP/1.1 302 Moved Temporarily"
158  [1]=>
159  string(%d) "Location: http://%s:%d/foo/bar2"
160}
161string(1) "1"
162string(%d) "GET /foo/bar HTTP/1.1
163Host: %s:%d
164Connection: close
165
166"
167-- Test: return at second redirection --
168resource(%d) of type (stream)
169array(4) {
170  [0]=>
171  string(30) "HTTP/1.1 302 Moved Temporarily"
172  [1]=>
173  string(%d) "Location: http://%s:%d/foo/bar2"
174  [2]=>
175  string(30) "HTTP/1.1 301 Moved Permanently"
176  [3]=>
177  string(%d) "Location: http://%s:%d/foo/bar3"
178}
179string(0) ""
180string(%d) "GET /foo/bar HTTP/1.1
181Host: %s:%d
182Connection: close
183
184GET /foo/bar2 HTTP/1.1
185Host: %s:%d
186Connection: close
187
188"
189