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