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-2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #ifndef PHP_HTTP_NEGOTIATE_H
14 #define PHP_HTTP_NEGOTIATE_H
16 PHP_HTTP_API HashTable
*php_http_negotiate(const char *value_str
, size_t value_len
, HashTable
*supported
, const char *primary_sep_str
, size_t primary_sep_len TSRMLS_DC
);
18 static inline HashTable
*php_http_negotiate_language(HashTable
*supported
, php_http_message_t
*request TSRMLS_DC
)
20 HashTable
*result
= NULL
;
22 char *value
= php_http_env_get_request_header(ZEND_STRL("Accept-Language"), &length
, request TSRMLS_CC
);
25 result
= php_http_negotiate(value
, length
, supported
, "-", 1 TSRMLS_CC
);
32 static inline HashTable
*php_http_negotiate_encoding(HashTable
*supported
, php_http_message_t
*request TSRMLS_DC
)
34 HashTable
*result
= NULL
;
36 char *value
= php_http_env_get_request_header(ZEND_STRL("Accept-Encoding"), &length
, request TSRMLS_CC
);
39 result
= php_http_negotiate(value
, length
, supported
, NULL
, 0 TSRMLS_CC
);
46 static inline HashTable
*php_http_negotiate_charset(HashTable
*supported
, php_http_message_t
*request TSRMLS_DC
)
48 HashTable
*result
= NULL
;
50 char *value
= php_http_env_get_request_header(ZEND_STRL("Accept-Charset"), &length
, request TSRMLS_CC
);
53 result
= php_http_negotiate(value
, length
, supported
, NULL
, 0 TSRMLS_CC
);
60 static inline HashTable
*php_http_negotiate_content_type(HashTable
*supported
, php_http_message_t
*request TSRMLS_DC
)
62 HashTable
*result
= NULL
;
64 char *value
= php_http_env_get_request_header(ZEND_STRL("Accept"), &length
, request TSRMLS_CC
);
67 result
= php_http_negotiate(value
, length
, supported
, "/", 1 TSRMLS_CC
);
74 #define PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported) \
78 zend_hash_internal_pointer_reset((supported)); \
79 if (SUCCESS == zend_hash_get_current_data((supported), (void *) &value)) { \
80 RETVAL_ZVAL(*value, 1, 0); \
86 #define PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array) \
87 PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported); \
92 FOREACH_HASH_VAL(pos, supported, value_ptr) { \
93 zval *value = php_http_ztyp(IS_STRING, *value_ptr); \
94 add_assoc_double(rs_array, Z_STRVAL_P(value), 1.0); \
95 zval_ptr_dtor(&value); \
99 #define PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array) \
105 if (zend_hash_num_elements(result) && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
106 RETVAL_STRINGL(key, key_len-1, 0); \
108 PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported); \
112 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
115 zend_hash_destroy(result); \
116 FREE_HASHTABLE(result); \
119 #define PHP_HTTP_DO_NEGOTIATE(type, supported, rs_array) \
122 if ((result = php_http_negotiate_ ##type(supported, NULL TSRMLS_CC))) { \
123 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array); \
125 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); \
137 * vim600: noet sw=4 ts=4 fdm=marker
138 * vim<600: noet sw=4 ts=4