fix macro
[m6w6/ext-http] / src / 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);
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_DEBUG
143 # undef HASH_OF
144 # define HASH_OF(p) ((HashTable*)(Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties((p)) : NULL))))
145 #endif
146
147 #ifndef GC_SET_REFCOUNT
148 # define GC_SET_REFCOUNT(gc, rc) GC_REFCOUNT(gc) = rc
149 #endif
150 #ifndef GC_ADDREF
151 # define GC_ADDREF(gc) ++GC_REFCOUNT(gc)
152 #endif
153 #ifndef GC_DELREF
154 # define GC_DELREF(gc) --GC_REFCOUNT(gc)
155 #endif
156
157 #ifdef ZEND_HASH_GET_APPLY_COUNT
158 # define HT_IS_RECURSIVE(ht) (ZEND_HASH_GET_APPLY_COUNT(ht) > 0)
159 #else
160 # define HT_IS_RECURSIVE(ht) GC_IS_RECURSIVE(ht)
161 #endif
162 #ifdef ZEND_HASH_INC_APPLY_COUNT
163 # define HT_PROTECT_RECURSION(ht) ZEND_HASH_INC_APPLY_COUNT(ht)
164 #else
165 # define HT_PROTECT_RECURSION(ht) GC_PROTECT_RECURSION(ht)
166 #endif
167 #ifdef ZEND_HASH_DEC_APPLY_COUNT
168 # define HT_UNPROTECT_RECURSION(ht) ZEND_HASH_DEC_APPLY_COUNT(ht)
169 #else
170 # define HT_UNPROTECT_RECURSION(ht) GC_UNPROTECT_RECURSION(ht)
171 #endif
172
173 static inline void *PHP_HTTP_OBJ(zend_object *zo, zval *zv)
174 {
175 if (!zo) {
176 zo = Z_OBJ_P(zv);
177 }
178 return (char *) zo - zo->handlers->offset;
179 }
180
181 static inline zend_string *php_http_cs2zs(char *s, size_t l)
182 {
183 zend_string *str = erealloc(s, sizeof(*str) + l);
184
185 memmove(str->val, str, l);
186 str->val[l] = 0;
187 str->len = l;
188 str->h = 0;
189
190 GC_SET_REFCOUNT(str, 1);
191 GC_TYPE_INFO(str) = IS_STRING;
192
193 return str;
194 }
195
196 static inline ZEND_RESULT_CODE php_http_ini_entry(const char *name_str, size_t name_len, const char **val_str, size_t *val_len, zend_bool orig)
197 {
198 zend_ini_entry *ini_entry;
199
200 if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives), name_str, name_len))) {
201 if (orig && ini_entry->modified) {
202 *val_str = ini_entry->orig_value->val;
203 *val_len = ini_entry->orig_value->len;
204 } else {
205 *val_str = ini_entry->value->val;
206 *val_len = ini_entry->value->len;
207 }
208 return SUCCESS;
209 }
210 return FAILURE;
211 }
212
213 #define Z_ISUSER(zv) (Z_TYPE(zv) <= 10)
214 #define Z_ISUSER_P(zvp) Z_ISUSER(*(zvp))
215
216 /* return object(values) */
217 #define ZVAL_OBJECT(z, o, addref) \
218 ZVAL_OBJ(z, o); \
219 if (addref) { \
220 Z_ADDREF_P(z); \
221 }
222 #define RETVAL_OBJECT(o, addref) \
223 ZVAL_OBJECT(return_value, o, addref)
224 #define RETURN_OBJECT(o, addref) \
225 RETVAL_OBJECT(o, addref); \
226 return
227
228 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
229
230 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
231 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
232 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
233 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
234
235 /* ARRAYS */
236
237 PHP_HTTP_API unsigned php_http_array_list(HashTable *ht, unsigned argc, ...);
238
239 typedef struct php_http_arrkey {
240 zend_ulong h;
241 zend_string *key;
242 unsigned allocated:1;
243 unsigned stringified:1;
244 } php_http_arrkey_t;
245
246 static inline void *php_http_arrkey_stringify(php_http_arrkey_t *arrkey, zend_hash_key *key)
247 {
248 if (arrkey) {
249 arrkey->allocated = 0;
250 } else {
251 arrkey = emalloc(sizeof(*arrkey));
252 arrkey->allocated = 1;
253 }
254
255 if (key) {
256 memcpy(arrkey, key, sizeof(*key));
257 }
258 if ((arrkey->stringified = !arrkey->key)) {
259 arrkey->key = zend_long_to_str(arrkey->h);
260 }
261 return arrkey;
262 }
263
264 static inline void php_http_arrkey_dtor(php_http_arrkey_t *arrkey)
265 {
266 if (arrkey->stringified) {
267 zend_string_release(arrkey->key);
268 }
269 if (arrkey->allocated) {
270 efree(arrkey);
271 }
272 }
273
274 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref)
275 #define array_copy_strings(src, dst) zend_hash_copy(dst, src, php_http_array_copy_strings)
276 #define ARRAY_JOIN_STRONLY 0x01
277 #define ARRAY_JOIN_PRETTIFY 0x02
278 #define ARRAY_JOIN_STRINGIFY 0x04
279 #define array_join(src, dst, append, flags) zend_hash_apply_with_arguments(src, (append)?php_http_array_apply_append_func:php_http_array_apply_merge_func, 2, dst, (int)flags)
280
281 void php_http_array_copy_strings(zval *zp);
282 int php_http_array_apply_append_func(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
283 int php_http_array_apply_merge_func(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
284
285 /* PASS CALLBACK */
286
287 typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len);
288 typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str);
289 typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...);
290
291 typedef struct php_http_pass_fcall_arg {
292 zval fcz;
293 zend_fcall_info fci;
294 zend_fcall_info_cache fcc;
295 } php_http_pass_fcall_arg_t;
296
297 PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
298
299 #endif
300
301 /*
302 * Local variables:
303 * tab-width: 4
304 * c-basic-offset: 4
305 * End:
306 * vim600: noet sw=4 ts=4 fdm=marker
307 * vim<600: noet sw=4 ts=4
308 */