prepared for libevent2 usage in config.w32, though libevent usage seems to be broken...
[m6w6/ext-http] / php_http_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 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)
16 {
17 zend_object_value ov;
18
19 if (!ce) {
20 ce = parent_ce;
21 } else if (parent_ce && !instanceof_function(ce, parent_ce TSRMLS_CC)) {
22 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "Class %s does not extend %s", ce->name, parent_ce->name);
23 return FAILURE;
24 }
25
26 ov = create(ce, intern_ptr, obj_ptr TSRMLS_CC);
27 if (ovp) {
28 *ovp = ov;
29 }
30 return SUCCESS;
31 }
32
33 PHP_HTTP_API zend_error_handling_t php_http_object_get_error_handling(zval *object TSRMLS_DC)
34 {
35 zval *zeh, *lzeh;
36 long eh;
37
38 zeh = zend_read_property(Z_OBJCE_P(object), object, ZEND_STRL("errorHandling"), 0 TSRMLS_CC);
39 if (Z_TYPE_P(zeh) != IS_NULL) {
40 lzeh = php_http_ztyp(IS_LONG, zeh);
41 eh = Z_LVAL_P(lzeh);
42 zval_ptr_dtor(&lzeh);
43 return eh;
44 }
45 zeh = zend_read_static_property(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), 0 TSRMLS_CC);
46 if (Z_TYPE_P(zeh) != IS_NULL) {
47 lzeh = php_http_ztyp(IS_LONG, zeh);
48 eh = Z_LVAL_P(lzeh);
49 zval_ptr_dtor(&lzeh);
50 return eh;
51 }
52 return EH_NORMAL;
53 }
54
55 zend_object_value php_http_object_new(zend_class_entry *ce TSRMLS_DC)
56 {
57 return php_http_object_new_ex(ce, NULL, NULL TSRMLS_CC);
58 }
59
60 zend_object_value php_http_object_new_ex(zend_class_entry *ce, void *nothing, php_http_object_t **ptr TSRMLS_DC)
61 {
62 php_http_object_t *o;
63
64 o = ecalloc(1, sizeof(php_http_object_t));
65 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
66 object_properties_init((zend_object *) o, ce);
67
68 if (ptr) {
69 *ptr = o;
70 }
71
72 o->zv.handle = zend_objects_store_put(o, NULL, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
73 o->zv.handlers = zend_get_std_object_handlers();
74
75 return o->zv;
76 }
77
78 ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_getErrorHandling, 0, 0, 0)
79 ZEND_END_ARG_INFO();
80 PHP_METHOD(HttpObject, getErrorHandling)
81 {
82 zval *zeh = zend_read_property(php_http_object_class_entry, getThis(), ZEND_STRL("errorHandling"), 0 TSRMLS_CC);
83 RETURN_ZVAL(zeh, 1, 0);
84 }
85
86 ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_setErrorHandling, 0, 0, 1)
87 ZEND_ARG_INFO(0, eh)
88 ZEND_END_ARG_INFO();
89 PHP_METHOD(HttpObject, setErrorHandling)
90 {
91 long eh;
92
93 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eh)) {
94 switch (eh) {
95 case EH_NORMAL:
96 case EH_SUPPRESS:
97 case EH_THROW:
98 zend_update_property_long(php_http_object_class_entry, getThis(), ZEND_STRL("errorHandling"), eh TSRMLS_CC);
99 break;
100
101 default:
102 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
103 break;
104 }
105 }
106
107 RETURN_ZVAL(getThis(), 1, 0);
108 }
109
110 ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_getDefaultErrorHandling, 0, 0, 0)
111 ZEND_END_ARG_INFO();
112 PHP_METHOD(HttpObject, getDefaultErrorHandling)
113 {
114 zval *zdeh = zend_read_static_property(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), 0 TSRMLS_CC);
115 RETURN_ZVAL(zdeh, 1, 0);
116 }
117
118 ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_setDefaultErrorHandling, 0, 0, 1)
119 ZEND_ARG_INFO(0, eh)
120 ZEND_END_ARG_INFO();
121 PHP_METHOD(HttpObject, setDefaultErrorHandling)
122 {
123 long eh;
124
125 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eh)) {
126 switch (eh) {
127 case EH_NORMAL:
128 case EH_SUPPRESS:
129 case EH_THROW:
130 zend_update_static_property_long(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), eh TSRMLS_CC);
131 break;
132
133 default:
134 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
135 break;
136 }
137 }
138 }
139
140 ZEND_BEGIN_ARG_INFO_EX(ai_HttpObject_triggerError, 0, 0, 3)
141 ZEND_ARG_INFO(0, error_type)
142 ZEND_ARG_INFO(0, error_code)
143 ZEND_ARG_INFO(0, error_message)
144 ZEND_END_ARG_INFO();
145 PHP_METHOD(HttpObject, triggerError)
146 {
147 long eh, code;
148 char *msg_str;
149 int msg_len;
150
151 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lls", &eh, &code, &msg_str, &msg_len)) {
152 php_http_error(eh TSRMLS_CC, code, "%.*s", msg_len, msg_str);
153 }
154 }
155
156 static zend_function_entry php_http_object_methods[] = {
157 PHP_ME(HttpObject, setErrorHandling, ai_HttpObject_setErrorHandling, ZEND_ACC_PUBLIC)
158 PHP_ME(HttpObject, getErrorHandling, ai_HttpObject_getErrorHandling, ZEND_ACC_PUBLIC)
159 PHP_ME(HttpObject, setDefaultErrorHandling, ai_HttpObject_setDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
160 PHP_ME(HttpObject, getDefaultErrorHandling, ai_HttpObject_getDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
161 PHP_ME(HttpObject, triggerError, ai_HttpObject_triggerError, ZEND_ACC_PUBLIC)
162
163 EMPTY_FUNCTION_ENTRY
164 };
165
166 zend_class_entry *php_http_object_class_entry;
167
168 PHP_MINIT_FUNCTION(http_object)
169 {
170 zend_class_entry ce = {0};
171
172 INIT_NS_CLASS_ENTRY(ce, "http", "Object", php_http_object_methods);
173 php_http_object_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
174 php_http_object_class_entry->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
175 php_http_object_class_entry->create_object = php_http_object_new;
176
177 zend_declare_property_null(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), (ZEND_ACC_STATIC|ZEND_ACC_PROTECTED) TSRMLS_CC);
178 zend_declare_property_null(php_http_object_class_entry, ZEND_STRL("errorHandling"), ZEND_ACC_PROTECTED TSRMLS_CC);
179
180 zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_NORMAL"), EH_NORMAL TSRMLS_CC);
181 zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_SUPPRESS"), EH_SUPPRESS TSRMLS_CC);
182 zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_THROW"), EH_THROW TSRMLS_CC);
183
184 return SUCCESS;
185 }
186
187
188 /*
189 * Local variables:
190 * tab-width: 4
191 * c-basic-offset: 4
192 * End:
193 * vim600: noet sw=4 ts=4 fdm=marker
194 * vim<600: noet sw=4 ts=4
195 */
196