- missing include
[m6w6/ext-http] / http_request_object.c
index e744a227ede89642442302d35629d9dcf856d90a..0c68635b1a662f1af3814bdf8b1abeb4336938b9 100644 (file)
 #include "php_http_message_object.h"
 #include "php_http_exception_object.h"
 
+#include "missing.h"
+
 #ifdef PHP_WIN32
 #      include <winsock2.h>
 #endif
 #include <curl/curl.h>
 
+ZEND_EXTERN_MODULE_GLOBALS(http);
+
 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args)     HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
 #define HTTP_EMPTY_ARGS(method, ret_ref)                       HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
 #define HTTP_REQUEST_ME(method, visibility)                    PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
@@ -203,11 +207,6 @@ HTTP_BEGIN_ARGS(methodExists, 0, 1)
        HTTP_ARG_VAL(method, 0)
 HTTP_END_ARGS;
 
-HTTP_BEGIN_ARGS(debugWrapper, 0, 2)
-       HTTP_ARG_VAL(info_type, 0)
-       HTTP_ARG_VAL(info_message, 0)
-HTTP_END_ARGS;
-
 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
 static inline void _http_request_object_declare_default_properties(TSRMLS_D);
 
@@ -282,8 +281,6 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
        HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
 
-       HTTP_REQUEST_ME(debugWrapper, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
-
        {NULL, NULL, NULL}
 };
 static zend_object_handlers http_request_object_handlers;
@@ -375,7 +372,6 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D)
        DCL_PROP(PROTECTED, string, queryData, "");
        DCL_PROP(PROTECTED, string, putFile, "");
 
-       DCL_PROP_N(PRIVATE, dbg_user_cb);
        DCL_PROP(PUBLIC, bool, recordHistory, 1);
 }
 
@@ -390,7 +386,9 @@ void _http_request_object_free(zend_object *object TSRMLS_DC)
        if (o->ch) {
                /* avoid nasty segfaults with already cleaned up callbacks */
                curl_easy_setopt(o->ch, CURLOPT_NOPROGRESS, 1);
+               curl_easy_setopt(o->ch, CURLOPT_PROGRESSFUNCTION, NULL);
                curl_easy_setopt(o->ch, CURLOPT_VERBOSE, 0);
+               curl_easy_setopt(o->ch, CURLOPT_DEBUGFUNCTION, NULL);
                curl_easy_cleanup(o->ch);
        }
        phpstr_dtor(&o->response);
@@ -409,7 +407,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                return FAILURE;
        }
        if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
-               http_error(E_WARNING, HTTP_E_CURL, "Could not initilaize curl");
+               http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initilaize curl");
                return FAILURE;
        }
 
@@ -432,24 +430,12 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
        }
 
-       /* ensure we have HttpRequest::debugWrapper as dbg callback */
-       {
-               zval *dbg_cb;
-               MAKE_STD_ZVAL(dbg_cb);
-               array_init(dbg_cb);
-               zval_add_ref(&getThis());
-               add_next_index_zval(dbg_cb, getThis());
-               add_next_index_stringl(dbg_cb, "debugWrapper", lenof("debugWrapper"), 1);
-               add_assoc_zval(opts, "ondebug", dbg_cb);
-       }
-       /* */
-
        switch (Z_LVAL_P(meth))
        {
                case HTTP_GET:
                case HTTP_HEAD:
                        body->type = -1;
-                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, NULL, Z_ARRVAL_P(opts), &obj->response);
+                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, NULL, Z_ARRVAL_P(opts));
                break;
 
                case HTTP_PUT:
@@ -464,7 +450,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                                body->data = stream;
                                body->size = ssb.sb.st_size;
 
-                               status = http_request_init(obj->ch, HTTP_PUT, request_uri, body, Z_ARRVAL_P(opts), &obj->response);
+                               status = http_request_init(obj->ch, HTTP_PUT, request_uri, body, Z_ARRVAL_P(opts));
                        } else {
                                status = FAILURE;
                        }
@@ -476,7 +462,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                        zval *fields = GET_PROP(obj, postFields), *files = GET_PROP(obj, postFiles);
 
                        if (SUCCESS == (status = http_request_body_fill(body, Z_ARRVAL_P(fields), Z_ARRVAL_P(files)))) {
-                               status = http_request_init(obj->ch, HTTP_POST, request_uri, body, Z_ARRVAL_P(opts), &obj->response);
+                               status = http_request_init(obj->ch, HTTP_POST, request_uri, body, Z_ARRVAL_P(opts));
                        }
                }
                break;
@@ -489,7 +475,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                        body->data = Z_STRVAL_P(post);
                        body->size = Z_STRLEN_P(post);
 
-                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(opts), &obj->response);
+                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(opts));
                }
                break;
        }
@@ -518,6 +504,7 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                        *info = GET_PROP(obj, responseInfo),
                        *hist = GET_PROP(obj, recordHistory);
 
+               /* should we record history? */
                if (Z_TYPE_P(hist) != IS_BOOL) {
                        convert_to_boolean_ex(&hist);
                }
@@ -648,30 +635,37 @@ PHP_METHOD(HttpRequest, setOptions)
                                zval **headers;
                                if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
                                        array_merge(*opt, *headers);
+                                       continue;
                                }
                        } else if (!strcmp(key, "cookies")) {
                                zval **cookies;
                                if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
                                        array_merge(*opt, *cookies);
+                                       continue;
                                }
