rm unused constant
[m6w6/ext-http] / php_http_misc.h
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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifndef PHP_HTTP_MISC_H
14 #define PHP_HTTP_MISC_H
15
16 /* DEFAULTS */
17
18 /* DATE FORMAT RFC1123 */
19 #define PHP_HTTP_DATE_FORMAT "D, d M Y H:i:s \\G\\M\\T"
20
21 /* CR LF */
22 #define PHP_HTTP_CRLF "\r\n"
23
24 /* def URL arg separator */
25 #define PHP_HTTP_URL_ARGSEP "&"
26
27 /* send buffer size */
28 #define PHP_HTTP_SENDBUF_SIZE 40960
29
30 /* SLEEP */
31
32 #define PHP_HTTP_DIFFSEC (0.001)
33 #define PHP_HTTP_MLLISEC (1000)
34 #define PHP_HTTP_MCROSEC (1000 * 1000)
35 #define PHP_HTTP_NANOSEC (1000 * 1000 * 1000)
36 #define PHP_HTTP_MSEC(s) ((long)(s * PHP_HTTP_MLLISEC))
37 #define PHP_HTTP_USEC(s) ((long)(s * PHP_HTTP_MCROSEC))
38 #define PHP_HTTP_NSEC(s) ((long)(s * PHP_HTTP_NANOSEC))
39
40 PHP_HTTP_API void php_http_sleep(double s);
41
42 /* STRING UTILITIES */
43
44 #ifndef STR_SET
45 # define STR_SET(STR, SET) \
46 { \
47 STR_FREE(STR); \
48 STR = SET; \
49 }
50 #endif
51
52 #define STR_PTR(s) (s?s:"")
53
54 #define lenof(S) (sizeof(S) - 1)
55
56 #define PHP_HTTP_MATCH_LOOSE 0
57 #define PHP_HTTP_MATCH_CASE 0x01
58 #define PHP_HTTP_MATCH_WORD 0x10
59 #define PHP_HTTP_MATCH_FULL 0x20
60 #define PHP_HTTP_MATCH_STRICT (PHP_HTTP_MATCH_CASE|PHP_HTTP_MATCH_FULL)
61
62 int php_http_match(const char *haystack, const char *needle, int flags);
63 char *php_http_pretty_key(register char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen);
64 size_t php_http_boundary(char *buf, size_t len TSRMLS_DC);
65 int php_http_select_str(const char *cmp, int argc, ...);
66
67 /* See "A Reusable Duff Device" By Ralf Holly, August 01, 2005 */
68 #define PHP_HTTP_DUFF_BREAK(i) do { \
69 times_##i = 1; \
70 } while (0)
71
72 #define PHP_HTTP_DUFF(i, c, a) do { \
73 size_t count_##i = (c); \
74 size_t times_##i = (count_##i + 7) >> 3; \
75 switch (count_##i & 7){ \
76 case 0: do { a; \
77 case 7: a; \
78 case 6: a; \
79 case 5: a; \
80 case 4: a; \
81 case 3: a; \
82 case 2: a; \
83 case 1: a; \
84 } while (--times_##i > 0); \
85 } \
86 } while (0)
87
88
89 static inline const char *php_http_locate_str(register const char *h, size_t h_len, const char *n, size_t n_len)
90 {
91 register const char *p1, *p2;
92
93 if (n_len && h_len && h_len >= n_len) {
94 PHP_HTTP_DUFF(1, h_len - n_len + 1,
95 if (*h == *n) {
96 p1 = h;
97 p2 = n;
98 PHP_HTTP_DUFF(2, n_len,
99 if (*p1++ != *p2++) {
100 PHP_HTTP_DUFF_BREAK(2);
101 } else if (p2 == n + n_len - 1) {
102 return h;
103 }
104 );
105 }
106 ++h;
107 );
108 }
109
110 return NULL;
111 }
112
113 static inline const char *php_http_locate_eol(const char *line, int *eol_len)
114 {
115 const char *eol = strpbrk(line, "\r\n");
116
117 if (eol_len) {
118 *eol_len = eol ? ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1) : 0;
119 }
120 return eol;
121 }
122
123 static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, int *eol_len)
124 {
125 register const char *eol = bin;
126
127 if (len > 0) {
128 PHP_HTTP_DUFF(1, len,
129 if (*eol == '\r' || *eol == '\n') {
130 if (eol_len) {
131 *eol_len = ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1);
132 }
133 return eol;
134 }
135 ++eol;
136 );
137 }
138 return NULL;
139 }
140
141 /* ZEND */
142
143 #if PHP_VERSION_ID < 50400
144 # define object_properties_init(o, ce) zend_hash_copy(((zend_object *) o)->properties, &(ce->default_properties), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*))
145 # define PHP_HTTP_ZEND_LITERAL_DC
146 # define PHP_HTTP_ZEND_LITERAL_CC
147 # define PHP_HTTP_ZEND_LITERAL_CCN
148 # define ZVAL_COPY_VALUE(zv, arr) do { \
149 (zv)->value = (arr)->value; \
150 Z_TYPE_P(zv) = Z_TYPE_P(arr); \
151 } while (0)
152 #else
153 # define PHP_HTTP_ZEND_LITERAL_DC , const zend_literal *literal_key
154 # define PHP_HTTP_ZEND_LITERAL_CC , (literal_key)
155 # define PHP_HTTP_ZEND_LITERAL_CCN , NULL
156 #endif
157
158 #if PHP_VERSION_ID < 50700
159 # define z_is_true zend_is_true
160 #else
161 # define z_is_true(z) zend_is_true(z TSRMLS_CC)
162 #endif
163
164 #define INIT_PZVAL_ARRAY(zv, ht) \
165 { \
166 INIT_PZVAL((zv)); \
167 Z_TYPE_P(zv) = IS_ARRAY; \
168 Z_ARRVAL_P(zv) = (ht); \
169 }
170
171 static inline zval *php_http_zconv(int type, zval *z)
172 {
173 switch (type) {
174 case IS_NULL: convert_to_null_ex(&z); break;
175 case IS_BOOL: convert_to_boolean_ex(&z); break;
176 case IS_LONG: convert_to_long_ex(&z); break;
177 case IS_DOUBLE: convert_to_double_ex(&z); break;
178 case IS_STRING: convert_to_string_ex(&z); break;
179 case IS_ARRAY: convert_to_array_ex(&z); break;
180 case IS_OBJECT: convert_to_object_ex(&z); break;
181 }
182 return z;
183 }
184
185 static inline zval *php_http_ztyp(int type, zval *z)
186 {
187 SEPARATE_ARG_IF_REF(z);
188 return (Z_TYPE_P(z) == type) ? z : php_http_zconv(type, z);
189 }
190
191 static inline zval *php_http_zsep(zend_bool add_ref, int type, zval *z)
192 {
193 if (add_ref) {
194 Z_ADDREF_P(z);
195 }
196 if (Z_TYPE_P(z) != type) {
197 return php_http_zconv(type, z);
198 } else {
199 SEPARATE_ZVAL_IF_NOT_REF(&z);
200 return z;
201 }
202 }
203
204 static inline STATUS php_http_ini_entry(const char *name_str, size_t name_len, const char **value_str, size_t *value_len, zend_bool orig TSRMLS_DC)
205 {
206 zend_ini_entry *ini_entry;
207
208 if (SUCCESS == zend_hash_find(EG(ini_directives), name_str, name_len + 1, (void *) &ini_entry)) {
209 if (orig && ini_entry->modified) {
210 *value_str = ini_entry->orig_value;
211 *value_len = (size_t) ini_entry->orig_value_length;
212 } else {
213 *value_str = ini_entry->value;
214 *value_len = (size_t) ini_entry->value_length;
215 }
216 return SUCCESS;
217 }
218 return FAILURE;
219 }
220
221 /* return object(values) */
222 #define RETVAL_OBJECT(o, addref) \
223 RETVAL_OBJVAL((o)->value.obj, addref)
224 #define RETURN_OBJECT(o, addref) \
225 RETVAL_OBJECT(o, addref); \
226 return
227 #define RETVAL_OBJVAL(ov, addref) \
228 ZVAL_OBJVAL(return_value, ov, addref)
229 #define RETURN_OBJVAL(ov, addref) \
230 RETVAL_OBJVAL(ov, addref); \
231 return
232 #define ZVAL_OBJVAL(zv, ov, addref) \
233 (zv)->type = IS_OBJECT; \
234 (zv)->value.obj = (ov);\
235 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
236 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
237 }
238
239 #define Z_OBJ_DELREF(z) \
240 if (Z_OBJ_HT(z)->del_ref) { \
241 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
242 }
243 #define Z_OBJ_ADDREF(z) \
244 if (Z_OBJ_HT(z)->add_ref) { \
245 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
246 }
247 #define Z_OBJ_DELREF_P(z) \
248 if (Z_OBJ_HT_P(z)->del_ref) { \
249 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
250 }
251 #define Z_OBJ_ADDREF_P(z) \
252 if (Z_OBJ_HT_P(z)->add_ref) { \
253 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
254 }
255 #define Z_OBJ_DELREF_PP(z) \
256 if (Z_OBJ_HT_PP(z)->del_ref) { \
257 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
258 }
259 #define Z_OBJ_ADDREF_PP(z) \
260 if (Z_OBJ_HT_PP(z)->add_ref) { \
261 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
262 }
263
264 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
265
266 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
267 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
268 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
269 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
270
271 /* ARRAYS */
272
273 #ifndef HASH_KEY_NON_EXISTENT
274 # define HASH_KEY_NON_EXISTENT HASH_KEY_NON_EXISTANT
275 #endif
276
277 PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...);
278
279 typedef struct php_http_array_hashkey {
280 char *str;
281 uint len;
282 ulong num;
283 uint dup:1;
284 uint type:31;
285 } php_http_array_hashkey_t;
286 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
287
288 static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t *key)
289 {
290 if (key->type != HASH_KEY_IS_STRING) {
291 key->len = spprintf(&key->str, 0, "%lu", key->num) + 1;
292 }
293 }
294
295 static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t *key)
296 {
297 if (key->type != HASH_KEY_IS_STRING || key->dup) {
298 STR_FREE(key->str);
299 }
300 }
301
302 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
303 #define FOREACH_HASH_VAL(pos, hash, val) \
304 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
305 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
306 zend_hash_move_forward_ex(hash, &pos))
307
308 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
309 #define FOREACH_HASH_KEY(pos, hash, _key) \
310 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
311 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
312 zend_hash_move_forward_ex(hash, &pos)) \
313
314 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
315 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
316 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
317 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
318 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
319 zend_hash_move_forward_ex(hash, &pos))
320
321 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
322 #define array_copy_strings(src, dst) zend_hash_copy(dst, src, php_http_array_copy_strings, NULL, sizeof(zval *))
323 #define ARRAY_JOIN_STRONLY 0x01
324 #define ARRAY_JOIN_PRETTIFY 0x02
325 #define ARRAY_JOIN_STRINGIFY 0x04
326 #define array_join(src, dst, append, flags) zend_hash_apply_with_arguments(src TSRMLS_CC, (append)?php_http_array_apply_append_func:php_http_array_apply_merge_func, 2, dst, (int)flags)
327
328 void php_http_array_copy_strings(void *zpp);
329 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
330 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
331
332 /* PASS CALLBACK */
333
334 typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len);
335 typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str);
336 typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...);
337
338 typedef struct php_http_pass_fcall_arg {
339 zval *fcz;
340 zend_fcall_info fci;
341 zend_fcall_info_cache fcc;
342 #ifdef ZTS
343 void ***ts;
344 #endif
345 } php_http_pass_fcall_arg_t;
346
347 PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
348
349 #endif
350
351 /*
352 * Local variables:
353 * tab-width: 4
354 * c-basic-offset: 4
355 * End:
356 * vim600: noet sw=4 ts=4 fdm=marker
357 * vim<600: noet sw=4 ts=4
358 */