2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #ifndef PHP_HTTP_MISC_H
14 #define PHP_HTTP_MISC_H
18 /* DATE FORMAT RFC1123 */
19 #define PHP_HTTP_DATE_FORMAT "D, d M Y H:i:s \\G\\M\\T"
22 #define PHP_HTTP_CRLF "\r\n"
24 /* default cache control */
25 #define PHP_HTTP_DEFAULT_CACHECONTROL "private, must-revalidate, max-age=0"
28 #define PHP_HTTP_URL_MAXLEN 4096
30 /* max request method length */
31 #define PHP_HTTP_REQUEST_METHOD_MAXLEN 31
33 /* def URL arg separator */
34 #define PHP_HTTP_URL_ARGSEP "&"
36 /* send buffer size */
37 #define PHP_HTTP_SENDBUF_SIZE 40960
39 /* CURL buffer size */
40 #define PHP_HTTP_CURLBUF_SIZE 16384
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))
52 PHP_HTTP_API
void php_http_sleep(double s
);
54 /* STRING UTILITIES */
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)); \
65 # define STR_SET(STR, SET) \
72 #define STR_PTR(s) (s?s:"")
74 #define lenof(S) (sizeof(S) - 1)
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)
82 int php_http_match(const char *haystack
, const char *needle
, int flags
);
83 char *php_http_pretty_key(char *key
, size_t key_len
, zend_bool uctitle
, zend_bool xhyphen
);
84 size_t php_http_boundary(char *buf
, size_t len TSRMLS_DC
);
85 int php_http_select_str(const char *cmp
, int argc
, ...);
87 static inline const char *php_http_locate_str(const char *h
, size_t h_len
, const char *n
, size_t n_len
)
95 for (p
= n
; *p
== h
[p
-n
]; ++p
) {
107 static inline const char *php_http_locate_body(const char *message
)
109 const char *body
= NULL
, *msg
= message
;
113 if (*(msg
+1) == '\n') {
116 } else if (*(msg
+1) == '\r' && *(msg
+2) == '\n') {
126 static inline const char *php_http_locate_eol(const char *line
, int *eol_len
)
128 const char *eol
= strpbrk(line
, "\r\n");
131 *eol_len
= eol
? ((eol
[0] == '\r' && eol
[1] == '\n') ? 2 : 1) : 0;
136 static inline const char *php_http_locate_bin_eol(const char *bin
, size_t len
, int *eol_len
)
140 for (eol
= bin
; eol
- bin
<= len
; ++eol
) {
141 if (*eol
== '\r' || *eol
== '\n') {
143 *eol_len
= ((eol
[0] == '\r' && eol
[1] == '\n') ? 2 : 1);
154 #define INIT_PZVAL_ARRAY(zv, ht) \
157 Z_TYPE_P(zv) = IS_ARRAY; \
158 Z_ARRVAL_P(zv) = (ht); \
161 static inline zval
*php_http_ztyp(int type
, zval
*z
)
163 SEPARATE_ARG_IF_REF(z
);
164 if (Z_TYPE_P(z
) != type
) {
166 case IS_NULL
: convert_to_null_ex(&z
); break;
167 case IS_BOOL
: convert_to_boolean_ex(&z
); break;
168 case IS_LONG
: convert_to_long_ex(&z
); break;
169 case IS_DOUBLE
: convert_to_double_ex(&z
); break;
170 case IS_STRING
: convert_to_string_ex(&z
); break;
171 case IS_ARRAY
: convert_to_array_ex(&z
); break;
172 case IS_OBJECT
: convert_to_object_ex(&z
); break;
178 static inline zval
*php_http_zsep(zend_bool add_ref
, int type
, zval
*z
)
183 if (Z_TYPE_P(z
) != type
) {
185 case IS_NULL
: convert_to_null_ex(&z
); break;
186 case IS_BOOL
: convert_to_boolean_ex(&z
); break;
187 case IS_LONG
: convert_to_long_ex(&z
); break;
188 case IS_DOUBLE
: convert_to_double_ex(&z
); break;
189 case IS_STRING
: convert_to_string_ex(&z
); break;
190 case IS_ARRAY
: convert_to_array_ex(&z
); break;
191 case IS_OBJECT
: convert_to_object_ex(&z
); break;
194 SEPARATE_ZVAL_IF_NOT_REF(&z
);
199 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
)
201 zend_ini_entry
*ini_entry
;
203 if (SUCCESS
== zend_hash_find(EG(ini_directives
), name_str
, name_len
+ 1, (void *) &ini_entry
)) {
204 if (orig
&& ini_entry
->modified
) {
205 *value_str
= ini_entry
->orig_value
;
206 *value_len
= (size_t) ini_entry
->orig_value_length
;
208 *value_str
= ini_entry
->value
;
209 *value_len
= (size_t) ini_entry
->value_length
;
216 /* return bool (v == SUCCESS) */
217 #define RETVAL_SUCCESS(v) RETVAL_BOOL(SUCCESS == (v))
218 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
219 /* return object(values) */
220 #define RETVAL_OBJECT(o, addref) \
221 RETVAL_OBJVAL((o)->value.obj, addref)
222 #define RETURN_OBJECT(o, addref) \
223 RETVAL_OBJECT(o, addref); \
225 #define RETVAL_OBJVAL(ov, addref) \
226 ZVAL_OBJVAL(return_value, ov, addref)
227 #define RETURN_OBJVAL(ov, addref) \
228 RETVAL_OBJVAL(ov, addref); \
230 #define ZVAL_OBJVAL(zv, ov, addref) \
231 (zv)->type = IS_OBJECT; \
232 (zv)->value.obj = (ov);\
233 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
234 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
236 /* return property */
237 #define RETVAL_PROP(CE, n) RETVAL_PROP_EX(CE, getThis(), n)
238 #define RETURN_PROP(CE, n) RETURN_PROP_EX(CE, getThis(), n)
239 #define RETVAL_PROP_EX(CE, this, n) \
241 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
242 RETVAL_ZVAL(__prop, 1, 0); \
244 #define RETURN_PROP_EX(CE, this, n) \
246 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
247 RETURN_ZVAL(__prop, 1, 0); \
249 #define RETVAL_SPROP(CE, n) \
251 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
252 RETVAL_ZVAL(__prop, 1, 0); \
254 #define RETURN_SPROP(CE, n) \
256 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
257 RETURN_ZVAL(__prop, 1, 0); \
260 #define Z_OBJ_DELREF(z) \
261 if (Z_OBJ_HT(z)->del_ref) { \
262 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
264 #define Z_OBJ_ADDREF(z) \
265 if (Z_OBJ_HT(z)->add_ref) { \
266 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
268 #define Z_OBJ_DELREF_P(z) \
269 if (Z_OBJ_HT_P(z)->del_ref) { \
270 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
272 #define Z_OBJ_ADDREF_P(z) \
273 if (Z_OBJ_HT_P(z)->add_ref) { \
274 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
276 #define Z_OBJ_DELREF_PP(z) \
277 if (Z_OBJ_HT_PP(z)->del_ref) { \
278 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
280 #define Z_OBJ_ADDREF_PP(z) \
281 if (Z_OBJ_HT_PP(z)->add_ref) { \
282 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
285 #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)
286 #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)
287 #define PHP_HTTP_END_ARGS }
288 #define PHP_HTTP_EMPTY_ARGS_EX(class, method, ret_ref) PHP_HTTP_BEGIN_ARGS_EX(class, method, ret_ref, 0) PHP_HTTP_END_ARGS
289 #define PHP_HTTP_ARGS(class, method) args_for_ ##class## _ ##method
290 #define PHP_HTTP_ARG_VAL(name, pass_ref) ZEND_ARG_INFO(pass_ref, name)
291 #define PHP_HTTP_ARG_OBJ(class, name, allow_null) ZEND_ARG_OBJ_INFO(0, name, class, allow_null)
292 #define PHP_HTTP_ARG_ARR(name, allow_null, pass_ref) ZEND_ARG_ARRAY_INFO(pass_ref, name, allow_null)
294 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
296 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
297 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
298 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
299 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
302 #define PHP_HTTP_INI_ENTRY(entry, default, scope, updater, global) \
303 STD_PHP_INI_ENTRY(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals)
304 #define PHP_HTTP_INI_ENTRY_EX(entry, default, scope, updater, displayer, global) \
305 STD_PHP_INI_ENTRY_EX(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals, displayer)
307 #define PHP_HTTP_REGISTER_CLASS(ns, classname, name, parent, flags) \
309 zend_class_entry ce; \
310 memset(&ce, 0, sizeof(zend_class_entry)); \
311 INIT_NS_CLASS_ENTRY(ce, #ns, #classname, php_ ##name## _method_entry); \
312 php_ ##name## _class_entry = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
313 php_ ##name## _class_entry->ce_flags |= flags; \
316 #define PHP_HTTP_REGISTER_INTERFACE(ns, ifacename, name, flags) \
318 zend_class_entry ce; \
319 memset(&ce, 0, sizeof(zend_class_entry)); \
320 INIT_NS_CLASS_ENTRY(ce, #ns, #ifacename, php_ ##name## _method_entry); \
321 php_ ##name## _class_entry = zend_register_internal_interface(&ce TSRMLS_CC); \
322 php_ ##name## _class_entry->ce_flags |= flags; \
325 #define PHP_HTTP_REGISTER_EXCEPTION(classname, cename, parent) \
327 zend_class_entry ce; \
328 memset(&ce, 0, sizeof(zend_class_entry)); \
329 INIT_NS_CLASS_ENTRY(ce, "http", #classname, NULL); \
330 ce.create_object = NULL; \
331 cename = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
334 #define ACC_PROP_PRIVATE(ce, flags) ((flags & ZEND_ACC_PRIVATE) && (EG(scope) && ce == EG(scope))
335 #define ACC_PROP_PROTECTED(ce, flags) ((flags & ZEND_ACC_PROTECTED) && (zend_check_protected(ce, EG(scope))))
336 #define ACC_PROP_PUBLIC(flags) (flags & ZEND_ACC_PUBLIC)
337 #define ACC_PROP(ce, flags) (ACC_PROP_PUBLIC(flags) || ACC_PROP_PRIVATE(ce, flags) || ACC_PROP_PROTECTED(ce, flags))
339 #ifdef PHP_HTTP_HAVE_CURL
340 # define PHP_HTTP_DECLARE_ARG_PASS_INFO() \
341 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_2, 0) \
342 ZEND_ARG_PASS_INFO(0) \
343 ZEND_ARG_PASS_INFO(1) \
344 ZEND_END_ARG_INFO(); \
346 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_3, 0) \
347 ZEND_ARG_PASS_INFO(0) \
348 ZEND_ARG_PASS_INFO(0) \
349 ZEND_ARG_PASS_INFO(1) \
350 ZEND_END_ARG_INFO(); \
352 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_4, 0) \
353 ZEND_ARG_PASS_INFO(0) \
354 ZEND_ARG_PASS_INFO(0) \
355 ZEND_ARG_PASS_INFO(0) \
356 ZEND_ARG_PASS_INFO(1) \
357 ZEND_END_ARG_INFO(); \
359 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_5, 0) \
360 ZEND_ARG_PASS_INFO(0) \
361 ZEND_ARG_PASS_INFO(0) \
362 ZEND_ARG_PASS_INFO(0) \
363 ZEND_ARG_PASS_INFO(0) \
364 ZEND_ARG_PASS_INFO(1) \
368 # define PHP_HTTP_DECLARE_ARG_PASS_INFO() \
369 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_2, 0) \
370 ZEND_ARG_PASS_INFO(0) \
371 ZEND_ARG_PASS_INFO(1) \
372 ZEND_END_ARG_INFO(); \
374 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_3, 0) \
375 ZEND_ARG_PASS_INFO(0) \
376 ZEND_ARG_PASS_INFO(0) \
377 ZEND_ARG_PASS_INFO(1) \
378 ZEND_END_ARG_INFO(); \
380 ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_4, 0) \
381 ZEND_ARG_PASS_INFO(0) \
382 ZEND_ARG_PASS_INFO(0) \
383 ZEND_ARG_PASS_INFO(0) \
384 ZEND_ARG_PASS_INFO(1) \
386 #endif /* PHP_HTTP_HAVE_CURL */
390 typedef struct php_http_array_hashkey
{
396 } php_http_array_hashkey_t
;
397 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
399 static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t
*key
)
401 if (key
->type
!= HASH_KEY_IS_STRING
) {
402 key
->len
= spprintf(&key
->str
, 0, "%lu", key
->num
) + 1;
406 static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t
*key
)
408 if (key
->type
!= HASH_KEY_IS_STRING
|| key
->dup
) {
413 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
414 #define FOREACH_HASH_VAL(pos, hash, val) \
415 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
416 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
417 zend_hash_move_forward_ex(hash, &pos))
419 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
420 #define FOREACH_HASH_KEY(pos, hash, _key) \
421 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
422 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
423 zend_hash_move_forward_ex(hash, &pos)) \
425 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
426 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
427 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
428 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
429 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
430 zend_hash_move_forward_ex(hash, &pos))
432 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
433 #define ARRAY_JOIN_STRONLY 1
434 #define ARRAY_JOIN_PRETTIFY 2
435 #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)
437 int php_http_array_apply_append_func(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
);
438 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
);
442 typedef size_t (*php_http_pass_callback_t
)(void *cb_arg
, const char *str
, size_t len
);
443 typedef size_t (*php_http_pass_php_http_buffer_callback_t
)(void *cb_arg
, php_http_buffer_t
*str
);
445 typedef struct php_http_pass_callback_arg
{
446 size_t (*cb_zts
)(void *cb_arg
, const char *str
, size_t len TSRMLS_DC
);
448 } php_http_pass_callback_arg_t
;
450 PHP_HTTP_API
size_t php_http_pass_wrapper(php_http_pass_callback_arg_t
*cb_arg
, const char *str
, size_t len
);
452 typedef struct php_http_pass_fcall_arg
{
455 zend_fcall_info_cache fcc
;
459 } php_http_pass_fcall_arg_t
;
461 PHP_HTTP_API
size_t php_http_pass_fcall_callback(void *cb_arg
, const char *str
, size_t len
);
465 extern void php_http_error(long type TSRMLS_DC
, long code
, const char *format
, ...);
467 #define with_error_handling(eh, ec) \
469 zend_error_handling __eh; \
470 zend_replace_error_handling((eh), (ec), &__eh TSRMLS_CC);
472 #define end_error_handling() \
473 zend_restore_error_handling(&__eh TSRMLS_CC); \
479 #define HE_THROW E_THROW TSRMLS_CC
480 #define HE_NOTICE E_NOTICE TSRMLS_CC
481 #define HE_WARNING E_WARNING TSRMLS_CC
482 #define HE_ERROR E_ERROR TSRMLS_CC
484 typedef enum php_http_error
{
485 PHP_HTTP_E_UNKNOWN
= 0,
487 PHP_HTTP_E_INVALID_PARAM
,
489 PHP_HTTP_E_MALFORMED_HEADERS
,
490 PHP_HTTP_E_REQUEST_METHOD
,
492 PHP_HTTP_E_MESSAGE_TYPE
,
493 PHP_HTTP_E_MESSAGE_BODY
,
496 PHP_HTTP_E_CLIENT_POOL
,
497 PHP_HTTP_E_CLIENT_DATASHARE
,
498 PHP_HTTP_E_REQUEST_FACTORY
,
502 PHP_HTTP_E_QUERYSTRING
,
512 * vim600: noet sw=4 ts=4 fdm=marker
513 * vim<600: noet sw=4 ts=4