Skip to content

Commit 7f8f99e

Browse files
committed
Add function to determine name
1 parent e2b1709 commit 7f8f99e

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

ext/curl/interface.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,38 @@ ZEND_DECLARE_MODULE_GLOBALS(curl)
6262
# define php_curl_ret(__ret) RETVAL_FALSE; return;
6363
#endif
6464

65+
// php_curl_option_get_name(CURLOPT_HTTPHEADER) -> "HTTPHEADER"
66+
static const char * php_curl_option_get_name(zend_long option) {
67+
#if LIBCURL_VERSION_NUM >= 0x074900
68+
const struct curl_easyoption * opt = curl_easy_option_by_id(option);
69+
if (EXPECTED(opt != NULL)) {
70+
return opt->name;
71+
}
72+
#else
73+
const char * prefix = "CURLOPT_";
74+
const size_t prefix_len = sizeof(prefix) - 1;
75+
zend_string *key;
76+
zend_constant *constant;
77+
78+
ZEND_HASH_FOREACH_STR_KEY_PTR(EG(zend_constants), key, constant) {
79+
if (!key
80+
|| Z_TYPE(constant->value) != IS_LONG
81+
|| strncmp(ZSTR_VAL(key), prefix, prefix_len) != 0) {
82+
continue;
83+
}
84+
85+
if (Z_LVAL(constant->value) == option) {
86+
return ZSTR_VAL(key) + prefix_len;
87+
}
88+
} ZEND_HASH_FOREACH_END();
89+
#endif
90+
return "UNKNOWN_OPTION";
91+
}
92+
6593
static zend_result php_curl_option_str(php_curl *ch, zend_long option, const char *str, const size_t len)
6694
{
6795
if (zend_char_has_nul_byte(str, len)) {
68-
const struct curl_easyoption *option_info = curl_easy_option_by_id(option);
69-
zend_value_error("%s(): cURL option CURLOPT_%s must not contain any null bytes", get_active_function_name(), option_info->name);
96+
zend_value_error("%s(): cURL option CURLOPT_%s must not contain any null bytes", get_active_function_name(), php_curl_option_get_name(option));
7097
return FAILURE;
7198
}
7299

@@ -2088,8 +2115,7 @@ static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue
20882115
struct curl_slist *slist = NULL;
20892116

20902117
if (Z_TYPE_P(zvalue) != IS_ARRAY) {
2091-
const struct curl_easyoption *option_info = curl_easy_option_by_id(option);
2092-
zend_type_error("%s(): The CURLOPT_%s option must have an array value", get_active_function_name(), option_info->name);
2118+
zend_type_error("%s(): The CURLOPT_%s option must have an array value", get_active_function_name(), php_curl_option_get_name(option));
20932119
return FAILURE;
20942120
}
20952121

@@ -2101,8 +2127,7 @@ static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue
21012127
if (zend_str_has_nul_byte(val)) {
21022128
curl_slist_free_all(slist);
21032129
zend_tmp_string_release(tmp_val);
2104-
const struct curl_easyoption *option_info = curl_easy_option_by_id(option);
2105-
zend_value_error("%s(): cURL option CURLOPT_%s must not contain any null bytes", get_active_function_name(), option_info->name);
2130+
zend_value_error("%s(): cURL option CURLOPT_%s must not contain any null bytes", get_active_function_name(), php_curl_option_get_name(option));
21062131
return FAILURE;
21072132
}
21082133

0 commit comments

Comments
 (0)