FPM: refactor fpm_php_get_string_from_table() to better match usage (#11051)

Pass the key length to improve the existence check for the key
This commit is contained in:
George Peter Banyard 2023-05-20 22:49:41 +01:00 committed by GitHub
parent a94fe872b1
commit 548e0615cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 16 deletions

View File

@ -252,13 +252,13 @@ int fpm_php_limit_extensions(char *path) /* {{{ */
} }
/* }}} */ /* }}} */
char* fpm_php_get_string_from_table(zend_string *table, char *key) /* {{{ */ bool fpm_php_is_key_in_table(zend_string *table, const char *key, size_t key_len) /* {{{ */
{ {
zval *data, *tmp; zval *data;
zend_string *str; zend_string *str;
if (!table || !key) {
return NULL; ZEND_ASSERT(table);
} ZEND_ASSERT(key);
/* inspired from ext/standard/info.c */ /* inspired from ext/standard/info.c */
@ -270,12 +270,12 @@ char* fpm_php_get_string_from_table(zend_string *table, char *key) /* {{{ */
return NULL; return NULL;
} }
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(data), str, tmp) { ZEND_HASH_FOREACH_STR_KEY(Z_ARRVAL_P(data), str) {
if (str && !strncmp(ZSTR_VAL(str), key, ZSTR_LEN(str))) { if (str && zend_string_equals_cstr(str, key, key_len)) {
return Z_STRVAL_P(tmp); return true;
} }
} ZEND_HASH_FOREACH_END(); } ZEND_HASH_FOREACH_END();
return NULL; return false;
} }
/* }}} */ /* }}} */

View File

@ -41,6 +41,6 @@ void fpm_php_soft_quit(void);
int fpm_php_init_main(void); int fpm_php_init_main(void);
int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode); int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode);
int fpm_php_limit_extensions(char *path); int fpm_php_limit_extensions(char *path);
char* fpm_php_get_string_from_table(zend_string *table, char *key); bool fpm_php_is_key_in_table(zend_string *table, const char *key, size_t key_len);
#endif #endif

View File

@ -145,7 +145,6 @@ int fpm_status_handle_request(void) /* {{{ */
bool encode_html, encode_json; bool encode_html, encode_json;
char *short_syntax, *short_post; char *short_syntax, *short_post;
char *full_pre, *full_syntax, *full_post, *full_separator; char *full_pre, *full_syntax, *full_post, *full_separator;
zend_string *_GET_str;
if (!SG(request_info).request_uri) { if (!SG(request_info).request_uri) {
return 0; return 0;
@ -170,11 +169,13 @@ int fpm_status_handle_request(void) /* {{{ */
/* STATUS */ /* STATUS */
if (fpm_status_uri && !strcmp(fpm_status_uri, SG(request_info).request_uri)) { if (fpm_status_uri && !strcmp(fpm_status_uri, SG(request_info).request_uri)) {
zend_string *_GET_str;
fpm_request_executing(); fpm_request_executing();
/* full status ? */ /* full status ? */
_GET_str = ZSTR_INIT_LITERAL("_GET", 0); _GET_str = ZSTR_INIT_LITERAL("_GET", 0);
full = (fpm_php_get_string_from_table(_GET_str, "full") != NULL); full = fpm_php_is_key_in_table(_GET_str, ZEND_STRL("full"));
short_syntax = short_post = NULL; short_syntax = short_post = NULL;
full_separator = full_pre = full_syntax = full_post = NULL; full_separator = full_pre = full_syntax = full_post = NULL;
encode_html = false; encode_html = false;
@ -218,7 +219,7 @@ int fpm_status_handle_request(void) /* {{{ */
} }
/* HTML */ /* HTML */
if (fpm_php_get_string_from_table(_GET_str, "html")) { if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("html"))) {
sapi_add_header_ex(ZEND_STRL("Content-Type: text/html"), 1, 1); sapi_add_header_ex(ZEND_STRL("Content-Type: text/html"), 1, 1);
time_format = "%d/%b/%Y:%H:%M:%S %z"; time_format = "%d/%b/%Y:%H:%M:%S %z";
encode_html = true; encode_html = true;
@ -287,7 +288,7 @@ int fpm_status_handle_request(void) /* {{{ */
} }
/* XML */ /* XML */
} else if (fpm_php_get_string_from_table(_GET_str, "xml")) { } else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("xml"))) {
sapi_add_header_ex(ZEND_STRL("Content-Type: text/xml"), 1, 1); sapi_add_header_ex(ZEND_STRL("Content-Type: text/xml"), 1, 1);
time_format = "%s"; time_format = "%s";
encode_html = true; encode_html = true;
@ -335,7 +336,7 @@ int fpm_status_handle_request(void) /* {{{ */
} }
/* JSON */ /* JSON */
} else if (fpm_php_get_string_from_table(_GET_str, "json")) { } else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("json"))) {
sapi_add_header_ex(ZEND_STRL("Content-Type: application/json"), 1, 1); sapi_add_header_ex(ZEND_STRL("Content-Type: application/json"), 1, 1);
time_format = "%s"; time_format = "%s";
@ -384,7 +385,7 @@ int fpm_status_handle_request(void) /* {{{ */
} }
/* OpenMetrics */ /* OpenMetrics */
} else if (fpm_php_get_string_from_table(_GET_str, "openmetrics")) { } else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("openmetrics"))) {
sapi_add_header_ex(ZEND_STRL("Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8"), 1, 1); sapi_add_header_ex(ZEND_STRL("Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8"), 1, 1);
time_format = "%s"; time_format = "%s";