flush WIP
[m6w6/ext-http] / php_http_client.c
index 6dced6632a3817bdacb9936ad70996ef268b9149..504e961d56a75c6f2744117da972f422bffab08d 100644 (file)
@@ -6,7 +6,7 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2013, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
  */
 static HashTable php_http_client_drivers;
 
-STATUS php_http_client_driver_add(php_http_client_driver_t *driver)
+ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver)
 {
        return zend_hash_add(&php_http_client_drivers, driver->name_str, driver->name_len + 1, (void *) driver, sizeof(php_http_client_driver_t), NULL);
 }
 
-STATUS php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver)
+ZEND_RESULT_CODE php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver)
 {
        php_http_client_driver_t *tmp;
 
@@ -216,7 +216,7 @@ void php_http_client_free(php_http_client_t **h) {
        }
 }
 
-STATUS php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
+ZEND_RESULT_CODE php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
 {
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
@@ -231,7 +231,7 @@ STATUS php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *
        return FAILURE;
 }
 
-STATUS php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
+ZEND_RESULT_CODE php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
 {
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
@@ -267,7 +267,7 @@ php_http_client_enqueue_t *php_http_client_enqueued(php_http_client_t *h, void *
        return el ? (php_http_client_enqueue_t *) el->data : NULL;
 }
 
-STATUS php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout)
+ZEND_RESULT_CODE php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout)
 {
        if (h->ops->wait) {
                return h->ops->wait(h, custom_timeout);
@@ -285,7 +285,7 @@ int php_http_client_once(php_http_client_t *h)
        return FAILURE;
 }
 
-STATUS php_http_client_exec(php_http_client_t *h)
+ZEND_RESULT_CODE php_http_client_exec(php_http_client_t *h)
 {
        if (h->ops->exec) {
                return h->ops->exec(h);
@@ -304,7 +304,7 @@ void php_http_client_reset(php_http_client_t *h)
        zend_llist_clean(&h->responses);
 }
 
-STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
+ZEND_RESULT_CODE php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
 {
        if (h->ops->setopt) {
                return h->ops->setopt(h, opt, arg);
@@ -313,7 +313,7 @@ STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t
        return FAILURE;
 }
 
-STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr)
+ZEND_RESULT_CODE php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr)
 {
        if (h->ops->getopt) {
                return h->ops->getopt(h, opt, arg, res_ptr);
@@ -361,7 +361,9 @@ zend_object_value php_http_client_object_new(zend_class_entry *ce TSRMLS_DC)
 static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response TSRMLS_DC)
 {
        zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0 TSRMLS_CC);
-       php_http_message_t *zipped = php_http_message_zip(response, request);
+       php_http_message_t *req_copy = php_http_message_copy(request, NULL);
+       php_http_message_t *res_copy = php_http_message_copy(response, NULL);
+       php_http_message_t *zipped = php_http_message_zip(res_copy, req_copy);
        zend_object_value ov = php_http_message_object_new_ex(php_http_message_class_entry, zipped, NULL TSRMLS_CC);
 
        MAKE_STD_ZVAL(new_hist);
@@ -375,8 +377,9 @@ static void handle_history(zval *zclient, php_http_message_t *request, php_http_
        zval_ptr_dtor(&new_hist);
 }
 
-static STATUS handle_response(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_message_t **request, php_http_message_t **response)
+static ZEND_RESULT_CODE handle_response(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_message_t **request, php_http_message_t **response)
 {
+       zend_bool dequeue = 0;
        zval zclient;
        php_http_message_t *msg;
        php_http_client_progress_state_t *progress;
@@ -390,7 +393,10 @@ static STATUS handle_response(void *arg, php_http_client_t *client, php_http_cli
                zval *info, *zresponse, *zrequest;
                HashTable *info_ht;
 
-               if (i_zend_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
+               /* ensure the message is of type response (could be uninitialized in case of early error, like DNS) */
+               php_http_message_set_type(msg, PHP_HTTP_RESPONSE);
+
+               if (z_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
                        handle_history(&zclient, *request, *response TSRMLS_CC);
                }
 
@@ -427,8 +433,8 @@ static STATUS handle_response(void *arg, php_http_client_t *client, php_http_cli
                        zend_fcall_info_argn(&e->closure.fci TSRMLS_CC, 0);
 
                        if (retval) {
-                               if (Z_TYPE_P(retval) == IS_BOOL && Z_BVAL_P(retval)) {
-                                       php_http_client_dequeue(client, e->request);
+                               if (Z_TYPE_P(retval) == IS_BOOL) {
+                                       dequeue = Z_BVAL_P(retval);
                                }
                                zval_ptr_dtor(&retval);
                        }
@@ -444,6 +450,10 @@ static STATUS handle_response(void *arg, php_http_client_t *client, php_http_cli
                client->callback.progress.func(client->callback.progress.arg, client, e, progress);
        }
 
+       if (dequeue) {
+               php_http_client_dequeue(client, e->request);
+       }
+
        return SUCCESS;
 }
 
@@ -720,7 +730,9 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_count, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, count)
 {
-       if (SUCCESS == zend_parse_parameters_none()) {
+       long count_mode = -1;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) {
                php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
                RETVAL_LONG(zend_llist_count(&obj->client->requests));