Fix php_http_message.c:136:14: warning: comparison between 'enum <anonymous>' and...
[m6w6/ext-http] / php_http_client.c
index 06e90bafd889e2e7815f9bc533ef7b9bec87737f..29da4c90ec4788b592e027742f59550a9bd437e7 100644 (file)
@@ -13,6 +13,7 @@
 #include "php_http_api.h"
 
 #include <ext/spl/spl_observer.h>
+#include <ext/standard/php_array.h>
 
 PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC)
 {
@@ -29,9 +30,13 @@ PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_h
        } else if (ops->rsrc) {
                h->rf = php_http_resource_factory_init(NULL, h->ops->rsrc, h, NULL);
        }
-       h->buffer = php_http_buffer_init(NULL);
-       h->parser = php_http_message_parser_init(NULL TSRMLS_CC);
-       h->message = php_http_message_init(NULL, 0 TSRMLS_CC);
+       h->request.buffer = php_http_buffer_init(NULL);
+       h->request.parser = php_http_message_parser_init(NULL TSRMLS_CC);
+       h->request.message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
+
+       h->response.buffer = php_http_buffer_init(NULL);
+       h->response.parser = php_http_message_parser_init(NULL TSRMLS_CC);
+       h->response.message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
 
        TSRMLS_SET_CTX(h->ts);
 
@@ -56,9 +61,13 @@ PHP_HTTP_API void php_http_client_dtor(php_http_client_t *h)
 
        php_http_resource_factory_free(&h->rf);
 
-       php_http_message_parser_free(&h->parser);
-       php_http_message_free(&h->message);
-       php_http_buffer_free(&h->buffer);
+       php_http_message_parser_free(&h->request.parser);
+       php_http_message_free(&h->request.message);
+       php_http_buffer_free(&h->request.buffer);
+
+       php_http_message_parser_free(&h->response.parser);
+       php_http_message_free(&h->response.message);
+       php_http_buffer_free(&h->response.buffer);
 }
 
 PHP_HTTP_API void php_http_client_free(php_http_client_t **h)
@@ -88,9 +97,14 @@ PHP_HTTP_API php_http_client_t *php_http_client_copy(php_http_client_t *from, ph
                } else if (to->ops->rsrc){
                        to->rf = php_http_resource_factory_init(NULL, to->ops->rsrc, to, NULL);
                }
-               to->buffer = php_http_buffer_init(NULL);
-               to->parser = php_http_message_parser_init(NULL TSRMLS_CC);
-               to->message = php_http_message_init(NULL, 0 TSRMLS_CC);
+
+               to->request.buffer = php_http_buffer_init(NULL);
+               to->request.parser = php_http_message_parser_init(NULL TSRMLS_CC);
+               to->request.message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
+
+               to->response.buffer = php_http_buffer_init(NULL);
+               to->response.parser = php_http_message_parser_init(NULL TSRMLS_CC);
+               to->response.message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
 
                TSRMLS_SET_CTX(to->ts);
 
@@ -197,6 +211,13 @@ PHP_HTTP_BEGIN_ARGS(getTransferInfo, 0)
        PHP_HTTP_ARG_VAL(name, 0)
 PHP_HTTP_END_ARGS;
 
+PHP_HTTP_BEGIN_ARGS(request, 2)
+       PHP_HTTP_ARG_VAL(method, 0)
+       PHP_HTTP_ARG_VAL(url, 0)
+       PHP_HTTP_ARG_ARR(headers, 1, 0)
+       PHP_HTTP_ARG_VAL(body, 0)
+       PHP_HTTP_ARG_ARR(options, 1, 0)
+PHP_HTTP_END_ARGS;
 
 static zend_class_entry *php_http_client_class_entry;
 
@@ -239,6 +260,8 @@ static zend_function_entry php_http_client_method_entry[] = {
        PHP_HTTP_CLIENT_ME(getResponseMessageClass, ZEND_ACC_PUBLIC)
        PHP_HTTP_CLIENT_ME(setResponseMessageClass, ZEND_ACC_PUBLIC)
 
+       PHP_HTTP_CLIENT_ME(request, ZEND_ACC_PUBLIC)
+
        EMPTY_FUNCTION_ENTRY
 };
 
@@ -275,7 +298,7 @@ zend_object_value php_http_client_object_new_ex(zend_class_entry *ce, php_http_c
        o = ecalloc(1, sizeof(*o));
        zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
        object_properties_init((zend_object *) o, ce);
-
+       
        ov.handle = zend_objects_store_put(o, NULL, php_http_client_object_free, NULL TSRMLS_CC);
        ov.handlers = &php_http_client_object_handlers;
 
@@ -318,10 +341,10 @@ static inline zend_object_value php_http_client_object_message(zval *this_ptr, p
 
        if (Z_STRLEN_P(zcn)
        &&      (class_entry = zend_fetch_class(Z_STRVAL_P(zcn), Z_STRLEN_P(zcn), 0 TSRMLS_CC))
-       &&      (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_message_object_new_ex, php_http_client_response_class_entry, msg, NULL TSRMLS_CC))) {
+       &&      (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_message_object_new_ex, php_http_client_response_get_class_entry(), msg, NULL TSRMLS_CC))) {
                return ov;
        } else {
-               return php_http_message_object_new_ex(php_http_client_response_class_entry, msg, NULL TSRMLS_CC);
+               return php_http_message_object_new_ex(php_http_client_response_get_class_entry(), msg, NULL TSRMLS_CC);
        }
 }
 
