fix test
[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(zval *tmp_name, int argc, va_list argv, zend_hash_key *key)
16 {
17 zval *zfiles, *name, *zname, *error, *zerror, *type, *ztype, *size, *zsize;
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 ((zname = zend_hash_index_find(Z_ARRVAL_P(name), key->h))
28 && (zsize = zend_hash_index_find(Z_ARRVAL_P(size), key->h))
29 && (ztype = zend_hash_index_find(Z_ARRVAL_P(type), key->h))
30 && (zerror = zend_hash_index_find(Z_ARRVAL_P(error), key->h))
31 ) {
32 zval entry, *array;
33
34 array_init(&entry);
35
36 Z_TRY_ADDREF_P(tmp_name);
37 add_assoc_zval_ex(&entry, ZEND_STRL("file"), tmp_name);
38 Z_TRY_ADDREF_P(zname);
39 add_assoc_zval_ex(&entry, ZEND_STRL("name"), zname);
40 Z_TRY_ADDREF_P(zsize);
41 add_assoc_zval_ex(&entry, ZEND_STRL("size"), zsize);
42 Z_TRY_ADDREF_P(ztype);
43 add_assoc_zval_ex(&entry, ZEND_STRL("type"), ztype);
44 Z_TRY_ADDREF_P(zerror);
45 add_assoc_zval_ex(&entry, ZEND_STRL("error"), zerror);
46
47 if (file_key->key && (array = zend_hash_find(Z_ARRVAL_P(zfiles), file_key->key))) {
48 add_next_index_zval(array, &entry);
49 } else if (!file_key->key && (array = zend_hash_index_find(Z_ARRVAL_P(zfiles), file_key->h))) {
50 add_next_index_zval(array, &entry);
51 } else {
52 zval tmp;
53
54 array_init(&tmp);
55 add_next_index_zval(&tmp, &entry);
56 if (file_key->key) {
57 zend_hash_update(Z_ARRVAL_P(zfiles), file_key->key, &tmp);
58 } else {
59 zend_hash_index_update(Z_ARRVAL_P(zfiles), file_key->h, &tmp);
60 }
61 }
62 }
63
64 return ZEND_HASH_APPLY_KEEP;
65 }
66
67 static int grab_files(zval *val, int argc, va_list argv, zend_hash_key *key)
68 {
69 zval *zfiles, *name, *tmp_name, *error, *type, *size;
70
71 zfiles = (zval *) va_arg(argv, zval *);
72
73 if ((Z_TYPE_P(val) == IS_ARRAY)
74 && (tmp_name = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("tmp_name")))
75 && (name = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("name")))
76 && (size = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("size")))
77 && (type = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("type")))
78 && (error = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("error")))
79 ) {
80 int count;
81
82 if (Z_TYPE_P(tmp_name) == IS_ARRAY && (count = zend_hash_num_elements(Z_ARRVAL_P(tmp_name))) > 1) {
83 if (count == zend_hash_num_elements(Z_ARRVAL_P(name))
84 && count == zend_hash_num_elements(Z_ARRVAL_P(size))
85 && count == zend_hash_num_elements(Z_ARRVAL_P(type))
86 && count == zend_hash_num_elements(Z_ARRVAL_P(error))
87 ) {
88 zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp_name), grab_file, 6, zfiles, key, name, size, type, error);
89 } else {
90 /* wat?! */
91 return ZEND_HASH_APPLY_STOP;
92 }
93 } else {
94 zval *tmp, entry;
95
96 ZVAL_DUP(&entry, val);
97 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("tmp_name")))) {
98 Z_ADDREF_P(tmp);
99 add_assoc_zval_ex(&entry, ZEND_STRL("file"), tmp);
100 zend_hash_str_del(Z_ARRVAL(entry), ZEND_STRL("tmp_name"));
101 }
102 if (key->key) {
103 zend_hash_update(Z_ARRVAL_P(zfiles), key->key, &entry);
104 } else {
105 zend_hash_index_update(Z_ARRVAL_P(zfiles), key->h, &entry);
106 }
107 }
108 }
109
110 return ZEND_HASH_APPLY_KEEP;
111 }
112
113 #define PHP_HTTP_ENV_REQUEST_OBJECT_INIT(obj) \
114 do { \
115 if (!obj->message) { \
116 obj->message = php_http_message_init_env(NULL, PHP_HTTP_REQUEST); \
117 } \
118 } while(0)
119
120
121 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest___construct, 0, 0, 0)
122 ZEND_END_ARG_INFO();
123 static PHP_METHOD(HttpEnvRequest, __construct)
124 {
125 php_http_message_object_t *obj;
126 zval *zsg, zqs;
127
128 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
129
130 obj = PHP_HTTP_OBJ(NULL, getThis());
131 obj->body = NULL;
132
133 php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_REQUEST), unexpected_val, return);
134
135 zsg = php_http_env_get_superglobal(ZEND_STRL("_GET"));
136 object_init_ex(&zqs, php_http_querystring_class_entry);
137 php_http_expect(SUCCESS == php_http_querystring_ctor(&zqs, zsg), unexpected_val, return);
138 zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("query"), &zqs);
139 zval_ptr_dtor(&zqs);
140
141 zsg = php_http_env_get_superglobal(ZEND_STRL("_POST"));
142 object_init_ex(&zqs, php_http_querystring_class_entry);
143 php_http_expect(SUCCESS == php_http_querystring_ctor(&zqs, zsg), unexpected_val, return);
144 zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("form"), &zqs);
145 zval_ptr_dtor(&zqs);
146
147 zsg = php_http_env_get_superglobal(ZEND_STRL("_COOKIE"));
148 object_init_ex(&zqs, php_http_querystring_class_entry);
149 php_http_expect(SUCCESS == php_http_querystring_ctor(&zqs, zsg), unexpected_val, return);
150 zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("cookie"), &zqs);
151 zval_ptr_dtor(&zqs);
152
153 array_init(&zqs);
154 if ((zsg = php_http_env_get_superglobal(ZEND_STRL("_FILES")))) {
155 zend_hash_apply_with_arguments(Z_ARRVAL_P(zsg), grab_files, 1, &zqs);
156 }
157 zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("files"), &zqs);
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 *this_ptr = getThis(); \
167 zval qs_tmp, *qs = zend_read_property(Z_OBJCE_P(this_ptr), this_ptr, ZEND_STRL(prop), 0, &qs_tmp); \
168 \
169 array_init(&mn); \
170 Z_TRY_ADDREF_P(qs); \
171 add_next_index_zval(&mn, qs); \
172 add_next_index_stringl(&mn, ZEND_STRL("get")); \
173 zend_fcall_info_init(&mn, 0, &fci, &fcc, NULL, NULL); \
174 zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args); \
175 zend_fcall_info_argp(&fci, ZEND_NUM_ARGS(), args); \
176 zend_fcall_info_call(&fci, &fcc, &rv, NULL); \
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_tmp, *zform = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("form"), 0, &zform_tmp);
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_tmp, *zquery = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("query"), 0, &zquery_tmp);
211 RETURN_ZVAL(zquery, 1, 0);
212 }
213 }
214
215 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getCookie, 0, 0, 0)
216 ZEND_ARG_INFO(0, name)
217 ZEND_ARG_INFO(0, type)
218 ZEND_ARG_INFO(0, defval)
219 ZEND_ARG_INFO(0, delete)
220 ZEND_END_ARG_INFO();
221 static PHP_METHOD(HttpEnvRequest, getCookie)
222 {
223 if (ZEND_NUM_ARGS()) {
224 call_querystring_get("cookie");
225 } else {
226 zval zcookie_tmp, *zcookie = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("cookie"), 0, &zcookie_tmp);
227 RETURN_ZVAL(zcookie, 1, 0);
228 }
229 }
230
231 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getFiles, 0, 0, 0)
232 ZEND_END_ARG_INFO();
233 static PHP_METHOD(HttpEnvRequest, getFiles)
234 {
235 if (SUCCESS == zend_parse_parameters_none()) {
236 zval zfiles_tmp, *zfiles = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("files"), 0, &zfiles_tmp);
237 RETURN_ZVAL(zfiles, 1, 0);
238 }
239 }
240
241 static zend_function_entry php_http_env_request_methods[] = {
242 PHP_ME(HttpEnvRequest, __construct, ai_HttpEnvRequest___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
243 PHP_ME(HttpEnvRequest, getForm, ai_HttpEnvRequest_getForm, ZEND_ACC_PUBLIC)
244 PHP_ME(HttpEnvRequest, getQuery, ai_HttpEnvRequest_getQuery, ZEND_ACC_PUBLIC)
245 PHP_ME(HttpEnvRequest, getCookie, ai_HttpEnvRequest_getCookie, ZEND_ACC_PUBLIC)
246 PHP_ME(HttpEnvRequest, getFiles, ai_HttpEnvRequest_getFiles, ZEND_ACC_PUBLIC)
247 EMPTY_FUNCTION_ENTRY
248 };
249
250 zend_class_entry *php_http_env_request_class_entry;
251
252 PHP_MINIT_FUNCTION(http_env_request)
253 {
254 zend_class_entry ce = {0};
255
256 INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Request", php_http_env_request_methods);
257 php_http_env_request_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry);
258
259 zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("query"), ZEND_ACC_PROTECTED);
260 zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("form"), ZEND_ACC_PROTECTED);
261 zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("cookie"), ZEND_ACC_PROTECTED);
262 zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("files"), ZEND_ACC_PROTECTED);
263
264 return SUCCESS;
265 }
266
267 /*
268 * Local variables:
269 * tab-width: 4
270 * c-basic-offset: 4
271 * End:
272 * vim600: noet sw=4 ts=4 fdm=marker
273 * vim<600: noet sw=4 ts=4
274 */