rename info tests
[m6w6/ext-http] / php_http_client_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 void php_http_client_options_set_subr(zval *this_ptr, char *key, size_t len, zval *opts, int overwrite TSRMLS_DC);
16 void php_http_client_options_set(zval *this_ptr, zval *opts TSRMLS_DC);
17 void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zval *return_value TSRMLS_DC);
18
19 #define PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj) \
20 do { \
21 if (!obj->message) { \
22 obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST, NULL TSRMLS_CC); \
23 } \
24 } while(0)
25
26 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest___construct, 0, 0, 0)
27 ZEND_ARG_INFO(0, method)
28 ZEND_ARG_INFO(0, url)
29 ZEND_ARG_ARRAY_INFO(0, headers, 1)
30 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 1)
31 ZEND_END_ARG_INFO();
32 static PHP_METHOD(HttpClientRequest, __construct)
33 {
34 char *meth_str = NULL;
35 int meth_len = 0;
36 zval *zheaders = NULL, *zbody = NULL, *zurl = NULL;
37 php_http_message_object_t *obj;
38
39 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!z!a!O!", &meth_str, &meth_len, &zurl, &zheaders, &zbody, php_http_message_body_class_entry), invalid_arg, return);
40
41 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
42
43 if (obj->message) {
44 php_http_message_set_type(obj->message, PHP_HTTP_REQUEST);
45 } else {
46 obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST, NULL TSRMLS_CC);
47 }
48
49 if (zbody) {
50 php_http_expect(SUCCESS == php_http_message_object_set_body(obj, zbody TSRMLS_CC), unexpected_val, return);
51 }
52 if (meth_str && meth_len) {
53 PHP_HTTP_INFO(obj->message).request.method = estrndup(meth_str, meth_len);
54 }
55 if (zurl) {
56 PHP_HTTP_INFO(obj->message).request.url = php_http_url_from_zval(zurl, ~0 TSRMLS_CC);
57 }
58 if (zheaders) {
59 array_copy(Z_ARRVAL_P(zheaders), &obj->message->hdrs);
60 }
61 }
62
63 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_setContentType, 0, 0, 1)
64 ZEND_ARG_INFO(0, content_type)
65 ZEND_END_ARG_INFO();
66 static PHP_METHOD(HttpClientRequest, setContentType)
67 {
68 char *ct_str;
69 int ct_len;
70 php_http_message_object_t *obj;
71 zval *zct;
72
73 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ct_str, &ct_len), invalid_arg, return);
74
75 if (ct_len && !strchr(ct_str, '/')) {
76 php_http_throw(unexpected_val, "Content type \"%s\" does not seem to contain a primary and a secondary part", ct_str);
77 return;
78 }
79
80 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
81
82 PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
83
84 MAKE_STD_ZVAL(zct);
85 ZVAL_STRINGL(zct, ct_str, ct_len, 1);
86 zend_hash_update(&obj->message->hdrs, "Content-Type", sizeof("Content-Type"), (void *) &zct, sizeof(void *), NULL);
87
88 RETVAL_ZVAL(getThis(), 1, 0);
89 }
90
91 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_getContentType, 0, 0, 0)
92 ZEND_END_ARG_INFO();
93 static PHP_METHOD(HttpClientRequest, getContentType)
94 {
95 if (SUCCESS == zend_parse_parameters_none()) {
96 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
97 zval *zct;
98
99 PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
100
101 php_http_message_update_headers(obj->message);
102 zct = php_http_message_header(obj->message, ZEND_STRL("Content-Type"), 1);
103 if (zct) {
104 RETURN_ZVAL(zct, 0, 1);
105 }
106 }
107 }
108
109 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_setQuery, 0, 0, 0)
110 ZEND_ARG_INFO(0, query_data)
111 ZEND_END_ARG_INFO();
112 static PHP_METHOD(HttpClientRequest, setQuery)
113 {
114 zval *qdata = NULL;
115 php_http_message_object_t *obj;
116 php_http_url_t *old_url = NULL, new_url = {NULL};
117 char empty[] = "";
118 unsigned flags = PHP_HTTP_URL_REPLACE;
119
120 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata), invalid_arg, return);
121
122 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
123
124 PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
125
126 if (qdata) {
127 zval arr, str;
128
129 INIT_PZVAL(&arr);
130 array_init(&arr);
131 INIT_PZVAL(&str);
132 ZVAL_NULL(&str);
133
134 php_http_expect(SUCCESS == php_http_querystring_update(&arr, qdata, &str TSRMLS_CC), bad_querystring,
135 zval_dtor(&arr);
136 return;
137 );
138 new_url.query = Z_STRVAL(str);
139 zval_dtor(&arr);
140 } else {
141 flags = PHP_HTTP_URL_STRIP_QUERY;
142 }
143
144 if (obj->message->http.info.request.url) {
145 old_url = obj->message->http.info.request.url;
146 }
147
148 obj->message->http.info.request.url = php_http_url_mod(old_url, &new_url, flags TSRMLS_CC);
149
150 if (old_url) {
151 php_http_url_free(&old_url);
152 }
153 if (new_url.query != &empty[0]) {
154 PTR_FREE(new_url.query);
155 }
156
157 RETVAL_ZVAL(getThis(), 1, 0);
158 }
159
160 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_getQuery, 0, 0, 0)
161 ZEND_END_ARG_INFO();
162 static PHP_METHOD(HttpClientRequest, getQuery)
163 {
164 if (SUCCESS == zend_parse_parameters_none()) {
165 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
166
167 PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
168
169 if (obj->message->http.info.request.url && obj->message->http.info.request.url->query) {
170 RETVAL_STRING(obj->message->http.info.request.url->query, 1);
171 }
172 }
173 }
174
175 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_addQuery, 0, 0, 1)
176 ZEND_ARG_INFO(0, query_data)
177 ZEND_END_ARG_INFO();
178 static PHP_METHOD(HttpClientRequest, addQuery)
179 {
180 zval *qdata, arr, str;
181 php_http_message_object_t *obj;
182 php_http_url_t *old_url = NULL, new_url = {NULL};
183
184 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata), invalid_arg, return);
185
186 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
187
188 PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
189
190 INIT_PZVAL(&arr);
191 array_init(&arr);
192 INIT_PZVAL(&str);
193 ZVAL_NULL(&str);
194
195 php_http_expect(SUCCESS == php_http_querystring_update(&arr, qdata, &str TSRMLS_CC), bad_querystring,
196 zval_dtor(&arr);
197 return;
198 );
199 new_url.query = Z_STRVAL(str);
200 zval_dtor(&arr);
201
202 if (obj->message->http.info.request.url) {
203 old_url = obj->message->http.info.request.url;
204 }
205
206 obj->message->http.info.request.url = php_http_url_mod(old_url, &new_url, PHP_HTTP_URL_JOIN_QUERY TSRMLS_CC);
207
208 if (old_url) {
209 php_http_url_free(&old_url);
210 }
211 PTR_FREE(new_url.query);
212
213 RETVAL_ZVAL(getThis(), 1, 0);
214 }
215
216 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_setOptions, 0, 0, 0)
217 ZEND_ARG_ARRAY_INFO(0, options, 1)
218 ZEND_END_ARG_INFO();
219 static PHP_METHOD(HttpClientRequest, setOptions)
220 {
221 zval *opts = NULL;
222
223 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
224
225 php_http_client_options_set(getThis(), opts TSRMLS_CC);
226
227 RETVAL_ZVAL(getThis(), 1, 0);
228 }
229
230 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_getOptions, 0, 0, 0)
231 ZEND_END_ARG_INFO();
232 static PHP_METHOD(HttpClientRequest, getOptions)
233 {
234 if (SUCCESS == zend_parse_parameters_none()) {
235 zval *zoptions = zend_read_property(php_http_client_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
236 RETURN_ZVAL(zoptions, 1, 0);
237 }
238 }
239
240 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_setSslOptions, 0, 0, 0)
241 ZEND_ARG_ARRAY_INFO(0, ssl_options, 1)
242 ZEND_END_ARG_INFO();
243 static PHP_METHOD(HttpClientRequest, setSslOptions)
244 {
245 zval *opts = NULL;
246
247 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
248
249 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
250
251 RETVAL_ZVAL(getThis(), 1, 0);
252 }
253
254 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_addSslOptions, 0, 0, 0)
255 ZEND_ARG_ARRAY_INFO(0, ssl_options, 1)
256 ZEND_END_ARG_INFO();
257 static PHP_METHOD(HttpClientRequest, addSslOptions)
258 {
259 zval *opts = NULL;
260
261 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
262
263 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
264
265 RETVAL_ZVAL(getThis(), 1, 0);
266 }
267
268 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_getSslOptions, 0, 0, 0)
269 ZEND_END_ARG_INFO();
270 static PHP_METHOD(HttpClientRequest, getSslOptions)
271 {
272 if (SUCCESS == zend_parse_parameters_none()) {
273 php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC);
274 }
275 }
276
277 static zend_function_entry php_http_client_request_methods[] = {
278 PHP_ME(HttpClientRequest, __construct, ai_HttpClientRequest___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
279 PHP_ME(HttpClientRequest, setContentType, ai_HttpClientRequest_setContentType, ZEND_ACC_PUBLIC)
280 PHP_ME(HttpClientRequest, getContentType, ai_HttpClientRequest_getContentType, ZEND_ACC_PUBLIC)
281 PHP_ME(HttpClientRequest, setQuery, ai_HttpClientRequest_setQuery, ZEND_ACC_PUBLIC)
282 PHP_ME(HttpClientRequest, getQuery, ai_HttpClientRequest_getQuery, ZEND_ACC_PUBLIC)
283 PHP_ME(HttpClientRequest, addQuery, ai_HttpClientRequest_addQuery, ZEND_ACC_PUBLIC)
284 PHP_ME(HttpClientRequest, setOptions, ai_HttpClientRequest_setOptions, ZEND_ACC_PUBLIC)
285 PHP_ME(HttpClientRequest, getOptions, ai_HttpClientRequest_getOptions, ZEND_ACC_PUBLIC)
286 PHP_ME(HttpClientRequest, setSslOptions, ai_HttpClientRequest_setSslOptions, ZEND_ACC_PUBLIC)
287 PHP_ME(HttpClientRequest, getSslOptions, ai_HttpClientRequest_getSslOptions, ZEND_ACC_PUBLIC)
288 PHP_ME(HttpClientRequest, addSslOptions, ai_HttpClientRequest_addSslOptions, ZEND_ACC_PUBLIC)
289 EMPTY_FUNCTION_ENTRY
290 };
291
292 zend_class_entry *php_http_client_request_class_entry;
293
294 PHP_MINIT_FUNCTION(http_client_request)
295 {
296 zend_class_entry ce = {0};
297
298 INIT_NS_CLASS_ENTRY(ce, "http\\Client", "Request", php_http_client_request_methods);
299 php_http_client_request_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
300
301 zend_declare_property_null(php_http_client_request_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC);
302
303 return SUCCESS;
304 }
305
306 /*
307 * Local variables:
308 * tab-width: 4
309 * c-basic-offset: 4
310 * End:
311 * vim600: noet sw=4 ts=4 fdm=marker
312 * vim<600: noet sw=4 ts=4
313 */
314