fixes for windows and 5.3 compatibility
[m6w6/ext-http] / php_http_object.c
index 215ae8cbceca9b0713ce4af03d0674e2e18b1718..f85960f6acbb3cc3979f2e4a121313d1ac763642 100644 (file)
@@ -10,9 +10,9 @@
     +--------------------------------------------------------------------+
 */
 
-#include "php_http.h"
+#include "php_http_api.h"
 
-STATUS php_http_new(zend_object_value *ov, zend_class_entry *ce, php_http_new_t create, zend_class_entry *parent_ce, void *intern_ptr, void **obj_ptr TSRMLS_DC)
+PHP_HTTP_API STATUS php_http_new(zend_object_value *ov, zend_class_entry *ce, php_http_new_t create, zend_class_entry *parent_ce, void *intern_ptr, void **obj_ptr TSRMLS_DC)
 {
        if (!ce) {
                ce = parent_ce;
@@ -37,7 +37,7 @@ PHP_HTTP_API zend_error_handling_t php_http_object_get_error_handling(zval *obje
                zval_ptr_dtor(&lzeh);
                return eh;
        }
-       zeh = zend_read_static_property(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), 0 TSRMLS_CC);
+       zeh = zend_read_static_property(php_http_object_get_class_entry(), ZEND_STRL("defaultErrorHandling"), 0 TSRMLS_CC);
        if (Z_TYPE_P(zeh) != IS_NULL) {
                lzeh = php_http_ztyp(IS_LONG, zeh);
                eh = Z_LVAL_P(lzeh);
@@ -51,11 +51,6 @@ PHP_HTTP_API zend_error_handling_t php_http_object_get_error_handling(zval *obje
 #define PHP_HTTP_EMPTY_ARGS(method)                                            PHP_HTTP_EMPTY_ARGS_EX(HttpObject, method, 0)
 #define PHP_HTTP_OBJECT_ME(method, visibility)                 PHP_ME(HttpObject, method, PHP_HTTP_ARGS(HttpObject, method), visibility)
 
-PHP_HTTP_BEGIN_ARGS(factory, 1)
-       PHP_HTTP_ARG_VAL(class_name, 0)
-       PHP_HTTP_ARG_VAL(ctor_args, 0)
-PHP_HTTP_END_ARGS;
-
 PHP_HTTP_BEGIN_ARGS(setErrorHandling, 1)
        PHP_HTTP_ARG_VAL(eh, 0)
 PHP_HTTP_END_ARGS;
@@ -68,15 +63,26 @@ PHP_HTTP_END_ARGS;
 
 PHP_HTTP_EMPTY_ARGS(getDefaultErrorHandling);
 
-zend_class_entry *php_http_object_class_entry;
-zend_function_entry php_http_object_method_entry[] = {
-       PHP_HTTP_OBJECT_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+PHP_HTTP_BEGIN_ARGS(triggerError, 3)
+       PHP_HTTP_ARG_VAL(error_type, 0)
+       PHP_HTTP_ARG_VAL(error_code, 0)
+       PHP_HTTP_ARG_VAL(error_message, 0)
+PHP_HTTP_END_ARGS;
+
+static zend_class_entry *php_http_object_class_entry;
+
+zend_class_entry *php_http_object_get_class_entry(void)
+{
+       return php_http_object_class_entry;
+}
 
+static zend_function_entry php_http_object_method_entry[] = {
        PHP_HTTP_OBJECT_ME(setErrorHandling, ZEND_ACC_PUBLIC)
        PHP_HTTP_OBJECT_ME(getErrorHandling, ZEND_ACC_PUBLIC)
        PHP_HTTP_OBJECT_ME(setDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        PHP_HTTP_OBJECT_ME(getDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
-       
+       PHP_HTTP_OBJECT_ME(triggerError, ZEND_ACC_PUBLIC)
+
        EMPTY_FUNCTION_ENTRY
 };
 
@@ -92,7 +98,11 @@ zend_object_value php_http_object_new_ex(zend_class_entry *ce, void *nothing, ph
 
        o = ecalloc(1, sizeof(php_http_object_t));
        zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
+#if PHP_VERSION_ID < 50339
+       zend_hash_copy(((zend_object *) o)->properties, &(ce->default_properties), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*));
+#else
        object_properties_init((zend_object *) o, ce);
+#endif
 
        if (ptr) {
                *ptr = o;
@@ -104,36 +114,9 @@ zend_object_value php_http_object_new_ex(zend_class_entry *ce, void *nothing, ph
        return ov;
 }
 
-PHP_METHOD(HttpObject, factory)
-{
-       zval *ctor_args = NULL;
-       zend_class_entry *class_entry = NULL;
-       zval *object_ctor;
-       zend_fcall_info fci;
-       zend_fcall_info_cache fcc;
-
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "C|a/!", &class_entry, &ctor_args)) {
-                       object_init_ex(return_value, class_entry);
-
-                       MAKE_STD_ZVAL(object_ctor);
-                       array_init(object_ctor);
-
-                       Z_ADDREF_P(return_value);
-                       add_next_index_zval(object_ctor, return_value);
-                       add_next_index_stringl(object_ctor, ZEND_STRL("__construct"), 1);
-
-                       zend_fcall_info_init(object_ctor, 0, &fci, &fcc, NULL, NULL TSRMLS_CC);
-                       zend_fcall_info_call(&fci, &fcc, NULL, ctor_args TSRMLS_CC);
-
-                       zval_ptr_dtor(&object_ctor);
-               }
-       } end_error_handling();
-}
-
 PHP_METHOD(HttpObject, getErrorHandling)
 {
-       RETURN_PROP(php_http_object_class_entry, "errorHandling");
+       RETURN_PROP(php_http_object_get_class_entry(), "errorHandling");
 }
 
 PHP_METHOD(HttpObject, setErrorHandling)
@@ -145,11 +128,12 @@ PHP_METHOD(HttpObject, setErrorHandling)
                        case EH_NORMAL:
                        case EH_SUPPRESS:
                        case EH_THROW:
-                               zend_update_property_long(php_http_object_class_entry, getThis(), ZEND_STRL("errorHandling"), eh TSRMLS_CC);
+                               zend_update_property_long(php_http_object_get_class_entry(), getThis(), ZEND_STRL("errorHandling"), eh TSRMLS_CC);
                                break;
 
                        default:
                                php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
+                               break;
                }
        }
 
@@ -158,7 +142,7 @@ PHP_METHOD(HttpObject, setErrorHandling)
 
 PHP_METHOD(HttpObject, getDefaultErrorHandling)
 {
-       RETURN_SPROP(php_http_object_class_entry, "defaultErrorHandling");
+       RETURN_SPROP(php_http_object_get_class_entry(), "defaultErrorHandling");
 }
 
 PHP_METHOD(HttpObject, setDefaultErrorHandling)
@@ -170,26 +154,38 @@ PHP_METHOD(HttpObject, setDefaultErrorHandling)
                        case EH_NORMAL:
                        case EH_SUPPRESS:
                        case EH_THROW:
-                               zend_update_static_property_long(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), eh TSRMLS_CC);
+                               zend_update_static_property_long(php_http_object_get_class_entry(), ZEND_STRL("defaultErrorHandling"), eh TSRMLS_CC);
                                break;
 
                        default:
                                php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
+                               break;
                }
        }
 }
 