@@ -330,6 +353,7 @@ STATUS php_http_client_object_handle_request(zval *zclient, zval **zreq TSRMLS_D
        php_http_client_object_t *obj = zend_object_store_get_object(zclient TSRMLS_CC);
        php_http_client_progress_t *progress;
        zval *zoptions;
+       HashTable options;
 
        /* do we have a valid request? */
        if (*zreq) {
@@ -349,14 +373,21 @@ STATUS php_http_client_object_handle_request(zval *zclient, zval **zreq TSRMLS_D
        php_http_client_reset(obj->client);
 
        /* reset transfer info */
-       zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("info") TSRMLS_CC);
+       zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("transferInfo") TSRMLS_CC);
+
 
        /* set client options */
+       zend_hash_init(&options, 0, NULL, ZVAL_PTR_DTOR, 0);
        zoptions = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("options"), 0 TSRMLS_CC);
-       php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, Z_ARRVAL_P(zoptions));
-       /* set request options */
+       if (Z_TYPE_P(zoptions) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(zoptions))) {
+               php_array_merge(&options, Z_ARRVAL_P(zoptions), 1 TSRMLS_CC);
+       }
        zoptions = zend_read_property(php_http_client_request_get_class_entry(), *zreq, ZEND_STRL("options"), 0 TSRMLS_CC);
