names changing )
[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-2013, 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 #define PHP_HTTP_CHECK_CONTENT_TYPE(ct, action) \
45 if (!strchr((ct), '/')) { \
46 php_http_error(HE_WARNING, PHP_HTTP_E_INVALID_PARAM, \
47 "Content type \"%s\" does not seem to contain a primary and a secondary part", (ct)); \
48 action; \
49 }
50
51
52 #ifndef STR_SET
53 # define STR_SET(STR, SET) \
54 { \
55 STR_FREE(STR); \
56 STR = SET; \
57 }
58 #endif
59
60 #define STR_PTR(s) (s?s:"")
61
62 #define lenof(S) (sizeof(S) - 1)
63
64 #define PHP_HTTP_MATCH_LOOSE 0
65 #define PHP_HTTP_MATCH_CASE 0x01
66 #define PHP_HTTP_MATCH_WORD 0x10
67 #define PHP_HTTP_MATCH_FULL 0x20
68 #define PHP_HTTP_MATCH_STRICT (PHP_HTTP_MATCH_CASE|PHP_HTTP_MATCH_FULL)
69
70 int php_http_match(const char *haystack, const char *needle, int flags);
71 char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen);
72 size_t php_http_boundary(char *buf, size_t len TSRMLS_DC);
73 int php_http_select_str(const char *cmp, int argc, ...);
74
75 static inline const char *php_http_locate_str(const char *h, size_t h_len, const char *n, size_t n_len)
76 {
77 const char *p, *e;
78
79 if (n_len && h_len) {
80 e = h + h_len;
81 do {
82 if (*h == *n) {
83 for (p = n; *p == h[p-n]; ++p) {
84 if (p == n+n_len-1) {
85 return h;
86 }
87 }
88 }
89 } while (h++ != e);
90 }
91
92 return NULL;
93 }
94
95 static inline const char *php_http_locate_eol(const char *line, int *eol_len)
96 {
97 const char *eol = strpbrk(line, "\r\n");
98
99 if (eol_len) {
100 *eol_len = eol ? ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1) : 0;
101 }
102 return eol;
103 }
104
105 static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, int *eol_len)
106 {
107 const char *eol;
108
109 for (eol = bin; eol - bin < len; ++eol) {
110 if (*eol == '\r' || *eol == '\n') {
111 if (eol_len) {
112 *eol_len = ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1);
113 }
114 return eol;
115 }
116 }
117
118 return NULL;
119 }
120
121 /* ZEND */
122
123 #if PHP_VERSION_ID < 50400
124 # 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*))
125 # define PHP_HTTP_ZEND_LITERAL_DC
126 # define PHP_HTTP_ZEND_LITERAL_CC
127 # define PHP_HTTP_ZEND_LITERAL_CCN
128 # define ZVAL_COPY_VALUE(zv, arr) do { \
129 (zv)->value = (arr)->value; \
130 Z_TYPE_P(zv) = Z_TYPE_P(arr); \
131 } while (0)
132 #else
133 # define PHP_HTTP_ZEND_LITERAL_DC , const zend_literal *literal_key
134 # define PHP_HTTP_ZEND_LITERAL_CC , (literal_key)
135 # define PHP_HTTP_ZEND_LITERAL_CCN , NULL
136 #endif
137
138 #define INIT_PZVAL_ARRAY(zv, ht) \
139 { \
140 INIT_PZVAL((zv)); \
141 Z_TYPE_P(zv) = IS_ARRAY; \
142 Z_ARRVAL_P(zv) = (ht); \
143 }
144
145 static inline zval *php_http_ztyp(int type, zval *z)
146 {
147 SEPARATE_ARG_IF_REF(z);
148 if (Z_TYPE_P(z) != type) {
149 switch (type) {
150 case IS_NULL: convert_to_null_ex(&z); break;
151 case IS_BOOL: convert_to_boolean_ex(&z); break;
152 case IS_LONG: convert_to_long_ex(&z); break;
153 case IS_DOUBLE: convert_to_double_ex(&z); break;
154 case IS_STRING: convert_to_string_ex(&z); break;
155 case IS_ARRAY: convert_to_array_ex(&z); break;
156 case IS_OBJECT: convert_to_object_ex(&z); break;
157 }
158 }
159 return z;
160 }
161
162 static inline zval *php_http_zsep(zend_bool add_ref, int type, zval *z)
163 {
164 if (add_ref) {
165 Z_ADDREF_P(z);
166 }
167 if (Z_TYPE_P(z) != type) {
168 switch (type) {
169 case IS_NULL: convert_to_null_ex(&z); break;
170 case IS_BOOL: convert_to_boolean_ex(&z); break;
171 case IS_LONG: convert_to_long_ex(&z); break;
172 case IS_DOUBLE: convert_to_double_ex(&z); break;
173 case IS_STRING: convert_to_string_ex(&z); break;
174 case IS_ARRAY: convert_to_array_ex(&z); break;
175 case IS_OBJECT: convert_to_object_ex(&z); break;
176 }
177 } else {
178 SEPARATE_ZVAL_IF_NOT_REF(&z);
179 }
180 return z;
181 }
182
183 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)
184 {
185 zend_ini_entry *ini_entry;
186
187 if (SUCCESS == zend_hash_find(EG(ini_directives), name_str, name_len + 1, (void *) &ini_entry)) {
188 if (orig && ini_entry->modified) {
189 *value_str = ini_entry->orig_value;
190 *value_len = (size_t) ini_entry->orig_value_length;
191 } else {
192 *value_str = ini_entry->value;
193 *value_len = (size_t) ini_entry->value_length;
194 }
195 return SUCCESS;
196 }
197 return FAILURE;
198 }
199
200 STATUS php_http_method_call(zval *object, const char *method_str, size_t method_len, int argc, zval **argv[], zval **retval_ptr TSRMLS_DC);
201
202 /* return object(values) */
203 #define RETVAL_OBJECT(o, addref) \
204 RETVAL_OBJVAL((o)->value.obj, addref)
205 #define RETURN_OBJECT(o, addref) \
206 RETVAL_OBJECT(o, addref); \
207 return
208 #define RETVAL_OBJVAL(ov, addref) \
209 ZVAL_OBJVAL(return_value, ov, addref)
210 #define RETURN_OBJVAL(ov, addref) \
211 RETVAL_OBJVAL(ov, addref); \
212 return
213 #define ZVAL_OBJVAL(zv, ov, addref) \
214 (zv)->type = IS_OBJECT; \
215 (zv)->value.obj = (ov);\
216 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
217 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
218 }
219
220 #define Z_OBJ_DELREF(z) \
221 if (Z_OBJ_HT(z)->del_ref) { \
222 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
223 }
224 #define Z_OBJ_ADDREF(z) \
225 if (Z_OBJ_HT(z)->add_ref) { \
226 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
227 }
228 #define Z_OBJ_DELREF_P(z) \
229 if (Z_OBJ_HT_P(z)->del_ref) { \
230 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
231 }
232 #define Z_OBJ_ADDREF_P(z) \
233 if (Z_OBJ_HT_P(z)->add_ref) { \
234 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
235 }
236 #define Z_OBJ_DELREF_PP(z) \
237 if (Z_OBJ_HT_PP(z)->del_ref) { \
238 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
239 }
240 #define Z_OBJ_ADDREF_PP(z) \
241 if (Z_OBJ_HT_PP(z)->add_ref) { \
242 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
243 }
244
245 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
246
247 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
248 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
249 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
250 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
251
252 /* ARRAYS */
253 PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...);
254
255 typedef struct php_http_array_hashkey {
256 char *str;
257 uint len;
258 ulong num;
259 uint dup:1;
260 uint type:31;
261 } php_http_array_hashkey_t;
262 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
263
264 static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t *key)
265 {
266 if (key->type != HASH_KEY_IS_STRING) {
267 key->len = spprintf(&key->str, 0, "%lu", key->num) + 1;
268 }
269 }
270
271 static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t *key)
272 {
273 if (key->type != HASH_KEY_IS_STRING || key->dup) {
274 STR_FREE(key->str);
275 }
276 }
277
278 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
279 #define FOREACH_HASH_VAL(pos, hash, val) \
280 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
281 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
282 zend_hash_move_forward_ex(hash, &pos))
283
284 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
285 #define FOREACH_HASH_KEY(pos, hash, _key) \
286 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
287 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
288 zend_hash_move_forward_ex(hash, &pos)) \
289
290 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
291 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
292 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
293 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
294 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
295 zend_hash_move_forward_ex(hash, &pos))
296
297 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
298 #define ARRAY_JOIN_STRONLY 1
299 #define ARRAY_JOIN_PRETTIFY 2
300 #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)
301
302 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
303 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
304
305 /* PASS CALLBACK */
306
307 typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len);
308 typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str);
309 typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...);
310
311 typedef struct php_http_pass_fcall_arg {
312 zval *fcz;
313 zend_fcall_info fci;
314 zend_fcall_info_cache fcc;
315 #ifdef ZTS
316 void ***ts;
317 #endif
318 } php_http_pass_fcall_arg_t;
319
320 PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
321
322 /* ERROR */
323
324 extern void php_http_error(long type TSRMLS_DC, long code, const char *format, ...);
325
326 #define with_error_handling(eh, ec) \
327 { \
328 zend_error_handling __eh; \
329 zend_replace_error_handling((eh), (ec), &__eh TSRMLS_CC);
330
331 #define end_error_handling() \
332 zend_restore_error_handling(&__eh TSRMLS_CC); \
333 }
334
335 #ifndef E_THROW
336 # define E_THROW -1
337 #endif
338 #define HE_THROW E_THROW TSRMLS_CC
339 #define HE_NOTICE E_NOTICE TSRMLS_CC
340 #define HE_WARNING E_WARNING TSRMLS_CC
341 #define HE_ERROR E_ERROR TSRMLS_CC
342
343 typedef enum php_http_error {
344 PHP_HTTP_E_UNKNOWN = 0,
345 PHP_HTTP_E_RUNTIME,
346 PHP_HTTP_E_INVALID_PARAM,
347 PHP_HTTP_E_HEADER,
348 PHP_HTTP_E_MALFORMED_HEADERS,
349 PHP_HTTP_E_REQUEST_METHOD,
350 PHP_HTTP_E_MESSAGE,
351 PHP_HTTP_E_MESSAGE_TYPE,
352 PHP_HTTP_E_MESSAGE_BODY,
353 PHP_HTTP_E_ENCODING,
354 PHP_HTTP_E_CLIENT,
355 PHP_HTTP_E_CLIENT_POOL,
356 PHP_HTTP_E_CLIENT_DATASHARE,
357 PHP_HTTP_E_REQUEST_FACTORY,
358 PHP_HTTP_E_SOCKET,
359 PHP_HTTP_E_RESPONSE,
360 PHP_HTTP_E_URL,
361 PHP_HTTP_E_QUERYSTRING,
362 PHP_HTTP_E_COOKIE,
363 } php_http_error_t;
364
365 #endif
366
367 /*
368 * Local variables:
369 * tab-width: 4
370 * c-basic-offset: 4
371 * End:
372 * vim600: noet sw=4 ts=4 fdm=marker
373 * vim<600: noet sw=4 ts=4
374 */