-                       } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
+                       } else if (!strcmp(key, "ssl")) {
+                               zval **ssl;
+                               if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl)) {
+                                       array_merge(*opt, *ssl);
+                                       continue;
+                               }
+                       }else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
                                if (Z_TYPE_PP(opt) != IS_STRING) {
                                        convert_to_string_ex(opt);
                                }
                                UPD_PROP(obj, string, url, Z_STRVAL_PP(opt));
+                               continue;
                        } else if (!strcmp(key, "method")) {
                                if (Z_TYPE_PP(opt) != IS_LONG) {
                                        convert_to_long_ex(opt);
                                }
                                UPD_PROP(obj, long, method, Z_LVAL_PP(opt));
-                       } else {
-                               if (!strcmp(key, "ondebug")) {
-                                       SET_PROP(obj, dbg_user_cb, *opt);
-                               }
-                               zval_add_ref(opt);
-                               add_assoc_zval(old_opts, key, *opt);
+                               continue;
                        }
 
+                       zval_add_ref(opt);
+                       add_assoc_zval(old_opts, key, *opt);
+
                        /* reset */
                        key = NULL;
                }
@@ -712,7 +706,6 @@ PHP_METHOD(HttpRequest, unsetOptions)
 
        FREE_PARR(obj, options);
        INIT_PARR(obj, options);
-       zend_update_property_null(http_request_object_ce, getThis(), "dbg_user_cb", lenof("dbg_user_cb") TSRMLS_CC);
 }
 /* }}} */
 
@@ -1000,7 +993,7 @@ PHP_METHOD(HttpRequest, setContentType)
        }
 
        if (!strchr(ctype, '/')) {
-               http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
+               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
                RETURN_FALSE;
        }
 
@@ -1217,7 +1210,7 @@ PHP_METHOD(HttpRequest, addPostFile)
 
        if (type_len) {
                if (!strchr(type, '/')) {
-                       http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
+                       http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
                        RETURN_FALSE;
                }
        } else {
@@ -1556,7 +1549,7 @@ PHP_METHOD(HttpRequest, getResponseInfo)
                        if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
                                RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
                        } else {
-                               http_error_ex(E_NOTICE, HTTP_E_PARAM, "Could not find response info named %s", info_name);
+                               http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
                                RETURN_FALSE;
                        }
                } else {
@@ -1600,7 +1593,6 @@ PHP_METHOD(HttpRequest, getRequestMessage)
        NO_ARGS;
 
        IF_RETVAL_USED {
-               zval *message;
                http_message *msg;
                getObject(http_request_object, obj);
 
@@ -1618,7 +1610,6 @@ PHP_METHOD(HttpRequest, getHistory)
        NO_ARGS;
 
        IF_RETVAL_USED {
-               zval *history;
                http_message *msg;
                getObject(http_request_object, obj);
 
@@ -1675,13 +1666,13 @@ PHP_METHOD(HttpRequest, send)
        SET_EH_THROW_HTTP();
 
        if (obj->pool) {
-               http_error(E_WARNING, HTTP_E_CURL, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
+               http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
                SET_EH_NORMAL();
                RETURN_FALSE;
        }
 
        if (SUCCESS == (status = http_request_object_requesthandler(obj, getThis(), &body))) {
-               status = http_request_exec(obj->ch, NULL);
+               status = http_request_exec(obj->ch, NULL, &obj->response, &obj->request);
        }
        http_request_body_dtor(&body);
 
@@ -1695,59 +1686,6 @@ PHP_METHOD(HttpRequest, send)
 }
 /* }}} */
 
-/* {{{ proto private HttpRequest::debugWrapper(long type, string message)
- */
-PHP_METHOD(HttpRequest, debugWrapper)
-{
-       static int curl_ignores_body = 0;
-       getObject(http_request_object, obj);
-       zval *type, *message, *dbg_user_cb = GET_PROP(obj, dbg_user_cb);
-
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &type, &message)) {
-               RETURN_NULL();
-       }
-
-       if (Z_TYPE_P(type) != IS_LONG) {
-               convert_to_long_ex(&type);
-       }
-       if (Z_TYPE_P(message) != IS_STRING) {
-               convert_to_string_ex(&message);
-       }
-
-       switch (Z_LVAL_P(type))
-       {
-               case CURLINFO_DATA_IN:
-                       /* fetch ignored body */
-                       if (curl_ignores_body && Z_LVAL_P(type) == CURLINFO_DATA_IN) {
-                               phpstr_append(&obj->response, Z_STRVAL_P(message), Z_STRLEN_P(message));
-                       }
-               break;
-
-               case CURLINFO_TEXT:
-                       /* check if following incoming data would be ignored */
-                       curl_ignores_body = !strcmp(Z_STRVAL_P(message), "Ignoring the response-body\n");
-               break;
-
-               case CURLINFO_HEADER_OUT:
-               case CURLINFO_DATA_OUT:
-                       /* fetch outgoing request message */
-                       phpstr_append(&obj->request, Z_STRVAL_P(message), Z_STRLEN_P(message));
-               break;
-       }
-
-       /* call user debug callback */
-       if (Z_TYPE_P(dbg_user_cb) != IS_NULL) {
-               zval *args[2], cb_ret;
-
-               args[0] = type;
-               args[1] = message;
-               call_user_function(EG(function_table), NULL, dbg_user_cb, &cb_ret, 2, args TSRMLS_CC);
-       }
-
-       RETURN_NULL();
-}
-/* }}} */
-
 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
 
 /*