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 : #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 51 : static PHP_METHOD(HttpClientRequest, __construct)
33 : {
34 51 : char *meth_str = NULL;
35 51 : int meth_len = 0;
36 51 : zval *zheaders = NULL, *zbody = NULL, *zurl = NULL;
37 : php_http_message_object_t *obj;
38 :
39 51 : 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 51 : obj = zend_object_store_get_object(getThis() TSRMLS_CC);
42 :
43 51 : if (obj->message) {
44 0 : php_http_message_set_type(obj->message, PHP_HTTP_REQUEST);
45 : } else {
46 51 : obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST, NULL TSRMLS_CC);
47 : }
48 :
49 51 : if (zbody) {
50 1 : php_http_expect(SUCCESS == php_http_message_object_set_body(obj, zbody TSRMLS_CC), unexpected_val, return);
51 : }
52 51 : if (meth_str && meth_len) {
53 49 : PHP_HTTP_INFO(obj->message).request.method = estrndup(meth_str, meth_len);
54 : }
55 51 : if (zurl) {
56 49 : PHP_HTTP_INFO(obj->message).request.url = php_http_url_from_zval(zurl, ~0 TSRMLS_CC);
57 : }
58 51 : if (zheaders) {
59 2 : 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 2 : 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 2 : php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ct_str, &ct_len), invalid_arg, return);
74 :
75 2 : if (ct_len && !strchr(ct_str, '/')) {
76 0 : php_http_throw(unexpected_val, "Content type \"%s\" does not seem to contain a primary and a secondary part", ct_str);
77 0 : return;
78 : }
79 :
80 2 : obj = zend_object_store_get_object(getThis() TSRMLS_CC);
81 :
82 2 : PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
83 :
84 2 : MAKE_STD_ZVAL(zct);
85 2 : ZVAL_STRINGL(zct, ct_str, ct_len, 1);
86 2 : zend_hash_update(&obj->message->hdrs, "Content-Type", sizeof("Content-Type"), (void *) &zct, sizeof(void *), NULL);
87 :
88 2 : 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 1 : static PHP_METHOD(HttpClientRequest, getContentType)
94 : {
95 1 : if (SUCCESS == zend_parse_parameters_none()) {
96 1 : php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
97 : zval *zct;
98 :
99 1 : PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
100 :
101 1 : php_http_message_update_headers(obj->message);
102 1 : zct = php_http_message_header(obj->message, ZEND_STRL("Content-Type"), 1);
103 1 : if (zct) {
104 1 : 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 2 : static PHP_METHOD(HttpClientRequest, setQuery)
113 : {
114 2 : zval *qdata = NULL;
115 : php_http_message_object_t *obj;
116 2 : php_http_url_t *old_url = NULL, new_url = {NULL};
117 2 : char empty[] = "";
118 2 : unsigned flags = PHP_HTTP_URL_REPLACE;
119 :
120 2 : php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata), invalid_arg, return);
121 :
122 2 : obj = zend_object_store_get_object(getThis() TSRMLS_CC);
123 :
124 2 : PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
125 :
126 2 : if (qdata) {
127 : zval arr, str;
128 :
129 1 : INIT_PZVAL(&arr);
130 1 : array_init(&arr);
131 1 : INIT_PZVAL(&str);
132 1 : ZVAL_NULL(&str);
133 :
134 1 : php_http_expect(SUCCESS == php_http_querystring_update(&arr, qdata, &str TSRMLS_CC), bad_querystring,
135 : zval_dtor(&arr);
136 : return;
137 : );
138 1 : new_url.query = Z_STRVAL(str);
139 1 : zval_dtor(&arr);
140 : } else {
141 1 : flags = PHP_HTTP_URL_STRIP_QUERY;
142 : }
143 :
144 2 : if (obj->message->http.info.request.url) {
145 2 : old_url = obj->message->http.info.request.url;
146 : }
147 :
148 2 : obj->message->http.info.request.url = php_http_url_mod(old_url, &new_url, flags TSRMLS_CC);
149 :
150 2 : if (old_url) {
151 2 : php_http_url_free(&old_url);
152 : }
153 2 : if (new_url.query != &empty[0]) {
154 2 : PTR_FREE(new_url.query);
155 : }
156 :
157 2 : 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 4 : static PHP_METHOD(HttpClientRequest, getQuery)
163 : {
164 4 : if (SUCCESS == zend_parse_parameters_none()) {
165 4 : php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
166 :
167 4 : PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
168 :
169 4 : if (obj->message->http.info.request.url && obj->message->http.info.request.url->query) {
170 2 : RETVAL_STRING(obj->message->http.info.request.url->query, 1);
171 : }
172 : }
173 4 : }
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 1 : static PHP_METHOD(HttpClientRequest, addQuery)
179 : {
180 : zval *qdata, arr, str;
181 : php_http_message_object_t *obj;
182 1 : php_http_url_t *old_url = NULL, new_url = {NULL};
183 :
184 1 : php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata), invalid_arg, return);
185 :
186 1 : obj = zend_object_store_get_object(getThis() TSRMLS_CC);
187 :
188 1 : PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
189 :
190 1 : INIT_PZVAL(&arr);
191 1 : array_init(&arr);
192 1 : INIT_PZVAL(&str);
193 1 : ZVAL_NULL(&str);
194 :
195 1 : php_http_expect(SUCCESS == php_http_querystring_update(&arr, qdata, &str TSRMLS_CC), bad_querystring,
196 : zval_dtor(&arr);
197 : return;
198 : );
199 1 : new_url.query = Z_STRVAL(str);
200 1 : zval_dtor(&arr);
201 :
202 1 : if (obj->message->http.info.request.url) {
203 1 : old_url = obj->message->http.info.request.url;
204 : }
205 :
206 1 : obj->message->http.info.request.url = php_http_url_mod(old_url, &new_url, PHP_HTTP_URL_JOIN_QUERY TSRMLS_CC);
207 :
208 1 : if (old_url) {
209 1 : php_http_url_free(&old_url);
210 : }
211 1 : PTR_FREE(new_url.query);
212 :
213 1 : 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 12 : static PHP_METHOD(HttpClientRequest, setOptions)
220 : {
221 12 : zval *opts = NULL;
222 :
223 24 : php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
224 :
225 12 : php_http_client_options_set(getThis(), opts TSRMLS_CC);
226 :
227 12 : 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 67 : static PHP_METHOD(HttpClientRequest, getOptions)
233 : {
234 67 : if (SUCCESS == zend_parse_parameters_none()) {
235 67 : zval *zoptions = zend_read_property(php_http_client_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
236 67 : 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 1 : static PHP_METHOD(HttpClientRequest, setSslOptions)
244 : {
245 1 : zval *opts = NULL;
246 :
247 2 : php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
248 :
249 1 : php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
250 :
251 1 : 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 0 : static PHP_METHOD(HttpClientRequest, addSslOptions)
258 : {
259 0 : zval *opts = NULL;
260 :
261 0 : php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
262 :
263 0 : php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
264 :
265 0 : 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 1 : static PHP_METHOD(HttpClientRequest, getSslOptions)
271 : {
272 1 : if (SUCCESS == zend_parse_parameters_none()) {
273 1 : php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC);
274 : }
275 1 : }
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 374 : PHP_MINIT_FUNCTION(http_client_request)
295 : {
296 374 : zend_class_entry ce = {0};
297 :
298 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Client", "Request", php_http_client_request_methods);
299 374 : php_http_client_request_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
300 :
301 374 : zend_declare_property_null(php_http_client_request_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC);
302 :
303 374 : 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 :
|