prepare v4.2.5
[m6w6/ext-http] / src / php_http_negotiate.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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifndef PHP_HTTP_NEGOTIATE_H
14 #define PHP_HTTP_NEGOTIATE_H
15
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);
17
18 static inline HashTable *php_http_negotiate_language(HashTable *supported, php_http_message_t *request)
19 {
20 HashTable *result = NULL;
21 size_t length;
22 char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Language"), &length, request);
23
24 if (value) {
25 result = php_http_negotiate(value, length, supported, "-", 1);
26 }
27 PTR_FREE(value);
28
29 return result;
30 }
31
32 static inline HashTable *php_http_negotiate_encoding(HashTable *supported, php_http_message_t *request)
33 {
34 HashTable *result = NULL;
35 size_t length;
36 char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Encoding"), &length, request);
37
38 if (value) {
39 result = php_http_negotiate(value, length, supported, NULL, 0);
40 }
41 PTR_FREE(value);
42
43 return result;
44 }
45
46 static inline HashTable *php_http_negotiate_charset(HashTable *supported, php_http_message_t *request)
47 {
48 HashTable *result = NULL;
49 size_t length;
50 char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Charset"), &length, request);
51
52 if (value) {
53 result = php_http_negotiate(value, length, supported, NULL, 0);
54 }
55 PTR_FREE(value);
56
57 return result;
58 }
59
60 static inline HashTable *php_http_negotiate_content_type(HashTable *supported, php_http_message_t *request)
61 {
62 HashTable *result = NULL;
63 size_t length;
64 char *value = php_http_env_get_request_header(ZEND_STRL("Accept"), &length, request);
65
66 if (value) {
67 result = php_http_negotiate(value, length, supported, "/", 1);
68 }
69 PTR_FREE(value);
70
71 return result;
72 }
73
74 #define PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported) \
75 { \
76 zval *value; \
77 HashPosition pos; \
78 \
79 zend_hash_internal_pointer_reset_ex((supported), &pos); \
80 if ((value = zend_hash_get_current_data_ex((supported), &pos))) { \
81 RETVAL_ZVAL(value, 1, 0); \
82 } else { \
83 RETVAL_NULL(); \
84 } \
85 }
86
87 #define PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array) \
88 PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported); \
89 if (rs_array) { \
90 zval *value; \
91 \
92 ZEND_HASH_FOREACH_VAL(supported, value) \
93 { \
94 zend_string *zs = zval_get_string(value); \
95 add_assoc_double_ex(rs_array, zs->val, zs->len, 1.0); \
96 zend_string_release(zs); \
97 } \
98 ZEND_HASH_FOREACH_END(); \
99 }
100
101 #define PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array) \
102 { \
103 zend_string *key; \
104 zend_ulong idx; \
105 \
106 if (zend_hash_num_elements(result) && HASH_KEY_IS_STRING == zend_hash_get_current_key(result, &key, &idx)) { \
107 RETVAL_STR_COPY(key); \
108 } else { \
109 PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported); \
110 } \
111 \
112 if (rs_array) { \
113 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref); \
114 } \
115 \
116 zend_hash_destroy(result); \
117 FREE_HASHTABLE(result); \
118 }
119
120 #define PHP_HTTP_DO_NEGOTIATE(type, supported, rs_array) \
121 { \
122 HashTable *result; \
123 if ((result = php_http_negotiate_ ##type(supported, NULL))) { \
124 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array); \
125 } else { \
126 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); \
127 } \
128 }
129
130 #endif
131
132 /*
133 * Local variables:
134 * tab-width: 4
135 * c-basic-offset: 4
136 * End:
137 * vim600: noet sw=4 ts=4 fdm=marker
138 * vim<600: noet sw=4 ts=4
139 */
140