- fixed a gotcha in http_chunked_decode (-size_t is always > 0)
[m6w6/ext-http] / php_http_std_defs.h
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifndef PHP_HTTP_STD_DEFS_H
19 #define PHP_HTTP_STD_DEFS_H
20
21 #if defined(PHP_WIN32)
22 # if defined(HTTP_EXPORTS)
23 # define PHP_HTTP_API __declspec(dllexport)
24 # elif defined(COMPILE_DL_HTTP)
25 # define PHP_HTTP_API __declspec(dllimport)
26 # else
27 # define PHP_HTTP_API
28 # endif
29 #else
30 # define PHP_HTTP_API
31 #endif
32
33 /* make functions that return SUCCESS|FAILURE more obvious */
34 typedef int STATUS;
35
36 /* lenof() */
37 #define lenof(S) (sizeof(S) - 1)
38
39 #ifndef MIN
40 # define MIN(a,b) (a<b?a:b)
41 #endif
42 #ifndef MAX
43 # define MAX(a,b) (a>b?a:b)
44 #endif
45
46 /* STR_SET() */
47 #ifndef STR_SET
48 # define STR_SET(STR, SET) \
49 { \
50 STR_FREE(STR); \
51 STR = SET; \
52 }
53 #endif
54
55 #define INIT_ZARR(zv, ht) \
56 { \
57 INIT_PZVAL(&(zv)); \
58 Z_TYPE(zv) = IS_ARRAY; \
59 Z_ARRVAL(zv) = (ht); \
60 }
61
62 /* return bool (v == SUCCESS) */
63 #define RETVAL_SUCCESS(v) RETVAL_BOOL(SUCCESS == (v))
64 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
65 /* return object(values) */
66 #define RETVAL_OBJECT(o) \
67 RETVAL_OBJVAL((o)->value.obj)
68 #define RETURN_OBJECT(o) \
69 RETVAL_OBJECT(o); \
70 return
71 #define RETVAL_OBJVAL(ov) \
72 ZVAL_OBJVAL(return_value, ov)
73 #define RETURN_OBJVAL(ov) \
74 RETVAL_OBJVAL(ov); \
75 return
76 #define ZVAL_OBJVAL(zv, ov) \
77 (zv)->is_ref = 1; \
78 (zv)->type = IS_OBJECT; \
79 (zv)->value.obj = (ov); \
80 if (Z_OBJ_HT_P(zv)->add_ref) { \
81 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
82 }
83
84 /* function accepts no args */
85 #define NO_ARGS \
86 if (ZEND_NUM_ARGS()) { \
87 zend_error(E_NOTICE, "Wrong parameter count for %s()", get_active_function_name(TSRMLS_C)); \
88 }
89
90 /* check if return value is used */
91 #define IF_RETVAL_USED \
92 if (!return_value_used) { \
93 return; \
94 } else
95
96 /* CR LF */
97 #define HTTP_CRLF "\r\n"
98
99 /* default cache control */
100 #define HTTP_DEFAULT_CACHECONTROL "private, must-revalidate, max-age=0"
101
102 /* max URL length */
103 #define HTTP_URL_MAXLEN 2048
104 #define HTTP_URI_MAXLEN HTTP_URL_MAXLEN
105
106 /* def URL arg separator */
107 #define HTTP_URL_ARGSEP "&"
108 #define HTTP_URI_ARGSEP HTTP_URL_ARGSEP
109
110 /* send buffer size */
111 #define HTTP_SENDBUF_SIZE 2097152
112
113 /* CURL buffer size */
114 #define HTTP_CURLBUF_SIZE 16384
115
116 /* known methods */
117 #define HTTP_KNOWN_METHODS \
118 /* HTTP 1.1 */ \
119 "GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, CONNECT, " \
120 /* WebDAV - RFC 2518 */ \
121 "PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, " \
122 /* WebDAV Versioning - RFC 3253 */ \
123 "VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, " \
124 "MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, " \
125 /* WebDAV Access Control - RFC 3744 */ \
126 "ACL, " \
127 /* END */
128
129
130 #define HTTP_PHP_INI_ENTRY(entry, default, scope, updater, global) \
131 STD_PHP_INI_ENTRY(entry, default, scope, updater, global, zend_http_globals, http_globals)
132 #define HTTP_PHP_INI_ENTRY_EX(entry, default, scope, updater, displayer, global) \
133 STD_PHP_INI_ENTRY_EX(entry, default, scope, updater, global, zend_http_globals, http_globals, displayer)
134
135 /* {{{ arrays */
136 #define FOREACH_VAL(array, val) FOREACH_HASH_VAL(Z_ARRVAL_P(array), val)
137 #define FOREACH_HASH_VAL(hash, val) \
138 for ( zend_hash_internal_pointer_reset(hash); \
139 zend_hash_get_current_data(hash, (void **) &val) == SUCCESS; \
140 zend_hash_move_forward(hash))
141
142 #define FOREACH_KEY(array, strkey, numkey) FOREACH_HASH_KEY(Z_ARRVAL_P(array), strkey, numkey)
143 #define FOREACH_HASH_KEY(hash, strkey, numkey) \
144 for ( zend_hash_internal_pointer_reset(hash); \
145 zend_hash_get_current_key(hash, &strkey, &numkey, 0) != HASH_KEY_NON_EXISTANT; \
146 zend_hash_move_forward(hash)) \
147
148 #define FOREACH_KEYVAL(array, strkey, numkey, val) FOREACH_HASH_KEYVAL(Z_ARRVAL_P(array), strkey, numkey, val)
149 #define FOREACH_HASH_KEYVAL(hash, strkey, numkey, val) \
150 for ( zend_hash_internal_pointer_reset(hash); \
151 zend_hash_get_current_key(hash, &strkey, &numkey, 0) != HASH_KEY_NON_EXISTANT && \
152 zend_hash_get_current_data(hash, (void **) &val) == SUCCESS; \
153 zend_hash_move_forward(hash)) \
154
155 #define array_copy(src, dst) zend_hash_copy(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
156 #define array_merge(src, dst) zend_hash_merge(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *), 1)
157 #define array_append(src, dst) \
158 { \
159 ulong idx; \
160 uint klen; \
161 char *key = NULL; \
162 zval **data; \
163 \
164 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(src)); \
165 zend_hash_get_current_key_ex(Z_ARRVAL_P(src), &key, &klen, &idx, 0, NULL) != HASH_KEY_NON_EXISTANT && \
166 zend_hash_get_current_data(Z_ARRVAL_P(src), (void **) &data) == SUCCESS; \
167 zend_hash_move_forward(Z_ARRVAL_P(src))) \
168 { \
169 if (key) { \
170 zval **tmp; \
171 \
172 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(dst), key, klen, (void **) &tmp)) { \
173 if (Z_TYPE_PP(tmp) != IS_ARRAY) { \
174 convert_to_array_ex(tmp); \
175 } \
176 add_next_index_zval(*tmp, *data); \
177 } else { \
178 add_assoc_zval(dst, key, *data); \
179 } \
180 zval_add_ref(data); \
181 key = NULL; \
182 } \
183 } \
184 }
185 /* }}} */
186
187 #define HTTP_LONG_CONSTANT(name, const) REGISTER_LONG_CONSTANT(name, const, CONST_CS | CONST_PERSISTENT)
188
189 /* {{{ objects & properties */
190 #ifdef ZEND_ENGINE_2
191
192 # define HTTP_STATIC_ME_ALIAS(me, al, ai) ZEND_FENTRY(me, ZEND_FN(al), ai, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
193
194 # define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \
195 { \
196 zend_class_entry ce; \
197 INIT_CLASS_ENTRY(ce, #classname, name## _fe); \
198 ce.create_object = name## _new; \
199 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
200 name## _ce->ce_flags |= flags; \
201 memcpy(& name## _handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
202 zend_hash_init(& name## _ce->constants_table, 0, NULL, ZVAL_INTERNAL_PTR_DTOR, 1); \
203 name## _declare_default_properties(); \
204 }
205
206 # define HTTP_REGISTER_CLASS(classname, name, parent, flags) \
207 { \
208 zend_class_entry ce; \
209 INIT_CLASS_ENTRY(ce, #classname, name## _fe); \
210 ce.create_object = NULL; \
211 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
212 name## _ce->ce_flags |= flags; \
213 }
214
215 # define HTTP_REGISTER_EXCEPTION(classname, cename, parent) \
216 { \
217 zend_class_entry ce; \
218 INIT_CLASS_ENTRY(ce, #classname, NULL); \
219 ce.create_object = NULL; \
220 cename = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
221 }
222
223 # define getObject(t, o) getObjectEx(t, o, getThis())
224 # define getObjectEx(t, o, v) t * o = ((t *) zend_object_store_get_object(v TSRMLS_CC))
225 # define putObject(t, o) zend_objects_store_put(o, (zend_objects_store_dtor_t) zend_objects_destroy_object, (zend_objects_free_object_storage_t) t## _free, NULL TSRMLS_CC);
226 # define OBJ_PROP(o) (o)->zo.properties
227
228 # define DCL_STATIC_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n)-1, (v), (ZEND_ACC_ ##a | ZEND_ACC_STATIC) TSRMLS_CC)
229 # define DCL_STATIC_PROP_Z(a, n, v) zend_declare_property(ce, (#n), sizeof(#n)-1, (v), (ZEND_ACC_ ##a | ZEND_ACC_STATIC) TSRMLS_CC)
230 # define DCL_STATIC_PROP_N(a, n) zend_declare_property_null(ce, (#n), sizeof(#n)-1, (ZEND_ACC_ ##a | ZEND_ACC_STATIC) TSRMLS_CC)
231 # define GET_STATIC_PROP_EX(ce, n) zend_std_get_static_property(ce, (#n), sizeof(#n)-1, 0 TSRMLS_CC)
232 # define UPD_STATIC_PROP_EX(ce, t, n, v) zend_update_static_property_ ##t(ce, #n, sizeof(#n)-1, (v) TSRMLS_CC)
233 # define UPD_STATIC_STRL_EX(ce, n, v, l) zend_update_static_property_stringl(ce, #n, sizeof(#n)-1, (v), (l) TSRMLS_CC)
234 # define SET_STATIC_PROP_EX(ce, n, v) zend_update_static_property(ce, #n, sizeof(#n)-1, v TSRMLS_CC)
235
236 # define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n)-1, (v), (ZEND_ACC_ ##a) TSRMLS_CC)
237 # define DCL_PROP_Z(a, n, v) zend_declare_property(ce, (#n), sizeof(#n)-1, (v), (ZEND_ACC_ ##a) TSRMLS_CC)
238 # define DCL_PROP_N(a, n) zend_declare_property_null(ce, (#n), sizeof(#n)-1, (ZEND_ACC_ ##a) TSRMLS_CC)
239 # define UPD_PROP(o, t, n, v) UPD_PROP_EX(o, getThis(), t, n, v)
240 # define UPD_PROP_EX(o, this, t, n, v) zend_update_property_ ##t(o->zo.ce, this, (#n), sizeof(#n)-1, (v) TSRMLS_CC)
241 # define UPD_STRL(o, n, v, l) zend_update_property_stringl(o->zo.ce, getThis(), (#n), sizeof(#n)-1, (v), (l) TSRMLS_CC)
242 # define SET_PROP(o, n, z) SET_PROP_EX(o, getThis(), n, z)
243 # define SET_PROP_EX(o, this, n, z) zend_update_property(o->zo.ce, this, (#n), sizeof(#n)-1, (z) TSRMLS_CC)
244 # define GET_PROP(o, n) GET_PROP_EX(o, getThis(), n)
245 # define GET_PROP_EX(o, this, n) zend_read_property(o->zo.ce, this, (#n), sizeof(#n)-1, 0 TSRMLS_CC)
246
247 # define DCL_CONST(t, n, v) zend_declare_class_constant_ ##t(ce, (n), sizeof(n)-1, (v) TSRMLS_CC)
248
249 # define ACC_PROP_PRIVATE(ce, flags) ((flags & ZEND_ACC_PRIVATE) && (EG(scope) && ce == EG(scope))
250 # define ACC_PROP_PROTECTED(ce, flags) ((flags & ZEND_ACC_PROTECTED) && (zend_check_protected(ce, EG(scope))))
251 # define ACC_PROP_PUBLIC(flags) (flags & ZEND_ACC_PUBLIC)
252 # define ACC_PROP(ce, flags) (ACC_PROP_PUBLIC(flags) || ACC_PROP_PRIVATE(ce, flags) || ACC_PROP_PROTECTED(ce, flags))
253
254 # define INIT_PARR(o, n) \
255 { \
256 zval *__tmp; \
257 MAKE_STD_ZVAL(__tmp); \
258 array_init(__tmp); \
259 SET_PROP(o, n, __tmp); \
260 }
261
262 # define FREE_PARR(o, p) \
263 { \
264 zval *__tmp = GET_PROP(o, p); \
265 if (__tmp) { \
266 zval_ptr_dtor(&__tmp); \
267 } \
268 }
269
270 # define SET_EH_THROW() SET_EH_THROW_EX(zend_exception_get_default())
271 # define SET_EH_THROW_HTTP() SET_EH_THROW_EX(http_exception_get_default())
272 # define SET_EH_THROW_EX(ex) php_set_error_handling(EH_THROW, ex TSRMLS_CC)
273 # define SET_EH_NORMAL() php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC)
274
275 #endif /* ZEND_ENGINE_2 */
276 /* }}} */
277
278 #ifndef E_THROW
279 # define E_THROW 0
280 #endif
281 #ifdef ZEND_ENGINE_2
282 # define HE_THROW E_THROW TSRMLS_CC
283 # define HE_NOTICE (HTTP_G(only_exceptions) ? E_THROW : E_NOTICE) TSRMLS_CC
284 # define HE_WARNING (HTTP_G(only_exceptions) ? E_THROW : E_WARNING) TSRMLS_CC
285 # define HE_ERROR (HTTP_G(only_exceptions) ? E_THROW : E_ERROR) TSRMLS_CC
286 #else
287 # define HE_THROW E_WARNING TSRMLS_CC
288 # define HE_NOTICE E_NOTICE TSRMLS_CC
289 # define HE_WARNING E_WARNING TSRMLS_CC
290 # define HE_ERROR E_ERROR TSRMLS_CC
291 #endif
292
293 #define HTTP_E_RUNTIME 1L
294 #define HTTP_E_INVALID_PARAM 2L
295 #define HTTP_E_HEADER 3L
296 #define HTTP_E_MALFORMED_HEADERS 4L
297 #define HTTP_E_REQUEST_METHOD 5L
298 #define HTTP_E_MESSAGE_TYPE 6L
299 #define HTTP_E_ENCODING 7L
300 #define HTTP_E_REQUEST 8L
301 #define HTTP_E_REQUEST_POOL 9L
302 #define HTTP_E_SOCKET 10L
303 #define HTTP_E_RESPONSE 11L
304 #define HTTP_E_URL 12L
305
306 #ifdef ZEND_ENGINE_2
307 # define HTTP_BEGIN_ARGS_EX(class, method, ret_ref, req_args) static ZEND_BEGIN_ARG_INFO_EX(args_for_ ##class## _ ##method , 0, ret_ref, req_args)
308 # define HTTP_BEGIN_ARGS_AR(class, method, ret_ref, req_args) static ZEND_BEGIN_ARG_INFO_EX(args_for_ ##class## _ ##method , 1, ret_ref, req_args)
309 # define HTTP_END_ARGS }
310 # define HTTP_EMPTY_ARGS_EX(class, method, ret_ref) HTTP_BEGIN_ARGS_EX(class, method, ret_ref, 0) HTTP_END_ARGS
311 # define HTTP_ARGS(class, method) args_for_ ##class## _ ##method
312 # define HTTP_ARG_VAL(name, pass_ref) ZEND_ARG_INFO(pass_ref, name)
313 # define HTTP_ARG_OBJ(class, name, allow_null) ZEND_ARG_OBJ_INFO(0, name, class, allow_null)
314 #endif
315
316 #ifdef ZEND_ENGINE_2
317 # define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
318 #else
319 # define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL}
320 #endif
321
322 #ifdef HTTP_HAVE_CURL
323 # ifdef ZEND_ENGINE_2
324 # define HTTP_DECLARE_ARG_PASS_INFO() \
325 static \
326 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_3, 0) \
327 ZEND_ARG_PASS_INFO(0) \
328 ZEND_ARG_PASS_INFO(0) \
329 ZEND_ARG_PASS_INFO(1) \
330 ZEND_END_ARG_INFO(); \
331 \
332 static \
333 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_4, 0) \
334 ZEND_ARG_PASS_INFO(0) \
335 ZEND_ARG_PASS_INFO(0) \
336 ZEND_ARG_PASS_INFO(0) \
337 ZEND_ARG_PASS_INFO(1) \
338 ZEND_END_ARG_INFO(); \
339 \
340 static \
341 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_5, 0) \
342 ZEND_ARG_PASS_INFO(0) \
343 ZEND_ARG_PASS_INFO(0) \
344 ZEND_ARG_PASS_INFO(0) \
345 ZEND_ARG_PASS_INFO(0) \
346 ZEND_ARG_PASS_INFO(1) \
347 ZEND_END_ARG_INFO();
348
349 # else
350 # define HTTP_DECLARE_ARG_PASS_INFO() \
351 static unsigned char http_arg_pass_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE}; \
352 static unsigned char http_arg_pass_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE}; \
353 static unsigned char http_arg_pass_ref_5[] = {5, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
354 # endif /* ZEND_ENGINE_2 */
355 #else
356 # define HTTP_DECLARE_ARG_PASS_INFO()
357 #endif /* HTTP_HAVE_CURL */
358
359
360 #ifndef TSRMLS_FETCH_FROM_CTX
361 # ifdef ZTS
362 # define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx
363 # else
364 # define TSRMLS_FETCH_FROM_CTX(ctx)
365 # endif
366 #endif
367
368 #ifndef TSRMLS_SET_CTX
369 # ifdef ZTS
370 # define TSRMLS_SET_CTX(ctx) ctx = (void ***) tsrm_ls
371 # else
372 # define TSRMLS_SET_CTX(ctx)
373 # endif
374 #endif
375
376 #ifndef ZVAL_ZVAL
377 #define ZVAL_ZVAL(z, zv, copy, dtor) { \
378 int is_ref, refcount; \
379 is_ref = (z)->is_ref; \
380 refcount = (z)->refcount; \
381 *(z) = *(zv); \
382 if (copy) { \
383 zval_copy_ctor(z); \
384 } \
385 if (dtor) { \
386 if (!copy) { \
387 ZVAL_NULL(zv); \
388 } \
389 zval_ptr_dtor(&zv); \
390 } \
391 (z)->is_ref = is_ref; \
392 (z)->refcount = refcount; \
393 }
394 #endif
395 #ifndef RETVAL_ZVAL
396 #define RETVAL_ZVAL(zv, copy, dtor) ZVAL_ZVAL(return_value, zv, copy, dtor)
397 #endif
398 #ifndef RETURN_ZVAL
399 #define RETURN_ZVAL(zv, copy, dtor) { RETVAL_ZVAL(zv, copy, dtor); return; }
400 #endif
401
402
403 #endif /* PHP_HTTP_STD_DEFS_H */
404
405 /*
406 * Local variables:
407 * tab-width: 4
408 * c-basic-offset: 4
409 * End:
410 * vim600: noet sw=4 ts=4 fdm=marker
411 * vim<600: noet sw=4 ts=4
412 */
413