Bug #79718 [Opn]: header_register_callback behaves differently for requests with no output
| From: | requinix@php.net | Date: | Fri, 19 Jun 2020 18:04:24 +0000 |
| Subject: | Bug #79718 [Opn]: header_register_callback behaves differently for requests with no output | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-227562@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79718&edit=1
ID: 79718
Updated by: requinix@php.net
Reported by: brianlmoon@php.net
Summary: header_register_callback behaves differently for
requests with no output
Status: Open
Type: Bug
Package: Output Control
Operating System: Linux
PHP Version: 7.4.7
Block user comment: N
Private report: N
New Comment:
Given that the callback is running when output is being flushed after script execution and when
autoloading is no longer available (?), this makes sense. If there is output then what that means is
you started output while the script was still running and thus autoloading was available.
Which leads to an easy solution: have your script flush at the end.
After all, shutdown callbacks happen when the engine is in the process of shutting down, so
it's hard to know exactly what capabilities will still be available.
Previous Comments:
------------------------------------------------------------------------
[2020-06-19 17:48:37] brianlmoon@php.net
Description:
------------
When a request has no output (a redirect or 204 no content for example), or the request method is
HEAD (even if the request would have generated output normally), autoloading inside the callback
provided to header_register_callback fails. For requests with output, autoloading works as expected.
Test script:
---------------
<?php
error_reporting(-1);
ini_set("display_errors", true);
header("Location: /foo");
header("X-Bar: 2");
spl_autoload_register(function($class) {
include __DIR__."/Foo.php";
});
header_register_callback(function() {
header("X-Baz: 3");
header("X-Foo: ".Foo::BAR);
});
if(!empty($_GET["output"])) {
echo "test";
}
/* Foo.php
<?php
class Foo {
const BAR = 1;
}
*/
Expected result:
----------------
The following headers are returned for all requests regardless if there is output or not.
X-Bar: 2
X-Baz: 3
X-Foo: 1
For example, with output:
< HTTP/1.1 302 Found
< Server: nginx/1.17.10
< Date: Fri, 19 Jun 2020 17:45:39 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Location: /foo
< X-Bar: 2
< X-Baz: 3
< X-Foo: 1
< Strict-Transport-Security: max-age=31536000; includeSubdomains
<
* Connection #0 to host labs.moonspot.net left intact
test
Actual result:
--------------
< HTTP/1.1 302 Found
< Server: nginx/1.17.10
< Date: Fri, 19 Jun 2020 17:46:28 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Location: /foo
< X-Bar: 2
< X-Baz: 3
< Strict-Transport-Security: max-age=31536000; includeSubdomains
<
<br />
<b>Fatal error</b>: Uncaught Error: Class 'Foo' not found in
/xxx/public/test.php:16
Stack trace:
#0 [internal function]: {closure}()
#1 {main}
thrown in <b>/xxx/public/test.php</b> on line <b>16</b><br />
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79718&edit=1