cebf78da3d0a61b6025d3ed016074628f4296589
[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-2011, 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 /* default cache control */
25 #define PHP_HTTP_DEFAULT_CACHECONTROL "private, must-revalidate, max-age=0"
26
27 /* max URL length */
28 #define PHP_HTTP_URL_MAXLEN 4096
29
30 /* max request method length */
31 #define PHP_HTTP_REQUEST_METHOD_MAXLEN 31
32
33 /* def URL arg separator */
34 #define PHP_HTTP_URL_ARGSEP "&"
35
36 /* send buffer size */
37 #define PHP_HTTP_SENDBUF_SIZE 40960
38
39 /* CURL buffer size */
40 #define PHP_HTTP_CURLBUF_SIZE 16384
41
42 /* SLEEP */
43
44 #define PHP_HTTP_DIFFSEC (0.001)
45 #define PHP_HTTP_MLLISEC (1000)
46 #define PHP_HTTP_MCROSEC (1000 * 1000)
47 #define PHP_HTTP_NANOSEC (1000 * 1000 * 1000)
48 #define PHP_HTTP_MSEC(s) ((long)(s * PHP_HTTP_MLLISEC))
49 #define PHP_HTTP_USEC(s) ((long)(s * PHP_HTTP_MCROSEC))
50 #define PHP_HTTP_NSEC(s) ((long)(s * PHP_HTTP_NANOSEC))
51
52 PHP_HTTP_API void php_http_sleep(double s);
53
54 /* STRING UTILITIES */
55
56 #define PHP_HTTP_CHECK_CONTENT_TYPE(ct, action) \
57 if (!strchr((ct), '/')) { \
58 php_http_error(HE_WARNING, PHP_HTTP_E_INVALID_PARAM, \
59 "Content type \"%s\" does not seem to contain a primary and a secondary part", (ct)); \
60 action; \
61 }
62
63
64 #ifndef STR_SET
65 # define STR_SET(STR, SET) \
66 { \
67 STR_FREE(STR); \
68 STR = SET; \
69 }
70 #endif
71
72 #define STR_PTR(s) (s?s:"")
73
74 #define lenof(S) (sizeof(S) - 1)
75
76 #define PHP_HTTP_MATCH_LOOSE 0
77 #define PHP_HTTP_MATCH_CASE 0x01
78 #define PHP_HTTP_MATCH_WORD 0x10
79 #define PHP_HTTP_MATCH_FULL 0x20
80 #define PHP_HTTP_MATCH_STRICT (PHP_HTTP_MATCH_CASE|PHP_HTTP_MATCH_FULL)
81
82 extern int php_http_match(const char *haystack, const char *needle, int flags);
83
84 extern char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen);
85 extern size_t php_http_boundary(char *buf, size_t len TSRMLS_DC);
86
87 static inline const char *php_http_locate_str(const char *h, size_t h_len, const char *n, size_t n_len)
88 {
89 const char *p, *e;
90
91 if (n_len && h_len) {
92 e = h + h_len;
93 do {
94 if (*h == *n) {
95 for (p = n; *p == h[p-n]; ++p) {
96 if (p == n+n_len-1) {
97 return h;
98 }
99 }
100 }
101 } while (h++ != e);
102 }
103
104 return NULL;
105 }
106
107 static inline const char *php_http_locate_body(const char *message)
108 {
109 const char *body = NULL, *msg = message;
110
111 while (*msg) {
112 if (*msg == '\n') {
113 if (*(msg+1) == '\n') {
114 body = msg + 2;
115 break;
116 } else if (*(msg+1) == '\r' && *(msg+2) == '\n') {
117 body = msg + 3;
118 break;
119 }
120 }
121 ++msg;
122 }
123 return body;
124 }
125
126 static inline const char *php_http_locate_eol(const char *line, int *eol_len)
127 {
128 const char *eol = strpbrk(line, "\r\n");
129
130 if (eol_len) {
131 *eol_len = eol ? ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1) : 0;
132 }
133 return eol;
134 }
135
136 static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, int *eol_len)
137 {
138 const char *eol;
139
140 for (eol = bin; eol - bin < len; ++eol) {
141 if (*eol == '\r' || *eol == '\n') {
142 *eol_len = eol ? ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1) : 0;
143 return eol;
144 }
145 }
146
147 return NULL;
148 }
149
150 /* ZEND */
151
152 #define INIT_PZVAL_ARRAY(zv, ht) \
153 { \
154 INIT_PZVAL((zv)); \
155 Z_TYPE_P(zv) = IS_ARRAY; \
156 Z_ARRVAL_P(zv) = (ht); \
157 }
158
159 static inline zval *php_http_ztyp(int type, zval *z)
160 {
161 SEPARATE_ARG_IF_REF(z);
162 if (Z_TYPE_P(z) != type) {
163 switch (type) {
164 case IS_NULL: convert_to_null_ex(&z); break;
165 case IS_BOOL: convert_to_boolean_ex(&z); break;
166 case IS_LONG: convert_to_long_ex(&z); break;
167 case IS_DOUBLE: convert_to_double_ex(&z); break;
168 case IS_STRING: convert_to_string_ex(&z); break;
169 case IS_ARRAY: convert_to_array_ex(&z); break;
170 case IS_OBJECT: convert_to_object_ex(&z); break;
171 }
172 }
173 return z;
174 }
175
176 static inline zval *php_http_zsep(zend_bool add_ref, int type, zval *z) {
177 if (add_ref) {
178 Z_ADDREF_P(z);
179 }
180 if (Z_TYPE_P(z) != type) {
181 switch (type) {
182 case IS_NULL: convert_to_null_ex(&z); break;
183 case IS_BOOL: convert_to_boolean_ex(&z); break;
184 case IS_LONG: convert_to_long_ex(&z); break;
185 case IS_DOUBLE: convert_to_double_ex(&z); break;
186 case IS_STRING: convert_to_string_ex(&z); break;
187 case IS_ARRAY: convert_to_array_ex(&z); break;
188 case IS_OBJECT: convert_to_object_ex(&z); break;
189 }
190 } else {
191 SEPARATE_ZVAL_IF_NOT_REF(&z);
192 }
193 return z;
194 }
195
196
197 /* return bool (v == SUCCESS) */
198 #define RETVAL_SUCCESS(v) RETVAL_BOOL(SUCCESS == (v))
199 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
200 /* return object(values) */
201 #define RETVAL_OBJECT(o, addref) \
202 RETVAL_OBJVAL((o)->value.obj, addref)
203 #define RETURN_OBJECT(o, addref) \
204 RETVAL_OBJECT(o, addref); \
205 return
206 #define RETVAL_OBJVAL(ov, addref) \
207 ZVAL_OBJVAL(return_value, ov, addref)
208 #define RETURN_OBJVAL(ov, addref) \
209 RETVAL_OBJVAL(ov, addref); \
210 return
211 #define ZVAL_OBJVAL(zv, ov, addref) \
212 (zv)->type = IS_OBJECT; \
213 (zv)->value.obj = (ov);\
214 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
215 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
216 }
217 /* return property */
218 #define RETVAL_PROP(CE, n) RETVAL_PROP_EX(CE, getThis(), n)
219 #define RETURN_PROP(CE, n) RETURN_PROP_EX(CE, getThis(), n)
220 #define RETVAL_PROP_EX(CE, this, n) \
221 { \
222 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
223 RETVAL_ZVAL(__prop, 1, 0); \
224 }
225 #define RETURN_PROP_EX(CE, this, n) \
226 { \
227 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
228 RETURN_ZVAL(__prop, 1, 0); \
229 }
230 #define RETVAL_SPROP(CE, n) \
231 { \
232 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
233 RETVAL_ZVAL(__prop, 1, 0); \
234 }
235 #define RETURN_SPROP(CE, n) \
236 { \
237 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
238 RETURN_ZVAL(__prop, 1, 0); \
239 }
240
241 #define Z_OBJ_DELREF(z) \
242 if (Z_OBJ_HT(z)->del_ref) { \
243 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
244 }
245 #define Z_OBJ_ADDREF(z) \
246 if (Z_OBJ_HT(z)->add_ref) { \
247 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
248 }
249 #define Z_OBJ_DELREF_P(z) \
250 if (Z_OBJ_HT_P(z)->del_ref) { \
251 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
252 }
253 #define Z_OBJ_ADDREF_P(z) \
254 if (Z_OBJ_HT_P(z)->add_ref) { \
255 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
256 }
257 #define Z_OBJ_DELREF_PP(z) \
258 if (Z_OBJ_HT_PP(z)->del_ref) { \
259 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
260 }
261 #define Z_OBJ_ADDREF_PP(z) \
262 if (Z_OBJ_HT_PP(z)->add_ref) { \
263 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
264 }
265
266 #define PHP_HTTP_BEGIN_ARGS_EX(class, method, ret_ref, req_args) ZEND_BEGIN_ARG_INFO_EX(args_for_ ##class## _ ##method , 0, ret_ref, req_args)
267 #define PHP_HTTP_BEGIN_ARGS_AR(class, method, ret_ref, req_args) ZEND_BEGIN_ARG_INFO_EX(args_for_ ##class## _ ##method , 1, ret_ref, req_args)
268 #define PHP_HTTP_END_ARGS }
269 #define PHP_HTTP_EMPTY_ARGS_EX(class, method, ret_ref) PHP_HTTP_BEGIN_ARGS_EX(class, method, ret_ref, 0) PHP_HTTP_END_ARGS
270 #define PHP_HTTP_ARGS(class, method) args_for_ ##class## _ ##method
271 #define PHP_HTTP_ARG_VAL(name, pass_ref) ZEND_ARG_INFO(pass_ref, name)
272 #define PHP_HTTP_ARG_OBJ(class, name, allow_null) ZEND_ARG_OBJ_INFO(0, name, class, allow_null)
273 #define PHP_HTTP_ARG_ARR(name, allow_null, pass_ref) ZEND_ARG_ARRAY_INFO(pass_ref, name, allow_null)
274
275 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
276
277 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
278 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
279 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
280 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
281
282
283 #define PHP_HTTP_INI_ENTRY(entry, default, scope, updater, global) \
284 STD_PHP_INI_ENTRY(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals)
285 #define PHP_HTTP_INI_ENTRY_EX(entry, default, scope, updater, displayer, global) \
286 STD_PHP_INI_ENTRY_EX(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals, displayer)
287
288 #define PHP_HTTP_REGISTER_CLASS(ns, classname, name, parent, flags) \
289 { \
290 zend_class_entry ce; \
291 memset(&ce, 0, sizeof(zend_class_entry)); \
292 INIT_NS_CLASS_ENTRY(ce, #ns, #classname, php_ ##name## _method_entry); \
293 php_ ##name## _class_entry = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
294 php_ ##name## _class_entry->ce_flags |= flags; \
295 }
296
297 #define PHP_HTTP_REGISTER_INTERFACE(ns, ifacename, name, flags) \
298 { \
299 zend_class_entry ce; \
300 memset(&ce, 0, sizeof(zend_class_entry)); \
301 INIT_NS_CLASS_ENTRY(ce, #ns, #ifacename, php_ ##name## _method_entry); \
302 php_ ##name## _class_entry = zend_register_internal_interface(&ce TSRMLS_CC); \
303 php_ ##name## _class_entry->ce_flags |= flags; \
304 }
305
306 #define PHP_HTTP_REGISTER_EXCEPTION(classname, cename, parent) \
307 { \
308 zend_class_entry ce; \
309 memset(&ce, 0, sizeof(zend_class_entry)); \
310 INIT_NS_CLASS_ENTRY(ce, "http", #classname, NULL); \
311 ce.create_object = NULL; \
312 cename = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
313 }
314
315 #define ACC_PROP_PRIVATE(ce, flags) ((flags & ZEND_ACC_PRIVATE) && (EG(scope) && ce == EG(scope))
316 #define ACC_PROP_PROTECTED(ce, flags) ((flags & ZEND_ACC_PROTECTED) && (zend_check_protected(ce, EG(scope))))
317 #define ACC_PROP_PUBLIC(flags) (flags & ZEND_ACC_PUBLIC)
318 #define ACC_PROP(ce, flags) (ACC_PROP_PUBLIC(flags) || ACC_PROP_PRIVATE(ce, flags) || ACC_PROP_PROTECTED(ce, flags))
319
320 #ifdef PHP_HTTP_HAVE_CURL
321 # define PHP_HTTP_DECLARE_ARG_PASS_INFO() \
322 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_2, 0) \
323 ZEND_ARG_PASS_INFO(0) \
324 ZEND_ARG_PASS_INFO(1) \
325 ZEND_END_ARG_INFO(); \
326 \
327 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_3, 0) \
328 ZEND_ARG_PASS_INFO(0) \
329 ZEND_ARG_PASS_INFO(0) \
330 ZEND_ARG_PASS_INFO(1) \
331 ZEND_END_ARG_INFO(); \
332 \
333 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_4, 0) \
334 ZEND_ARG_PASS_INFO(0) \
335 ZEND_ARG_PASS_INFO(0) \
336 ZEND_ARG_PASS_INFO(0) \
337 ZEND_ARG_PASS_INFO(1) \
338 ZEND_END_ARG_INFO(); \
339 \
340 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_5, 0) \
341 ZEND_ARG_PASS_INFO(0) \
342 ZEND_ARG_PASS_INFO(0) \
343 ZEND_ARG_PASS_INFO(0) \
344 ZEND_ARG_PASS_INFO(0) \
345 ZEND_ARG_PASS_INFO(1) \
346 ZEND_END_ARG_INFO();
347
348 #else
349 # define PHP_HTTP_DECLARE_ARG_PASS_INFO() \
350 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_2, 0) \
351 ZEND_ARG_PASS_INFO(0) \
352 ZEND_ARG_PASS_INFO(1) \
353 ZEND_END_ARG_INFO(); \
354 \
355 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_3, 0) \
356 ZEND_ARG_PASS_INFO(0) \
357 ZEND_ARG_PASS_INFO(0) \
358 ZEND_ARG_PASS_INFO(1) \
359 ZEND_END_ARG_INFO(); \
360 \
361 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_4, 0) \
362 ZEND_ARG_PASS_INFO(0) \
363 ZEND_ARG_PASS_INFO(0) \
364 ZEND_ARG_PASS_INFO(0) \
365 ZEND_ARG_PASS_INFO(1) \
366 ZEND_END_ARG_INFO();
367 #endif /* PHP_HTTP_HAVE_CURL */
368
369 /* ARRAYS */
370
371 typedef struct php_http_array_hashkey {
372 char *str;
373 uint len;
374 ulong num;
375 uint dup:1;
376 uint type:31;
377 } php_http_array_hashkey_t;
378 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
379
380 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
381 #define FOREACH_HASH_VAL(pos, hash, val) \
382 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
383 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
384 zend_hash_move_forward_ex(hash, &pos))
385
386 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
387 #define FOREACH_HASH_KEY(pos, hash, _key) \
388 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
389 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
390 zend_hash_move_forward_ex(hash, &pos)) \
391
392 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
393 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
394 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
395 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
396 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
397 zend_hash_move_forward_ex(hash, &pos))
398
399 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
400 #define ARRAY_JOIN_STRONLY 1
401 #define ARRAY_JOIN_PRETTIFY 2
402 #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)
403
404 extern int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
405 extern int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
406
407 /* PASS CALLBACK */
408
409 typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len);
410 typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str);
411
412 typedef struct php_http_pass_callback_arg {
413 size_t (*cb_zts)(void *cb_arg, const char *str, size_t len TSRMLS_DC);
414 void *cb_arg;
415 } php_http_pass_callback_arg_t;
416
417 PHP_HTTP_API size_t php_http_pass_wrapper(php_http_pass_callback_arg_t *cb_arg, const char *str, size_t len);
418
419 /* ERROR */
420
421 extern void php_http_error(long type TSRMLS_DC, long code, const char *format, ...);
422
423 #define with_error_handling(eh, ec) \
424 { \
425 zend_error_handling __eh; \
426 zend_replace_error_handling((eh), (ec), &__eh TSRMLS_CC);
427
428 #define end_error_handling() \
429 zend_restore_error_handling(&__eh TSRMLS_CC); \
430 }
431
432 #ifndef E_THROW
433 # define E_THROW -1
434 #endif
435 #define HE_THROW E_THROW TSRMLS_CC
436 #define HE_NOTICE E_NOTICE TSRMLS_CC
437 #define HE_WARNING E_WARNING TSRMLS_CC
438 #define HE_ERROR E_ERROR TSRMLS_CC
439
440 typedef enum php_http_error {
441 PHP_HTTP_E_UNKNOWN = 0,
442 PHP_HTTP_E_RUNTIME,
443 PHP_HTTP_E_INVALID_PARAM,
444 PHP_HTTP_E_HEADER,
445 PHP_HTTP_E_MALFORMED_HEADERS,
446 PHP_HTTP_E_REQUEST_METHOD,
447 PHP_HTTP_E_MESSAGE,
448 PHP_HTTP_E_MESSAGE_TYPE,
449 PHP_HTTP_E_MESSAGE_BODY,
450 PHP_HTTP_E_ENCODING,
451 PHP_HTTP_E_REQUEST,
452 PHP_HTTP_E_REQUEST_POOL,
453 PHP_HTTP_E_REQUEST_DATASHARE,
454 PHP_HTTP_E_REQUEST_FACTORY,
455 PHP_HTTP_E_SOCKET,
456 PHP_HTTP_E_RESPONSE,
457 PHP_HTTP_E_URL,
458 PHP_HTTP_E_QUERYSTRING,
459 PHP_HTTP_E_COOKIE,
460 } php_http_error_t;
461
462 #endif
463 /*
464 * Local variables:
465 * tab-width: 4
466 * c-basic-offset: 4
467 * End:
468 * vim600: noet sw=4 ts=4 fdm=marker
469 * vim<600: noet sw=4 ts=4
470 */