Skip to content

Commit 7afadfb

Browse files
committed
Add http_(get|clear)_last_reponse_headers() functions
This is to provide an alternative to the $http_response_header magic variable
1 parent 0de88df commit 7afadfb

17 files changed

+217
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ PHP NEWS
198198
. Changed return type of long2ip to string from string|false. (Jorg Sowa)
199199
. Fix GH-12143 (Extend the maximum precision round can handle by one digit).
200200
(SakiTakamachi)
201+
. Added the http_get_last_response_headers() and
202+
http_clear_last_response_headers() that allows retrieving the same content
203+
as the magic $http_response_header variable.
201204

202205
- XML:
203206
. Added XML_OPTION_PARSE_HUGE parser option. (nielsdos)

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@ PHP 8.4 UPGRADE NOTES
428428
. sodium_crypto_aead_aes256gcm_*() functions are now enabled on aarch64 CPUs
429429
with the ARM cryptographic extensions.
430430

431+
- Standard:
432+
. Added the http_get_last_response_headers() and
433+
http_clear_last_response_headers() that allows retrieving the same content
434+
as the magic $http_response_header variable.
435+
431436
- XSL:
432437
. Added XSLTProcessor::registerPhpFunctionNS().
433438
RFC: https://wiki.php.net/rfc/improve_callbacks_dom_and_xsl

ext/standard/basic_functions.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ PHP_RINIT_FUNCTION(basic) /* {{{ */
418418
BASIC_RINIT_SUBMODULE(dir)
419419
BASIC_RINIT_SUBMODULE(url_scanner_ex)
420420

421+
/* Initialize memory for last http headers */
422+
ZVAL_UNDEF(&BG(last_http_headers));
423+
421424
/* Setup default context */
422425
FG(default_context) = NULL;
423426

@@ -482,6 +485,9 @@ PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */
482485
BASIC_RSHUTDOWN_SUBMODULE(user_filters)
483486
BASIC_RSHUTDOWN_SUBMODULE(browscap)
484487

488+
/* Free last http headers */
489+
zval_ptr_dtor(&BG(last_http_headers));
490+
485491
BG(page_uid) = -1;
486492
BG(page_gid) = -1;
487493
return SUCCESS;

ext/standard/basic_functions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ typedef struct _php_basic_globals {
7070

7171
zval active_ini_file_section;
7272

73+
/* http_fopen_wrapper.c */
74+
zval last_http_headers;
75+
7376
/* pageinfo.c */
7477
zend_long page_uid;
7578
zend_long page_gid;

ext/standard/basic_functions.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3025,6 +3025,10 @@ function pfsockopen(string $hostname, int $port = -1, &$error_code = null, &$err
30253025
/** @refcount 1 */
30263026
function http_build_query(array|object $data, string $numeric_prefix = "", ?string $arg_separator = null, int $encoding_type = PHP_QUERY_RFC1738): string {}
30273027

3028+
function http_get_last_response_headers(): ?array {}
3029+
3030+
function http_clear_last_response_headers(): void {}
3031+
30283032
/**
30293033
* @param array|null $options
30303034
* @return array<int, array>

ext/standard/basic_functions_arginfo.h

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/http.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "SAPI.h"
2121
#include "zend_exceptions.h"
2222
#include "ext/spl/spl_exceptions.h"
23+
#include "basic_functions.h"
2324

2425
static void php_url_encode_scalar(zval *scalar, smart_str *form_str,
2526
int encoding_type, zend_ulong index_int,
@@ -360,3 +361,27 @@ PHP_FUNCTION(request_parse_body)
360361
SG(request_parse_body_context).throw_exceptions = false;
361362
memset(&SG(request_parse_body_context).options_cache, 0, sizeof(SG(request_parse_body_context).options_cache));
362363
}
364+
365+
PHP_FUNCTION(http_get_last_response_headers)
366+
{
367+
if (zend_parse_parameters_none() == FAILURE) {
368+
RETURN_THROWS();
369+
}
370+
371+
if (!Z_ISUNDEF(BG(last_http_headers))) {
372+
RETURN_COPY_VALUE(&BG(last_http_headers));
373+
} else {
374+
RETURN_NULL();
375+
}
376+
}
377+
378+
PHP_FUNCTION(http_clear_last_response_headers)
379+
{
380+
if (zend_parse_parameters_none() == FAILURE) {
381+
RETURN_THROWS();
382+
}
383+
384+
zval_ptr_dtor(&BG(last_http_headers));
385+
ZVAL_UNDEF(&BG(last_http_headers));
386+
RETURN_NULL();
387+
}

ext/standard/http_fopen_wrapper.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,13 +982,19 @@ php_stream *php_stream_url_wrap_http(php_stream_wrapper *wrapper, const char *pa
982982
{
983983
php_stream *stream;
984984
zval headers;
985+
985986
ZVAL_UNDEF(&headers);
986987

988+
zval_ptr_dtor(&BG(last_http_headers));
989+
ZVAL_UNDEF(&BG(last_http_headers));
990+
987991
stream = php_stream_url_wrap_http_ex(
988992
wrapper, path, mode, options, opened_path, context,
989993
PHP_URL_REDIRECT_MAX, HTTP_WRAPPER_HEADER_INIT, &headers STREAMS_CC);
990994

991995
if (!Z_ISUNDEF(headers)) {
996+
ZVAL_COPY(&BG(last_http_headers), &headers);
997+
992998
if (FAILURE == zend_set_local_var_str(
993999
"http_response_header", sizeof("http_response_header")-1, &headers, 0)) {
9941000
zval_ptr_dtor(&headers);

ext/standard/tests/http/bug75535.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,27 @@ $responses = array(
1414

1515
['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);
1616

17+
var_dump(http_get_last_response_headers());
18+
1719
var_dump(file_get_contents($uri));
1820
var_dump($http_response_header);
21+
var_dump(http_get_last_response_headers());
1922

2023
http_server_kill($pid);
2124

2225
?>
2326
--EXPECT--
27+
NULL
2428
string(0) ""
2529
array(2) {
2630
[0]=>
2731
string(15) "HTTP/1.0 200 Ok"
2832
[1]=>
2933
string(14) "Content-Length"
3034
}
35+
array(2) {
36+
[0]=>
37+
string(15) "HTTP/1.0 200 Ok"
38+
[1]=>
39+
string(14) "Content-Length"
40+
}

ext/standard/tests/http/bug80838.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,26 @@ $options = [
2323

2424
$ctx = stream_context_create($options);
2525

26+
var_dump(http_get_last_response_headers());
27+
2628
$fd = fopen($uri, 'rb', false, $ctx);
2729
fclose($fd);
2830
var_dump($http_response_header);
31+
var_dump(http_get_last_response_headers());
2932

3033
http_server_kill($pid);
3134

3235
?>
3336
--EXPECT--
37+
NULL
38+
array(3) {
39+
[0]=>
40+
string(32) "HTTP/1.1 101 Switching Protocols"
41+
[1]=>
42+
string(15) "Header1: Value1"
43+
[2]=>
44+
string(15) "Header2: Value2"
45+
}
3446
array(3) {
3547
[0]=>
3648
string(32) "HTTP/1.1 101 Switching Protocols"

0 commit comments

Comments
 (0)