save funccall
[m6w6/ext-http] / php_http_env_request.c
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 #include "php_http_api.h"
14
15 static int grab_file(void *zpp TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
16 {
17 zval *zfiles, **name, **zname, **error, **zerror, **type, **ztype, **size, **zsize, **tmp_name = zpp;
18 zend_hash_key *file_key;
19
20 zfiles = (zval *) va_arg(argv, zval *);
21 file_key = (zend_hash_key *) va_arg(argv, zend_hash_key *);
22 name = (zval **) va_arg(argv, zval **);
23 size = (zval **) va_arg(argv, zval **);
24 type = (zval **) va_arg(argv, zval **);
25 error = (zval **) va_arg(argv, zval **);
26
27 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(name), key->h, (void *) &zname)
28 && SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(size), key->h, (void *) &zsize)
29 && SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(type), key->h, (void *) &ztype)
30 && SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(error), key->h, (void *) &zerror)
31 ) {
32 zval *entry, **array;
33
34 MAKE_STD_ZVAL(entry);
35 array_init(entry);
36
37 Z_ADDREF_PP(tmp_name);
38 add_assoc_zval_ex(entry, ZEND_STRS("file"), *tmp_name);
39 Z_ADDREF_PP(zname);
40 add_assoc_zval_ex(entry, ZEND_STRS("name"), *zname);
41 Z_ADDREF_PP(zsize);
42 add_assoc_zval_ex(entry, ZEND_STRS("size"), *zsize);
43 Z_ADDREF_PP(ztype);
44 add_assoc_zval_ex(entry, ZEND_STRS("type"), *ztype);
45 Z_ADDREF_PP(zerror);
46 add_assoc_zval_ex(entry, ZEND_STRS("error"), *zerror);
47
48 if (SUCCESS == zend_hash_quick_find(Z_ARRVAL_P(zfiles), file_key->arKey, file_key->nKeyLength, file_key->h, (void *) &array)) {
49 add_next_index_zval(*array, entry);
50 } else {
51 zval *tmp;
52
53 MAKE_STD_ZVAL(tmp);
54 array_init(tmp);
55 add_next_index_zval(tmp, entry);
56 zend_hash_quick_update(Z_ARRVAL_P(zfiles), file_key->arKey, file_key->nKeyLength, file_key->h, (void *) &tmp, sizeof(zval *), NULL);
57 }
58 }
59
60 return ZEND_HASH_APPLY_KEEP;
61 }
62
63 static int grab_files(void *zpp TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
64 {
65 zval *zfiles, **name, **tmp_name, **error, **type, **size, **val = zpp;
66
67 zfiles = (zval *) va_arg(argv, zval *);
68
69 if (Z_TYPE_PP(val) == IS_ARRAY
70 && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("tmp_name"), (void *) &tmp_name)
71 && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("name"), (void *) &name)
72 && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("size"), (void *) &size)
73 && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("type"), (void *) &type)
74 && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("error"), (void *) &error)
75 ) {
76 int count;
77
78 if (Z_TYPE_PP(tmp_name) == IS_ARRAY && (count = zend_hash_num_elements(Z_ARRVAL_PP(tmp_name))) > 1) {
79 if (count == zend_hash_num_elements(Z_ARRVAL_PP(name))
80 && count == zend_hash_num_elements(Z_ARRVAL_PP(size))
81 && count == zend_hash_num_elements(Z_ARRVAL_PP(type))
82 && count == zend_hash_num_elements(Z_ARRVAL_PP(error))
83 ) {
84 zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp_name) TSRMLS_CC, grab_file, 6, zfiles, key, name, size, type, error);
85 } else {
86 /* wat?! */
87 return ZEND_HASH_APPLY_STOP;
88 }
89 } else {
90 zval *cpy, **tmp;
91
92 MAKE_STD_ZVAL(cpy);
93 MAKE_COPY_ZVAL(val, cpy);
94 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(cpy), ZEND_STRS("tmp_name"), (void *) &tmp)) {
95 Z_ADDREF_PP(tmp);
96 add_assoc_zval_ex(cpy, ZEND_STRS("file"), *tmp);
97 zend_hash_del_key_or_index(Z_ARRVAL_P(cpy), ZEND_STRS("tmp_name"), 0, HASH_DEL_KEY);
98 }
99 if (key->nKeyLength > 0) {
100 zend_hash_quick_update(Z_ARRVAL_P(zfiles), key->arKey, key->nKeyLength, key->h, (void *) &cpy, sizeof(zval *), NULL);
101 } else {
102 zend_hash_index_update(Z_ARRVAL_P(zfiles), key->h, (void *) &cpy, sizeof(zval *), NULL);
103 }
104 }
105 }
106
107 return ZEND_HASH_APPLY_KEEP;
108 }
109
110 #define PHP_HTTP_ENV_REQUEST_OBJECT_INIT(obj) \
111 do { \
112 if (!obj->message) { \
113 obj->message = php_http_message_init_env(NULL, PHP_HTTP_REQUEST TSRMLS_CC); \
114 } \
115 } while(0)
116
117
118 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest___construct, 0, 0, 0)
119 ZEND_END_ARG_INFO();
120 static PHP_METHOD(HttpEnvRequest, __construct)
121 {
122 php_http_message_object_t *obj;
123 zval *zsg, *zqs;
124
125 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
126
127 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
128 obj->body = NULL;
129
130 php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_REQUEST TSRMLS_CC), unexpected_val, return);
131
132 zsg = php_http_env_get_superglobal(ZEND_STRL("_GET") TSRMLS_CC);
133 MAKE_STD_ZVAL(zqs);
134 object_init_ex(zqs, php_http_querystring_class_entry);
135 php_http_expect(SUCCESS == php_http_querystring_ctor(zqs, zsg TSRMLS_CC), unexpected_val,
136 zval_ptr_dtor(&zqs);
137 return;
138 );
139 zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("query"), zqs TSRMLS_CC);
140 zval_ptr_dtor(&zqs);
141
142 zsg = php_http_env_get_superglobal(ZEND_STRL("_POST") TSRMLS_CC);
143 MAKE_STD_ZVAL(zqs);
144 object_init_ex(zqs, php_http_querystring_class_entry);
145 php_http_expect(SUCCESS == php_http_querystring_ctor(zqs, zsg TSRMLS_CC), unexpected_val,
146 zval_ptr_dtor(&zqs);
147 return;
148 );
149 zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("form"), zqs TSRMLS_CC);
150 zval_ptr_dtor(&zqs);
151
152 MAKE_STD_ZVAL(zqs);
153 array_init(zqs);
154 if ((zsg = php_http_env_get_superglobal(ZEND_STRL("_FILES") TSRMLS_CC))) {
155 zend_hash_apply_with_arguments(Z_ARRVAL_P(zsg) TSRMLS_CC, grab_files, 1, zqs);
156 }
157 zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("files"), zqs TSRMLS_CC);
158 zval_ptr_dtor(&zqs);
159 }
160
161 #define call_querystring_get(prop) \
162 do {\
163 zend_fcall_info fci; \
164 zend_fcall_info_cache fcc; \
165 zval *rv, mn, ***args = ecalloc(sizeof(zval **), ZEND_NUM_ARGS()); \
166 zval *qs = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL(prop), 0 TSRMLS_CC); \
167 \
168 INIT_PZVAL(&mn); \
169 array_init(&mn); \
170 Z_ADDREF_P(qs); \
171 add_next_index_zval(&mn, qs); \
172 add_next_index_stringl(&mn, ZEND_STRL("get"), 1); \
173 zend_fcall_info_init(&mn, 0, &fci, &fcc, NULL, NULL TSRMLS_CC); \
174 zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args); \
175 zend_fcall_info_argp(&fci TSRMLS_CC, ZEND_NUM_ARGS(), args); \
176 zend_fcall_info_call(&fci, &fcc, &rv, NULL TSRMLS_CC); \
177 zend_fcall_info_args_clear(&fci, 1); \
178 efree(args); \
179 zval_dtor(&mn); \
180 RETVAL_ZVAL(rv, 0, 1); \
181 } while(0);
182
183 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getForm, 0, 0, 0)
184 ZEND_ARG_INFO(0, name)
185 ZEND_ARG_INFO(0, type)
186 ZEND_ARG_INFO(0, defval)
187 ZEND_ARG_INFO(0, delete)
188 ZEND_END_ARG_INFO();
189 static PHP_METHOD(HttpEnvRequest, getForm)
190 {
191 if (ZEND_NUM_ARGS()) {
192 call_querystring_get("form");
193 } else {
194 zval *zform = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("form"), 0 TSRMLS_CC);
195 RETURN_ZVAL(zform, 1, 0);
196 }
197 }
198
199 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getQuery, 0, 0, 0)
200 ZEND_ARG_INFO(0, name)
201 ZEND_ARG_INFO(0, type)
202 ZEND_ARG_INFO(0, defval)
203 ZEND_ARG_INFO(0, delete)
204 ZEND_END_ARG_INFO();
205 static PHP_METHOD(HttpEnvRequest, getQuery)
206 {
207 if (ZEND_NUM_ARGS()) {
208 call_querystring_get("query");
209 } else {
210 zval *zquery = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("query"), 0 TSRMLS_CC);
211 RETURN_ZVAL(zquery, 1, 0);
212 }
213 }
214
215 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getFiles, 0, 0, 0)
216 ZEND_END_ARG_INFO();
217 static PHP_METHOD(HttpEnvRequest, getFiles)
218 {
219 if (SUCCESS == zend_parse_parameters_none()) {
220 zval *zfiles = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("files"), 0 TSRMLS_CC);
221 RETURN_ZVAL(zfiles, 1, 0);
222 }
223 }
224
225 static zend_function_entry php_http_env_request_methods[] = {
226 PHP_ME(HttpEnvRequest, __construct, ai_HttpEnvRequest___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
227 PHP_ME(HttpEnvRequest, getForm, ai_HttpEnvRequest_getForm, ZEND_ACC_PUBLIC)
228 PHP_ME(HttpEnvRequest, getQuery, ai_HttpEnvRequest_getQuery, ZEND_ACC_PUBLIC)
229 PHP_ME(HttpEnvRequest, getFiles, ai_HttpEnvRequest_getFiles, ZEND_ACC_PUBLIC)
230 EMPTY_FUNCTION_ENTRY
231 };
232
233 zend_class_entry *php_http_env_request_class_entry;
234
235 PHP_MINIT_FUNCTION(http_env_request)
236 {
237 zend_class_entry ce = {0};
238
239 INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Request", php_http_env_request_methods);
240 php_http_env_request_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
241
242 zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("query"), ZEND_ACC_PROTECTED TSRMLS_CC);
243 zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("form"), ZEND_ACC_PROTECTED TSRMLS_CC);
244 zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("files"), ZEND_ACC_PROTECTED TSRMLS_CC);
245
246 return SUCCESS;
247 }
248
249 /*
250 * Local variables:
251 * tab-width: 4
252 * c-basic-offset: 4
253 * End:
254 * vim600: noet sw=4 ts=4 fdm=marker
255 * vim<600: noet sw=4 ts=4
256 */