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