Skip to content

Add http_last_response_headers() function #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ static const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(rawurlencode, arginfo_rawurlencode)
PHP_FE(rawurldecode, arginfo_rawurldecode)
PHP_FE(http_build_query, arginfo_http_build_query)
PHP_FE(http_last_response_headers, arginfo_http_last_response_headers)

#if defined(HAVE_SYMLINK) || defined(PHP_WIN32)
PHP_FE(readlink, arginfo_readlink)
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,8 @@ function pfsockopen(string $hostname, int $port = -1, &$errno = null, &$errstr =

function http_build_query(array|object $data, string $numeric_prefix = "", $arg_separator = UNKNOWN, int $enc_type = PHP_QUERY_RFC1738): string|false {}

function http_last_response_headers(bool $format = false): array|false {}

/* image.c */

function image_type_to_mime_type(int $image_type): string {}
Expand Down
4 changes: 4 additions & 0 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_http_build_query, 0, 1, MAY_BE_S
ZEND_ARG_TYPE_INFO(0, enc_type, IS_LONG, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_http_last_response_headers, 0, 0, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, format, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_image_type_to_mime_type, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, image_type, IS_LONG, 0)
ZEND_END_ARG_INFO()
Expand Down
20 changes: 20 additions & 0 deletions ext/standard/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "php_http.h"
#include "php_ini.h"
#include "url.h"
#include "php_fopen_wrappers.h"

ZEND_DECLARE_MODULE_GLOBALS(http)

#define URL_DEFAULT_ARG_SEP "&"

Expand Down Expand Up @@ -261,4 +264,21 @@ PHP_FUNCTION(http_build_query)

RETURN_NEW_STR(formstr.s);
}

PHP_FUNCTION(http_last_response_headers)
{
if (Z_TYPE(HTTP_G(last_headers)) != IS_ARRAY) {
RETURN_FALSE;
}

zend_long format = 0;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(format)
ZEND_PARSE_PARAMETERS_END();

array_init(return_value);
parse_http_headers(&HTTP_G(last_headers), return_value, format);
}
/* }}} */
4 changes: 4 additions & 0 deletions ext/standard/http_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
#define HTTP_WRAPPER_HEADER_INIT 1
#define HTTP_WRAPPER_REDIRECTED 2

ZEND_DECLARE_MODULE_GLOBALS(http)

static inline void strip_header(char *header_bag, char *lc_header_bag,
const char *lc_header_name)
{
Expand Down Expand Up @@ -982,9 +984,11 @@ php_stream *php_stream_url_wrap_http(php_stream_wrapper *wrapper, const char *pa
PHP_URL_REDIRECT_MAX, HTTP_WRAPPER_HEADER_INIT, &headers STREAMS_CC);

if (!Z_ISUNDEF(headers)) {
HTTP_G(last_headers) = headers;
if (FAILURE == zend_set_local_var_str(
"http_response_header", sizeof("http_response_header")-1, &headers, 1)) {
zval_ptr_dtor(&headers);
zval_ptr_dtor(&HTTP_G(last_headers));
}
}

Expand Down
10 changes: 10 additions & 0 deletions ext/standard/php_fopen_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
+----------------------------------------------------------------------+
*/

ZEND_BEGIN_MODULE_GLOBALS(http)
zval last_headers;
ZEND_END_MODULE_GLOBALS(http)

#ifdef ZTS
#define HTTP_G(v) TSRMG(http_globals_id, zend_http_globals *, v)
#else
#define HTTP_G(v) (http_globals.v)
#endif

#ifndef PHP_FOPEN_WRAPPERS_H
#define PHP_FOPEN_WRAPPERS_H

Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
#define php_url_encode_hash(ht, formstr) php_url_encode_hash_ex((ht), (formstr), NULL, 0, NULL, 0, NULL, 0, NULL)

PHP_FUNCTION(http_build_query);
PHP_FUNCTION(http_last_response_headers);

#endif
16 changes: 10 additions & 6 deletions ext/standard/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,6 @@ PHP_FUNCTION(get_headers)
char *url;
size_t url_len;
php_stream *stream;
zval *prev_val, *hdr = NULL;
zend_long format = 0;
zval *zcontext = NULL;
php_stream_context *context;
Expand All @@ -687,8 +686,16 @@ PHP_FUNCTION(get_headers)
}

array_init(return_value);
parse_http_headers(&stream->wrapperdata, return_value, format);
php_stream_close(stream);
}
/* }}} */

void parse_http_headers(zval *headers, zval *return_value, zend_long format)
{
zval *prev_val, *hdr = NULL;

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&stream->wrapperdata), hdr) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(headers), hdr) {
if (Z_TYPE_P(hdr) != IS_STRING) {
continue;
}
Expand All @@ -709,7 +716,7 @@ PHP_FUNCTION(get_headers)

if ((prev_val = zend_hash_str_find(Z_ARRVAL_P(return_value), Z_STRVAL_P(hdr), (p - Z_STRVAL_P(hdr)))) == NULL) {
add_assoc_stringl_ex(return_value, Z_STRVAL_P(hdr), (p - Z_STRVAL_P(hdr)), s, (Z_STRLEN_P(hdr) - (s - Z_STRVAL_P(hdr))));
} else { /* some headers may occur more than once, therefor we need to remake the string into an array */
} else { /* some headers may occur more than once, therefore we need to remake the string into an array */
convert_to_array(prev_val);
add_next_index_stringl(prev_val, s, (Z_STRLEN_P(hdr) - (s - Z_STRVAL_P(hdr))));
}
Expand All @@ -720,7 +727,4 @@ PHP_FUNCTION(get_headers)
}
}
} ZEND_HASH_FOREACH_END();

php_stream_close(stream);
}
/* }}} */
1 change: 1 addition & 0 deletions ext/standard/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ PHPAPI size_t php_raw_url_decode(char *str, size_t len); /* return value: length
PHPAPI zend_string *php_url_encode(char const *s, size_t len);
PHPAPI zend_string *php_raw_url_encode(char const *s, size_t len);
PHPAPI char *php_replace_controlchars_ex(char *str, size_t len);
void parse_http_headers(zval *headers, zval *return_value, zend_long format);

PHP_FUNCTION(parse_url);
PHP_FUNCTION(urlencode);
Expand Down