+PHP_METHOD(HttpObject, triggerError)
+{
+       long eh, code;
+       char *msg_str;
+       int msg_len;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lls", &eh, &code, &msg_str, &msg_len)) {
+               php_http_error(eh TSRMLS_CC, code, "%.*s", msg_len, msg_str);
+       }
+}
+
 PHP_MINIT_FUNCTION(http_object)
 {
        PHP_HTTP_REGISTER_CLASS(http, Object, http_object, NULL, ZEND_ACC_ABSTRACT);
-       php_http_object_class_entry->create_object = php_http_object_new;
+       php_http_object_get_class_entry()->create_object = php_http_object_new;
 
-       zend_declare_property_null(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), (ZEND_ACC_STATIC|ZEND_ACC_PROTECTED) TSRMLS_CC);
-       zend_declare_property_null(php_http_object_class_entry, ZEND_STRL("errorHandling"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_object_get_class_entry(), ZEND_STRL("defaultErrorHandling"), (ZEND_ACC_STATIC|ZEND_ACC_PROTECTED) TSRMLS_CC);
+       zend_declare_property_null(php_http_object_get_class_entry(), ZEND_STRL("errorHandling"), ZEND_ACC_PROTECTED TSRMLS_CC);
 
-       zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_NORMAL"), EH_NORMAL TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_SUPPRESS"), EH_SUPPRESS TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_THROW"), EH_THROW TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_object_get_class_entry(), ZEND_STRL("EH_NORMAL"), EH_NORMAL TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_object_get_class_entry(), ZEND_STRL("EH_SUPPRESS"), EH_SUPPRESS TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_object_get_class_entry(), ZEND_STRL("EH_THROW"), EH_THROW TSRMLS_CC);
 
        return SUCCESS;
 }