c073512ed5f02871e4054bb9b1cec20e4dc1ef86
[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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientRequest, method, 0, req_args)
16 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientRequest, method, 0)
17 #define PHP_HTTP_CLIENT_REQUEST_ME(method, visibility) PHP_ME(HttpClientRequest, method, PHP_HTTP_ARGS(HttpClientRequest, method), visibility)
18 #define PHP_HTTP_CLIENT_REQUEST_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientRequest, method))
19 #define PHP_HTTP_CLIENT_REQUEST_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientRequest_##al), PHP_HTTP_ARGS(HttpClientRequest, al), vis)
20
21 PHP_HTTP_BEGIN_ARGS(__construct, 0)
22 PHP_HTTP_ARG_VAL(method, 0)
23 PHP_HTTP_ARG_VAL(url, 0)
24 PHP_HTTP_ARG_ARR(headers, 1, 0)
25 PHP_HTTP_ARG_OBJ(http\\Message\\Body, body, 1)
26 PHP_HTTP_END_ARGS;
27
28 PHP_HTTP_EMPTY_ARGS(getContentType);
29 PHP_HTTP_BEGIN_ARGS(setContentType, 1)
30 PHP_HTTP_ARG_VAL(content_type, 0)
31 PHP_HTTP_END_ARGS;
32
33 PHP_HTTP_EMPTY_ARGS(getQuery);
34 PHP_HTTP_BEGIN_ARGS(setQuery, 0)
35 PHP_HTTP_ARG_VAL(query_data, 0)
36 PHP_HTTP_END_ARGS;
37
38 PHP_HTTP_BEGIN_ARGS(addQuery, 1)
39 PHP_HTTP_ARG_VAL(query_data, 0)
40 PHP_HTTP_END_ARGS;
41
42 PHP_HTTP_EMPTY_ARGS(getOptions);
43 PHP_HTTP_BEGIN_ARGS(setOptions, 0)
44 PHP_HTTP_ARG_ARR(options, 1, 0)
45 PHP_HTTP_END_ARGS;
46
47 PHP_HTTP_EMPTY_ARGS(getSslOptions);
48 PHP_HTTP_BEGIN_ARGS(setSslOptions, 0)
49 PHP_HTTP_ARG_ARR(ssl_options, 1, 0)
50 PHP_HTTP_END_ARGS;
51
52 PHP_HTTP_BEGIN_ARGS(addSslOptions, 0)
53 PHP_HTTP_ARG_ARR(ssl_options, 1, 0)
54 PHP_HTTP_END_ARGS;
55
56
57 static zend_class_entry *php_http_client_request_class_entry;
58
59 zend_class_entry * php_http_client_request_get_class_entry(void)
60 {
61 return php_http_client_request_class_entry;
62 }
63
64 static zend_function_entry php_http_client_request_method_entry[] = {
65 PHP_HTTP_CLIENT_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
66 PHP_HTTP_CLIENT_REQUEST_ME(getQuery, ZEND_ACC_PUBLIC)
67 PHP_HTTP_CLIENT_REQUEST_ME(setQuery, ZEND_ACC_PUBLIC)
68 PHP_HTTP_CLIENT_REQUEST_ME(addQuery, ZEND_ACC_PUBLIC)
69 PHP_HTTP_CLIENT_REQUEST_ME(getContentType, ZEND_ACC_PUBLIC)
70 PHP_HTTP_CLIENT_REQUEST_ME(setContentType, ZEND_ACC_PUBLIC)
71 PHP_HTTP_CLIENT_REQUEST_ME(setOptions, ZEND_ACC_PUBLIC)
72 PHP_HTTP_CLIENT_REQUEST_ME(getOptions, ZEND_ACC_PUBLIC)
73 PHP_HTTP_CLIENT_REQUEST_ME(setSslOptions, ZEND_ACC_PUBLIC)
74 PHP_HTTP_CLIENT_REQUEST_ME(getSslOptions, ZEND_ACC_PUBLIC)
75 PHP_HTTP_CLIENT_REQUEST_ME(addSslOptions, ZEND_ACC_PUBLIC)
76 EMPTY_FUNCTION_ENTRY
77 };
78
79
80 PHP_METHOD(HttpClientRequest, __construct)
81 {
82 char *meth_str = NULL, *url_str = NULL;
83 int meth_len = 0, url_len = 0;
84 zval *zheaders = NULL, *zbody = NULL;
85
86 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
87 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!a!O!", &meth_str, &meth_len, &url_str, &url_len, &zheaders, &zbody, php_http_message_body_get_class_entry())) {
88 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
89
90 if (obj->message) {
91 php_http_message_set_type(obj->message, PHP_HTTP_REQUEST);
92 } else {
93 obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST TSRMLS_CC);
94 }
95
96 if (meth_str && meth_len) {
97 PHP_HTTP_INFO(obj->message).request.method = estrndup(meth_str, meth_len);
98 }
99 if (url_str && url_len) {
100 PHP_HTTP_INFO(obj->message).request.url = estrndup(url_str, url_len);
101 }
102 if (zheaders) {
103 array_copy(Z_ARRVAL_P(zheaders), &obj->message->hdrs);
104 }
105 if (zbody) {
106 php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
107
108 php_http_message_body_dtor(&obj->message->body);
109 php_http_message_body_copy(body_obj->body, &obj->message->body, 0);
110 Z_OBJ_ADDREF_P(zbody);
111 obj->body = Z_OBJVAL_P(zbody);
112 }
113 }
114 } end_error_handling();
115 }
116
117
118 PHP_METHOD(HttpClientRequest, setContentType)
119 {
120 char *ct_str;
121 int ct_len;
122
123 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ct_str, &ct_len)) {
124 int invalid = 0;
125
126 if (ct_len) {
127 PHP_HTTP_CHECK_CONTENT_TYPE(ct_str, invalid = 1);
128 }
129
130 if (!invalid) {
131 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
132 zval *zct;
133
134 MAKE_STD_ZVAL(zct);
135 ZVAL_STRINGL(zct, ct_str, ct_len, 1);
136 zend_hash_update(&obj->message->hdrs, "Content-Type", sizeof("Content-Type"), (void *) &zct, sizeof(void *), NULL);
137 }
138 }
139 RETVAL_ZVAL(getThis(), 1, 0);
140 }
141
142 PHP_METHOD(HttpClientRequest, getContentType)
143 {
144 if (SUCCESS == zend_parse_parameters_none()) {
145 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
146 zval *zct = php_http_message_header(obj->message, ZEND_STRL("Content-Type"), 1);
147
148 RETURN_ZVAL(zct, 0, 0);
149 }
150 RETURN_FALSE;
151 }
152
153 PHP_METHOD(HttpClientRequest, setQuery)
154 {
155 zval *qdata = NULL;
156
157 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
158 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
159 php_url *old_url = NULL, new_url = {NULL};
160 char empty[] = "";
161
162 if (qdata) {
163 zval arr, str;
164
165 INIT_PZVAL(&arr);
166 array_init(&arr);
167 INIT_PZVAL(&str);
168 ZVAL_NULL(&str);
169
170 php_http_querystring_update(&arr, qdata, &str TSRMLS_CC);
171 new_url.query = Z_STRVAL(str);
172 zval_dtor(&arr);
173 } else {
174 new_url.query = &empty[0];
175 }
176
177 if (obj->message->http.info.request.url) {
178 old_url = php_url_parse(obj->message->http.info.request.url);
179 efree(obj->message->http.info.request.url);
180 }
181
182 php_http_url(PHP_HTTP_URL_REPLACE, old_url, &new_url, NULL, &obj->message->http.info.request.url, NULL TSRMLS_CC);
183
184 if (old_url) {
185 php_url_free(old_url);
186 }
187 if (new_url.query != &empty[0]) {
188 STR_FREE(new_url.query);
189 }
190 }
191 RETVAL_ZVAL(getThis(), 1, 0);
192 }
193
194 PHP_METHOD(HttpClientRequest, getQuery)
195 {
196 if (SUCCESS == zend_parse_parameters_none()) {
197 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
198
199 if (obj->message->http.info.request.url) {
200 php_url *purl = php_url_parse(obj->message->http.info.request.url);
201
202 if (purl) {
203 if (purl->query) {
204 RETVAL_STRING(purl->query, 0);
205 purl->query = NULL;
206 }
207 php_url_free(purl);
208 }
209 }
210 }
211 }
212
213 PHP_METHOD(HttpClientRequest, addQuery)
214 {
215 zval *qdata;
216
217 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
218 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
219 php_url *old_url = NULL, new_url = {NULL};
220
221 zval arr, str;
222
223 INIT_PZVAL(&arr);
224 array_init(&arr);
225 INIT_PZVAL(&str);
226 ZVAL_NULL(&str);
227
228 php_http_querystring_update(&arr, qdata, &str TSRMLS_CC);
229 new_url.query = Z_STRVAL(str);
230 zval_dtor(&arr);
231
232 if (obj->message->http.info.request.url) {
233 old_url = php_url_parse(obj->message->http.info.request.url);
234 efree(obj->message->http.info.request.url);
235 }
236
237 php_http_url(PHP_HTTP_URL_JOIN_QUERY, old_url, &new_url, NULL, &obj->message->http.info.request.url, NULL TSRMLS_CC);
238
239 if (old_url) {
240 php_url_free(old_url);
241 }
242 STR_FREE(new_url.query);
243 }
244 RETVAL_ZVAL(getThis(), 1, 0);
245 }
246
247 PHP_METHOD(HttpClientRequest, setOptions)
248 {
249 zval *opts = NULL;
250
251 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
252 php_http_client_options_set(getThis(), opts TSRMLS_CC);
253
254 RETVAL_ZVAL(getThis(), 1, 0);
255 }
256 }
257
258 PHP_METHOD(HttpClientRequest, getOptions)
259 {
260 if (SUCCESS == zend_parse_parameters_none()) {
261 RETURN_PROP(php_http_client_request_class_entry, "options");
262 }
263 RETURN_FALSE;
264 }
265
266 PHP_METHOD(HttpClientRequest, setSslOptions)
267 {
268 zval *opts = NULL;
269
270 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
271 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
272
273 RETVAL_ZVAL(getThis(), 1, 0);
274 }
275 }
276
277 PHP_METHOD(HttpClientRequest, addSslOptions)
278 {
279 zval *opts = NULL;
280
281 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
282 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
283
284 RETVAL_ZVAL(getThis(), 1, 0);
285 }
286 }
287
288 PHP_METHOD(HttpClientRequest, getSslOptions)
289 {
290 if (SUCCESS == zend_parse_parameters_none()) {
291 php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC);
292 }
293 }
294
295 PHP_MINIT_FUNCTION(http_client_request)
296 {
297 PHP_HTTP_REGISTER_CLASS(http\\Client, Request, http_client_request, php_http_message_get_class_entry(), 0);
298
299 zend_declare_property_null(php_http_client_request_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC);
300
301 return SUCCESS;
302 }
303
304 /*
305 * Local variables:
306 * tab-width: 4
307 * c-basic-offset: 4
308 * End:
309 * vim600: noet sw=4 ts=4 fdm=marker
310 * vim<600: noet sw=4 ts=4
311 */
312