7b8d85967ecd3e629da3b3c6c47835e1bbb8663d
[m6w6/ext-http] / php_http_client.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 #include <ext/spl/spl_observer.h>
16
17 PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC)
18 {
19 php_http_client_t *free_h = NULL;
20
21 if (!h) {
22 free_h = h = emalloc(sizeof(*h));
23 }
24 memset(h, 0, sizeof(*h));
25
26 h->ops = ops;
27 h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, h, NULL);
28 h->buffer = php_http_buffer_init(NULL);
29 h->parser = php_http_message_parser_init(NULL TSRMLS_CC);
30 h->message = php_http_message_init(NULL, 0 TSRMLS_CC);
31
32 TSRMLS_SET_CTX(h->ts);
33
34 if (h->ops->init) {
35 if (!(h = h->ops->init(h, init_arg))) {
36 php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Could not initialize request");
37 if (free_h) {
38 h->ops->dtor = NULL;
39 php_http_client_free(&free_h);
40 }
41 }
42 }
43
44 return h;
45 }
46
47 PHP_HTTP_API void php_http_client_dtor(php_http_client_t *h)
48 {
49 if (h->ops->dtor) {
50 h->ops->dtor(h);
51 }
52
53 php_http_resource_factory_free(&h->rf);
54
55 php_http_message_parser_free(&h->parser);
56 php_http_message_free(&h->message);
57 php_http_buffer_free(&h->buffer);
58 }
59
60 PHP_HTTP_API void php_http_client_free(php_http_client_t **h)
61 {
62 if (*h) {
63 php_http_client_dtor(*h);
64 efree(*h);
65 *h = NULL;
66 }
67 }
68
69 PHP_HTTP_API php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to)
70 {
71 if (!from->ops->copy) {
72 return NULL;
73 } else {
74 TSRMLS_FETCH_FROM_CTX(from->ts);
75
76 if (!to) {
77 to = ecalloc(1, sizeof(*to));
78 }
79
80 to->ops = from->ops;
81 if (from->rf) {
82 php_http_resource_factory_addref(from->rf);
83 to->rf = from->rf;
84 } else {
85 to->rf = php_http_resource_factory_init(NULL, to->ops->rsrc, to, NULL);
86 }
87 to->buffer = php_http_buffer_init(NULL);
88 to->parser = php_http_message_parser_init(NULL TSRMLS_CC);
89 to->message = php_http_message_init(NULL, 0 TSRMLS_CC);
90
91 TSRMLS_SET_CTX(to->ts);
92
93 return to->ops->copy(from, to);
94 }
95 }
96
97 PHP_HTTP_API STATUS php_http_client_exec(php_http_client_t *h, php_http_message_t *msg)
98 {
99 if (h->ops->exec) {
100 return h->ops->exec(h, msg);
101 }
102 return FAILURE;
103 }
104
105 PHP_HTTP_API STATUS php_http_client_reset(php_http_client_t *h)
106 {
107 if (h->ops->reset) {
108 return h->ops->reset(h);
109 }
110 return FAILURE;
111 }
112
113 PHP_HTTP_API STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
114 {
115 if (h->ops->setopt) {
116 return h->ops->setopt(h, opt, arg);
117 }
118 return FAILURE;
119 }
120
121 PHP_HTTP_API STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg)
122 {
123 if (h->ops->getopt) {
124 return h->ops->getopt(h, opt, arg);
125 }
126 return FAILURE;
127 }
128
129 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClient, method, 0, req_args)
130 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClient, method, 0)
131 #define PHP_HTTP_CLIENT_ME(method, visibility) PHP_ME(HttpClient, method, PHP_HTTP_ARGS(HttpClient, method), visibility)
132 #define PHP_HTTP_CLIENT_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClient, method))
133 #define PHP_HTTP_CLIENT_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClient_##al), PHP_HTTP_ARGS(HttpClient, al), vis)
134
135 PHP_HTTP_BEGIN_ARGS(__construct, 0)
136 PHP_HTTP_ARG_ARR(options, 1, 0)
137 PHP_HTTP_END_ARGS;
138
139 PHP_HTTP_EMPTY_ARGS(getOptions);
140 PHP_HTTP_BEGIN_ARGS(setOptions, 0)
141 PHP_HTTP_ARG_ARR(options, 1, 0)
142 PHP_HTTP_END_ARGS;
143
144 PHP_HTTP_EMPTY_ARGS(getSslOptions);
145 PHP_HTTP_BEGIN_ARGS(setSslOptions, 0)
146 PHP_HTTP_ARG_ARR(ssl_options, 1, 0)
147 PHP_HTTP_END_ARGS;
148
149 PHP_HTTP_BEGIN_ARGS(addSslOptions, 0)
150 PHP_HTTP_ARG_ARR(ssl_options, 1, 0)
151 PHP_HTTP_END_ARGS;
152
153 PHP_HTTP_EMPTY_ARGS(getCookies);
154 PHP_HTTP_BEGIN_ARGS(setCookies, 0)
155 PHP_HTTP_ARG_VAL(cookies, 0)
156 PHP_HTTP_END_ARGS;
157
158 PHP_HTTP_BEGIN_ARGS(addCookies, 1)
159 PHP_HTTP_ARG_VAL(cookies, 0)
160 PHP_HTTP_END_ARGS;
161
162 PHP_HTTP_EMPTY_ARGS(enableCookies);
163 PHP_HTTP_BEGIN_ARGS(resetCookies, 0)
164 PHP_HTTP_ARG_VAL(session_only, 0)
165 PHP_HTTP_END_ARGS;
166 PHP_HTTP_EMPTY_ARGS(flushCookies);
167
168 PHP_HTTP_EMPTY_ARGS(getMessageClass);
169 PHP_HTTP_BEGIN_ARGS(setMessageClass, 1)
170 PHP_HTTP_ARG_VAL(message_class_name, 0)
171 PHP_HTTP_END_ARGS;
172
173 PHP_HTTP_EMPTY_ARGS(getResponseMessage);
174 PHP_HTTP_EMPTY_ARGS(getRequestMessage);
175 PHP_HTTP_EMPTY_ARGS(getHistory);
176 PHP_HTTP_EMPTY_ARGS(clearHistory);
177 PHP_HTTP_BEGIN_ARGS(send, 1)
178 PHP_HTTP_ARG_VAL(request, 0)
179 PHP_HTTP_END_ARGS;
180
181 PHP_HTTP_EMPTY_ARGS(getObservers);
182 PHP_HTTP_BEGIN_ARGS(attach, 1)
183 PHP_HTTP_ARG_OBJ(SplObserver, observer, 0)
184 PHP_HTTP_END_ARGS;
185 PHP_HTTP_BEGIN_ARGS(detach, 1)
186 PHP_HTTP_ARG_OBJ(SplObserver, observer, 0)
187 PHP_HTTP_END_ARGS;
188 PHP_HTTP_EMPTY_ARGS(notify);
189 PHP_HTTP_EMPTY_ARGS(getProgress);
190 PHP_HTTP_BEGIN_ARGS(getTransferInfo, 0)
191 PHP_HTTP_ARG_VAL(name, 0)
192 PHP_HTTP_END_ARGS;
193
194
195 zend_class_entry *php_http_client_class_entry;
196 zend_function_entry php_http_client_method_entry[] = {
197 PHP_HTTP_CLIENT_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
198 PHP_HTTP_CLIENT_ME(getObservers, ZEND_ACC_PUBLIC)
199 PHP_HTTP_CLIENT_ME(notify, ZEND_ACC_PUBLIC)
200 PHP_HTTP_CLIENT_ME(attach, ZEND_ACC_PUBLIC)
201 PHP_HTTP_CLIENT_ME(detach, ZEND_ACC_PUBLIC)
202 PHP_HTTP_CLIENT_ME(getProgress, ZEND_ACC_PUBLIC)
203 PHP_HTTP_CLIENT_ME(getTransferInfo, ZEND_ACC_PUBLIC)
204
205 PHP_HTTP_CLIENT_ME(setOptions, ZEND_ACC_PUBLIC)
206 PHP_HTTP_CLIENT_ME(getOptions, ZEND_ACC_PUBLIC)
207 PHP_HTTP_CLIENT_ME(setSslOptions, ZEND_ACC_PUBLIC)
208 PHP_HTTP_CLIENT_ME(getSslOptions, ZEND_ACC_PUBLIC)
209 PHP_HTTP_CLIENT_ME(addSslOptions, ZEND_ACC_PUBLIC)
210
211 PHP_HTTP_CLIENT_ME(addCookies, ZEND_ACC_PUBLIC)
212 PHP_HTTP_CLIENT_ME(getCookies, ZEND_ACC_PUBLIC)
213 PHP_HTTP_CLIENT_ME(setCookies, ZEND_ACC_PUBLIC)
214
215 PHP_HTTP_CLIENT_ME(enableCookies, ZEND_ACC_PUBLIC)
216 PHP_HTTP_CLIENT_ME(resetCookies, ZEND_ACC_PUBLIC)
217 PHP_HTTP_CLIENT_ME(flushCookies, ZEND_ACC_PUBLIC)
218
219 PHP_HTTP_CLIENT_ME(send, ZEND_ACC_PUBLIC)
220
221 PHP_HTTP_CLIENT_ME(getResponseMessage, ZEND_ACC_PUBLIC)
222 PHP_HTTP_CLIENT_ME(getRequestMessage, ZEND_ACC_PUBLIC)
223 PHP_HTTP_CLIENT_ME(getHistory, ZEND_ACC_PUBLIC)
224 PHP_HTTP_CLIENT_ME(clearHistory, ZEND_ACC_PUBLIC)
225
226 PHP_HTTP_CLIENT_ME(getMessageClass, ZEND_ACC_PUBLIC)
227 PHP_HTTP_CLIENT_ME(setMessageClass, ZEND_ACC_PUBLIC)
228
229 EMPTY_FUNCTION_ENTRY
230 };
231
232 static zend_object_handlers php_http_client_object_handlers;
233
234 zend_object_handlers *php_http_client_get_object_handlers(void)
235 {
236 return &php_http_client_object_handlers;
237 }
238
239 zend_object_value php_http_client_object_new(zend_class_entry *ce TSRMLS_DC)
240 {
241 return php_http_client_object_new_ex(ce, NULL, NULL TSRMLS_CC);
242 }
243
244 zend_object_value php_http_client_object_new_ex(zend_class_entry *ce, php_http_client_t *r, php_http_client_object_t **ptr TSRMLS_DC)
245 {
246 zend_object_value ov;
247 php_http_client_object_t *o;
248
249 o = ecalloc(1, sizeof(php_http_client_object_t));
250 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
251 object_properties_init((zend_object *) o, ce);
252
253 if (!(o->client = r)) {
254 o->client = php_http_client_init(NULL, NULL, NULL, NULL TSRMLS_CC);
255 }
256
257 if (ptr) {
258 *ptr = o;
259 }
260
261 ov.handle = zend_objects_store_put(o, NULL, php_http_client_object_free, NULL TSRMLS_CC);
262 ov.handlers = &php_http_client_object_handlers;
263
264 return ov;
265 }
266
267 zend_object_value php_http_client_object_clone(zval *this_ptr TSRMLS_DC)
268 {
269 zend_object_value new_ov;
270 php_http_client_object_t *new_obj, *old_obj = zend_object_store_get_object(this_ptr TSRMLS_CC);
271
272 new_ov = php_http_client_object_new_ex(old_obj->zo.ce, php_http_client_copy(old_obj->client, NULL), &new_obj TSRMLS_CC);
273 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
274
275 return new_ov;
276 }
277
278 void php_http_client_object_free(void *object TSRMLS_DC)
279 {
280 php_http_client_object_t *o = (php_http_client_object_t *) object;
281
282 php_http_client_free(&o->client);
283 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
284 efree(o);
285 }
286
287 static inline void php_http_client_object_check_request_content_type(zval *this_ptr TSRMLS_DC)
288 {
289 zval *ctype = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("contentType"), 0 TSRMLS_CC);
290
291 if (Z_STRLEN_P(ctype)) {
292 zval **headers, *opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
293
294 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
295 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), ZEND_STRS("headers"), (void *) &headers)) &&
296 (Z_TYPE_PP(headers) == IS_ARRAY)) {
297 zval **ct_header;
298
299 /* only override if not already set */
300 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), ZEND_STRS("Content-Type"), (void *) &ct_header))) {
301 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
302 } else
303 /* or not a string, zero length string or a string of spaces */
304 if ((Z_TYPE_PP(ct_header) != IS_STRING) || !Z_STRLEN_PP(ct_header)) {
305 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
306 } else {
307 int i, only_space = 1;
308
309 /* check for spaces only */
310 for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) {
311 if (!PHP_HTTP_IS_CTYPE(space, Z_STRVAL_PP(ct_header)[i])) {
312 only_space = 0;
313 break;
314 }
315 }
316 if (only_space) {
317 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
318 }
319 }
320 } else {
321 zval *headers;
322
323 MAKE_STD_ZVAL(headers);
324 array_init(headers);
325 add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
326 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers);
327 zval_ptr_dtor(&headers);
328 }
329 }
330 }
331
332 static inline zend_object_value php_http_client_object_message(zval *this_ptr, php_http_message_t *msg TSRMLS_DC)
333 {
334 zend_object_value ov;
335 zval *zcn = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("messageClass"), 0 TSRMLS_CC);
336 zend_class_entry *class_entry;
337
338 if (Z_STRLEN_P(zcn)
339 && (class_entry = zend_fetch_class(Z_STRVAL_P(zcn), Z_STRLEN_P(zcn), 0 TSRMLS_CC))
340 && (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_message_object_new_ex, php_http_client_response_class_entry, msg, NULL TSRMLS_CC))) {
341 return ov;
342 } else {
343 return php_http_message_object_new_ex(php_http_client_response_class_entry, msg, NULL TSRMLS_CC);
344 }
345 }
346
347 STATUS php_http_client_object_handle_request(zval *zclient, zval *zreq TSRMLS_DC)
348 {
349 php_http_client_object_t *obj = zend_object_store_get_object(zclient TSRMLS_CC);
350 php_http_client_progress_t *progress;
351 zval *zoptions;
352
353 /* reset request handle */
354 php_http_client_reset(obj->client);
355
356 /* remember the request */
357 zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("request"), zreq TSRMLS_CC);
358
359 /* reset transfer info */
360 zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("info") TSRMLS_CC);
361
362 /* set request options */
363 zoptions = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("options"), 0 TSRMLS_CC);
364 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, Z_ARRVAL_P(zoptions));
365
366 /* set progress callback */
367 if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) {
368 if (!progress->callback) {
369 php_http_client_progress_callback_t *callback = emalloc(sizeof(*callback));
370
371 callback->type = PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER;
372 callback->pass_state = 0;
373 MAKE_STD_ZVAL(callback->func.user);
374 array_init(callback->func.user);
375 Z_ADDREF_P(zclient);
376 add_next_index_zval(callback->func.user, zclient);
377 add_next_index_stringl(callback->func.user, ZEND_STRL("notify"), 1);
378
379 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, callback);
380 }
381 progress->state.info = "start";
382 php_http_client_progress_notify(progress TSRMLS_CC);
383 progress->state.started = 1;
384 }
385
386 return SUCCESS;
387 }
388
389 STATUS php_http_client_object_requesthandler(php_http_client_object_t *obj, zval *this_ptr, char **meth, char **url, php_http_message_body_t **body TSRMLS_DC)
390 {
391 zval *zoptions;
392 php_http_client_progress_t *progress;
393
394 /* reset request handle */
395 php_http_client_reset(obj->client);
396 /* reset transfer info */
397 zend_update_property_null(php_http_client_class_entry, getThis(), ZEND_STRL("info") TSRMLS_CC);
398
399 if (meth) {
400 *meth = Z_STRVAL_P(zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("method"), 0 TSRMLS_CC));
401 }
402
403 if (url) {
404 php_url *tmp, qdu = {NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL};
405 zval *zurl = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("url"), 0 TSRMLS_CC);
406 zval *zqdata = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("queryData"), 0 TSRMLS_CC);
407
408 if (Z_STRLEN_P(zqdata)) {
409 qdu.query = Z_STRVAL_P(zqdata);
410 }
411 php_http_url(0, tmp = php_url_parse(Z_STRVAL_P(zurl)), &qdu, NULL, url, NULL TSRMLS_CC);
412 php_url_free(tmp);
413 }
414
415 if (body) {
416 zval *zbody = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("requestBody"), 0 TSRMLS_CC);
417
418 if (Z_TYPE_P(zbody) == IS_OBJECT) {
419 *body = ((php_http_message_body_object_t *)zend_object_store_get_object(zbody TSRMLS_CC))->body;
420 if (*body) {
421 php_stream_rewind(php_http_message_body_stream(*body));
422 }
423 }
424 }
425
426 php_http_client_object_check_request_content_type(getThis() TSRMLS_CC);
427 zoptions = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
428 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, Z_ARRVAL_P(zoptions));
429
430 if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) {
431 if (!progress->callback) {
432 php_http_client_progress_callback_t *callback = emalloc(sizeof(*callback));
433
434 callback->type = PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER;
435 callback->pass_state = 0;
436 MAKE_STD_ZVAL(callback->func.user);
437 array_init(callback->func.user);
438 Z_ADDREF_P(getThis());
439 add_next_index_zval(callback->func.user, getThis());
440 add_next_index_stringl(callback->func.user, ZEND_STRL("notify"), 1);
441
442 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, callback);
443 }
444 progress->state.info = "start";
445 php_http_client_progress_notify(progress TSRMLS_CC);
446 progress->state.started = 1;
447 }
448 return SUCCESS;
449 }
450
451 static inline void empty_response(zval *this_ptr TSRMLS_DC)
452 {
453 zend_update_property_null(php_http_client_class_entry, getThis(), ZEND_STRL("responseMessage") TSRMLS_CC);
454 }
455
456 STATUS php_http_client_object_handle_response(zval *zclient TSRMLS_DC)
457 {
458 php_http_client_object_t *obj = zend_object_store_get_object(zclient TSRMLS_CC);
459 php_http_client_progress_t *progress;
460 php_http_message_t *msg;
461 zval *info;
462
463 /* always fetch info */
464 MAKE_STD_ZVAL(info);
465 array_init(info);
466 php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, Z_ARRVAL_P(info));
467 zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("transferInfo"), info TSRMLS_CC);
468 zval_ptr_dtor(&info);
469
470 if ((msg = obj->client->message)) {
471 /* update history */
472 if (i_zend_is_true(zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
473 zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0 TSRMLS_CC);
474 zend_object_value ov = php_http_client_object_message(zclient, php_http_message_copy(msg, NULL) TSRMLS_CC);
475
476 MAKE_STD_ZVAL(new_hist);
477 ZVAL_OBJVAL(new_hist, ov, 0);
478
479 if (Z_TYPE_P(old_hist) == IS_OBJECT) {
480 php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC);
481 }
482
483 zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), new_hist TSRMLS_CC);
484 zval_ptr_dtor(&new_hist);
485 }
486
487 /* update response info */
488 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, msg)) {
489 zval *message;
490
491 MAKE_STD_ZVAL(message);
492 ZVAL_OBJVAL(message, php_http_client_object_message(zclient, msg TSRMLS_CC), 0);
493 zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage"), message TSRMLS_CC);
494 zval_ptr_dtor(&message);
495
496 obj->client->message = php_http_message_init(NULL, 0 TSRMLS_CC);
497 msg = msg->parent;
498 } else {
499 zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage") TSRMLS_CC);
500 }
501 } else {
502 zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage") TSRMLS_CC);
503 }
504
505 /* there might be a 100-Continue response in between */
506 while (msg && !PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) {
507 msg = msg->parent;
508 }
509
510 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) {
511 zval *message;
512
513 /* update the actual request message */
514 MAKE_STD_ZVAL(message);
515 ZVAL_OBJVAL(message, php_http_message_object_new_ex(php_http_message_class_entry, php_http_message_copy_ex(msg, NULL, 0), NULL TSRMLS_CC), 0);
516 zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("requestMessage"), message TSRMLS_CC);
517 zval_ptr_dtor(&message);
518 }
519
520 if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) {
521 progress->state.info = "finished";
522 progress->state.finished = 1;
523 php_http_client_progress_notify(progress TSRMLS_CC);
524 }
525 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, NULL);
526
527 return SUCCESS;
528 }
529
530 STATUS php_http_client_object_responsehandler(php_http_client_object_t *obj, zval *this_ptr TSRMLS_DC)
531 {
532 STATUS ret = SUCCESS;
533 zval *info;
534 php_http_message_t *msg;
535 php_http_client_progress_t *progress;
536
537 /* always fetch info */
538 MAKE_STD_ZVAL(info);
539 array_init(info);
540 php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, Z_ARRVAL_P(info));
541 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("transferInfo"), info TSRMLS_CC);
542 zval_ptr_dtor(&info);
543
544 if ((msg = obj->client->message)) {
545 /* update history */
546 if (i_zend_is_true(zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
547 zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC);
548 zend_object_value ov = php_http_client_object_message(getThis(), php_http_message_copy(msg, NULL) TSRMLS_CC);
549
550 MAKE_STD_ZVAL(new_hist);
551 ZVAL_OBJVAL(new_hist, ov, 0);
552
553 if (Z_TYPE_P(old_hist) == IS_OBJECT) {
554 php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC);
555 }
556
557 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), new_hist TSRMLS_CC);
558 zval_ptr_dtor(&new_hist);
559 }
560
561 /* update response info */
562 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, msg)) {
563 zval *message;
564
565 MAKE_STD_ZVAL(message);
566 ZVAL_OBJVAL(message, php_http_client_object_message(getThis(), msg TSRMLS_CC), 0);
567 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("responseMessage"), message TSRMLS_CC);
568 zval_ptr_dtor(&message);
569
570 obj->client->message = php_http_message_init(NULL, 0 TSRMLS_CC);
571 msg = msg->parent;
572 } else {
573 empty_response(getThis() TSRMLS_CC);
574 }
575 } else {
576 /* update properties with empty values */
577 empty_response(getThis() TSRMLS_CC);
578 }
579
580 while (msg && !PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) {
581 msg = msg->parent;
582 }
583 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) {
584 zval *message;
585
586 MAKE_STD_ZVAL(message);
587 ZVAL_OBJVAL(message, php_http_client_object_message(getThis(), php_http_message_copy_ex(msg, NULL, 0) TSRMLS_CC), 0);
588 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("requestMessage"), message TSRMLS_CC);
589 zval_ptr_dtor(&message);
590 }
591
592 if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) {
593 progress->state.info = "finished";
594 progress->state.finished = 1;
595 php_http_client_progress_notify(progress TSRMLS_CC);
596 }
597 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, NULL);
598
599 return ret;
600 }
601
602 static int apply_pretty_key(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
603 {
604 zval **zpp = pDest, *arr = va_arg(args, zval *);
605
606 if (hash_key->arKey && hash_key->nKeyLength > 1) {
607 char *tmp = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 0);
608
609 Z_ADDREF_PP(zpp);
610 add_assoc_zval_ex(arr, tmp, hash_key->nKeyLength, *zpp);
611 efree(tmp);
612 }
613 return ZEND_HASH_APPLY_KEEP;
614 }
615
616 static void php_http_client_object_set_options(INTERNAL_FUNCTION_PARAMETERS)
617 {
618 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
619 HashPosition pos;
620 zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
621
622 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
623 RETURN_FALSE;
624 }
625
626 MAKE_STD_ZVAL(new_opts);
627 array_init(new_opts);
628
629 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
630 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
631 zval_ptr_dtor(&new_opts);
632 } else {
633 MAKE_STD_ZVAL(add_opts);
634 array_init(add_opts);
635 /* some options need extra attention -- thus cannot use array_merge() directly */
636 FOREACH_KEYVAL(pos, opts, key, opt) {
637 if (key.type == HASH_KEY_IS_STRING) {
638 #define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s))
639 if (KEYMATCH(key, "ssl")) {
640 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt);
641 } else if (KEYMATCH(key, "cookies")) {
642 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt);
643 } else if (KEYMATCH(key, "recordHistory")) {
644 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("recordHistory"), *opt TSRMLS_CC);
645 } else if (KEYMATCH(key, "messageClass")) {
646 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmessageclass", NULL, *opt);
647 } else if (Z_TYPE_PP(opt) == IS_NULL) {
648 old_opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
649 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
650 zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len);
651 }
652 } else {
653 Z_ADDREF_P(*opt);
654 add_assoc_zval_ex(add_opts, key.str, key.len, *opt);
655 }
656 }
657 }
658
659 old_opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
660 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
661 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
662 }
663 array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0);
664 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
665 zval_ptr_dtor(&new_opts);
666 zval_ptr_dtor(&add_opts);
667 }
668
669 RETVAL_ZVAL(getThis(), 1, 0);
670 }
671
672 static inline void php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys)
673 {
674 zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
675
676 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
677 MAKE_STD_ZVAL(new_opts);
678 array_init(new_opts);
679 old_opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
680 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
681 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
682 }
683
684 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
685 if (overwrite) {
686 zend_hash_clean(Z_ARRVAL_PP(entry));
687 }
688 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
689 if (overwrite) {
690 array_copy(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry));
691 } else {
692 array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, prettify_keys ? ARRAY_JOIN_PRETTIFY : 0);
693 }
694 }
695 } else if (opts) {
696 if (prettify_keys) {
697 zval *tmp;
698
699 MAKE_STD_ZVAL(tmp);
700 array_init_size(tmp, zend_hash_num_elements(Z_ARRVAL_P(opts)));
701 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts) TSRMLS_CC, apply_pretty_key, 1, tmp);
702 opts = tmp;
703 } else {
704 Z_ADDREF_P(opts);
705 }
706 add_assoc_zval_ex(new_opts, key, len, opts);
707 }
708 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
709 zval_ptr_dtor(&new_opts);
710 }
711
712 RETVAL_ZVAL(getThis(), 1, 0);
713 }
714
715 static inline void php_http_client_object_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
716 {
717 if (SUCCESS == zend_parse_parameters_none()) {
718 zval *opts, **options;
719
720 opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
721 array_init(return_value);
722
723 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
724 (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
725 convert_to_array(*options);
726 array_copy(Z_ARRVAL_PP(options), Z_ARRVAL_P(return_value));
727 }
728 }
729 }
730
731 PHP_METHOD(HttpClient, __construct)
732 {
733 with_error_handling(EH_THROW, php_http_exception_class_entry) {
734 php_http_client_object_set_options(INTERNAL_FUNCTION_PARAM_PASSTHRU);
735 } end_error_handling();
736 }
737
738 PHP_METHOD(HttpClient, getObservers)
739 {
740 with_error_handling(EH_THROW, php_http_exception_class_entry) {
741 if (SUCCESS == zend_parse_parameters_none()) {
742 RETVAL_PROP(php_http_client_class_entry, "observers");
743 }
744 } end_error_handling();
745 }
746
747 static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC)
748 {
749 zval **observer = NULL;
750
751 iter->funcs->get_current_data(iter, &observer TSRMLS_CC);
752 if (observer) {
753 zval *retval;
754
755 zend_call_method_with_1_params(observer, NULL, NULL, "update", &retval, puser);
756 zval_ptr_dtor(&retval);
757 return SUCCESS;
758 }
759 return FAILURE;
760 }
761
762 PHP_METHOD(HttpClient, notify)
763 {
764 if (SUCCESS == zend_parse_parameters_none()) {
765 zval *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
766
767 if (Z_TYPE_P(observers) == IS_OBJECT) {
768 Z_ADDREF_P(getThis());
769 spl_iterator_apply(observers, notify, getThis() TSRMLS_CC);
770 zval_ptr_dtor(&getThis());
771 }
772 }
773
774 RETVAL_ZVAL(getThis(), 1, 0);
775 }
776
777 PHP_METHOD(HttpClient, attach)
778 {
779 zval *observer;
780
781 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) {
782 zval *retval, *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
783 zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer);
784 zval_ptr_dtor(&retval);
785 }
786
787 RETVAL_ZVAL(getThis(), 1, 0);
788 }
789
790 PHP_METHOD(HttpClient, detach)
791 {
792 zval *observer;
793
794 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) {
795 zval *retval, *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
796 zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer);
797 zval_ptr_dtor(&retval);
798 }
799
800 RETVAL_ZVAL(getThis(), 1, 0);
801 }
802
803 PHP_METHOD(HttpClient, getProgress)
804 {
805 if (SUCCESS == zend_parse_parameters_none()) {
806 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
807 php_http_client_progress_t *progress;
808
809 php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress);
810 object_init(return_value);
811 add_property_bool(return_value, "started", progress->state.started);
812 add_property_bool(return_value, "finished", progress->state.finished);
813 add_property_string(return_value, "info", STR_PTR(progress->state.info), 1);
814 add_property_double(return_value, "dltotal", progress->state.dl.total);
815 add_property_double(return_value, "dlnow", progress->state.dl.now);
816 add_property_double(return_value, "ultotal", progress->state.ul.total);
817 add_property_double(return_value, "ulnow", progress->state.ul.now);
818 }
819 }
820
821 PHP_METHOD(HttpClient, getTransferInfo)
822 {
823 char *info_name = NULL;
824 int info_len = 0;
825
826 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
827 zval **infop, *temp = NULL, *info = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("transferInfo"), 0 TSRMLS_CC);
828
829 /* request completed? */
830 if (Z_TYPE_P(info) != IS_ARRAY) {
831 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
832
833 MAKE_STD_ZVAL(temp);
834 array_init(temp);
835 php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, Z_ARRVAL_P(temp));
836 info = temp;
837 }
838
839 if (info_len && info_name) {
840 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(info), php_http_pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) {
841 RETVAL_ZVAL(*infop, 1, 0);
842 } else {
843 php_http_error(HE_NOTICE, PHP_HTTP_E_INVALID_PARAM, "Could not find transfer info named %s", info_name);
844 RETVAL_FALSE;
845 }
846 } else {
847 RETVAL_ZVAL(info, 1, 0);
848 }
849
850 if (temp) {
851 zval_ptr_dtor(&temp);
852 }
853 return;
854 }
855 RETURN_FALSE;
856 }
857
858 PHP_METHOD(HttpClient, setOptions)
859 {
860 php_http_client_object_set_options(INTERNAL_FUNCTION_PARAM_PASSTHRU);
861 }
862
863 PHP_METHOD(HttpClient, getOptions)
864 {
865 if (SUCCESS == zend_parse_parameters_none()) {
866 RETURN_PROP(php_http_client_class_entry, "options");
867 }
868 RETURN_FALSE;
869 }
870
871 PHP_METHOD(HttpClient, setSslOptions)
872 {
873 php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl"), 1, 0);
874 }
875
876 PHP_METHOD(HttpClient, addSslOptions)
877 {
878 php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl"), 0, 0);
879 }
880
881 PHP_METHOD(HttpClient, getSslOptions)
882 {
883 php_http_client_object_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl"));
884 }
885
886 PHP_METHOD(HttpClient, setCookies)
887 {
888 php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies"), 1, 0);
889 }
890
891 PHP_METHOD(HttpClient, addCookies)
892 {
893 php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies"), 0, 0);
894 }
895
896 PHP_METHOD(HttpClient, getCookies)
897 {
898 php_http_client_object_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies"));
899 }
900
901 PHP_METHOD(HttpClient, enableCookies)
902 {
903 if (SUCCESS == zend_parse_parameters_none()){
904 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
905
906 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_COOKIES_ENABLE, NULL);
907 }
908 RETVAL_ZVAL(getThis(), 1, 0);
909 }
910
911 PHP_METHOD(HttpClient, resetCookies)
912 {
913 zend_bool session_only = 0;
914
915 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) {
916 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
917 if (session_only) {
918 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_COOKIES_RESET_SESSION, NULL);
919 } else {
920 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_COOKIES_RESET, NULL);
921 }
922 }
923 RETVAL_ZVAL(getThis(), 1, 0);
924 }
925
926 PHP_METHOD(HttpClient, flushCookies)
927 {
928 if (SUCCESS == zend_parse_parameters_none()) {
929 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
930
931 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_COOKIES_FLUSH, NULL);
932 }
933 RETVAL_ZVAL(getThis(), 1, 0);
934 }
935
936 PHP_METHOD(HttpClient, getResponseMessage)
937 {
938 with_error_handling(EH_THROW, php_http_exception_class_entry) {
939 if (SUCCESS == zend_parse_parameters_none()) {
940 zval *message = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC);
941
942 if (Z_TYPE_P(message) == IS_OBJECT) {
943 RETVAL_OBJECT(message, 1);
944 } else {
945 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HttpClient does not contain a response message");
946 }
947 }
948 } end_error_handling();
949 }
950
951 PHP_METHOD(HttpClient, getRequestMessage)
952 {
953 with_error_handling(EH_THROW, php_http_exception_class_entry) {
954 if (SUCCESS == zend_parse_parameters_none()) {
955 zval *message = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("requestMessage"), 0 TSRMLS_CC);
956
957 if (Z_TYPE_P(message) == IS_OBJECT) {
958 RETVAL_OBJECT(message, 1);
959 } else {
960 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HttpClient does not contain a request message");
961 }
962 }
963 } end_error_handling();
964 }
965
966 PHP_METHOD(HttpClient, getHistory)
967 {
968 with_error_handling(EH_THROW, php_http_exception_class_entry) {
969 if (SUCCESS == zend_parse_parameters_none()) {
970 zval *hist = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC);
971
972 if (Z_TYPE_P(hist) == IS_OBJECT) {
973 RETVAL_OBJECT(hist, 1);
974 } else {
975 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "The history is empty");
976 }
977 }
978 } end_error_handling();
979 }
980
981 PHP_METHOD(HttpClient, clearHistory)
982 {
983 if (SUCCESS == zend_parse_parameters_none()) {
984 zend_update_property_null(php_http_client_class_entry, getThis(), ZEND_STRL("history") TSRMLS_CC);
985 }
986 RETVAL_ZVAL(getThis(), 1, 0);
987 }
988
989 PHP_METHOD(HttpClient, getMessageClass)
990 {
991 if (SUCCESS == zend_parse_parameters_none()) {
992 RETURN_PROP(php_http_client_class_entry, "messageClass");
993 }
994 RETURN_FALSE;
995 }
996
997 PHP_METHOD(HttpClient, setMessageClass)
998 {
999 char *cn;
1000 int cl;
1001
1002 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cn, &cl)) {
1003 zend_update_property_stringl(php_http_client_class_entry, getThis(), ZEND_STRL("messageClass"), cn, cl TSRMLS_CC);
1004 }
1005 RETVAL_ZVAL(getThis(), 1, 0);
1006 }
1007
1008 PHP_METHOD(HttpClient, send)
1009 {
1010 zval *zreq;
1011
1012 RETVAL_FALSE;
1013
1014 with_error_handling(EH_THROW, php_http_exception_class_entry) {
1015 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zreq, php_http_client_request_class_entry)) {
1016 if (SUCCESS == php_http_client_object_handle_request(getThis(), zreq TSRMLS_CC)) {
1017 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1018 php_http_message_object_t *req = zend_object_store_get_object(zreq TSRMLS_CC);
1019
1020 php_http_client_exec(obj->client, req->message);
1021
1022 if (SUCCESS == php_http_client_object_handle_response(getThis() TSRMLS_CC)) {
1023 RETVAL_PROP(php_http_client_class_entry, "responseMessage");
1024 } else {
1025 php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to handle response");
1026 }
1027 } else {
1028 php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to handle request");
1029 }
1030 }
1031 } end_error_handling();
1032 }
1033
1034 PHP_MINIT_FUNCTION(http_client)
1035 {
1036 PHP_HTTP_REGISTER_CLASS(http\\Client, AbstractClient, http_client, php_http_object_class_entry, ZEND_ACC_ABSTRACT);
1037 php_http_client_class_entry->create_object = php_http_object_new;//php_http_client_object_new;
1038 memcpy(&php_http_client_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1039 php_http_client_object_handlers.clone_obj = php_http_client_object_clone;
1040
1041 zend_class_implements(php_http_client_class_entry TSRMLS_CC, 2, spl_ce_SplSubject, php_http_client_interface_class_entry);
1042
1043 zend_declare_property_string(php_http_client_class_entry, ZEND_STRL("messageClass"), "", ZEND_ACC_PRIVATE TSRMLS_CC);
1044 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC);
1045 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PRIVATE TSRMLS_CC);
1046 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("transferInfo"), ZEND_ACC_PRIVATE TSRMLS_CC);
1047 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("responseMessage"), ZEND_ACC_PRIVATE TSRMLS_CC);
1048 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("requestMessage"), ZEND_ACC_PRIVATE TSRMLS_CC);
1049 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PRIVATE TSRMLS_CC);
1050
1051 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("request"), ZEND_ACC_PROTECTED TSRMLS_CC);
1052
1053 zend_declare_property_bool(php_http_client_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
1054
1055 return SUCCESS;
1056 }
1057
1058 /*
1059 * Local variables:
1060 * tab-width: 4
1061 * c-basic-offset: 4
1062 * End:
1063 * vim600: noet sw=4 ts=4 fdm=marker
1064 * vim<600: noet sw=4 ts=4
1065 */
1066