fix skipif
[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(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 static inline const char *php_http_locate_str(const char *h, size_t h_len, const char *n, size_t n_len)
68 {
69 const char *p, *e;
70
71 if (n_len && h_len) {
72 e = h + h_len;
73 do {
74 if (*h == *n) {
75 for (p = n; *p == h[p-n]; ++p) {
76 if (p == n+n_len-1) {
77 return h;
78 }
79 }
80 }
81 } while (h++ != e);
82 }
83
84 return NULL;
85 }
86
87 static inline const char *php_http_locate_eol(const char *line, int *eol_len)
88 {
89 const char *eol = strpbrk(line, "\r\n");
90
91 if (eol_len) {
92 *eol_len = eol ? ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1) : 0;
93 }
94 return eol;
95 }
96
97 static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, int *eol_len)
98 {
99 const char *eol;
100
101 for (eol = bin; eol - bin < len; ++eol) {
102 if (*eol == '\r' || *eol == '\n') {
103 if (eol_len) {
104 *eol_len = ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1);
105 }
106 return eol;
107 }
108 }
109
110 return NULL;
111 }
112
113 /* ZEND */
114
115 #if PHP_VERSION_ID < 50400
116 # 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*))
117 # define PHP_HTTP_ZEND_LITERAL_DC
118 # define PHP_HTTP_ZEND_LITERAL_CC
119 # define PHP_HTTP_ZEND_LITERAL_CCN
120 # define ZVAL_COPY_VALUE(zv, arr) do { \
121 (zv)->value = (arr)->value; \
122 Z_TYPE_P(zv) = Z_TYPE_P(arr); \
123 } while (0)
124 #else
125 # define PHP_HTTP_ZEND_LITERAL_DC , const zend_literal *literal_key
126 # define PHP_HTTP_ZEND_LITERAL_CC , (literal_key)
127 # define PHP_HTTP_ZEND_LITERAL_CCN , NULL
128 #endif
129
130 #define INIT_PZVAL_ARRAY(zv, ht) \
131 { \
132 INIT_PZVAL((zv)); \
133 Z_TYPE_P(zv) = IS_ARRAY; \
134 Z_ARRVAL_P(zv) = (ht); \
135 }
136
137 static inline zval *php_http_ztyp(int type, zval *z)
138 {
139 SEPARATE_ARG_IF_REF(z);
140 if (Z_TYPE_P(z) != type) {
141 switch (type) {
142 case IS_NULL: convert_to_null_ex(&z); break;
143 case IS_BOOL: convert_to_boolean_ex(&z); break;
144 case IS_LONG: convert_to_long_ex(&z); break;
145 case IS_DOUBLE: convert_to_double_ex(&z); break;
146 case IS_STRING: convert_to_string_ex(&z); break;
147 case IS_ARRAY: convert_to_array_ex(&z); break;
148 case IS_OBJECT: convert_to_object_ex(&z); break;
149 }
150 }
151 return z;
152 }
153
154 static inline zval *php_http_zsep(zend_bool add_ref, int type, zval *z)
155 {
156 if (add_ref) {
157 Z_ADDREF_P(z);
158 }
159 if (Z_TYPE_P(z) != type) {
160 switch (type) {
161 case IS_NULL: convert_to_null_ex(&z); break;
162 case IS_BOOL: convert_to_boolean_ex(&z); break;
163 case IS_LONG: convert_to_long_ex(&z); break;
164 case IS_DOUBLE: convert_to_double_ex(&z); break;
165 case IS_STRING: convert_to_string_ex(&z); break;
166 case IS_ARRAY: convert_to_array_ex(&z); break;
167 case IS_OBJECT: convert_to_object_ex(&z); break;
168 }
169 } else {
170 SEPARATE_ZVAL_IF_NOT_REF(&z);
171 }
172 return z;
173 }
174
175 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)
176 {
177 zend_ini_entry *ini_entry;
178
179 if (SUCCESS == zend_hash_find(EG(ini_directives), name_str, name_len + 1, (void *) &ini_entry)) {
180 if (orig && ini_entry->modified) {
181 *value_str = ini_entry->orig_value;
182 *value_len = (size_t) ini_entry->orig_value_length;
183 } else {
184 *value_str = ini_entry->value;
185 *value_len = (size_t) ini_entry->value_length;
186 }
187 return SUCCESS;
188 }
189 return FAILURE;
190 }
191
192 /* return object(values) */
193 #define RETVAL_OBJECT(o, addref) \
194 RETVAL_OBJVAL((o)->value.obj, addref)
195 #define RETURN_OBJECT(o, addref) \
196 RETVAL_OBJECT(o, addref); \
197 return
198 #define RETVAL_OBJVAL(ov, addref) \
199 ZVAL_OBJVAL(return_value, ov, addref)
200 #define RETURN_OBJVAL(ov, addref) \
201 RETVAL_OBJVAL(ov, addref); \
202 return
203 #define ZVAL_OBJVAL(zv, ov, addref) \
204 (zv)->type = IS_OBJECT; \
205 (zv)->value.obj = (ov);\
206 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
207 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
208 }
209
210 #define Z_OBJ_DELREF(z) \
211 if (Z_OBJ_HT(z)->del_ref) { \
212 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
213 }
214 #define Z_OBJ_ADDREF(z) \
215 if (Z_OBJ_HT(z)->add_ref) { \
216 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
217 }
218 #define Z_OBJ_DELREF_P(z) \
219 if (Z_OBJ_HT_P(z)->del_ref) { \
220 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
221 }
222 #define Z_OBJ_ADDREF_P(z) \
223 if (Z_OBJ_HT_P(z)->add_ref) { \
224 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
225 }
226 #define Z_OBJ_DELREF_PP(z) \
227 if (Z_OBJ_HT_PP(z)->del_ref) { \
228 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
229 }
230 #define Z_OBJ_ADDREF_PP(z) \
231 if (Z_OBJ_HT_PP(z)->add_ref) { \
232 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
233 }
234
235 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
236
237 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
238 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
239 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
240 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
241
242 /* ARRAYS */
243 PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...);
244
245 typedef struct php_http_array_hashkey {
246 char *str;
247 uint len;
248 ulong num;
249 uint dup:1;
250 uint type:31;
251 } php_http_array_hashkey_t;
252 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
253
254 static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t *key)
255 {
256 if (key->type != HASH_KEY_IS_STRING) {
257 key->len = spprintf(&key->str, 0, "%lu", key->num) + 1;
258 }
259 }
260
261 static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t *key)
262 {
263 if (key->type != HASH_KEY_IS_STRING || key->dup) {
264 STR_FREE(key->str);
265 }
266 }
267
268 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
269 #define FOREACH_HASH_VAL(pos, hash, val) \
270 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
271 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
272 zend_hash_move_forward_ex(hash, &pos))
273
274 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
275 #define FOREACH_HASH_KEY(pos, hash, _key) \
276 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
277 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
278 zend_hash_move_forward_ex(hash, &pos)) \
279
280 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
281 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
282 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
283 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
284 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
285 zend_hash_move_forward_ex(hash, &pos))
286
287 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
288 #define ARRAY_JOIN_STRONLY 1
289 #define ARRAY_JOIN_PRETTIFY 2
290 #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)
291
292 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
293 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
294
295 /* PASS CALLBACK */
296
297 typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len);
298 typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str);
299 typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...);
300
301 typedef struct php_http_pass_fcall_arg {
302 zval *fcz;
303 zend_fcall_info fci;
304 zend_fcall_info_cache fcc;
305 #ifdef ZTS
306 void ***ts;
307 #endif
308 } php_http_pass_fcall_arg_t;
309
310 PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
311
312 #endif
313
314 /*
315 * Local variables:
316 * tab-width: 4
317 * c-basic-offset: 4
318 * End:
319 * vim600: noet sw=4 ts=4 fdm=marker
320 * vim<600: noet sw=4 ts=4
321 */