Line data Source code
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 TSRMLS_DC);
17 :
18 3 : static inline HashTable *php_http_negotiate_language(HashTable *supported, php_http_message_t *request TSRMLS_DC)
19 : {
20 3 : HashTable *result = NULL;
21 : size_t length;
22 3 : char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Language"), &length, request TSRMLS_CC);
23 :
24 3 : if (value) {
25 3 : result = php_http_negotiate(value, length, supported, "-", 1 TSRMLS_CC);
26 : }
27 3 : PTR_FREE(value);
28 :
29 3 : return result;
30 : }
31 :
32 5 : static inline HashTable *php_http_negotiate_encoding(HashTable *supported, php_http_message_t *request TSRMLS_DC)
33 : {
34 5 : HashTable *result = NULL;
35 : size_t length;
36 5 : char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Encoding"), &length, request TSRMLS_CC);
37 :
38 5 : if (value) {
39 5 : result = php_http_negotiate(value, length, supported, NULL, 0 TSRMLS_CC);
40 : }
41 5 : PTR_FREE(value);
42 :
43 5 : return result;
44 : }
45 :
46 3 : static inline HashTable *php_http_negotiate_charset(HashTable *supported, php_http_message_t *request TSRMLS_DC)
47 : {
48 3 : HashTable *result = NULL;
49 : size_t length;
50 3 : char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Charset"), &length, request TSRMLS_CC);
51 :
52 3 : if (value) {
53 3 : result = php_http_negotiate(value, length, supported, NULL, 0 TSRMLS_CC);
54 : }
55 3 : PTR_FREE(value);
56 :
57 3 : return result;
58 : }
59 :
60 3 : static inline HashTable *php_http_negotiate_content_type(HashTable *supported, php_http_message_t *request TSRMLS_DC)
61 : {
62 3 : HashTable *result = NULL;
63 : size_t length;
64 3 : char *value = php_http_env_get_request_header(ZEND_STRL("Accept"), &length, request TSRMLS_CC);
65 :
66 3 : if (value) {
67 3 : result = php_http_negotiate(value, length, supported, "/", 1 TSRMLS_CC);
68 : }
69 3 : PTR_FREE(value);
70 :
71 3 : return result;
72 : }
73 :
74 : #define PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported) \
75 : { \
76 : zval **value; \
77 : \
78 : zend_hash_internal_pointer_reset((supported)); \
79 : if (SUCCESS == zend_hash_get_current_data((supported), (void *) &value)) { \
80 : RETVAL_ZVAL(*value, 1, 0); \
81 : } else { \
82 : RETVAL_NULL(); \
83 : } \
84 : }
85 :
86 : #define PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array) \
87 : PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported); \
88 : if (rs_array) { \
89 : HashPosition pos; \
90 : zval **value_ptr; \
91 : \
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); \
96 : } \
97 : }
98 :
99 : #define PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array) \
100 : { \
101 : char *key; \
102 : uint key_len; \
103 : ulong idx; \
104 : \
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); \
107 : } else { \
108 : PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported); \
109 : } \
110 : \
111 : if (rs_array) { \
112 : zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
113 : } \
114 : \
115 : zend_hash_destroy(result); \
116 : FREE_HASHTABLE(result); \
117 : }
118 :
119 : #define PHP_HTTP_DO_NEGOTIATE(type, supported, rs_array) \
120 : { \
121 : HashTable *result; \
122 : if ((result = php_http_negotiate_ ##type(supported, NULL TSRMLS_CC))) { \
123 : PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array); \
124 : } else { \
125 : PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); \
126 : } \
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 :
|