-       php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, Z_ARRVAL_P(zoptions));
+       if (Z_TYPE_P(zoptions) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(zoptions))) {
+               php_array_merge(&options, Z_ARRVAL_P(zoptions), 1 TSRMLS_CC);
+       }
+       php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, &options);
+       zend_hash_destroy(&options);
 
        /* set progress callback */
        if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) {
@@ -364,7 +395,6 @@ STATUS php_http_client_object_handle_request(zval *zclient, zval **zreq TSRMLS_D
                        php_http_client_progress_callback_t *callback = emalloc(sizeof(*callback));
 
                        callback->type = PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER;
-                       callback->pass_state = 0;
                        MAKE_STD_ZVAL(callback->func.user);
                        array_init(callback->func.user);
                        Z_ADDREF_P(zclient);
@@ -396,11 +426,11 @@ STATUS php_http_client_object_handle_response(zval *zclient TSRMLS_DC)
        zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("transferInfo"), info TSRMLS_CC);
        zval_ptr_dtor(&info);
 
-       if ((msg = obj->client->message)) {
-               /* update history */
+       if ((msg = obj->client->response.message)) {
                if (i_zend_is_true(zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
                        zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0 TSRMLS_CC);
-                       zend_object_value ov = php_http_client_object_message(zclient, php_http_message_copy(msg, NULL) TSRMLS_CC);
+                       php_http_message_t *zipped = php_http_message_zip(obj->client->response.message, obj->client->request.message);
+                       zend_object_value ov = php_http_client_object_message(zclient, zipped TSRMLS_CC);
 
                        MAKE_STD_ZVAL(new_hist);
                        ZVAL_OBJVAL(new_hist, ov, 0);
@@ -422,8 +452,7 @@ STATUS php_http_client_object_handle_response(zval *zclient TSRMLS_DC)
                        zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage"), message TSRMLS_CC);
                        zval_ptr_dtor(&message);
 
-                       obj->client->message = php_http_message_init(NULL, 0 TSRMLS_CC);
-                       msg = msg->parent;
+                       obj->client->response.message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
                } else {
                        zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage") TSRMLS_CC);
                }
@@ -431,19 +460,17 @@ STATUS php_http_client_object_handle_response(zval *zclient TSRMLS_DC)
                zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage") TSRMLS_CC);
        }
 
-       /* there might be a 100-Continue response in between */
-       while (msg && !PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) {
-               msg = msg->parent;
-       }
-
-       if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) {
-               zval *message;
+       if ((msg = obj->client->request.message)) {
+               if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) {
+                       zval *message;
 
-               /* update the actual request message */
-               MAKE_STD_ZVAL(message);
-               ZVAL_OBJVAL(message, php_http_message_object_new_ex(php_http_message_class_entry, php_http_message_copy_ex(msg, NULL, 0), NULL TSRMLS_CC), 0);
-               zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("requestMessage"), message TSRMLS_CC);
-               zval_ptr_dtor(&message);
+                       /* update the actual request message */
+                       MAKE_STD_ZVAL(message);
+                       ZVAL_OBJVAL(message, php_http_message_object_new_ex(php_http_message_get_class_entry(), msg, NULL TSRMLS_CC), 0);
+                       zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("requestMessage"), message TSRMLS_CC);
+                       zval_ptr_dtor(&message);
+                       obj->client->request.message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
+               }
        }
 
        if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) {
@@ -543,7 +570,7 @@ void php_http_client_options_set_subr(zval *this_ptr, char *key, size_t len, zva
 void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zval *return_value TSRMLS_DC)
 {
        zend_class_entry *this_ce = Z_OBJCE_P(getThis());
-       zval **options, *opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
+       zval **options, *opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
 
        if ((Z_TYPE_P(opts) == IS_ARRAY) && (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
                RETVAL_ZVAL(*options, 1, 0);
@@ -552,7 +579,7 @@ void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zva
 
 PHP_METHOD(HttpClient, __construct)
 {
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                zval *os, *opts = NULL;
 
                MAKE_STD_ZVAL(os);
@@ -569,7 +596,7 @@ PHP_METHOD(HttpClient, __construct)
 
 PHP_METHOD(HttpClient, getObservers)
 {
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        RETVAL_PROP(php_http_client_class_entry, "observers");
                }
@@ -752,7 +779,7 @@ PHP_METHOD(HttpClient, addCookies)
        zval *opts = NULL;
 
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
-               php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
+               php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 0 TSRMLS_CC);
 
                RETVAL_ZVAL(getThis(), 1, 0);
        }
@@ -802,7 +829,7 @@ PHP_METHOD(HttpClient, flushCookies)
 
 PHP_METHOD(HttpClient, getResponseMessage)
 {
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        zval *message = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC);
 
@@ -817,7 +844,7 @@ PHP_METHOD(HttpClient, getResponseMessage)
 
 PHP_METHOD(HttpClient, getRequestMessage)
 {
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        zval *message = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("requestMessage"), 0 TSRMLS_CC);
 
@@ -832,7 +859,7 @@ PHP_METHOD(HttpClient, getRequestMessage)
 
 PHP_METHOD(HttpClient, getHistory)
 {
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        zval *hist = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC);
 
@@ -889,14 +916,53 @@ PHP_METHOD(HttpClient, getRequest)
        }
 }
 
+PHP_METHOD(HttpClient, request)
+{
+       char *meth_str, *url_str;
+       int meth_len, url_len;
+       zval *zheader, *zbody, *zoptions;
+
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a!z!a!/", &meth_str, &meth_len, &url_str, &url_len, &zheader, &zbody, &zoptions)) {
+                       php_http_message_object_t *msg_obj;
+                       zend_object_value ov;
+                       zval *req, *res = NULL;
+
+                       php_http_new(&ov, php_http_client_request_get_class_entry(), (php_http_new_t) php_http_message_object_new_ex, NULL, NULL, (void *) &msg_obj TSRMLS_CC);
+                       MAKE_STD_ZVAL(req);
+                       ZVAL_OBJVAL(req, ov, 0);
+
+                       msg_obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST, NULL TSRMLS_CC);
+                       PHP_HTTP_INFO(msg_obj->message).request.url = estrndup(url_str, url_len);
+                       PHP_HTTP_INFO(msg_obj->message).request.method = estrndup(meth_str, meth_len);
+
+                       if (zheader) {
+                               array_copy(Z_ARRVAL_P(zheader), &msg_obj->message->hdrs);
+                       }
+
+                       if (zbody) {
+                               php_http_message_object_set_body(msg_obj, zbody TSRMLS_CC);
+                       }
+
+                       if (zoptions) {
+                               php_http_client_options_set(req, zoptions TSRMLS_CC);
+                       }
+
+                       zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "send", &res, req);
+                       RETVAL_ZVAL(res, 0, 1);
+                       zval_ptr_dtor(&req);
+               }
+       } end_error_handling();
+}
+
 PHP_METHOD(HttpClient, send)
 {
        zval *zreq = NULL;
 
        RETVAL_FALSE;
 
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O!", &zreq, php_http_client_request_get_class_entry())) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O!", &zreq, php_http_client_request_get_class_entry())) {
                        if (SUCCESS == php_http_client_object_handle_request(getThis(), &zreq TSRMLS_CC)) {
                                php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
                                php_http_message_object_t *req = zend_object_store_get_object(zreq TSRMLS_CC);
@@ -917,12 +983,12 @@ PHP_METHOD(HttpClient, send)
 
 PHP_MINIT_FUNCTION(http_client)
 {
-       PHP_HTTP_REGISTER_CLASS(http\\Client, AbstractClient, http_client, php_http_object_class_entry, ZEND_ACC_ABSTRACT);
+       PHP_HTTP_REGISTER_CLASS(http\\Client, AbstractClient, http_client, php_http_object_get_class_entry(), ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
        php_http_client_class_entry->create_object = php_http_client_object_new;
        memcpy(&php_http_client_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        php_http_client_object_handlers.clone_obj = php_http_client_object_clone;
 
-       zend_class_implements(php_http_client_class_entry TSRMLS_CC, 2, spl_ce_SplSubject, php_http_client_interface_class_entry);
+       zend_class_implements(php_http_client_class_entry TSRMLS_CC, 2, spl_ce_SplSubject, php_http_client_interface_get_class_entry());
 
        zend_declare_property_string(php_http_client_class_entry, ZEND_STRL("responseMessageClass"), "", ZEND_ACC_PRIVATE TSRMLS_CC);
        zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC);