add http\Client::getAvailable{Options,Configuration}()
authorMichael Wallner <mike@php.net>
Thu, 12 Feb 2015 12:21:50 +0000 (13:21 +0100)
committerMichael Wallner <mike@php.net>
Thu, 12 Feb 2015 12:21:50 +0000 (13:21 +0100)
php_http_client.c
php_http_client.h
php_http_client_curl.c

index 948ca70..4f0de3c 100644 (file)
@@ -1189,6 +1189,30 @@ static PHP_METHOD(HttpClient, getAvailableDrivers) {
        }
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableOptions, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(HttpClient, getAvailableOptions)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               array_init(return_value);
+               php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS, NULL, &Z_ARRVAL_P(return_value));
+       }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableConfiguration, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(HttpClient, getAvailableConfiguration)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               array_init(return_value);
+               php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION, NULL, &Z_ARRVAL_P(return_value));
+       }
+}
+
 static zend_function_entry php_http_client_methods[] = {
        PHP_ME(HttpClient, __construct,          ai_HttpClient_construct,            ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
        PHP_ME(HttpClient, reset,                ai_HttpClient_reset,                ZEND_ACC_PUBLIC)
@@ -1219,6 +1243,8 @@ static zend_function_entry php_http_client_methods[] = {
        PHP_ME(HttpClient, addCookies,           ai_HttpClient_addCookies,           ZEND_ACC_PUBLIC)
        PHP_ME(HttpClient, getCookies,           ai_HttpClient_getCookies,           ZEND_ACC_PUBLIC)
        PHP_ME(HttpClient, getAvailableDrivers,  ai_HttpClient_getAvailableDrivers,  ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+       PHP_ME(HttpClient, getAvailableOptions,  ai_HttpClient_getAvailableOptions,  ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getAvailableConfiguration, ai_HttpClient_getAvailableConfiguration, ZEND_ACC_PUBLIC)
        EMPTY_FUNCTION_ENTRY
 };
 
index 47b3020..4008e87 100644 (file)
@@ -20,8 +20,10 @@ typedef enum php_http_client_setopt_opt {
 } php_http_client_setopt_opt_t;
 
 typedef enum php_http_client_getopt_opt {
-       PHP_HTTP_CLIENT_OPT_PROGRESS_INFO,              /* php_http_client_progress_state_t** */
-       PHP_HTTP_CLIENT_OPT_TRANSFER_INFO,              /* HashTable* */
+       PHP_HTTP_CLIENT_OPT_PROGRESS_INFO,              /* php_http_client_enqueue_t*, php_http_client_progress_state_t** */
+       PHP_HTTP_CLIENT_OPT_TRANSFER_INFO,              /* php_http_client_enqueue_t*, HashTable* */
+       PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS,          /* NULL, HashTable* */
+       PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION,/* NULL, HashTable */
 } php_http_client_getopt_opt_t;
 
 typedef struct php_http_client_enqueue {
index e0994cf..c46ffca 100644 (file)
@@ -2322,9 +2322,42 @@ static STATUS php_http_client_curl_setopt(php_http_client_t *h, php_http_client_
        return SUCCESS;
 }
 
+static int apply_available_options(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
+{
+       php_http_option_t *opt = pDest;
+       HashTable *ht;
+       zval *entry;
+       int c;
+
+       ht = va_arg(args, HashTable*);
+
+       MAKE_STD_ZVAL(entry);
+
+       if ((c = zend_hash_num_elements(&opt->suboptions.options))) {
+               array_init_size(entry, c);
+               zend_hash_apply_with_arguments(&opt->suboptions.options TSRMLS_CC, apply_available_options, 1, Z_ARRVAL_P(entry));
+       } else {
+               /* catch deliberate NULL options */
+               if (Z_TYPE(opt->defval) == IS_STRING && !Z_STRVAL(opt->defval)) {
+                       ZVAL_NULL(entry);
+               } else {
+                       ZVAL_ZVAL(entry, &opt->defval, 1, 0);
+               }
+       }
+
+       if (hash_key->nKeyLength) {
+               zend_hash_quick_update(ht, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &entry, sizeof(zval *), NULL);
+       } else {
+               zend_hash_index_update(ht, hash_key->h, (void *) &entry, sizeof(zval *), NULL);
+       }
+
+       return ZEND_HASH_APPLY_KEEP;
+}
+
 static STATUS php_http_client_curl_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void **res)
 {
        php_http_client_enqueue_t *enqueue;
+       TSRMLS_FETCH_FROM_CTX(h->ts);
 
        switch (opt) {
        case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO:
@@ -2345,6 +2378,14 @@ static STATUS php_http_client_curl_getopt(php_http_client_t *h, php_http_client_
                }
                break;
 
+       case PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS:
+               zend_hash_apply_with_arguments(&php_http_curle_options.options TSRMLS_CC, apply_available_options, 1, *(HashTable **) res);
+               break;
+
+       case PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION:
+               zend_hash_apply_with_arguments(&php_http_curlm_options.options TSRMLS_CC, apply_available_options, 1, *(HashTable **) res);
+               break;
+
        default:
                break;
        }