flush WIP
[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 PTR_SET
45 # define PTR_SET(STR, SET) \
46 { \
47 PTR_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);
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() times_=1
69 #define PHP_HTTP_DUFF(c, a) do { \
70 size_t count_ = (c); \
71 size_t times_ = (count_ + 7) >> 3; \
72 switch (count_ & 7){ \
73 case 0: do { \
74 a; \
75 case 7: \
76 a; \
77 case 6: \
78 a; \
79 case 5: \
80 a; \
81 case 4: \
82 a; \
83 case 3: \
84 a; \
85 case 2: \
86 a; \
87 case 1: \
88 a; \
89 } while (--times_ > 0); \
90 } \
91 } while (0)
92
93 static inline const char *php_http_locate_str(register const char *h, size_t h_len, const char *n, size_t n_len)
94 {
95 if (!n_len || !h_len || h_len < n_len) {
96 return NULL;
97 }
98
99 PHP_HTTP_DUFF(h_len - n_len + 1,
100 if (*h == *n && !strncmp(h + 1, n + 1, n_len - 1)) {
101 return h;
102 }
103 ++h;
104 );
105
106 return NULL;
107 }
108
109 static inline const char *php_http_locate_eol(const char *line, int *eol_len)
110 {
111 const char *eol = strpbrk(line, "\r\n");
112
113 if (eol_len) {
114 *eol_len = eol ? ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1) : 0;
115 }
116 return eol;
117 }
118
119 static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, int *eol_len)
120 {
121 register const char *eol = bin;
122
123 if (len > 0) {
124 PHP_HTTP_DUFF(len,
125 if (*eol == '\r' || *eol == '\n') {
126 if (eol_len) {
127 *eol_len = ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1);
128 }
129 return eol;
130 }
131 ++eol;
132 );
133 }
134 return NULL;
135 }
136
137 /* ZEND */
138
139 static inline void *PHP_HTTP_OBJ(zend_object *zo, zval *zv)
140 {
141 if (!zo) {
142 zo = Z_OBJ_P(zv);
143 }
144 return (char *) zo - zo->handlers->offset;
145 }
146
147 static inline zend_string *php_http_cs2zs(char *s, size_t l)
148 {
149 zend_string *str = erealloc(s, sizeof(*str) + l);
150
151 memmove(str->val, str, l);
152 str->val[l] = 0;
153 str->len = l;
154 str->h = 0;
155
156 GC_REFCOUNT(str) = 1;
157 GC_TYPE_INFO(str) = IS_STRING;
158
159 return str;
160 }
161
162 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)
163 {
164 zend_ini_entry *ini_entry;
165
166 if (ini_entry == zend_hash_str_find_ptr(EG(ini_directives), name_str, name_len)) {
167 if (orig && ini_entry->modified) {
168 *val_str = ini_entry->orig_value->val;
169 *val_len = ini_entry->orig_value->len;
170 } else {
171 *val_str = ini_entry->value->val;
172 *val_len = ini_entry->value->len;
173 }
174 return SUCCESS;
175 }
176 return FAILURE;
177 }
178
179 #define RETVAL_STR_COPY(zs) ZVAL_STR_COPY(return_value, zs)
180 #define RETURN_STR_COPY(zs) do { \
181 ZVAL_STR_COPY(return_value, zs); \
182 return; \
183 }
184
185 /* return object(values) */
186 #define ZVAL_OBJECT(z, o, addref) \
187 ZVAL_OBJ(z, o); \
188 if (addref) { \
189 Z_ADDREF_P(z); \
190 }
191 #define RETVAL_OBJECT(o, addref) \
192 ZVAL_OBJECT(return_value, o, addref)
193 #define RETURN_OBJECT(o, addref) \
194 RETVAL_OBJECT(o, addref); \
195 return
196
197 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
198
199 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
200 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
201 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
202 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
203
204 /* ARRAYS */
205
206 PHP_HTTP_API unsigned php_http_array_list(HashTable *ht, unsigned argc, ...);
207
208 typedef struct php_http_arrkey {
209 zend_ulong h;
210 zend_string *key;
211 unsigned allocated:1;
212 unsigned stringified:1;
213 } php_http_arrkey_t;
214
215 static inline void *php_http_arrkey_stringify(php_http_arrkey_t *arrkey, zend_hash_key *key)
216 {
217 if (arrkey) {
218 arrkey->allocated = 0;
219 } else {
220 arrkey = emalloc(sizeof(*arrkey));
221 arrkey->allocated = 1;
222 }
223
224 if (key) {
225 memcpy(&arrkey, key, sizeof(*key));
226 }
227 if ((arrkey->stringified = !arrkey->key)) {
228 arrkey->key = zend_long_to_str(arrkey->h);
229 }
230 return arrkey;
231 }
232
233 static inline void php_http_arrkey_dtor(php_http_arrkey_t *arrkey)
234 {
235 if (arrkey->stringified) {
236 zend_string_release(arrkey->key);
237 }
238 if (arrkey->allocated) {
239 efree(arrkey);
240 }
241 }
242
243 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref)
244 #define array_copy_strings(src, dst) zend_hash_copy(dst, src, php_http_array_copy_strings)
245 #define ARRAY_JOIN_STRONLY 0x01
246 #define ARRAY_JOIN_PRETTIFY 0x02
247 #define ARRAY_JOIN_STRINGIFY 0x04
248 #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)
249
250 void php_http_array_copy_strings(zval *zp);
251 int php_http_array_apply_append_func(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
252 int php_http_array_apply_merge_func(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
253
254 /* PASS CALLBACK */
255
256 typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len);
257 typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str);
258 typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...);
259
260 typedef struct php_http_pass_fcall_arg {
261 zval fcz;
262 zend_fcall_info fci;
263 zend_fcall_info_cache fcc;
264 } php_http_pass_fcall_arg_t;
265
266 PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
267
268 #endif
269
270 /*
271 * Local variables:
272 * tab-width: 4
273 * c-basic-offset: 4
274 * End:
275 * vim600: noet sw=4 ts=4 fdm=marker
276 * vim<600: noet sw=4 ts=4
277 */