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"
25 #define PHP_HTTP_URL_MAXLEN 4096
27 /* def URL arg separator */
28 #define PHP_HTTP_URL_ARGSEP "&"
30 /* send buffer size */
31 #define PHP_HTTP_SENDBUF_SIZE 40960
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))
43 PHP_HTTP_API
void php_http_sleep(double s
);
45 /* STRING UTILITIES */
47 #define PHP_HTTP_CHECK_CONTENT_TYPE(ct, action) \
48 if (!strchr((ct), '/')) { \
49 php_http_error(HE_WARNING, PHP_HTTP_E_INVALID_PARAM, \
50 "Content type \"%s\" does not seem to contain a primary and a secondary part", (ct)); \
56 # define STR_SET(STR, SET) \
63 #define STR_PTR(s) (s?s:"")
65 #define lenof(S) (sizeof(S) - 1)
67 #define PHP_HTTP_MATCH_LOOSE 0
68 #define PHP_HTTP_MATCH_CASE 0x01
69 #define PHP_HTTP_MATCH_WORD 0x10
70 #define PHP_HTTP_MATCH_FULL 0x20
71 #define PHP_HTTP_MATCH_STRICT (PHP_HTTP_MATCH_CASE|PHP_HTTP_MATCH_FULL)
73 int php_http_match(const char *haystack
, const char *needle
, int flags
);
74 char *php_http_pretty_key(char *key
, size_t key_len
, zend_bool uctitle
, zend_bool xhyphen
);
75 size_t php_http_boundary(char *buf
, size_t len TSRMLS_DC
);
76 int php_http_select_str(const char *cmp
, int argc
, ...);
78 static inline const char *php_http_locate_str(const char *h
, size_t h_len
, const char *n
, size_t n_len
)
86 for (p
= n
; *p
== h
[p
-n
]; ++p
) {
98 static inline const char *php_http_locate_eol(const char *line
, int *eol_len
)
100 const char *eol
= strpbrk(line
, "\r\n");
103 *eol_len
= eol
? ((eol
[0] == '\r' && eol
[1] == '\n') ? 2 : 1) : 0;
108 static inline const char *php_http_locate_bin_eol(const char *bin
, size_t len
, int *eol_len
)
112 for (eol
= bin
; eol
- bin
< len
; ++eol
) {
113 if (*eol
== '\r' || *eol
== '\n') {
115 *eol_len
= ((eol
[0] == '\r' && eol
[1] == '\n') ? 2 : 1);
126 #if PHP_VERSION_ID < 50400
127 # 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*))
128 # define PHP_HTTP_ZEND_LITERAL_DC
129 # define PHP_HTTP_ZEND_LITERAL_CC
130 # define PHP_HTTP_ZEND_LITERAL_CCN
131 # define ZVAL_COPY_VALUE(zv, arr) do { \
132 (zv)->value = (arr)->value; \
133 Z_TYPE_P(zv) = Z_TYPE_P(arr); \
136 # define PHP_HTTP_ZEND_LITERAL_DC , const zend_literal *literal_key
137 # define PHP_HTTP_ZEND_LITERAL_CC , (literal_key)
138 # define PHP_HTTP_ZEND_LITERAL_CCN , NULL
141 #define INIT_PZVAL_ARRAY(zv, ht) \
144 Z_TYPE_P(zv) = IS_ARRAY; \
145 Z_ARRVAL_P(zv) = (ht); \
148 static inline zval
*php_http_ztyp(int type
, zval
*z
)
150 SEPARATE_ARG_IF_REF(z
);
151 if (Z_TYPE_P(z
) != type
) {
153 case IS_NULL
: convert_to_null_ex(&z
); break;
154 case IS_BOOL
: convert_to_boolean_ex(&z
); break;
155 case IS_LONG
: convert_to_long_ex(&z
); break;
156 case IS_DOUBLE
: convert_to_double_ex(&z
); break;
157 case IS_STRING
: convert_to_string_ex(&z
); break;
158 case IS_ARRAY
: convert_to_array_ex(&z
); break;
159 case IS_OBJECT
: convert_to_object_ex(&z
); break;
165 static inline zval
*php_http_zsep(zend_bool add_ref
, int type
, zval
*z
)
170 if (Z_TYPE_P(z
) != type
) {
172 case IS_NULL
: convert_to_null_ex(&z
); break;
173 case IS_BOOL
: convert_to_boolean_ex(&z
); break;
174 case IS_LONG
: convert_to_long_ex(&z
); break;
175 case IS_DOUBLE
: convert_to_double_ex(&z
); break;
176 case IS_STRING
: convert_to_string_ex(&z
); break;
177 case IS_ARRAY
: convert_to_array_ex(&z
); break;
178 case IS_OBJECT
: convert_to_object_ex(&z
); break;
181 SEPARATE_ZVAL_IF_NOT_REF(&z
);
186 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
)
188 zend_ini_entry
*ini_entry
;
190 if (SUCCESS
== zend_hash_find(EG(ini_directives
), name_str
, name_len
+ 1, (void *) &ini_entry
)) {
191 if (orig
&& ini_entry
->modified
) {
192 *value_str
= ini_entry
->orig_value
;
193 *value_len
= (size_t) ini_entry
->orig_value_length
;
195 *value_str
= ini_entry
->value
;
196 *value_len
= (size_t) ini_entry
->value_length
;
203 /* return bool (v == SUCCESS) */
204 #define RETVAL_SUCCESS(v) RETVAL_BOOL(SUCCESS == (v))
205 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
206 /* return object(values) */
207 #define RETVAL_OBJECT(o, addref) \
208 RETVAL_OBJVAL((o)->value.obj, addref)
209 #define RETURN_OBJECT(o, addref) \
210 RETVAL_OBJECT(o, addref); \
212 #define RETVAL_OBJVAL(ov, addref) \
213 ZVAL_OBJVAL(return_value, ov, addref)
214 #define RETURN_OBJVAL(ov, addref) \
215 RETVAL_OBJVAL(ov, addref); \
217 #define ZVAL_OBJVAL(zv, ov, addref) \
218 (zv)->type = IS_OBJECT; \
219 (zv)->value.obj = (ov);\
220 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
221 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
223 /* return property */
224 #define RETVAL_PROP(CE, n) RETVAL_PROP_EX(CE, getThis(), n)
225 #define RETURN_PROP(CE, n) RETURN_PROP_EX(CE, getThis(), n)
226 #define RETVAL_PROP_EX(CE, this, n) \
228 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
229 RETVAL_ZVAL(__prop, 1, 0); \
231 #define RETURN_PROP_EX(CE, this, n) \
233 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
234 RETURN_ZVAL(__prop, 1, 0); \
236 #define RETVAL_SPROP(CE, n) \
238 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
239 RETVAL_ZVAL(__prop, 1, 0); \
241 #define RETURN_SPROP(CE, n) \
243 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
244 RETURN_ZVAL(__prop, 1, 0); \
247 #define Z_OBJ_DELREF(z) \
248 if (Z_OBJ_HT(z)->del_ref) { \
249 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
251 #define Z_OBJ_ADDREF(z) \
252 if (Z_OBJ_HT(z)->add_ref) { \
253 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
255 #define Z_OBJ_DELREF_P(z) \
256 if (Z_OBJ_HT_P(z)->del_ref) { \
257 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
259 #define Z_OBJ_ADDREF_P(z) \
260 if (Z_OBJ_HT_P(z)->add_ref) { \
261 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
263 #define Z_OBJ_DELREF_PP(z) \
264 if (Z_OBJ_HT_PP(z)->del_ref) { \
265 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
267 #define Z_OBJ_ADDREF_PP(z) \
268 if (Z_OBJ_HT_PP(z)->add_ref) { \
269 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
272 #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)
273 #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)
274 #define PHP_HTTP_END_ARGS }
275 #define PHP_HTTP_EMPTY_ARGS_EX(class, method, ret_ref) PHP_HTTP_BEGIN_ARGS_EX(class, method, ret_ref, 0) PHP_HTTP_END_ARGS
276 #define PHP_HTTP_ARGS(class, method) args_for_ ##class## _ ##method
277 #define PHP_HTTP_ARG_VAL(name, pass_ref) ZEND_ARG_INFO(pass_ref, name)
278 #define PHP_HTTP_ARG_OBJ(class, name, allow_null) ZEND_ARG_OBJ_INFO(0, name, class, allow_null)
279 #define PHP_HTTP_ARG_ARR(name, allow_null, pass_ref) ZEND_ARG_ARRAY_INFO(pass_ref, name, allow_null)
281 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
283 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
284 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
285 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
286 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
289 #define PHP_HTTP_INI_ENTRY(entry, default, scope, updater, global) \
290 STD_PHP_INI_ENTRY(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals)
291 #define PHP_HTTP_INI_ENTRY_EX(entry, default, scope, updater, displayer, global) \
292 STD_PHP_INI_ENTRY_EX(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals, displayer)
294 #define PHP_HTTP_REGISTER_CLASS(ns, classname, name, parent, flags) \
296 zend_class_entry ce; \
297 memset(&ce, 0, sizeof(zend_class_entry)); \
298 INIT_NS_CLASS_ENTRY(ce, #ns, #classname, php_ ##name## _method_entry); \
299 php_ ##name## _class_entry = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
300 php_ ##name## _class_entry->ce_flags |= flags; \
301 php_http_register_class(php_ ##name## _get_class_entry); \
304 #define PHP_HTTP_REGISTER_INTERFACE(ns, ifacename, name, flags) \
306 zend_class_entry ce; \
307 memset(&ce, 0, sizeof(zend_class_entry)); \
308 INIT_NS_CLASS_ENTRY(ce, #ns, #ifacename, php_ ##name## _method_entry); \
309 php_ ##name## _class_entry = zend_register_internal_interface(&ce TSRMLS_CC); \
310 php_ ##name## _class_entry->ce_flags |= flags; \
311 php_http_register_class(php_ ##name## _get_class_entry); \
314 #define PHP_HTTP_REGISTER_EXCEPTION(classname, cename, parent) \
316 zend_class_entry ce; \
317 memset(&ce, 0, sizeof(zend_class_entry)); \
318 INIT_NS_CLASS_ENTRY(ce, "http", #classname, NULL); \
319 ce.create_object = NULL; \
320 cename = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
324 PHP_HTTP_API
unsigned php_http_array_list(HashTable
*ht TSRMLS_DC
, unsigned argc
, ...);
326 typedef struct php_http_array_hashkey
{
332 } php_http_array_hashkey_t
;
333 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
335 static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t
*key
)
337 if (key
->type
!= HASH_KEY_IS_STRING
) {
338 key
->len
= spprintf(&key
->str
, 0, "%lu", key
->num
) + 1;
342 static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t
*key
)
344 if (key
->type
!= HASH_KEY_IS_STRING
|| key
->dup
) {
349 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
350 #define FOREACH_HASH_VAL(pos, hash, val) \
351 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
352 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
353 zend_hash_move_forward_ex(hash, &pos))
355 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
356 #define FOREACH_HASH_KEY(pos, hash, _key) \
357 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
358 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
359 zend_hash_move_forward_ex(hash, &pos)) \
361 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
362 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
363 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
364 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
365 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
366 zend_hash_move_forward_ex(hash, &pos))
368 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
369 #define ARRAY_JOIN_STRONLY 1
370 #define ARRAY_JOIN_PRETTIFY 2
371 #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)
373 int php_http_array_apply_append_func(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
);
374 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
);
378 typedef size_t (*php_http_pass_callback_t
)(void *cb_arg
, const char *str
, size_t len
);
379 typedef size_t (*php_http_pass_php_http_buffer_callback_t
)(void *cb_arg
, php_http_buffer_t
*str
);
380 typedef size_t (*php_http_pass_format_callback_t
)(void *cb_arg
, const char *fmt
, ...);
382 typedef struct php_http_pass_fcall_arg
{
385 zend_fcall_info_cache fcc
;
389 } php_http_pass_fcall_arg_t
;
391 PHP_HTTP_API
size_t php_http_pass_fcall_callback(void *cb_arg
, const char *str
, size_t len
);
395 extern void php_http_error(long type TSRMLS_DC
, long code
, const char *format
, ...);
397 #define with_error_handling(eh, ec) \
399 zend_error_handling __eh; \
400 zend_replace_error_handling((eh), (ec), &__eh TSRMLS_CC);
402 #define end_error_handling() \
403 zend_restore_error_handling(&__eh TSRMLS_CC); \
409 #define HE_THROW E_THROW TSRMLS_CC
410 #define HE_NOTICE E_NOTICE TSRMLS_CC
411 #define HE_WARNING E_WARNING TSRMLS_CC
412 #define HE_ERROR E_ERROR TSRMLS_CC
414 typedef enum php_http_error
{
415 PHP_HTTP_E_UNKNOWN
= 0,
417 PHP_HTTP_E_INVALID_PARAM
,
419 PHP_HTTP_E_MALFORMED_HEADERS
,
420 PHP_HTTP_E_REQUEST_METHOD
,
422 PHP_HTTP_E_MESSAGE_TYPE
,
423 PHP_HTTP_E_MESSAGE_BODY
,
426 PHP_HTTP_E_CLIENT_POOL
,
427 PHP_HTTP_E_CLIENT_DATASHARE
,
428 PHP_HTTP_E_REQUEST_FACTORY
,
432 PHP_HTTP_E_QUERYSTRING
,
442 * vim600: noet sw=4 ts=4 fdm=marker
443 * vim<600: noet sw=4 ts=4