Skip to content

Commit 75e306b

Browse files
committed
curl: Deduplicate features array and fix coding style
1 parent 8c860ce commit 75e306b

File tree

1 file changed

+86
-139
lines changed

1 file changed

+86
-139
lines changed

ext/curl/interface.c

Lines changed: 86 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,54 @@ static zend_object *curl_clone_obj(zend_object *object);
237237
php_curl *init_curl_handle_into_zval(zval *curl);
238238
static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpostfields);
239239

240+
struct php_curl_feature {
241+
const char *name;
242+
int bitmask;
243+
};
244+
245+
/* To update on each new cURL release using src/main.c in cURL sources */
246+
static const struct php_curl_feature php_curl_features[] = {
247+
{ "AsynchDNS", CURL_VERSION_ASYNCHDNS },
248+
{ "CharConv", CURL_VERSION_CONV },
249+
{ "Debug", CURL_VERSION_DEBUG },
250+
{ "GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE },
251+
{ "IDN", CURL_VERSION_IDN },
252+
{ "IPv6", CURL_VERSION_IPV6 },
253+
{ "krb4", CURL_VERSION_KERBEROS4 },
254+
{ "Largefile", CURL_VERSION_LARGEFILE },
255+
{ "libz", CURL_VERSION_LIBZ },
256+
{ "NTLM", CURL_VERSION_NTLM },
257+
{ "NTLMWB", CURL_VERSION_NTLM_WB },
258+
{ "SPNEGO", CURL_VERSION_SPNEGO },
259+
{ "SSL", CURL_VERSION_SSL },
260+
{ "SSPI", CURL_VERSION_SSPI },
261+
{ "TLS-SRP", CURL_VERSION_TLSAUTH_SRP },
262+
{ "HTTP2", CURL_VERSION_HTTP2 },
263+
{ "GSSAPI", CURL_VERSION_GSSAPI },
264+
{ "KERBEROS5", CURL_VERSION_KERBEROS5 },
265+
{ "UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS },
266+
{ "PSL", CURL_VERSION_PSL },
267+
{ "HTTPS_PROXY", CURL_VERSION_HTTPS_PROXY },
268+
{ "MULTI_SSL", CURL_VERSION_MULTI_SSL },
269+
{ "BROTLI", CURL_VERSION_BROTLI },
270+
#if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
271+
{ "ALTSVC", CURL_VERSION_ALTSVC },
272+
#endif
273+
#if LIBCURL_VERSION_NUM >= 0x074200 /* Available since 7.66.0 */
274+
{ "HTTP3", CURL_VERSION_HTTP3 },
275+
#endif
276+
#if LIBCURL_VERSION_NUM >= 0x074800 /* Available since 7.72.0 */
277+
{ "UNICODE", CURL_VERSION_UNICODE },
278+
{ "ZSTD", CURL_VERSION_ZSTD },
279+
#endif
280+
#if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
281+
{ "HSTS", CURL_VERSION_HSTS },
282+
#endif
283+
#if LIBCURL_VERSION_NUM >= 0x074c00 /* Available since 7.76.0 */
284+
{ "GSASL", CURL_VERSION_GSASL },
285+
#endif
286+
};
287+
240288
/* {{{ PHP_INI_BEGIN */
241289
PHP_INI_BEGIN()
242290
PHP_INI_ENTRY("curl.cainfo", "", PHP_INI_SYSTEM, NULL)
@@ -253,69 +301,17 @@ PHP_MINFO_FUNCTION(curl)
253301

254302
d = curl_version_info(CURLVERSION_NOW);
255303
php_info_print_table_start();
256-
php_info_print_table_row(2, "cURL support", "enabled");
304+
php_info_print_table_row(2, "cURL support", "enabled");
257305
php_info_print_table_row(2, "cURL Information", d->version);
258306
snprintf(str, sizeof(str), "%d", d->age);
259307
php_info_print_table_row(2, "Age", str);
260308

261-
/* To update on each new cURL release using src/main.c in cURL sources */
262-
/* make sure to sync this list with curl_version as well */
263309
if (d->features) {
264-
struct feat {
265-
const char *name;
266-
int bitmask;
267-
};
268-
269310
unsigned int i;
270311

271-
static const struct feat feats[] = {
272-
{"AsynchDNS", CURL_VERSION_ASYNCHDNS},
273-
{"CharConv", CURL_VERSION_CONV},
274-
{"Debug", CURL_VERSION_DEBUG},
275-
{"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
276-
{"IDN", CURL_VERSION_IDN},
277-
{"IPv6", CURL_VERSION_IPV6},
278-
{"krb4", CURL_VERSION_KERBEROS4},
279-
{"Largefile", CURL_VERSION_LARGEFILE},
280-
{"libz", CURL_VERSION_LIBZ},
281-
{"NTLM", CURL_VERSION_NTLM},
282-
{"NTLMWB", CURL_VERSION_NTLM_WB},
283-
{"SPNEGO", CURL_VERSION_SPNEGO},
284-
{"SSL", CURL_VERSION_SSL},
285-
{"SSPI", CURL_VERSION_SSPI},
286-
{"TLS-SRP", CURL_VERSION_TLSAUTH_SRP},
287-
{"HTTP2", CURL_VERSION_HTTP2},
288-
{"GSSAPI", CURL_VERSION_GSSAPI},
289-
{"KERBEROS5", CURL_VERSION_KERBEROS5},
290-
{"UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS},
291-
{"PSL", CURL_VERSION_PSL},
292-
{"HTTPS_PROXY", CURL_VERSION_HTTPS_PROXY},
293-
{"MULTI_SSL", CURL_VERSION_MULTI_SSL},
294-
{"BROTLI", CURL_VERSION_BROTLI},
295-
#if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
296-
{"ALTSVC", CURL_VERSION_ALTSVC},
297-
#endif
298-
#if LIBCURL_VERSION_NUM >= 0x074200 /* Available since 7.66.0 */
299-
{"HTTP3", CURL_VERSION_HTTP3},
300-
#endif
301-
#if LIBCURL_VERSION_NUM >= 0x074800 /* Available since 7.72.0 */
302-
{"UNICODE", CURL_VERSION_UNICODE},
303-
{"ZSTD", CURL_VERSION_ZSTD},
304-
#endif
305-
#if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
306-
{"HSTS", CURL_VERSION_HSTS},
307-
#endif
308-
#if LIBCURL_VERSION_NUM >= 0x074c00 /* Available since 7.76.0 */
309-
{"GSASL", CURL_VERSION_GSASL},
310-
#endif
311-
{NULL, 0}
312-
};
313-
314312
php_info_print_table_row(1, "Features");
315-
for(i=0; i<sizeof(feats)/sizeof(feats[0]); i++) {
316-
if (feats[i].name) {
317-
php_info_print_table_row(2, feats[i].name, d->features & feats[i].bitmask ? "Yes" : "No");
318-
}
313+
for (i = 0; i < sizeof(php_curl_features) / sizeof(php_curl_features[0]); i++) {
314+
php_info_print_table_row(2, php_curl_features[i].name, d->features & php_curl_features[i].bitmask ? "Yes" : "No");
319315
}
320316
}
321317

@@ -513,6 +509,7 @@ static HashTable *curl_get_gc(zend_object *object, zval **table, int *n)
513509
zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.prereq);
514510
}
515511
#endif
512+
516513
#if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
517514
if (ZEND_FCC_INITIALIZED(curl->handlers.sshhostkey)) {
518515
zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.sshhostkey);
@@ -728,8 +725,8 @@ static int curl_prereqfunction(void *clientp, char *conn_primary_ip, char *conn_
728725
// gets called. Return CURL_PREREQFUNC_OK immediately in this case to avoid
729726
// zend_call_known_fcc() with an uninitialized FCC.
730727
if (!ZEND_FCC_INITIALIZED(ch->handlers.prereq)) {
731-
return rval;
732-
}
728+
return rval;
729+
}
733730

734731
#if PHP_CURL_DEBUG
735732
fprintf(stderr, "curl_prereqfunction() called\n");
@@ -924,42 +921,42 @@ static int curl_debug(CURL *handle, curl_infotype type, char *data, size_t size,
924921
{
925922
php_curl *ch = (php_curl *)clientp;
926923

927-
#if PHP_CURL_DEBUG
928-
fprintf(stderr, "curl_debug() called\n");
929-
fprintf(stderr, "type = %d, data = %s\n", type, data);
930-
#endif
924+
#if PHP_CURL_DEBUG
925+
fprintf(stderr, "curl_debug() called\n");
926+
fprintf(stderr, "type = %d, data = %s\n", type, data);
927+
#endif
931928

932929
// Implicitly store the headers for compatibility with CURLINFO_HEADER_OUT
933930
// used as a Curl option. Previously, setting CURLINFO_HEADER_OUT set curl_debug
934931
// as the CURLOPT_DEBUGFUNCTION and stored the debug data when type is set to
935932
// CURLINFO_HEADER_OUT. For backward compatibility, we now store the headers
936933
// but also call the user-callback function if available.
937-
if (type == CURLINFO_HEADER_OUT) {
938-
if (ch->header.str) {
939-
zend_string_release_ex(ch->header.str, 0);
940-
}
941-
ch->header.str = zend_string_init(data, size, 0);
942-
}
934+
if (type == CURLINFO_HEADER_OUT) {
935+
if (ch->header.str) {
936+
zend_string_release_ex(ch->header.str, 0);
937+
}
938+
ch->header.str = zend_string_init(data, size, 0);
939+
}
943940

944-
if (!ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
945-
return 0;
946-
}
941+
if (!ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
942+
return 0;
943+
}
947944

948-
zval args[3];
945+
zval args[3];
949946

950-
GC_ADDREF(&ch->std);
951-
ZVAL_OBJ(&args[0], &ch->std);
952-
ZVAL_LONG(&args[1], type);
953-
ZVAL_STRINGL(&args[2], data, size);
947+
GC_ADDREF(&ch->std);
948+
ZVAL_OBJ(&args[0], &ch->std);
949+
ZVAL_LONG(&args[1], type);
950+
ZVAL_STRINGL(&args[2], data, size);
954951

955-
ch->in_callback = true;
956-
zend_call_known_fcc(&ch->handlers.debug, NULL, /* param_count */ 3, args, /* named_params */ NULL);
957-
ch->in_callback = false;
952+
ch->in_callback = true;
953+
zend_call_known_fcc(&ch->handlers.debug, NULL, /* param_count */ 3, args, /* named_params */ NULL);
954+
ch->in_callback = false;
958955

959-
zval_ptr_dtor(&args[0]);
960-
zval_ptr_dtor(&args[2]);
956+
zval_ptr_dtor(&args[0]);
957+
zval_ptr_dtor(&args[2]);
961958

962-
return 0;
959+
return 0;
963960
}
964961
/* }}} */
965962

@@ -1012,62 +1009,12 @@ PHP_FUNCTION(curl_version)
10121009
CAAL("features", d->features);
10131010
/* Add an array of features */
10141011
{
1015-
struct feat {
1016-
const char *name;
1017-
int bitmask;
1018-
};
1019-
10201012
unsigned int i;
10211013
zval feature_list;
1022-
array_init(&feature_list);
1023-
1024-
/* Sync this list with PHP_MINFO_FUNCTION(curl) as well */
1025-
static const struct feat feats[] = {
1026-
{"AsynchDNS", CURL_VERSION_ASYNCHDNS},
1027-
{"CharConv", CURL_VERSION_CONV},
1028-
{"Debug", CURL_VERSION_DEBUG},
1029-
{"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
1030-
{"IDN", CURL_VERSION_IDN},
1031-
{"IPv6", CURL_VERSION_IPV6},
1032-
{"krb4", CURL_VERSION_KERBEROS4},
1033-
{"Largefile", CURL_VERSION_LARGEFILE},
1034-
{"libz", CURL_VERSION_LIBZ},
1035-
{"NTLM", CURL_VERSION_NTLM},
1036-
{"NTLMWB", CURL_VERSION_NTLM_WB},
1037-
{"SPNEGO", CURL_VERSION_SPNEGO},
1038-
{"SSL", CURL_VERSION_SSL},
1039-
{"SSPI", CURL_VERSION_SSPI},
1040-
{"TLS-SRP", CURL_VERSION_TLSAUTH_SRP},
1041-
{"HTTP2", CURL_VERSION_HTTP2},
1042-
{"GSSAPI", CURL_VERSION_GSSAPI},
1043-
{"KERBEROS5", CURL_VERSION_KERBEROS5},
1044-
{"UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS},
1045-
{"PSL", CURL_VERSION_PSL},
1046-
{"HTTPS_PROXY", CURL_VERSION_HTTPS_PROXY},
1047-
{"MULTI_SSL", CURL_VERSION_MULTI_SSL},
1048-
{"BROTLI", CURL_VERSION_BROTLI},
1049-
#if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
1050-
{"ALTSVC", CURL_VERSION_ALTSVC},
1051-
#endif
1052-
#if LIBCURL_VERSION_NUM >= 0x074200 /* Available since 7.66.0 */
1053-
{"HTTP3", CURL_VERSION_HTTP3},
1054-
#endif
1055-
#if LIBCURL_VERSION_NUM >= 0x074800 /* Available since 7.72.0 */
1056-
{"UNICODE", CURL_VERSION_UNICODE},
1057-
{"ZSTD", CURL_VERSION_ZSTD},
1058-
#endif
1059-
#if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
1060-
{"HSTS", CURL_VERSION_HSTS},
1061-
#endif
1062-
#if LIBCURL_VERSION_NUM >= 0x074c00 /* Available since 7.76.0 */
1063-
{"GSASL", CURL_VERSION_GSASL},
1064-
#endif
1065-
};
10661014

1067-
for(i = 0; i < sizeof(feats) / sizeof(feats[0]); i++) {
1068-
if (feats[i].name) {
1069-
add_assoc_bool(&feature_list, feats[i].name, d->features & feats[i].bitmask ? true : false);
1070-
}
1015+
array_init(&feature_list);
1016+
for (i = 0; i < sizeof(php_curl_features) / sizeof(php_curl_features[0]); i++) {
1017+
add_assoc_bool(&feature_list, php_curl_features[i].name, d->features & php_curl_features[i].bitmask ? true : false);
10711018
}
10721019

10731020
CAAZ("feature_list", &feature_list);
@@ -2282,9 +2229,9 @@ static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue
22822229

22832230
case CURLINFO_HEADER_OUT:
22842231
if (ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
2285-
zend_value_error("CURLINFO_HEADER_OUT option must not be set when the CURLOPT_DEBUGFUNCTION option is set");
2286-
return FAILURE;
2287-
}
2232+
zend_value_error("CURLINFO_HEADER_OUT option must not be set when the CURLOPT_DEBUGFUNCTION option is set");
2233+
return FAILURE;
2234+
}
22882235

22892236
if (zend_is_true(zvalue)) {
22902237
curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
@@ -3071,7 +3018,7 @@ PHP_FUNCTION(curl_pause)
30713018
}
30723019
/* }}} */
30733020

3074-
#if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
3021+
#if LIBCURL_VERSION_NUM >= 0x073e00 /* Available since 7.62.0 */
30753022
/* {{{ perform connection upkeep checks */
30763023
PHP_FUNCTION(curl_upkeep)
30773024
{
@@ -3090,5 +3037,5 @@ PHP_FUNCTION(curl_upkeep)
30903037

30913038
RETURN_BOOL(error == CURLE_OK);
30923039
}
3093-
/*}}} */
3040+
/* }}} */
30943041
#endif

0 commit comments

Comments
 (0)