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 STATUS
php_http_method_call(zval
*object
, const char *method_str
, size_t method_len
, int argc
, zval
**argv
[], zval
**retval_ptr TSRMLS_DC
);
205 /* return bool (v == SUCCESS) */
206 #define RETVAL_SUCCESS(v) RETVAL_BOOL(SUCCESS == (v))
207 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
208 /* return object(values) */
209 #define RETVAL_OBJECT(o, addref) \
210 RETVAL_OBJVAL((o)->value.obj, addref)
211 #define RETURN_OBJECT(o, addref) \
212 RETVAL_OBJECT(o, addref); \
214 #define RETVAL_OBJVAL(ov, addref) \
215 ZVAL_OBJVAL(return_value, ov, addref)
216 #define RETURN_OBJVAL(ov, addref) \
217 RETVAL_OBJVAL(ov, addref); \
219 #define ZVAL_OBJVAL(zv, ov, addref) \
220 (zv)->type = IS_OBJECT; \
221 (zv)->value.obj = (ov);\
222 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
223 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
225 /* return property */
226 #define RETVAL_PROP(CE, n) RETVAL_PROP_EX(CE, getThis(), n)
227 #define RETURN_PROP(CE, n) RETURN_PROP_EX(CE, getThis(), n)
228 #define RETVAL_PROP_EX(CE, this, n) \
230 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
231 RETVAL_ZVAL(__prop, 1, 0); \
233 #define RETURN_PROP_EX(CE, this, n) \
235 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
236 RETURN_ZVAL(__prop, 1, 0); \
238 #define RETVAL_SPROP(CE, n) \
240 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
241 RETVAL_ZVAL(__prop, 1, 0); \
243 #define RETURN_SPROP(CE, n) \
245 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
246 RETURN_ZVAL(__prop, 1, 0); \
249 #define Z_OBJ_DELREF(z) \
250 if (Z_OBJ_HT(z)->del_ref) { \
251 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
253 #define Z_OBJ_ADDREF(z) \
254 if (Z_OBJ_HT(z)->add_ref) { \
255 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
257 #define Z_OBJ_DELREF_P(z) \
258 if (Z_OBJ_HT_P(z)->del_ref) { \
259 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
261 #define Z_OBJ_ADDREF_P(z) \
262 if (Z_OBJ_HT_P(z)->add_ref) { \
263 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
265 #define Z_OBJ_DELREF_PP(z) \
266 if (Z_OBJ_HT_PP(z)->del_ref) { \
267 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
269 #define Z_OBJ_ADDREF_PP(z) \
270 if (Z_OBJ_HT_PP(z)->add_ref) { \
271 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
274 #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)
275 #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)
276 #define PHP_HTTP_END_ARGS }
277 #define PHP_HTTP_EMPTY_ARGS_EX(class, method, ret_ref) PHP_HTTP_BEGIN_ARGS_EX(class, method, ret_ref, 0) PHP_HTTP_END_ARGS
278 #define PHP_HTTP_ARGS(class, method) args_for_ ##class## _ ##method
279 #define PHP_HTTP_ARG_VAL(name, pass_ref) ZEND_ARG_INFO(pass_ref, name)
280 #define PHP_HTTP_ARG_OBJ(class, name, allow_null) ZEND_ARG_OBJ_INFO(0, name, class, allow_null)
281 #define PHP_HTTP_ARG_ARR(name, allow_null, pass_ref) ZEND_ARG_ARRAY_INFO(pass_ref, name, allow_null)
283 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
285 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
286 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
287 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
288 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
291 #define PHP_HTTP_INI_ENTRY(entry, default, scope, updater, global) \
292 STD_PHP_INI_ENTRY(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals)
293 #define PHP_HTTP_INI_ENTRY_EX(entry, default, scope, updater, displayer, global) \
294 STD_PHP_INI_ENTRY_EX(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals, displayer)
296 #define PHP_HTTP_REGISTER_CLASS(ns, classname, name, parent, flags) \
298 zend_class_entry ce; \
299 memset(&ce, 0, sizeof(zend_class_entry)); \
300 INIT_NS_CLASS_ENTRY(ce, #ns, #classname, php_ ##name## _method_entry); \
301 php_ ##name## _class_entry = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
302 php_ ##name## _class_entry->ce_flags |= flags; \
303 php_http_register_class(php_ ##name## _get_class_entry); \
306 #define PHP_HTTP_REGISTER_INTERFACE(ns, ifacename, name, flags) \
308 zend_class_entry ce; \
309 memset(&ce, 0, sizeof(zend_class_entry)); \
310 INIT_NS_CLASS_ENTRY(ce, #ns, #ifacename, php_ ##name## _method_entry); \
311 php_ ##name## _class_entry = zend_register_internal_interface(&ce TSRMLS_CC); \
312 php_ ##name## _class_entry->ce_flags |= flags; \
313 php_http_register_class(php_ ##name## _get_class_entry); \
316 #define PHP_HTTP_REGISTER_EXCEPTION(classname, cename, parent) \
318 zend_class_entry ce; \
319 memset(&ce, 0, sizeof(zend_class_entry)); \
320 INIT_NS_CLASS_ENTRY(ce, "http", #classname, NULL); \
321 ce.create_object = NULL; \
322 cename = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
326 PHP_HTTP_API
unsigned php_http_array_list(HashTable
*ht TSRMLS_DC
, unsigned argc
, ...);
328 typedef struct php_http_array_hashkey
{
334 } php_http_array_hashkey_t
;
335 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
337 static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t
*key
)
339 if (key
->type
!= HASH_KEY_IS_STRING
) {
340 key
->len
= spprintf(&key
->str
, 0, "%lu", key
->num
) + 1;
344 static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t
*key
)
346 if (key
->type
!= HASH_KEY_IS_STRING
|| key
->dup
) {
351 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
352 #define FOREACH_HASH_VAL(pos, hash, val) \
353 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
354 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
355 zend_hash_move_forward_ex(hash, &pos))
357 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
358 #define FOREACH_HASH_KEY(pos, hash, _key) \
359 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
360 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
361 zend_hash_move_forward_ex(hash, &pos)) \
363 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
364 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
365 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
366 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
367 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
368 zend_hash_move_forward_ex(hash, &pos))
370 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
371 #define ARRAY_JOIN_STRONLY 1
372 #define ARRAY_JOIN_PRETTIFY 2
373 #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)
375 int php_http_array_apply_append_func(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
);
376 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
);
380 typedef size_t (*php_http_pass_callback_t
)(void *cb_arg
, const char *str
, size_t len
);
381 typedef size_t (*php_http_pass_php_http_buffer_callback_t
)(void *cb_arg
, php_http_buffer_t
*str
);
382 typedef size_t (*php_http_pass_format_callback_t
)(void *cb_arg
, const char *fmt
, ...);
384 typedef struct php_http_pass_fcall_arg
{
387 zend_fcall_info_cache fcc
;
391 } php_http_pass_fcall_arg_t
;
393 PHP_HTTP_API
size_t php_http_pass_fcall_callback(void *cb_arg
, const char *str
, size_t len
);
397 extern void php_http_error(long type TSRMLS_DC
, long code
, const char *format
, ...);
399 #define with_error_handling(eh, ec) \
401 zend_error_handling __eh; \
402 zend_replace_error_handling((eh), (ec), &__eh TSRMLS_CC);
404 #define end_error_handling() \
405 zend_restore_error_handling(&__eh TSRMLS_CC); \
411 #define HE_THROW E_THROW TSRMLS_CC
412 #define HE_NOTICE E_NOTICE TSRMLS_CC
413 #define HE_WARNING E_WARNING TSRMLS_CC
414 #define HE_ERROR E_ERROR TSRMLS_CC
416 typedef enum php_http_error
{
417 PHP_HTTP_E_UNKNOWN
= 0,
419 PHP_HTTP_E_INVALID_PARAM
,
421 PHP_HTTP_E_MALFORMED_HEADERS
,
422 PHP_HTTP_E_REQUEST_METHOD
,
424 PHP_HTTP_E_MESSAGE_TYPE
,
425 PHP_HTTP_E_MESSAGE_BODY
,
428 PHP_HTTP_E_CLIENT_POOL
,
429 PHP_HTTP_E_CLIENT_DATASHARE
,
430 PHP_HTTP_E_REQUEST_FACTORY
,
434 PHP_HTTP_E_QUERYSTRING
,
444 * vim600: noet sw=4 ts=4 fdm=marker
445 * vim<600: noet sw=4 ts=4