prepared for libevent2 usage in config.w32, though libevent usage seems to be broken...
[m6w6/ext-http] / php_http_object.c
index 9282e5d66c9ee3f3b0a7db84c09bf8c285377eb3..4ec54d374f20ba8381bc7d01c279d7794b565b07 100644 (file)
@@ -6,17 +6,16 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2013, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http_api.c 300299 2010-06-09 06:23:16Z mike $ */
+#include "php_http_api.h"
 
-
-#include "php_http.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 *ovp, zend_class_entry *ce, php_http_new_t create, zend_class_entry *parent_ce, void *intern_ptr, void **obj_ptr TSRMLS_DC)
 {
+       zend_object_value ov;
+
        if (!ce) {
                ce = parent_ce;
        } else if (parent_ce && !instanceof_function(ce, parent_ce TSRMLS_CC)) {
@@ -24,7 +23,10 @@ STATUS php_http_new(zend_object_value *ov, zend_class_entry *ce, php_http_new_t
                return FAILURE;
        }
 
-       *ov = create(ce, intern_ptr, obj_ptr TSRMLS_CC);
+       ov = create(ce, intern_ptr, obj_ptr TSRMLS_CC);
+       if (ovp) {
+               *ovp = ov;
+       }
        return SUCCESS;
 }
 
@@ -35,14 +37,14 @@ PHP_HTTP_API zend_error_handling_t php_http_object_get_error_handling(zval *obje
 
        zeh = zend_read_property(Z_OBJCE_P(object), object, ZEND_STRL("errorHandling"), 0 TSRMLS_CC);
        if (Z_TYPE_P(zeh) != IS_NULL) {
-               lzeh = php_http_zsep(IS_LONG, zeh);
+               lzeh = php_http_ztyp(IS_LONG, zeh);
                eh = Z_LVAL_P(lzeh);
                zval_ptr_dtor(&lzeh);
                return eh;
        }
        zeh = zend_read_static_property(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), 0 TSRMLS_CC);
        if (Z_TYPE_P(zeh) != IS_NULL) {
-               lzeh = php_http_zsep(IS_LONG, zeh);
+               lzeh = php_http_ztyp(IS_LONG, zeh);
                eh = Z_LVAL_P(lzeh);
                zval_ptr_dtor(&lzeh);
                return eh;
@@ -50,39 +52,6 @@ PHP_HTTP_API zend_error_handling_t php_http_object_get_error_handling(zval *obje
        return EH_NORMAL;
 }
 
-#define PHP_HTTP_BEGIN_ARGS(method, req_args)                  PHP_HTTP_BEGIN_ARGS_EX(HttpObject, method, 0, req_args)
-#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;
-
-PHP_HTTP_EMPTY_ARGS(getErrorHandling);
-
-PHP_HTTP_BEGIN_ARGS(setDefaultErrorHandling, 1)
-       PHP_HTTP_ARG_VAL(eh, 0)
-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_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)
-       
-       EMPTY_FUNCTION_ENTRY
-};
-
 zend_object_value php_http_object_new(zend_class_entry *ce TSRMLS_DC)
 {
        return php_http_object_new_ex(ce, NULL, NULL TSRMLS_CC);
@@ -90,59 +59,36 @@ zend_object_value php_http_object_new(zend_class_entry *ce TSRMLS_DC)
 
 zend_object_value php_http_object_new_ex(zend_class_entry *ce, void *nothing, php_http_object_t **ptr TSRMLS_DC)
 {
-       zend_object_value ov;
        php_http_object_t *o;
 
        o = ecalloc(1, sizeof(php_http_object_t));
-       zend_object_std_init((zend_object *)o, ce TSRMLS_CC);
+       zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
        object_properties_init((zend_object *) o, ce);
 
        if (ptr) {
                *ptr = o;
        }
 
-       ov.handle = zend_objects_store_put(o, NULL, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
-       ov.handlers = zend_get_std_object_handlers();
+       o->zv.handle = zend_objects_store_put(o, NULL, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
+       o->zv.handlers = zend_get_std_object_handlers();
 
-       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_EX_CE(runtime)) {
-               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();
+       return o->zv;
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_getErrorHandling, 0, 0, 0)
+ZEND_END_ARG_INFO();
 PHP_METHOD(HttpObject, getErrorHandling)
 {
-       RETURN_PROP(php_http_object_class_entry, "errorHandling");
+       zval *zeh = zend_read_property(php_http_object_class_entry, getThis(), ZEND_STRL("errorHandling"), 0 TSRMLS_CC);
+       RETURN_ZVAL(zeh, 1, 0);
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_setErrorHandling, 0, 0, 1)
+       ZEND_ARG_INFO(0, eh)
+ZEND_END_ARG_INFO();
 PHP_METHOD(HttpObject, setErrorHandling)
 {
        long eh;
-       zval *old;
 
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eh)) {
                switch (eh) {
@@ -154,21 +100,27 @@ PHP_METHOD(HttpObject, setErrorHandling)
 
                        default:
                                php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
+                               break;
                }
        }
 
        RETURN_ZVAL(getThis(), 1, 0);
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_getDefaultErrorHandling, 0, 0, 0)
+ZEND_END_ARG_INFO();
 PHP_METHOD(HttpObject, getDefaultErrorHandling)
 {
-       RETURN_SPROP(php_http_object_class_entry, "defaultErrorHandling");
+       zval *zdeh = zend_read_static_property(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), 0 TSRMLS_CC);
+       RETURN_ZVAL(zdeh, 1, 0);
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_setDefaultErrorHandling, 0, 0, 1)
+       ZEND_ARG_INFO(0, eh)
+ZEND_END_ARG_INFO();
 PHP_METHOD(HttpObject, setDefaultErrorHandling)
 {
        long eh;
-       zval *old;
 
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eh)) {
                switch (eh) {
@@ -180,13 +132,46 @@ PHP_METHOD(HttpObject, setDefaultErrorHandling)
 
                        default:
                                php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
+                               break;
                }
        }
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_triggerError, 0, 0, 3)
+       ZEND_ARG_INFO(0, error_type)
+       ZEND_ARG_INFO(0, error_code)
+       ZEND_ARG_INFO(0, error_message)
+ZEND_END_ARG_INFO();
+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);
+       }
+}
+
+static zend_function_entry php_http_object_methods[] = {
+       PHP_ME(HttpObject, setErrorHandling,        ai_HttpObject_setErrorHandling,        ZEND_ACC_PUBLIC)
+       PHP_ME(HttpObject, getErrorHandling,        ai_HttpObject_getErrorHandling,        ZEND_ACC_PUBLIC)
+       PHP_ME(HttpObject, setDefaultErrorHandling, ai_HttpObject_setDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+       PHP_ME(HttpObject, getDefaultErrorHandling, ai_HttpObject_getDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+       PHP_ME(HttpObject, triggerError,            ai_HttpObject_triggerError,            ZEND_ACC_PUBLIC)
+
+       EMPTY_FUNCTION_ENTRY
+};
+
+zend_class_entry *php_http_object_class_entry;
+
 PHP_MINIT_FUNCTION(http_object)
 {
-       PHP_HTTP_REGISTER_CLASS(http, Object, http_object, NULL, ZEND_ACC_ABSTRACT);
+       zend_class_entry ce = {0};
+
+       INIT_NS_CLASS_ENTRY(ce, "http", "Object", php_http_object_methods);
+       php_http_object_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
+       php_http_object_class_entry->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
        php_http_object_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);